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

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

Issue 1504923008: DevTools: [SASS] implement AST differ. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sass-module-2
Patch Set: Created 5 years 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
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/diff/Diff.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.CSSParser} parser 8 * @param {!WebInspector.CSSParser} parser
9 * @param {string} url 9 * @param {string} url
10 * @param {string} text 10 * @param {string} text
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 { 363 {
364 var rules = []; 364 var rules = [];
365 for (var i = 0; i < this.rules.length; ++i) 365 for (var i = 0; i < this.rules.length; ++i)
366 rules.push(this.rules[i].clone()); 366 rules.push(this.rules[i].clone());
367 return new WebInspector.SASSSupport.AST(this.document, rules); 367 return new WebInspector.SASSSupport.AST(this.document, rules);
368 }, 368 },
369 369
370 __proto__: WebInspector.SASSSupport.Node.prototype 370 __proto__: WebInspector.SASSSupport.Node.prototype
371 } 371 }
372 372
373
374 /** @enum {string} */
375 WebInspector.SASSSupport.PropertyChangeType = {
376 PropertyAdded: "PropertyAdded",
377 PropertyRemoved: "PropertyRemoved",
378 PropertyToggled: "PropertyToggled",
379 ValueChanged: "ValueChanged",
380 NameChanged: "NameChanged"
381 }
382
383 /**
384 * @constructor
385 * @param {!WebInspector.SASSSupport.PropertyChangeType} type
386 * @param {!WebInspector.SASSSupport.Rule} oldRule
387 * @param {!WebInspector.SASSSupport.Rule} newRule
388 * @param {number} oldPropertyIndex
389 * @param {number} newPropertyIndex
390 */
391 WebInspector.SASSSupport.PropertyChange = function(type, oldRule, newRule, oldPr opertyIndex, newPropertyIndex)
392 {
393 this.type = type;
394 this.oldRule = oldRule;
395 this.newRule = newRule;
396 this.oldPropertyIndex = oldPropertyIndex;
397 this.newPropertyIndex = newPropertyIndex;
398 }
399
400 /**
401 * @constructor
402 * @param {!Map<!WebInspector.SASSSupport.TextNode, !WebInspector.SASSSupport.Te xtNode>} mapping
403 * @param {!Array<!WebInspector.SASSSupport.PropertyChange>} changes
404 */
405 WebInspector.SASSSupport.ASTDiff = function(mapping, changes)
406 {
407 this.mapping = mapping;
408 this.changes = changes;
409 }
410
411 /**
412 * @param {!WebInspector.SASSSupport.AST} oldAST
413 * @param {!WebInspector.SASSSupport.AST} newAST
414 * @return {!WebInspector.SASSSupport.ASTDiff}
415 */
416 WebInspector.SASSSupport.diffModels = function(oldAST, newAST)
417 {
418 console.assert(oldAST.rules.length === newAST.rules.length, "Not implemented for rule diff.");
419 var T = WebInspector.SASSSupport.PropertyChangeType;
420 var changes = [];
421 /** @type {!Map<!WebInspector.SASSSupport.TextNode, !WebInspector.SASSSuppor t.TextNode>} */
422 var mapping = new Map();
423 for (var i = 0; i < oldAST.rules.length; ++i) {
424 var oldRule = oldAST.rules[i];
425 var newRule = newAST.rules[i];
426 computeRuleDiff(mapping, oldRule, newRule);
427 }
428 return new WebInspector.SASSSupport.ASTDiff(mapping, changes);
429
430 /**
431 * @param {!WebInspector.SASSSupport.PropertyChangeType} type
432 * @param {!WebInspector.SASSSupport.Rule} oldRule
433 * @param {!WebInspector.SASSSupport.Rule} newRule
434 * @param {number} oldPropertyIndex
435 * @param {number} newPropertyIndex
436 */
437 function addChange(type, oldRule, newRule, oldPropertyIndex, newPropertyInde x)
438 {
439 changes.push(new WebInspector.SASSSupport.PropertyChange(type, oldRule, newRule, oldPropertyIndex, newPropertyIndex));
440 }
441
442 /**
443 * @param {!Map<!WebInspector.SASSSupport.TextNode, !WebInspector.SASSSuppor t.TextNode>} mapping
444 * @param {!WebInspector.SASSSupport.Rule} oldRule
445 * @param {!WebInspector.SASSSupport.Rule} newRule
446 */
447 function computeRuleDiff(mapping, oldRule, newRule)
448 {
449 var oldLines = [];
450 for (var i = 0; i < oldRule.properties.length; ++i)
451 oldLines.push(oldRule.properties[i].name.text.trim() + ":" + oldRule .properties[i].value.text.trim());
452 var newLines = [];
453 for (var i = 0; i < newRule.properties.length; ++i)
454 newLines.push(newRule.properties[i].name.text.trim() + ":" + newRule .properties[i].value.text.trim());
455 var diff = WebInspector.Diff.lineDiff(oldLines, newLines);
456 diff = WebInspector.Diff.convertToEditDiff(diff);
457
458 var p1 = 0, p2 = 0;
459 for (var i = 0; i < diff.length; ++i) {
460 var token = diff[i];
461 if (token[0] === WebInspector.Diff.Operation.Delete) {
462 for (var j = 0; j < token[1]; ++j)
463 addChange(T.PropertyRemoved, oldRule, newRule, p1++, p2);
464 } else if (token[0] === WebInspector.Diff.Operation.Insert) {
465 for (var j = 0; j < token[1]; ++j)
466 addChange(T.PropertyAdded, oldRule, newRule, p1, p2++);
467 } else {
468 for (var j = 0; j < token[1]; ++j)
469 computePropertyDiff(mapping, oldRule, newRule, p1++, p2++);
470 }
471 }
472 }
473
474 /**
475 * @param {!Map<!WebInspector.SASSSupport.TextNode, !WebInspector.SASSSuppor t.TextNode>} mapping
476 * @param {!WebInspector.SASSSupport.Rule} oldRule
477 * @param {!WebInspector.SASSSupport.Rule} newRule
478 * @param {number} oldPropertyIndex
479 * @param {number} newPropertyIndex
480 */
481 function computePropertyDiff(mapping, oldRule, newRule, oldPropertyIndex, ne wPropertyIndex)
482 {
483 var oldProperty = oldRule.properties[oldPropertyIndex];
484 var newProperty = newRule.properties[newPropertyIndex];
485 mapping.set(oldProperty.name, newProperty.name);
486 mapping.set(oldProperty.value, newProperty.value);
487 if (oldProperty.name.text.trim() !== newProperty.name.text.trim())
488 addChange(T.NameChanged, oldRule, newRule, oldPropertyIndex, newProp ertyIndex);
489 if (oldProperty.value.text.trim() !== newProperty.value.text.trim())
490 addChange(T.ValueChanged, oldRule, newRule, oldPropertyIndex, newPro pertyIndex);
491 if (oldProperty.disabled !== newProperty.disabled)
492 addChange(T.PropertyToggled, oldRule, newRule, oldPropertyIndex, new PropertyIndex);
493 }
494 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/diff/Diff.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698