| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 7 /** |
| 8 * CSS polyfill emits CSS to be understood by older parsers that which do not | 8 * CSS polyfill emits CSS to be understood by older parsers that which do not |
| 9 * understand (var, calc, etc.). | 9 * understand (var, calc, etc.). |
| 10 */ | 10 */ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 processVars(styleSheet); | 34 processVars(styleSheet); |
| 35 | 35 |
| 36 // Remove all var definitions for this style sheet. | 36 // Remove all var definitions for this style sheet. |
| 37 new _RemoveVarDefinitions().visitTree(styleSheet); | 37 new _RemoveVarDefinitions().visitTree(styleSheet); |
| 38 } | 38 } |
| 39 | 39 |
| 40 /** Process all includes looking for var definitions. */ | 40 /** Process all includes looking for var definitions. */ |
| 41 void processVarDefinitions(List<StyleSheet> includes) { | 41 void processVarDefinitions(List<StyleSheet> includes) { |
| 42 for (var include in includes) { | 42 for (var include in includes) { |
| 43 _allVarDefinitions = (new _VarDefinitionsIncludes(_allVarDefinitions) | 43 _allVarDefinitions = (new _VarDefinitionsIncludes(_allVarDefinitions) |
| 44 ..visitTree(include)).varDefs; | 44 ..visitTree(include)).varDefs; |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 void processVars(StyleSheet styleSheet) { | 48 void processVars(StyleSheet styleSheet) { |
| 49 // Build list of all var definitions. | 49 // Build list of all var definitions. |
| 50 var mainStyleSheetVarDefs = | 50 var mainStyleSheetVarDefs = (new _VarDefAndUsage( |
| 51 (new _VarDefAndUsage(this._messages, _allVarDefinitions) | 51 this._messages, _allVarDefinitions)..visitTree(styleSheet)).varDefs; |
| 52 ..visitTree(styleSheet)).varDefs; | |
| 53 | 52 |
| 54 // Resolve all definitions to a non-VarUsage (terminal expression). | 53 // Resolve all definitions to a non-VarUsage (terminal expression). |
| 55 mainStyleSheetVarDefs.forEach((key, value) { | 54 mainStyleSheetVarDefs.forEach((key, value) { |
| 56 for (Expression expr in (value.expression as Expressions).expressions) { | 55 for (Expression expr in (value.expression as Expressions).expressions) { |
| 57 mainStyleSheetVarDefs[key] = | 56 mainStyleSheetVarDefs[key] = |
| 58 _findTerminalVarDefinition(_allVarDefinitions, value); | 57 _findTerminalVarDefinition(_allVarDefinitions, value); |
| 59 } | 58 } |
| 60 }); | 59 }); |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 | 62 |
| 64 /** Build list of all var definitions in all includes. */ | 63 /** Build list of all var definitions in all includes. */ |
| 65 class _VarDefinitionsIncludes extends Visitor { | 64 class _VarDefinitionsIncludes extends Visitor { |
| 66 final Map<String, VarDefinition> varDefs; | 65 final Map<String, VarDefinition> varDefs; |
| 67 | 66 |
| 68 _VarDefinitionsIncludes(this.varDefs); | 67 _VarDefinitionsIncludes(this.varDefs); |
| 69 | 68 |
| 70 void visitTree(StyleSheet tree) { | 69 void visitTree(StyleSheet tree) { |
| 71 visitStyleSheet(tree); | 70 visitStyleSheet(tree); |
| 72 } | 71 } |
| 73 | 72 |
| 74 visitVarDefinition(VarDefinition node) { | 73 visitVarDefinition(VarDefinition node) { |
| 75 // Replace with latest variable definition. | 74 // Replace with latest variable definition. |
| 76 varDefs[node.definedName] = node; | 75 varDefs[node.definedName] = node; |
| 77 super.visitVarDefinition(node); | 76 super.visitVarDefinition(node); |
| 78 } | 77 } |
| 79 | 78 |
| 80 void visitVarDefinitionDirective(VarDefinitionDirective node) { | 79 void visitVarDefinitionDirective(VarDefinitionDirective node) { |
| 81 visitVarDefinition(node.def); | 80 visitVarDefinition(node.def); |
| 82 } | 81 } |
| 83 } | 82 } |
| 84 | 83 |
| 85 /** | 84 /** |
| 86 * Find var- definitions in a style sheet. | 85 * Find var- definitions in a style sheet. |
| 87 * [found] list of known definitions. | 86 * [found] list of known definitions. |
| 88 */ | 87 */ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 visitVarDefinition(VarDefinition node) { | 102 visitVarDefinition(VarDefinition node) { |
| 104 // Replace with latest variable definition. | 103 // Replace with latest variable definition. |
| 105 currVarDefinition = node; | 104 currVarDefinition = node; |
| 106 | 105 |
| 107 _knownVarDefs[node.definedName] = node; | 106 _knownVarDefs[node.definedName] = node; |
| 108 varDefs[node.definedName] = node; | 107 varDefs[node.definedName] = node; |
| 109 | 108 |
| 110 super.visitVarDefinition(node); | 109 super.visitVarDefinition(node); |
| 111 | 110 |
| 112 currVarDefinition = null; | 111 currVarDefinition = null; |
| 113 } | 112 } |
| 114 | 113 |
| 115 void visitVarDefinitionDirective(VarDefinitionDirective node) { | 114 void visitVarDefinitionDirective(VarDefinitionDirective node) { |
| 116 visitVarDefinition(node.def); | 115 visitVarDefinition(node.def); |
| 117 } | 116 } |
| 118 | 117 |
| 119 void visitExpressions(Expressions node) { | 118 void visitExpressions(Expressions node) { |
| 120 currentExpressions = node.expressions; | 119 currentExpressions = node.expressions; |
| 121 super.visitExpressions(node); | 120 super.visitExpressions(node); |
| 122 currentExpressions = null; | 121 currentExpressions = null; |
| 123 } | 122 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 142 _resolveVarUsage(currentExpressions, index, | 141 _resolveVarUsage(currentExpressions, index, |
| 143 _findTerminalVarDefinition(_knownVarDefs, def)); | 142 _findTerminalVarDefinition(_knownVarDefs, def)); |
| 144 } else if (node.defaultValues.any((e) => e is VarUsage)) { | 143 } else if (node.defaultValues.any((e) => e is VarUsage)) { |
| 145 // Don't have a VarDefinition need to use default values resolve all | 144 // Don't have a VarDefinition need to use default values resolve all |
| 146 // default values. | 145 // default values. |
| 147 var terminalDefaults = <Expression>[]; | 146 var terminalDefaults = <Expression>[]; |
| 148 for (var defaultValue in node.defaultValues) { | 147 for (var defaultValue in node.defaultValues) { |
| 149 terminalDefaults.addAll(resolveUsageTerminal(defaultValue)); | 148 terminalDefaults.addAll(resolveUsageTerminal(defaultValue)); |
| 150 } | 149 } |
| 151 expressions.replaceRange(index, index + 1, terminalDefaults); | 150 expressions.replaceRange(index, index + 1, terminalDefaults); |
| 152 } else if (node.defaultValues.isNotEmpty){ | 151 } else if (node.defaultValues.isNotEmpty) { |
| 153 // No VarDefinition but default value is a terminal expression; use it. | 152 // No VarDefinition but default value is a terminal expression; use it. |
| 154 expressions.replaceRange(index, index + 1, node.defaultValues); | 153 expressions.replaceRange(index, index + 1, node.defaultValues); |
| 155 } else { | 154 } else { |
| 156 if (currVarDefinition != null) { | 155 if (currVarDefinition != null) { |
| 157 currVarDefinition.badUsage = true; | 156 currVarDefinition.badUsage = true; |
| 158 var mainStyleSheetDef = varDefs[node.name]; | 157 var mainStyleSheetDef = varDefs[node.name]; |
| 159 if (mainStyleSheetDef != null) { | 158 if (mainStyleSheetDef != null) { |
| 160 varDefs.remove(currVarDefinition.property); | 159 varDefs.remove(currVarDefinition.property); |
| 161 } | 160 } |
| 162 } | 161 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 192 } | 191 } |
| 193 | 192 |
| 194 // We're at a terminal just return the VarDefinition expression. | 193 // We're at a terminal just return the VarDefinition expression. |
| 195 if (result.isEmpty && varDef != null) { | 194 if (result.isEmpty && varDef != null) { |
| 196 result = (varDef.expression as Expressions).expressions; | 195 result = (varDef.expression as Expressions).expressions; |
| 197 } | 196 } |
| 198 | 197 |
| 199 return result; | 198 return result; |
| 200 } | 199 } |
| 201 | 200 |
| 202 _resolveVarUsage(List<Expression> expressions, int index, | 201 _resolveVarUsage(List<Expression> expressions, int index, VarDefinition def) { |
| 203 VarDefinition def) { | |
| 204 var defExpressions = (def.expression as Expressions).expressions; | 202 var defExpressions = (def.expression as Expressions).expressions; |
| 205 expressions.replaceRange(index, index + 1, defExpressions); | 203 expressions.replaceRange(index, index + 1, defExpressions); |
| 206 } | 204 } |
| 207 } | 205 } |
| 208 | 206 |
| 209 /** Remove all var definitions. */ | 207 /** Remove all var definitions. */ |
| 210 class _RemoveVarDefinitions extends Visitor { | 208 class _RemoveVarDefinitions extends Visitor { |
| 211 void visitTree(StyleSheet tree) { | 209 void visitTree(StyleSheet tree) { |
| 212 visitStyleSheet(tree); | 210 visitStyleSheet(tree); |
| 213 } | 211 } |
| 214 | 212 |
| 215 void visitStyleSheet(StyleSheet ss) { | 213 void visitStyleSheet(StyleSheet ss) { |
| 216 ss.topLevels.removeWhere((e) => e is VarDefinitionDirective); | 214 ss.topLevels.removeWhere((e) => e is VarDefinitionDirective); |
| 217 super.visitStyleSheet(ss); | 215 super.visitStyleSheet(ss); |
| 218 } | 216 } |
| 219 | 217 |
| 220 void visitDeclarationGroup(DeclarationGroup node) { | 218 void visitDeclarationGroup(DeclarationGroup node) { |
| 221 node.declarations.removeWhere((e) => e is VarDefinition); | 219 node.declarations.removeWhere((e) => e is VarDefinition); |
| 222 super.visitDeclarationGroup(node); | 220 super.visitDeclarationGroup(node); |
| 223 } | 221 } |
| 224 } | 222 } |
| 225 | 223 |
| 226 /** Find terminal definition (non VarUsage implies real CSS value). */ | 224 /** Find terminal definition (non VarUsage implies real CSS value). */ |
| 227 VarDefinition _findTerminalVarDefinition(Map<String, VarDefinition> varDefs, | 225 VarDefinition _findTerminalVarDefinition( |
| 228 VarDefinition varDef) { | 226 Map<String, VarDefinition> varDefs, VarDefinition varDef) { |
| 229 var expressions = varDef.expression as Expressions; | 227 var expressions = varDef.expression as Expressions; |
| 230 for (var expr in expressions.expressions) { | 228 for (var expr in expressions.expressions) { |
| 231 if (expr is VarUsage) { | 229 if (expr is VarUsage) { |
| 232 var usageName = (expr as VarUsage).name; | 230 var usageName = (expr as VarUsage).name; |
| 233 var foundDef = varDefs[usageName]; | 231 var foundDef = varDefs[usageName]; |
| 234 | 232 |
| 235 // If foundDef is unknown check if defaultValues; if it exist then resolve | 233 // If foundDef is unknown check if defaultValues; if it exist then resolve |
| 236 // to terminal value. | 234 // to terminal value. |
| 237 if (foundDef == null) { | 235 if (foundDef == null) { |
| 238 // We're either a VarUsage or terminal definition if in varDefs; | 236 // We're either a VarUsage or terminal definition if in varDefs; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 249 } | 247 } |
| 250 } else { | 248 } else { |
| 251 // Return real CSS property. | 249 // Return real CSS property. |
| 252 return varDef; | 250 return varDef; |
| 253 } | 251 } |
| 254 } | 252 } |
| 255 | 253 |
| 256 // Didn't point to a var definition that existed. | 254 // Didn't point to a var definition that existed. |
| 257 return varDef; | 255 return varDef; |
| 258 } | 256 } |
| OLD | NEW |