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

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

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month 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
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the 12 * in the documentation and/or other materials provided with the
13 * distribution. 13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its 14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from 15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission. 16 * this software without specific prior written permission.
17 * 17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 WebInspector.TextUtils = {
31 /**
32 * @param {string} char
33 * @return {boolean}
34 */
35 isStopChar: function(char) {
36 return (char > ' ' && char < '0') || (char > '9' && char < 'A') || (char > ' Z' && char < '_') ||
37 (char > '_' && char < 'a') || (char > 'z' && char <= '~');
38 },
30 39
31 WebInspector.TextUtils = { 40 /**
32 /** 41 * @param {string} char
33 * @param {string} char 42 * @return {boolean}
34 * @return {boolean} 43 */
35 */ 44 isWordChar: function(char) {
36 isStopChar: function(char) 45 return !WebInspector.TextUtils.isStopChar(char) && !WebInspector.TextUtils.i sSpaceChar(char);
37 { 46 },
38 return (char > " " && char < "0") ||
39 (char > "9" && char < "A") ||
40 (char > "Z" && char < "_") ||
41 (char > "_" && char < "a") ||
42 (char > "z" && char <= "~");
43 },
44 47
45 /** 48 /**
46 * @param {string} char 49 * @param {string} char
47 * @return {boolean} 50 * @return {boolean}
48 */ 51 */
49 isWordChar: function(char) 52 isSpaceChar: function(char) {
50 { 53 return WebInspector.TextUtils._SpaceCharRegex.test(char);
51 return !WebInspector.TextUtils.isStopChar(char) && !WebInspector.TextUti ls.isSpaceChar(char); 54 },
52 },
53 55
54 /** 56 /**
55 * @param {string} char 57 * @param {string} word
56 * @return {boolean} 58 * @return {boolean}
57 */ 59 */
58 isSpaceChar: function(char) 60 isWord: function(word) {
59 { 61 for (var i = 0; i < word.length; ++i) {
60 return WebInspector.TextUtils._SpaceCharRegex.test(char); 62 if (!WebInspector.TextUtils.isWordChar(word.charAt(i)))
61 }, 63 return false;
64 }
65 return true;
66 },
62 67
63 /** 68 /**
64 * @param {string} word 69 * @param {string} char
65 * @return {boolean} 70 * @return {boolean}
66 */ 71 */
67 isWord: function(word) 72 isOpeningBraceChar: function(char) {
68 { 73 return char === '(' || char === '{';
69 for (var i = 0; i < word.length; ++i) { 74 },
70 if (!WebInspector.TextUtils.isWordChar(word.charAt(i)))
71 return false;
72 }
73 return true;
74 },
75 75
76 /** 76 /**
77 * @param {string} char 77 * @param {string} char
78 * @return {boolean} 78 * @return {boolean}
79 */ 79 */
80 isOpeningBraceChar: function(char) 80 isClosingBraceChar: function(char) {
81 { 81 return char === ')' || char === '}';
82 return char === "(" || char === "{"; 82 },
83 },
84 83
85 /** 84 /**
86 * @param {string} char 85 * @param {string} char
87 * @return {boolean} 86 * @return {boolean}
88 */ 87 */
89 isClosingBraceChar: function(char) 88 isBraceChar: function(char) {
90 { 89 return WebInspector.TextUtils.isOpeningBraceChar(char) || WebInspector.TextU tils.isClosingBraceChar(char);
91 return char === ")" || char === "}"; 90 },
92 },
93 91
94 /** 92 /**
95 * @param {string} char 93 * @param {string} text
96 * @return {boolean} 94 * @param {function(string):boolean} isWordChar
97 */ 95 * @param {function(string)} wordCallback
98 isBraceChar: function(char) 96 */
99 { 97 textToWords: function(text, isWordChar, wordCallback) {
100 return WebInspector.TextUtils.isOpeningBraceChar(char) || WebInspector.T extUtils.isClosingBraceChar(char); 98 var startWord = -1;
101 }, 99 for (var i = 0; i < text.length; ++i) {
100 if (!isWordChar(text.charAt(i))) {
101 if (startWord !== -1)
102 wordCallback(text.substring(startWord, i));
103 startWord = -1;
104 } else if (startWord === -1)
105 startWord = i;
106 }
107 if (startWord !== -1)
108 wordCallback(text.substring(startWord));
109 },
110
111 /**
112 * @param {string} line
113 * @return {string}
114 */
115 lineIndent: function(line) {
116 var indentation = 0;
117 while (indentation < line.length && WebInspector.TextUtils.isSpaceChar(line. charAt(indentation)))
118 ++indentation;
119 return line.substr(0, indentation);
120 },
121
122 /**
123 * @param {string} text
124 * @return {boolean}
125 */
126 isUpperCase: function(text) {
127 return text === text.toUpperCase();
128 },
129
130 /**
131 * @param {string} text
132 * @return {boolean}
133 */
134 isLowerCase: function(text) {
135 return text === text.toLowerCase();
136 },
137
138 /**
139 * @param {string} text
140 * @param {!Array<!RegExp>} regexes
141 * @return {!Array<{value: string, position: number, regexIndex: number}>}
142 */
143 splitStringByRegexes(text, regexes) {
144 var matches = [];
145 var globalRegexes = [];
146 for (var i = 0; i < regexes.length; i++) {
147 var regex = regexes[i];
148 if (!regex.global)
149 globalRegexes.push(new RegExp(regex.source, regex.flags ? regex.flags + 'g' : 'g'));
150 else
151 globalRegexes.push(regex);
152 }
153 doSplit(text, 0, 0);
154 return matches;
102 155
103 /** 156 /**
104 * @param {string} text 157 * @param {string} text
105 * @param {function(string):boolean} isWordChar 158 * @param {number} regexIndex
106 * @param {function(string)} wordCallback 159 * @param {number} startIndex
107 */ 160 */
108 textToWords: function(text, isWordChar, wordCallback) 161 function doSplit(text, regexIndex, startIndex) {
109 { 162 if (regexIndex >= globalRegexes.length) {
110 var startWord = -1; 163 // Set regexIndex as -1 if text did not match with any regular expressio n
111 for (var i = 0; i < text.length; ++i) { 164 matches.push({value: text, position: startIndex, regexIndex: -1});
112 if (!isWordChar(text.charAt(i))) { 165 return;
113 if (startWord !== -1) 166 }
114 wordCallback(text.substring(startWord, i)); 167 var regex = globalRegexes[regexIndex];
115 startWord = -1; 168 var currentIndex = 0;
116 } else if (startWord === -1) 169 var result;
117 startWord = i; 170 regex.lastIndex = 0;
118 } 171 while ((result = regex.exec(text)) !== null) {
119 if (startWord !== -1) 172 var stringBeforeMatch = text.substring(currentIndex, result.index);
120 wordCallback(text.substring(startWord)); 173 if (stringBeforeMatch)
121 }, 174 doSplit(stringBeforeMatch, regexIndex + 1, startIndex + currentIndex);
122 175 var match = result[0];
123 /** 176 matches.push({value: match, position: startIndex + result.index, regexIn dex: regexIndex});
124 * @param {string} line 177 currentIndex = result.index + match.length;
125 * @return {string} 178 }
126 */ 179 var stringAfterMatches = text.substring(currentIndex);
127 lineIndent: function(line) 180 if (stringAfterMatches)
128 { 181 doSplit(stringAfterMatches, regexIndex + 1, startIndex + currentIndex);
129 var indentation = 0;
130 while (indentation < line.length && WebInspector.TextUtils.isSpaceChar(l ine.charAt(indentation)))
131 ++indentation;
132 return line.substr(0, indentation);
133 },
134
135 /**
136 * @param {string} text
137 * @return {boolean}
138 */
139 isUpperCase: function(text)
140 {
141 return text === text.toUpperCase();
142 },
143
144 /**
145 * @param {string} text
146 * @return {boolean}
147 */
148 isLowerCase: function(text)
149 {
150 return text === text.toLowerCase();
151 },
152
153 /**
154 * @param {string} text
155 * @param {!Array<!RegExp>} regexes
156 * @return {!Array<{value: string, position: number, regexIndex: number}>}
157 */
158 splitStringByRegexes(text, regexes)
159 {
160 var matches = [];
161 var globalRegexes = [];
162 for (var i = 0; i < regexes.length; i++) {
163 var regex = regexes[i];
164 if (!regex.global)
165 globalRegexes.push(new RegExp(regex.source, regex.flags ? regex. flags + "g" : "g"));
166 else
167 globalRegexes.push(regex);
168 }
169 doSplit(text, 0, 0);
170 return matches;
171
172 /**
173 * @param {string} text
174 * @param {number} regexIndex
175 * @param {number} startIndex
176 */
177 function doSplit(text, regexIndex, startIndex)
178 {
179 if (regexIndex >= globalRegexes.length) {
180 // Set regexIndex as -1 if text did not match with any regular e xpression
181 matches.push({
182 value: text,
183 position: startIndex,
184 regexIndex: -1
185 });
186 return;
187 }
188 var regex = globalRegexes[regexIndex];
189 var currentIndex = 0;
190 var result;
191 regex.lastIndex = 0;
192 while ((result = regex.exec(text)) !== null) {
193 var stringBeforeMatch = text.substring(currentIndex, result.inde x);
194 if (stringBeforeMatch)
195 doSplit(stringBeforeMatch, regexIndex + 1, startIndex + curr entIndex);
196 var match = result[0];
197 matches.push({
198 value: match,
199 position: startIndex + result.index,
200 regexIndex: regexIndex
201 });
202 currentIndex = result.index + match.length;
203 }
204 var stringAfterMatches = text.substring(currentIndex);
205 if (stringAfterMatches)
206 doSplit(stringAfterMatches, regexIndex + 1, startIndex + current Index);
207 }
208 } 182 }
183 }
209 }; 184 };
210 185
211 WebInspector.TextUtils._SpaceCharRegex = /\s/; 186 WebInspector.TextUtils._SpaceCharRegex = /\s/;
212 187
213 /** 188 /**
214 * @enum {string} 189 * @enum {string}
215 */ 190 */
216 WebInspector.TextUtils.Indent = { 191 WebInspector.TextUtils.Indent = {
217 TwoSpaces: " ", 192 TwoSpaces: ' ',
218 FourSpaces: " ", 193 FourSpaces: ' ',
219 EightSpaces: " ", 194 EightSpaces: ' ',
220 TabCharacter: "\t" 195 TabCharacter: '\t'
221 }; 196 };
222 197
223 /** 198 /**
224 * @constructor 199 * @unrestricted
225 * @param {function(string)} callback
226 * @param {boolean=} findMultiple
227 */ 200 */
228 WebInspector.TextUtils.BalancedJSONTokenizer = function(callback, findMultiple) 201 WebInspector.TextUtils.BalancedJSONTokenizer = class {
229 { 202 /**
203 * @param {function(string)} callback
204 * @param {boolean=} findMultiple
205 */
206 constructor(callback, findMultiple) {
230 this._callback = callback; 207 this._callback = callback;
231 this._index = 0; 208 this._index = 0;
232 this._balance = 0; 209 this._balance = 0;
233 this._buffer = ""; 210 this._buffer = '';
234 this._findMultiple = findMultiple || false; 211 this._findMultiple = findMultiple || false;
235 this._closingDoubleQuoteRegex = /[^\\](?:\\\\)*"/g; 212 this._closingDoubleQuoteRegex = /[^\\](?:\\\\)*"/g;
236 }; 213 }
237 214
238 WebInspector.TextUtils.BalancedJSONTokenizer.prototype = { 215 /**
239 /** 216 * @param {string} chunk
240 * @param {string} chunk 217 * @return {boolean}
241 * @return {boolean} 218 */
242 */ 219 write(chunk) {
243 write: function(chunk) 220 this._buffer += chunk;
244 { 221 var lastIndex = this._buffer.length;
245 this._buffer += chunk; 222 var buffer = this._buffer;
246 var lastIndex = this._buffer.length; 223 for (var index = this._index; index < lastIndex; ++index) {
247 var buffer = this._buffer; 224 var character = buffer[index];
248 for (var index = this._index; index < lastIndex; ++index) { 225 if (character === '"') {
249 var character = buffer[index]; 226 this._closingDoubleQuoteRegex.lastIndex = index;
250 if (character === "\"") { 227 if (!this._closingDoubleQuoteRegex.test(buffer))
251 this._closingDoubleQuoteRegex.lastIndex = index; 228 break;
252 if (!this._closingDoubleQuoteRegex.test(buffer)) 229 index = this._closingDoubleQuoteRegex.lastIndex - 1;
253 break; 230 } else if (character === '{') {
254 index = this._closingDoubleQuoteRegex.lastIndex - 1; 231 ++this._balance;
255 } else if (character === "{") { 232 } else if (character === '}') {
256 ++this._balance; 233 --this._balance;
257 } else if (character === "}") { 234 if (this._balance < 0) {
258 --this._balance; 235 this._reportBalanced();
259 if (this._balance < 0) { 236 return false;
260 this._reportBalanced();
261 return false;
262 }
263 if (!this._balance) {
264 this._lastBalancedIndex = index + 1;
265 if (!this._findMultiple)
266 break;
267 }
268 } else if (character === "]" && !this._balance) {
269 this._reportBalanced();
270 return false;
271 }
272 } 237 }
273 this._index = index; 238 if (!this._balance) {
239 this._lastBalancedIndex = index + 1;
240 if (!this._findMultiple)
241 break;
242 }
243 } else if (character === ']' && !this._balance) {
274 this._reportBalanced(); 244 this._reportBalanced();
275 return true; 245 return false;
276 }, 246 }
247 }
248 this._index = index;
249 this._reportBalanced();
250 return true;
251 }
277 252
278 _reportBalanced: function() 253 _reportBalanced() {
279 { 254 if (!this._lastBalancedIndex)
280 if (!this._lastBalancedIndex) 255 return;
281 return; 256 this._callback(this._buffer.slice(0, this._lastBalancedIndex));
282 this._callback(this._buffer.slice(0, this._lastBalancedIndex)); 257 this._buffer = this._buffer.slice(this._lastBalancedIndex);
283 this._buffer = this._buffer.slice(this._lastBalancedIndex); 258 this._index -= this._lastBalancedIndex;
284 this._index -= this._lastBalancedIndex; 259 this._lastBalancedIndex = 0;
285 this._lastBalancedIndex = 0; 260 }
286 },
287 261
288 /** 262 /**
289 * @return {string} 263 * @return {string}
290 */ 264 */
291 remainder: function() 265 remainder() {
292 { 266 return this._buffer;
293 return this._buffer; 267 }
294 }
295 }; 268 };
296 269
297 /** 270 /**
298 * @interface 271 * @interface
299 */ 272 */
300 WebInspector.TokenizerFactory = function() { }; 273 WebInspector.TokenizerFactory = function() {};
301 274
302 WebInspector.TokenizerFactory.prototype = { 275 WebInspector.TokenizerFactory.prototype = {
303 /** 276 /**
304 * @param {string} mimeType 277 * @param {string} mimeType
305 * @return {function(string, function(string, ?string, number, number))} 278 * @return {function(string, function(string, ?string, number, number))}
306 */ 279 */
307 createTokenizer: function(mimeType) { } 280 createTokenizer: function(mimeType) {}
308 }; 281 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698