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

Side by Side Diff: packages/csslib/lib/src/analyzer.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 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
« no previous file with comments | « packages/csslib/lib/parser.dart ('k') | packages/csslib/lib/src/css_printer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of csslib.parser; 5 part of csslib.parser;
6 6
7 // TODO(terry): Add optimizing phase to remove duplicated selectors in the same 7 // TODO(terry): Add optimizing phase to remove duplicated selectors in the same
8 // selector group (e.g., .btn, .btn { color: red; }). Also, look 8 // selector group (e.g., .btn, .btn { color: red; }). Also, look
9 // at simplifying selectors expressions too (much harder). 9 // at simplifying selectors expressions too (much harder).
10 // TODO(terry): Detect invalid directive usage. All @imports must occur before 10 // TODO(terry): Detect invalid directive usage. All @imports must occur before
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 * Build up the list of all inherited sequences from the parent selector 236 * Build up the list of all inherited sequences from the parent selector
237 * [node] is the current nested selector and it's parent is the last entry in 237 * [node] is the current nested selector and it's parent is the last entry in
238 * the [_nestedSelectorGroup]. 238 * the [_nestedSelectorGroup].
239 */ 239 */
240 SelectorGroup _mergeToFlatten(RuleSet node) { 240 SelectorGroup _mergeToFlatten(RuleSet node) {
241 // Create a new SelectorGroup for this nesting level. 241 // Create a new SelectorGroup for this nesting level.
242 var nestedSelectors = _nestedSelectorGroup.selectors; 242 var nestedSelectors = _nestedSelectorGroup.selectors;
243 var selectors = node.selectorGroup.selectors; 243 var selectors = node.selectorGroup.selectors;
244 244
245 // Create a merged set of previous parent selectors and current selectors. 245 // Create a merged set of previous parent selectors and current selectors.
246 var newSelectors = []; 246 var newSelectors = <Selector>[];
247 for (Selector selector in selectors) { 247 for (Selector selector in selectors) {
248 for (Selector nestedSelector in nestedSelectors) { 248 for (Selector nestedSelector in nestedSelectors) {
249 var seq = _mergeNestedSelector(nestedSelector.simpleSelectorSequences, 249 var seq = _mergeNestedSelector(nestedSelector.simpleSelectorSequences,
250 selector.simpleSelectorSequences); 250 selector.simpleSelectorSequences);
251 newSelectors.add(new Selector(seq, node.span)); 251 newSelectors.add(new Selector(seq, node.span));
252 } 252 }
253 } 253 }
254 254
255 return new SelectorGroup(newSelectors, node.span); 255 return new SelectorGroup(newSelectors, node.span);
256 } 256 }
257 257
258 /** 258 /**
259 * Merge the nested selector sequences [current] to the [parent] sequences or 259 * Merge the nested selector sequences [current] to the [parent] sequences or
260 * substitue any & with the parent selector. 260 * substitue any & with the parent selector.
261 */ 261 */
262 List<SimpleSelectorSequence> _mergeNestedSelector( 262 List<SimpleSelectorSequence> _mergeNestedSelector(
263 List<SimpleSelectorSequence> parent, 263 List<SimpleSelectorSequence> parent,
264 List<SimpleSelectorSequence> current) { 264 List<SimpleSelectorSequence> current) {
265
266 // If any & operator then the parent selector will be substituted otherwise 265 // If any & operator then the parent selector will be substituted otherwise
267 // the parent selector is pre-pended to the current selector. 266 // the parent selector is pre-pended to the current selector.
268 var hasThis = current.any((s) => s.simpleSelector.isThis); 267 var hasThis = current.any((s) => s.simpleSelector.isThis);
269 268
270 var newSequence = []; 269 var newSequence = <SimpleSelectorSequence>[];
271 270
272 if (!hasThis) { 271 if (!hasThis) {
273 // If no & in the sector group then prefix with the parent selector. 272 // If no & in the sector group then prefix with the parent selector.
274 newSequence.addAll(parent); 273 newSequence.addAll(parent);
275 newSequence.addAll(_convertToDescendentSequence(current)); 274 newSequence.addAll(_convertToDescendentSequence(current));
276 } else { 275 } else {
277 for (var sequence in current) { 276 for (var sequence in current) {
278 if (sequence.simpleSelector.isThis) { 277 if (sequence.simpleSelector.isThis) {
279 // Substitue the & with the parent selector and only use a combinator 278 // Substitue the & with the parent selector and only use a combinator
280 // descendant if & is prefix by a sequence with an empty name e.g., 279 // descendant if & is prefix by a sequence with an empty name e.g.,
(...skipping 14 matching lines...) Expand all
295 /** 294 /**
296 * Return selector sequences with first sequence combinator being a 295 * Return selector sequences with first sequence combinator being a
297 * descendant. Used for nested selectors when the parent selector needs to 296 * descendant. Used for nested selectors when the parent selector needs to
298 * be prefixed to a nested selector or to substitute the this (&) with the 297 * be prefixed to a nested selector or to substitute the this (&) with the
299 * parent selector. 298 * parent selector.
300 */ 299 */
301 List<SimpleSelectorSequence> _convertToDescendentSequence( 300 List<SimpleSelectorSequence> _convertToDescendentSequence(
302 List<SimpleSelectorSequence> sequences) { 301 List<SimpleSelectorSequence> sequences) {
303 if (sequences.isEmpty) return sequences; 302 if (sequences.isEmpty) return sequences;
304 303
305 var newSequences = []; 304 var newSequences = <SimpleSelectorSequence>[];
306 var first = sequences.first; 305 var first = sequences.first;
307 newSequences.add(new SimpleSelectorSequence( 306 newSequences.add(new SimpleSelectorSequence(
308 first.simpleSelector, first.span, TokenKind.COMBINATOR_DESCENDANT)); 307 first.simpleSelector, first.span, TokenKind.COMBINATOR_DESCENDANT));
309 newSequences.addAll(sequences.skip(1)); 308 newSequences.addAll(sequences.skip(1));
310 309
311 return newSequences; 310 return newSequences;
312 } 311 }
313 312
314 void visitDeclarationGroup(DeclarationGroup node) { 313 void visitDeclarationGroup(DeclarationGroup node) {
315 var span = node.span; 314 var span = node.span;
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 visitMixinRulesetDirective(mixinDef); 584 visitMixinRulesetDirective(mixinDef);
586 } else { 585 } else {
587 visitMixinDeclarationDirective(mixinDef); 586 visitMixinDeclarationDirective(mixinDef);
588 } 587 }
589 } 588 }
590 589
591 /** 590 /**
592 * Given a mixin's defined arguments return a cloned mixin defintion that has 591 * Given a mixin's defined arguments return a cloned mixin defintion that has
593 * replaced all defined arguments with user's supplied VarUsages. 592 * replaced all defined arguments with user's supplied VarUsages.
594 */ 593 */
595 MixinDefinition transform(List callArgs) { 594 MixinDefinition transform(List<List<Expression>> callArgs) {
596 // TODO(terry): Handle default arguments and varArgs. 595 // TODO(terry): Handle default arguments and varArgs.
597 // Transform mixin with callArgs. 596 // Transform mixin with callArgs.
598 for (var index = 0; index < _definedArgs.length; index++) { 597 for (var index = 0; index < _definedArgs.length; index++) {
599 var definedArg = _definedArgs[index]; 598 var definedArg = _definedArgs[index];
600 VarDefinition varDef; 599 VarDefinition varDef;
601 if (definedArg is VarDefinition) { 600 if (definedArg is VarDefinition) {
602 varDef = definedArg; 601 varDef = definedArg;
603 } else if (definedArg is VarDefinitionDirective) { 602 } else if (definedArg is VarDefinitionDirective) {
604 VarDefinitionDirective varDirective = definedArg; 603 VarDefinitionDirective varDirective = definedArg;
605 varDef = varDirective.def; 604 varDef = varDirective.def;
(...skipping 15 matching lines...) Expand all
621 k.expressions.replaceRange(usagesIndex, usagesIndex + 1, callArg); 620 k.expressions.replaceRange(usagesIndex, usagesIndex + 1, callArg);
622 } 621 }
623 }); 622 });
624 } 623 }
625 624
626 // Clone the mixin 625 // Clone the mixin
627 return mixinDef.clone(); 626 return mixinDef.clone();
628 } 627 }
629 628
630 /** Rip apart var def with multiple parameters. */ 629 /** Rip apart var def with multiple parameters. */
631 List<List<TreeNode>> _varDefsAsCallArgs(var callArg) { 630 List<List<Expression>> _varDefsAsCallArgs(var callArg) {
632 var defArgs = []; 631 var defArgs = <List<Expression>>[];
633 if (callArg is List && callArg[0] is VarUsage) { 632 if (callArg is List && callArg[0] is VarUsage) {
634 var varDef = varDefs[callArg[0].name]; 633 var varDef = varDefs[callArg[0].name];
635 var expressions = varDef.expression.expressions; 634 var expressions = (varDef.expression as Expressions).expressions;
636 assert(expressions.length > 1); 635 assert(expressions.length > 1);
637 for (var expr in expressions) { 636 for (var expr in expressions) {
638 if (expr is! OperatorComma) { 637 if (expr is! OperatorComma) {
639 defArgs.add([expr]); 638 defArgs.add([expr]);
640 } 639 }
641 } 640 }
642 } 641 }
643 return defArgs; 642 return defArgs;
644 } 643 }
645 644
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 738
740 void visitIncludeMixinAtDeclaration(IncludeMixinAtDeclaration node) { 739 void visitIncludeMixinAtDeclaration(IncludeMixinAtDeclaration node) {
741 if (map.containsKey(node.include.name)) { 740 if (map.containsKey(node.include.name)) {
742 var mixinDef = map[node.include.name]; 741 var mixinDef = map[node.include.name];
743 742
744 // Fix up any mixin that is really a Declaration but has includes. 743 // Fix up any mixin that is really a Declaration but has includes.
745 if (mixinDef is MixinRulesetDirective) { 744 if (mixinDef is MixinRulesetDirective) {
746 if (!_allIncludes(mixinDef.rulesets) && currDeclGroup != null) { 745 if (!_allIncludes(mixinDef.rulesets) && currDeclGroup != null) {
747 var index = _findInclude(currDeclGroup.declarations, node); 746 var index = _findInclude(currDeclGroup.declarations, node);
748 if (index != -1) { 747 if (index != -1) {
749 currDeclGroup.declarations.replaceRange( 748 currDeclGroup.declarations
750 index, index + 1, [new NoOp()]); 749 .replaceRange(index, index + 1, [new NoOp()]);
751 } 750 }
752 _messages.warning( 751 _messages.warning(
753 "Using top-level mixin ${node.include.name} as a declaration", 752 "Using top-level mixin ${node.include.name} as a declaration",
754 node.span); 753 node.span);
755 } else { 754 } else {
756 // We're a list of @include(s) inside of a mixin ruleset - convert 755 // We're a list of @include(s) inside of a mixin ruleset - convert
757 // to a list of IncludeMixinAtDeclaration(s). 756 // to a list of IncludeMixinAtDeclaration(s).
758 var origRulesets = mixinDef.rulesets; 757 var origRulesets = mixinDef.rulesets;
759 var rulesets = []; 758 var rulesets = <Declaration>[];
760 if (origRulesets.every((ruleset) => ruleset is IncludeDirective)) { 759 if (origRulesets.every((ruleset) => ruleset is IncludeDirective)) {
761 origRulesets.forEach((ruleset) { 760 origRulesets.forEach((ruleset) {
762 rulesets 761 rulesets.add(new IncludeMixinAtDeclaration(
763 .add(new IncludeMixinAtDeclaration(ruleset, ruleset.span)); 762 ruleset as IncludeDirective, ruleset.span));
764 }); 763 });
765 _IncludeReplacer.replace(_styleSheet, node, rulesets); 764 _IncludeReplacer.replace(_styleSheet, node, rulesets);
766 } 765 }
767 } 766 }
768 } 767 }
769 768
770 if (mixinDef.definedArgs.length > 0 && node.include.args.length > 0) { 769 if (mixinDef.definedArgs.length > 0 && node.include.args.length > 0) {
771 var callMixin = _createCallDeclMixin(mixinDef); 770 var callMixin = _createCallDeclMixin(mixinDef);
772 mixinDef = callMixin.transform(node.include.args); 771 mixinDef = callMixin.transform(node.include.args);
773 } 772 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 } 833 }
835 834
836 void visitVarDefinitionDirective(VarDefinitionDirective node) { 835 void visitVarDefinitionDirective(VarDefinitionDirective node) {
837 visitVarDefinition(node.def); 836 visitVarDefinition(node.def);
838 } 837 }
839 } 838 }
840 839
841 /** @include as a top-level with ruleset(s). */ 840 /** @include as a top-level with ruleset(s). */
842 class _IncludeReplacer extends Visitor { 841 class _IncludeReplacer extends Visitor {
843 final _include; 842 final _include;
844 final List<Declaration> _newDeclarations; 843 final List<TreeNode> _newDeclarations;
845 bool _foundAndReplaced = false; 844 bool _foundAndReplaced = false;
846 845
847 /** 846 /**
848 * Look for the [ruleSet] inside of a @media directive; if found then replace 847 * Look for the [ruleSet] inside of a @media directive; if found then replace
849 * with the [newRules]. 848 * with the [newRules].
850 */ 849 */
851 static void replace( 850 static void replace(
852 StyleSheet ss, var include, List<Declaration> newDeclarations) { 851 StyleSheet ss, var include, List<TreeNode> newDeclarations) {
853 var visitor = new _IncludeReplacer(include, newDeclarations); 852 var visitor = new _IncludeReplacer(include, newDeclarations);
854 visitor.visitStyleSheet(ss); 853 visitor.visitStyleSheet(ss);
855 } 854 }
856 855
857 _IncludeReplacer(this._include, this._newDeclarations); 856 _IncludeReplacer(this._include, this._newDeclarations);
858 857
859 void visitDeclarationGroup(DeclarationGroup node) { 858 void visitDeclarationGroup(DeclarationGroup node) {
860 var index = _findInclude(node.declarations, _include); 859 var index = _findInclude(node.declarations, _include);
861 if (index != -1) { 860 if (index != -1) {
862 node.declarations.insertAll(index + 1, _newDeclarations); 861 node.declarations.insertAll(index + 1, _newDeclarations);
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 isLastNone = false; 1004 isLastNone = false;
1006 } 1005 }
1007 } else { 1006 } else {
1008 isLastNone = simpleSeq.isCombinatorNone; 1007 isLastNone = simpleSeq.isCombinatorNone;
1009 } 1008 }
1010 } 1009 }
1011 } 1010 }
1012 super.visitSelectorGroup(node); 1011 super.visitSelectorGroup(node);
1013 } 1012 }
1014 } 1013 }
OLDNEW
« no previous file with comments | « packages/csslib/lib/parser.dart ('k') | packages/csslib/lib/src/css_printer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698