Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/common/Text.js

Issue 1809533003: DevTools: remove illusionary caching from String.prototype.lineEndings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6 * @constructor
7 * @param {string} value
8 */
9 WebInspector.Text = function(value)
10 {
11 this._value = value;
12 }
13
14 WebInspector.Text.prototype = {
15 /**
16 * @return {!Array<number>}
17 */
18 lineEndings: function()
19 {
20 if (!this._lineEndings)
21 this._lineEndings = this._value.lineEndings();
22 return this._lineEndings;
23 },
24
25 /**
26 * @return {string}
27 */
28 value: function()
29 {
30 return this._value;
31 },
32
33 /**
34 * @return {number}
35 */
36 lineCount: function()
37 {
38 var lineEndings = this.lineEndings();
39 return lineEndings.length;
40 },
41
42 /**
43 * @param {number} lineNumber
44 * @param {number} columNumber
45 * @return {number}
46 */
47 offsetFromPosition: function(lineNumber, columNumber)
48 {
49 return (lineNumber ? this.lineEndings()[lineNumber - 1] + 1 : 0) + colum Number;
50 },
51
52 /**
53 * @return {string}
54 */
55 lineAt: function(lineNumber)
56 {
57 var lineEndings = this.lineEndings();
58 var lineStart = lineNumber > 0 ? lineEndings[lineNumber - 1] + 1 : 0;
59 var lineEnd = lineEndings[lineNumber];
60 var lineContent = this._value.substring(lineStart, lineEnd);
61 if (lineContent.length > 0 && lineContent.charAt(lineContent.length - 1) === "\r")
62 lineContent = lineContent.substring(0, lineContent.length - 1);
63 return lineContent;
64 },
65
66 /**
67 * @param {!WebInspector.TextRange} range
68 * @return {!WebInspector.SourceRange}
69 */
70 toSourceRange: function(range)
71 {
72 var start = this.offsetFromPosition(range.startLine, range.startColumn);
73 var end = this.offsetFromPosition(range.endLine, range.endColumn);
74 return new WebInspector.SourceRange(start, end - start);
75 },
76
77 /**
78 * @param {!WebInspector.TextRange} range
79 * @param {string} replacement
80 * @return {string}
81 */
82 replaceRange: function(range, replacement)
83 {
84 var sourceRange = this.toSourceRange(range);
85 return this._value.substring(0, sourceRange.offset) + replacement + this ._value.substring(sourceRange.offset + sourceRange.length);
86 },
87
88 /**
89 * @param {!WebInspector.TextRange} range
90 * @return {string}
91 */
92 extract: function(range)
93 {
94 var sourceRange = this.toSourceRange(range);
95 return this._value.substr(sourceRange.offset, sourceRange.length);
96 },
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698