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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sass/SASSSupport.js

Issue 1809533003: DevTools: remove illusionary caching from String.prototype.lineEndings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 WebInspector.SASSSupport = {} 5 WebInspector.SASSSupport = {}
6 6
7 /** 7 /**
8 * @param {!WebInspector.CSSParserService} cssParserService 8 * @param {!WebInspector.CSSParserService} cssParserService
9 * @param {string} url 9 * @param {string} url
10 * @param {string} text 10 * @param {string} text
11 * @return {!Promise<!WebInspector.SASSSupport.AST>} 11 * @return {!Promise<!WebInspector.SASSSupport.AST>}
12 */ 12 */
13 WebInspector.SASSSupport.parseCSS = function(cssParserService, url, text) 13 WebInspector.SASSSupport.parseCSS = function(cssParserService, url, text)
14 { 14 {
15 return cssParserService.parseCSS(text) 15 return cssParserService.parseCSS(text)
16 .then(onParsed); 16 .then(onParsed);
17 17
18 /** 18 /**
19 * @param {!Array.<!WebInspector.CSSParser.Rule>} parsedCSS 19 * @param {!Array.<!WebInspector.CSSParser.Rule>} parsedCSS
20 * @return {!WebInspector.SASSSupport.AST} 20 * @return {!WebInspector.SASSSupport.AST}
21 */ 21 */
22 function onParsed(parsedCSS) 22 function onParsed(parsedCSS)
23 { 23 {
24 var document = new WebInspector.SASSSupport.ASTDocument(url, text); 24 var document = new WebInspector.SASSSupport.ASTDocument(url, new WebInsp ector.Text(text));
25 var rules = []; 25 var rules = [];
26 for (var i = 0; i < parsedCSS.length; ++i) { 26 for (var i = 0; i < parsedCSS.length; ++i) {
27 var rule = parsedCSS[i]; 27 var rule = parsedCSS[i];
28 if (!rule.properties) 28 if (!rule.properties)
29 continue; 29 continue;
30 var properties = []; 30 var properties = [];
31 for (var j = 0; j < rule.properties.length; ++j) { 31 for (var j = 0; j < rule.properties.length; ++j) {
32 var cssProperty = rule.properties[j]; 32 var cssProperty = rule.properties[j];
33 var name = new WebInspector.SASSSupport.TextNode(document, cssPr operty.name, WebInspector.TextRange.fromObject(cssProperty.nameRange)); 33 var name = new WebInspector.SASSSupport.TextNode(document, cssPr operty.name, WebInspector.TextRange.fromObject(cssProperty.nameRange));
34 var value = new WebInspector.SASSSupport.TextNode(document, cssP roperty.value, WebInspector.TextRange.fromObject(cssProperty.valueRange)); 34 var value = new WebInspector.SASSSupport.TextNode(document, cssP roperty.value, WebInspector.TextRange.fromObject(cssProperty.valueRange));
35 var property = new WebInspector.SASSSupport.Property(document, n ame, value, WebInspector.TextRange.fromObject(cssProperty.range), !!cssProperty. disabled); 35 var property = new WebInspector.SASSSupport.Property(document, n ame, value, WebInspector.TextRange.fromObject(cssProperty.range), !!cssProperty. disabled);
36 properties.push(property); 36 properties.push(property);
37 } 37 }
38 rules.push(new WebInspector.SASSSupport.Rule(document, rule.selector Text, WebInspector.TextRange.fromObject(rule.styleRange), properties)); 38 rules.push(new WebInspector.SASSSupport.Rule(document, rule.selector Text, WebInspector.TextRange.fromObject(rule.styleRange), properties));
39 } 39 }
40 return new WebInspector.SASSSupport.AST(document, rules); 40 return new WebInspector.SASSSupport.AST(document, rules);
41 } 41 }
42 } 42 }
43 43
44 /** 44 /**
45 * @param {!WebInspector.TokenizerFactory} tokenizerFactory 45 * @param {!WebInspector.TokenizerFactory} tokenizerFactory
46 * @param {string} url 46 * @param {string} url
47 * @param {string} text 47 * @param {string} text
48 * @return {!WebInspector.SASSSupport.AST} 48 * @return {!WebInspector.SASSSupport.AST}
49 */ 49 */
50 WebInspector.SASSSupport.parseSCSS = function(tokenizerFactory, url, text) 50 WebInspector.SASSSupport.parseSCSS = function(tokenizerFactory, url, text)
51 { 51 {
52 var document = new WebInspector.SASSSupport.ASTDocument(url, text); 52 var document = new WebInspector.SASSSupport.ASTDocument(url, new WebInspecto r.Text(text));
53 var result = WebInspector.SASSSupport._innerParseSCSS(document, tokenizerFac tory); 53 var result = WebInspector.SASSSupport._innerParseSCSS(document, tokenizerFac tory);
54 54
55 var rules = [ 55 var rules = [
56 new WebInspector.SASSSupport.Rule(document, "variables", WebInspector.Te xtRange.createFromLocation(0, 0), result.variables), 56 new WebInspector.SASSSupport.Rule(document, "variables", WebInspector.Te xtRange.createFromLocation(0, 0), result.variables),
57 new WebInspector.SASSSupport.Rule(document, "properties", WebInspector.T extRange.createFromLocation(0, 0), result.properties), 57 new WebInspector.SASSSupport.Rule(document, "properties", WebInspector.T extRange.createFromLocation(0, 0), result.properties),
58 new WebInspector.SASSSupport.Rule(document, "mixins", WebInspector.TextR ange.createFromLocation(0, 0), result.mixins) 58 new WebInspector.SASSSupport.Rule(document, "mixins", WebInspector.TextR ange.createFromLocation(0, 0), result.mixins)
59 ]; 59 ];
60 60
61 return new WebInspector.SASSSupport.AST(document, rules); 61 return new WebInspector.SASSSupport.AST(document, rules);
62 } 62 }
(...skipping 10 matching lines...) Expand all
73 Media: "Media" 73 Media: "Media"
74 } 74 }
75 75
76 /** 76 /**
77 * @param {!WebInspector.SASSSupport.ASTDocument} document 77 * @param {!WebInspector.SASSSupport.ASTDocument} document
78 * @param {!WebInspector.TokenizerFactory} tokenizerFactory 78 * @param {!WebInspector.TokenizerFactory} tokenizerFactory
79 * @return {!{variables: !Array<!WebInspector.SASSSupport.Property>, properties: !Array<!WebInspector.SASSSupport.Property>, mixins: !Array<!WebInspector.SASSSu pport.Property>}} 79 * @return {!{variables: !Array<!WebInspector.SASSSupport.Property>, properties: !Array<!WebInspector.SASSSupport.Property>, mixins: !Array<!WebInspector.SASSSu pport.Property>}}
80 */ 80 */
81 WebInspector.SASSSupport._innerParseSCSS = function(document, tokenizerFactory) 81 WebInspector.SASSSupport._innerParseSCSS = function(document, tokenizerFactory)
82 { 82 {
83 var lines = document.text.split("\n"); 83 var lines = document.text.value().split("\n");
84 var properties = []; 84 var properties = [];
85 var variables = []; 85 var variables = [];
86 var mixins = []; 86 var mixins = [];
87 87
88 var States = WebInspector.SASSSupport.SCSSParserStates; 88 var States = WebInspector.SASSSupport.SCSSParserStates;
89 var state = States.Initial; 89 var state = States.Initial;
90 var propertyName, propertyValue; 90 var propertyName, propertyValue;
91 var variableName, variableValue; 91 var variableName, variableValue;
92 var mixinName, mixinValue; 92 var mixinName, mixinValue;
93 var UndefTokenType = {}; 93 var UndefTokenType = {};
(...skipping 17 matching lines...) Expand all
111 state = States.PropertyName; 111 state = States.PropertyName;
112 } else if (tokenType["css-def"] && tokenValue === "@include") { 112 } else if (tokenType["css-def"] && tokenValue === "@include") {
113 mixinName = new WebInspector.SASSSupport.TextNode(document, toke nValue, new WebInspector.TextRange(lineNumber, column, lineNumber, newColumn)); 113 mixinName = new WebInspector.SASSSupport.TextNode(document, toke nValue, new WebInspector.TextRange(lineNumber, column, lineNumber, newColumn));
114 state = States.MixinName; 114 state = States.MixinName;
115 } else if (tokenType["css-comment"]) { 115 } else if (tokenType["css-comment"]) {
116 // Support only a one-line comments. 116 // Support only a one-line comments.
117 if (tokenValue.substring(0, 2) !== "/*" || tokenValue.substring( tokenValue.length - 2) !== "*/") 117 if (tokenValue.substring(0, 2) !== "/*" || tokenValue.substring( tokenValue.length - 2) !== "*/")
118 break; 118 break;
119 var uncommentedText = tokenValue.substring(2, tokenValue.length - 2); 119 var uncommentedText = tokenValue.substring(2, tokenValue.length - 2);
120 var fakeRuleText = "a{\n" + uncommentedText + "}"; 120 var fakeRuleText = "a{\n" + uncommentedText + "}";
121 var fakeDocument = new WebInspector.SASSSupport.ASTDocument("", fakeRuleText); 121 var fakeDocument = new WebInspector.SASSSupport.ASTDocument("", new WebInspector.Text(fakeRuleText));
122 var result = WebInspector.SASSSupport._innerParseSCSS(fakeDocume nt, tokenizerFactory); 122 var result = WebInspector.SASSSupport._innerParseSCSS(fakeDocume nt, tokenizerFactory);
123 if (result.properties.length === 1 && result.variables.length == = 0 && result.mixins.length === 0) { 123 if (result.properties.length === 1 && result.variables.length == = 0 && result.mixins.length === 0) {
124 var disabledProperty = result.properties[0]; 124 var disabledProperty = result.properties[0];
125 // We should offset property to current coordinates. 125 // We should offset property to current coordinates.
126 var offset = column + 2; 126 var offset = column + 2;
127 var nameRange = new WebInspector.TextRange(lineNumber, disab ledProperty.name.range.startColumn + offset, 127 var nameRange = new WebInspector.TextRange(lineNumber, disab ledProperty.name.range.startColumn + offset,
128 lineNumber, disabledProperty.name.range.endColumn + offset); 128 lineNumber, disabledProperty.name.range.endColumn + offset);
129 var valueRange = new WebInspector.TextRange(lineNumber, disa bledProperty.value.range.startColumn + offset, 129 var valueRange = new WebInspector.TextRange(lineNumber, disa bledProperty.value.range.startColumn + offset,
130 lineNumber, disabledProperty.value.range.endColumn + offset); 130 lineNumber, disabledProperty.value.range.endColumn + offset);
131 var name = new WebInspector.SASSSupport.TextNode(document, d isabledProperty.name.text, nameRange); 131 var name = new WebInspector.SASSSupport.TextNode(document, d isabledProperty.name.text, nameRange);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 return { 230 return {
231 variables: variables, 231 variables: variables,
232 properties: properties, 232 properties: properties,
233 mixins: mixins 233 mixins: mixins
234 }; 234 };
235 } 235 }
236 236
237 /** 237 /**
238 * @constructor 238 * @constructor
239 * @param {string} url 239 * @param {string} url
240 * @param {string} text 240 * @param {!WebInspector.Text} text
241 */ 241 */
242 WebInspector.SASSSupport.ASTDocument = function(url, text) 242 WebInspector.SASSSupport.ASTDocument = function(url, text)
243 { 243 {
244 this.url = url; 244 this.url = url;
245 this.text = text; 245 this.text = text;
246 this.edits = []; 246 this.edits = [];
247 } 247 }
248 248
249 WebInspector.SASSSupport.ASTDocument.prototype = { 249 WebInspector.SASSSupport.ASTDocument.prototype = {
250 /** 250 /**
251 * @return {!WebInspector.SASSSupport.ASTDocument} 251 * @return {!WebInspector.SASSSupport.ASTDocument}
252 */ 252 */
253 clone: function() 253 clone: function()
254 { 254 {
255 return new WebInspector.SASSSupport.ASTDocument(this.url, this.text); 255 return new WebInspector.SASSSupport.ASTDocument(this.url, this.text);
256 }, 256 },
257 257
258 /** 258 /**
259 * @return {boolean} 259 * @return {boolean}
260 */ 260 */
261 hasChanged: function() 261 hasChanged: function()
262 { 262 {
263 return !!this.edits.length; 263 return !!this.edits.length;
264 }, 264 },
265 265
266 /** 266 /**
267 * @return {string} 267 * @return {!WebInspector.Text}
268 */ 268 */
269 newText: function() 269 newText: function()
270 { 270 {
271 this.edits.stableSort(sequentialOrder); 271 this.edits.stableSort(sequentialOrder);
272 var text = this.text; 272 var text = this.text;
273 for (var i = this.edits.length - 1; i >= 0; --i) 273 for (var i = this.edits.length - 1; i >= 0; --i) {
274 text = this.edits[i].applyToText(text); 274 var range = this.edits[i].oldRange;
275 var newText = this.edits[i].newText;
276 text = new WebInspector.Text(text.replaceRange(range, newText));
277 }
275 return text; 278 return text;
276 279
277 /** 280 /**
278 * @param {!WebInspector.SourceEdit} edit1 281 * @param {!WebInspector.SourceEdit} edit1
279 * @param {!WebInspector.SourceEdit} edit2 282 * @param {!WebInspector.SourceEdit} edit2
280 * @return {number} 283 * @return {number}
281 */ 284 */
282 function sequentialOrder(edit1, edit2) 285 function sequentialOrder(edit1, edit2)
283 { 286 {
284 var range1 = edit1.oldRange.collapseToStart(); 287 var range1 = edit1.oldRange.collapseToStart();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 this.disabled = disabled; 418 this.disabled = disabled;
416 if (disabled) { 419 if (disabled) {
417 var oldRange1 = WebInspector.TextRange.createFromLocation(this.range .startLine, this.range.startColumn); 420 var oldRange1 = WebInspector.TextRange.createFromLocation(this.range .startLine, this.range.startColumn);
418 var edit1 = new WebInspector.SourceEdit(this.document.url, oldRange1 , "/* "); 421 var edit1 = new WebInspector.SourceEdit(this.document.url, oldRange1 , "/* ");
419 var oldRange2 = WebInspector.TextRange.createFromLocation(this.range .endLine, this.range.endColumn); 422 var oldRange2 = WebInspector.TextRange.createFromLocation(this.range .endLine, this.range.endColumn);
420 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2 , " */"); 423 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2 , " */");
421 this.document.edits.push(edit1, edit2); 424 this.document.edits.push(edit1, edit2);
422 return; 425 return;
423 } 426 }
424 var oldRange1 = new WebInspector.TextRange(this.range.startLine, this.ra nge.startColumn, this.range.startLine, this.name.range.startColumn); 427 var oldRange1 = new WebInspector.TextRange(this.range.startLine, this.ra nge.startColumn, this.range.startLine, this.name.range.startColumn);
425 var text = this.document.text;
426 var edit1 = new WebInspector.SourceEdit(this.document.url, oldRange1, "" ); 428 var edit1 = new WebInspector.SourceEdit(this.document.url, oldRange1, "" );
427 var oldRange2 = new WebInspector.TextRange(this.range.endLine, this.rang e.endColumn - 2, this.range.endLine, this.range.endColumn); 429 var oldRange2 = new WebInspector.TextRange(this.range.endLine, this.rang e.endColumn - 2, this.range.endLine, this.range.endColumn);
428 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2, "" ); 430 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2, "" );
429 this.document.edits.push(edit1, edit2); 431 this.document.edits.push(edit1, edit2);
430 }, 432 },
431 433
432 remove: function() 434 remove: function()
433 { 435 {
434 console.assert(this.parent); 436 console.assert(this.parent);
435 var rule = this.parent; 437 var rule = this.parent;
436 var index = rule.properties.indexOf(this); 438 var index = rule.properties.indexOf(this);
437 rule.properties.splice(index, 1); 439 rule.properties.splice(index, 1);
438 this.parent = null; 440 this.parent = null;
439 441
440 var lineRange = new WebInspector.TextRange(this.range.startLine, 0, this .range.endLine + 1, 0); 442 var lineRange = new WebInspector.TextRange(this.range.startLine, 0, this .range.endLine + 1, 0);
441 var oldRange; 443 var oldRange;
442 if (lineRange.extract(this.document.text).trim() === this.range.extract( this.document.text).trim()) 444 if (this.document.text.extract(lineRange).trim() === this.document.text. extract(this.range).trim())
443 oldRange = lineRange; 445 oldRange = lineRange;
444 else 446 else
445 oldRange = this.range; 447 oldRange = this.range;
446 this.document.edits.push(new WebInspector.SourceEdit(this.document.url, oldRange, "")); 448 this.document.edits.push(new WebInspector.SourceEdit(this.document.url, oldRange, ""));
447 }, 449 },
448 450
449 __proto__: WebInspector.SASSSupport.Node.prototype 451 __proto__: WebInspector.SASSSupport.Node.prototype
450 } 452 }
451 453
452 /** 454 /**
453 * @constructor 455 * @constructor
454 * @extends {WebInspector.SASSSupport.Node} 456 * @extends {WebInspector.SASSSupport.Node}
455 * @param {!WebInspector.SASSSupport.ASTDocument} document 457 * @param {!WebInspector.SASSSupport.ASTDocument} document
456 * @param {string} selector 458 * @param {string} selector
457 * @param {!WebInspector.TextRange} styleRange 459 * @param {!WebInspector.TextRange} styleRange
458 * @param {!Array<!WebInspector.SASSSupport.Property>} properties 460 * @param {!Array<!WebInspector.SASSSupport.Property>} properties
459 */ 461 */
460 WebInspector.SASSSupport.Rule = function(document, selector, styleRange, propert ies) 462 WebInspector.SASSSupport.Rule = function(document, selector, styleRange, propert ies)
461 { 463 {
462 WebInspector.SASSSupport.Node.call(this, document); 464 WebInspector.SASSSupport.Node.call(this, document);
463 this.selector = selector; 465 this.selector = selector;
464 this.properties = properties; 466 this.properties = properties;
465 this.styleRange = styleRange; 467 this.styleRange = styleRange;
466 for (var i = 0; i < this.properties.length; ++i) 468 for (var i = 0; i < this.properties.length; ++i)
467 this.properties[i].parent = this; 469 this.properties[i].parent = this;
468 470
469 this._hasTrailingSemicolon = !this.properties.length || this.properties.peek Last().range.extract(this.document.text).endsWith(";"); 471 this._hasTrailingSemicolon = !this.properties.length || this.document.text.e xtract(this.properties.peekLast().range).endsWith(";");
470 } 472 }
471 473
472 WebInspector.SASSSupport.Rule.prototype = { 474 WebInspector.SASSSupport.Rule.prototype = {
473 /** 475 /**
474 * @param {!WebInspector.SASSSupport.ASTDocument} document 476 * @param {!WebInspector.SASSSupport.ASTDocument} document
475 * @return {!WebInspector.SASSSupport.Rule} 477 * @return {!WebInspector.SASSSupport.Rule}
476 */ 478 */
477 clone: function(document) 479 clone: function(document)
478 { 480 {
479 var properties = []; 481 var properties = [];
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 * @param {string} nameText 559 * @param {string} nameText
558 * @param {string} valueText 560 * @param {string} valueText
559 * @param {boolean} disabled 561 * @param {boolean} disabled
560 * @param {!WebInspector.SASSSupport.Property} anchorProperty 562 * @param {!WebInspector.SASSSupport.Property} anchorProperty
561 * @param {boolean} insertBefore 563 * @param {boolean} insertBefore
562 * @return {!WebInspector.SourceEdit} 564 * @return {!WebInspector.SourceEdit}
563 */ 565 */
564 _insertPropertyEdit: function(nameText, valueText, disabled, anchorProperty, insertBefore) 566 _insertPropertyEdit: function(nameText, valueText, disabled, anchorProperty, insertBefore)
565 { 567 {
566 var oldRange = insertBefore ? anchorProperty.range.collapseToStart() : a nchorProperty.range.collapseToEnd(); 568 var oldRange = insertBefore ? anchorProperty.range.collapseToStart() : a nchorProperty.range.collapseToEnd();
567 var indent = (new WebInspector.TextRange(anchorProperty.range.startLine, 0, anchorProperty.range.startLine, anchorProperty.range.startColumn)).extract(t his.document.text); 569 var indent = this.document.text.extract(new WebInspector.TextRange(ancho rProperty.range.startLine, 0, anchorProperty.range.startLine, anchorProperty.ran ge.startColumn));
568 if (!/^\s+$/.test(indent)) indent = ""; 570 if (!/^\s+$/.test(indent)) indent = "";
569 571
570 var newText = ""; 572 var newText = "";
571 var leftComment = disabled ? "/* " : ""; 573 var leftComment = disabled ? "/* " : "";
572 var rightComment = disabled ? " */" : ""; 574 var rightComment = disabled ? " */" : "";
573 575
574 if (insertBefore) { 576 if (insertBefore) {
575 newText = String.sprintf("%s%s: %s;%s\n%s", leftComment, nameText, v alueText, rightComment, indent); 577 newText = String.sprintf("%s%s: %s;%s\n%s", leftComment, nameText, v alueText, rightComment, indent);
576 } else { 578 } else {
577 newText = String.sprintf("\n%s%s%s: %s;%s", indent, leftComment, nam eText, valueText, rightComment); 579 newText = String.sprintf("\n%s%s%s: %s;%s", indent, leftComment, nam eText, valueText, rightComment);
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 mapping.set(oldProperty.name, newProperty.name); 805 mapping.set(oldProperty.name, newProperty.name);
804 mapping.set(oldProperty.value, newProperty.value); 806 mapping.set(oldProperty.value, newProperty.value);
805 if (oldProperty.name.text.trim() !== newProperty.name.text.trim()) 807 if (oldProperty.name.text.trim() !== newProperty.name.text.trim())
806 addChange(T.NameChanged, oldRule, newRule, oldPropertyIndex, newProp ertyIndex); 808 addChange(T.NameChanged, oldRule, newRule, oldPropertyIndex, newProp ertyIndex);
807 if (oldProperty.value.text.trim() !== newProperty.value.text.trim()) 809 if (oldProperty.value.text.trim() !== newProperty.value.text.trim())
808 addChange(T.ValueChanged, oldRule, newRule, oldPropertyIndex, newPro pertyIndex); 810 addChange(T.ValueChanged, oldRule, newRule, oldPropertyIndex, newPro pertyIndex);
809 if (oldProperty.disabled !== newProperty.disabled) 811 if (oldProperty.disabled !== newProperty.disabled)
810 addChange(T.PropertyToggled, oldRule, newRule, oldPropertyIndex, new PropertyIndex); 812 addChange(T.PropertyToggled, oldRule, newRule, oldPropertyIndex, new PropertyIndex);
811 } 813 }
812 } 814 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698