OLD | NEW |
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 Loading... |
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 Loading... |
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 Loading... |
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 text = this.edits[i].applyToText(text); |
275 return text; | 275 return text; |
276 | 276 |
277 /** | 277 /** |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 this.disabled = disabled; | 415 this.disabled = disabled; |
416 if (disabled) { | 416 if (disabled) { |
417 var oldRange1 = WebInspector.TextRange.createFromLocation(this.range
.startLine, this.range.startColumn); | 417 var oldRange1 = WebInspector.TextRange.createFromLocation(this.range
.startLine, this.range.startColumn); |
418 var edit1 = new WebInspector.SourceEdit(this.document.url, oldRange1
, "/* "); | 418 var edit1 = new WebInspector.SourceEdit(this.document.url, oldRange1
, "/* "); |
419 var oldRange2 = WebInspector.TextRange.createFromLocation(this.range
.endLine, this.range.endColumn); | 419 var oldRange2 = WebInspector.TextRange.createFromLocation(this.range
.endLine, this.range.endColumn); |
420 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2
, " */"); | 420 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2
, " */"); |
421 this.document.edits.push(edit1, edit2); | 421 this.document.edits.push(edit1, edit2); |
422 return; | 422 return; |
423 } | 423 } |
424 var oldRange1 = new WebInspector.TextRange(this.range.startLine, this.ra
nge.startColumn, this.range.startLine, this.name.range.startColumn); | 424 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, ""
); | 425 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); | 426 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, ""
); | 427 var edit2 = new WebInspector.SourceEdit(this.document.url, oldRange2, ""
); |
429 this.document.edits.push(edit1, edit2); | 428 this.document.edits.push(edit1, edit2); |
430 }, | 429 }, |
431 | 430 |
432 remove: function() | 431 remove: function() |
433 { | 432 { |
434 console.assert(this.parent); | 433 console.assert(this.parent); |
435 var rule = this.parent; | 434 var rule = this.parent; |
436 var index = rule.properties.indexOf(this); | 435 var index = rule.properties.indexOf(this); |
437 rule.properties.splice(index, 1); | 436 rule.properties.splice(index, 1); |
438 this.parent = null; | 437 this.parent = null; |
439 | 438 |
440 var lineRange = new WebInspector.TextRange(this.range.startLine, 0, this
.range.endLine + 1, 0); | 439 var lineRange = new WebInspector.TextRange(this.range.startLine, 0, this
.range.endLine + 1, 0); |
441 var oldRange; | 440 var oldRange; |
442 if (lineRange.extract(this.document.text).trim() === this.range.extract(
this.document.text).trim()) | 441 if (this.document.text.extract(lineRange).trim() === this.document.text.
extract(this.range).trim()) |
443 oldRange = lineRange; | 442 oldRange = lineRange; |
444 else | 443 else |
445 oldRange = this.range; | 444 oldRange = this.range; |
446 this.document.edits.push(new WebInspector.SourceEdit(this.document.url,
oldRange, "")); | 445 this.document.edits.push(new WebInspector.SourceEdit(this.document.url,
oldRange, "")); |
447 }, | 446 }, |
448 | 447 |
449 __proto__: WebInspector.SASSSupport.Node.prototype | 448 __proto__: WebInspector.SASSSupport.Node.prototype |
450 } | 449 } |
451 | 450 |
452 /** | 451 /** |
453 * @constructor | 452 * @constructor |
454 * @extends {WebInspector.SASSSupport.Node} | 453 * @extends {WebInspector.SASSSupport.Node} |
455 * @param {!WebInspector.SASSSupport.ASTDocument} document | 454 * @param {!WebInspector.SASSSupport.ASTDocument} document |
456 * @param {string} selector | 455 * @param {string} selector |
457 * @param {!WebInspector.TextRange} styleRange | 456 * @param {!WebInspector.TextRange} styleRange |
458 * @param {!Array<!WebInspector.SASSSupport.Property>} properties | 457 * @param {!Array<!WebInspector.SASSSupport.Property>} properties |
459 */ | 458 */ |
460 WebInspector.SASSSupport.Rule = function(document, selector, styleRange, propert
ies) | 459 WebInspector.SASSSupport.Rule = function(document, selector, styleRange, propert
ies) |
461 { | 460 { |
462 WebInspector.SASSSupport.Node.call(this, document); | 461 WebInspector.SASSSupport.Node.call(this, document); |
463 this.selector = selector; | 462 this.selector = selector; |
464 this.properties = properties; | 463 this.properties = properties; |
465 this.styleRange = styleRange; | 464 this.styleRange = styleRange; |
466 for (var i = 0; i < this.properties.length; ++i) | 465 for (var i = 0; i < this.properties.length; ++i) |
467 this.properties[i].parent = this; | 466 this.properties[i].parent = this; |
468 | 467 |
469 this._hasTrailingSemicolon = !this.properties.length || this.properties.peek
Last().range.extract(this.document.text).endsWith(";"); | 468 this._hasTrailingSemicolon = !this.properties.length || this.document.text.e
xtract(this.properties.peekLast().range).endsWith(";"); |
470 } | 469 } |
471 | 470 |
472 WebInspector.SASSSupport.Rule.prototype = { | 471 WebInspector.SASSSupport.Rule.prototype = { |
473 /** | 472 /** |
474 * @param {!WebInspector.SASSSupport.ASTDocument} document | 473 * @param {!WebInspector.SASSSupport.ASTDocument} document |
475 * @return {!WebInspector.SASSSupport.Rule} | 474 * @return {!WebInspector.SASSSupport.Rule} |
476 */ | 475 */ |
477 clone: function(document) | 476 clone: function(document) |
478 { | 477 { |
479 var properties = []; | 478 var properties = []; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 * @param {string} nameText | 556 * @param {string} nameText |
558 * @param {string} valueText | 557 * @param {string} valueText |
559 * @param {boolean} disabled | 558 * @param {boolean} disabled |
560 * @param {!WebInspector.SASSSupport.Property} anchorProperty | 559 * @param {!WebInspector.SASSSupport.Property} anchorProperty |
561 * @param {boolean} insertBefore | 560 * @param {boolean} insertBefore |
562 * @return {!WebInspector.SourceEdit} | 561 * @return {!WebInspector.SourceEdit} |
563 */ | 562 */ |
564 _insertPropertyEdit: function(nameText, valueText, disabled, anchorProperty,
insertBefore) | 563 _insertPropertyEdit: function(nameText, valueText, disabled, anchorProperty,
insertBefore) |
565 { | 564 { |
566 var oldRange = insertBefore ? anchorProperty.range.collapseToStart() : a
nchorProperty.range.collapseToEnd(); | 565 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); | 566 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 = ""; | 567 if (!/^\s+$/.test(indent)) indent = ""; |
569 | 568 |
570 var newText = ""; | 569 var newText = ""; |
571 var leftComment = disabled ? "/* " : ""; | 570 var leftComment = disabled ? "/* " : ""; |
572 var rightComment = disabled ? " */" : ""; | 571 var rightComment = disabled ? " */" : ""; |
573 | 572 |
574 if (insertBefore) { | 573 if (insertBefore) { |
575 newText = String.sprintf("%s%s: %s;%s\n%s", leftComment, nameText, v
alueText, rightComment, indent); | 574 newText = String.sprintf("%s%s: %s;%s\n%s", leftComment, nameText, v
alueText, rightComment, indent); |
576 } else { | 575 } else { |
577 newText = String.sprintf("\n%s%s%s: %s;%s", indent, leftComment, nam
eText, valueText, rightComment); | 576 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 Loading... |
803 mapping.set(oldProperty.name, newProperty.name); | 802 mapping.set(oldProperty.name, newProperty.name); |
804 mapping.set(oldProperty.value, newProperty.value); | 803 mapping.set(oldProperty.value, newProperty.value); |
805 if (oldProperty.name.text.trim() !== newProperty.name.text.trim()) | 804 if (oldProperty.name.text.trim() !== newProperty.name.text.trim()) |
806 addChange(T.NameChanged, oldRule, newRule, oldPropertyIndex, newProp
ertyIndex); | 805 addChange(T.NameChanged, oldRule, newRule, oldPropertyIndex, newProp
ertyIndex); |
807 if (oldProperty.value.text.trim() !== newProperty.value.text.trim()) | 806 if (oldProperty.value.text.trim() !== newProperty.value.text.trim()) |
808 addChange(T.ValueChanged, oldRule, newRule, oldPropertyIndex, newPro
pertyIndex); | 807 addChange(T.ValueChanged, oldRule, newRule, oldPropertyIndex, newPro
pertyIndex); |
809 if (oldProperty.disabled !== newProperty.disabled) | 808 if (oldProperty.disabled !== newProperty.disabled) |
810 addChange(T.PropertyToggled, oldRule, newRule, oldPropertyIndex, new
PropertyIndex); | 809 addChange(T.PropertyToggled, oldRule, newRule, oldPropertyIndex, new
PropertyIndex); |
811 } | 810 } |
812 } | 811 } |
OLD | NEW |