OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class Validate { | 5 class Validate { |
6 static int _classNameCheck(var selector, int matches) { | 6 static int _classNameCheck(var selector, int matches) { |
7 if (selector.isCombinatorDescendant() || | 7 if (selector.isCombinatorDescendant() || |
8 (selector.isCombinatorNone() && matches == 0)) { | 8 (selector.isCombinatorNone() && matches == 0)) { |
9 if (matches < 0) { | 9 if (matches < 0) { |
10 String tooMany = selector.simpleSelector.toString(); | 10 String tooMany = selector.simpleSelector.toString(); |
(...skipping 20 matching lines...) Expand all Loading... |
31 'Use of Id selector must be singleton starting at $tooMany'); | 31 'Use of Id selector must be singleton starting at $tooMany'); |
32 } else { | 32 } else { |
33 String error = selector.simpleSelector.toString(); | 33 String error = selector.simpleSelector.toString(); |
34 throw new CssSelectorException( | 34 throw new CssSelectorException( |
35 'Selectors can not have combinators (>, +, or ~) before $error'); | 35 'Selectors can not have combinators (>, +, or ~) before $error'); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 // Validate the @{css expression} only .class and #elementId are valid inside | 39 // Validate the @{css expression} only .class and #elementId are valid inside |
40 // of @{...}. | 40 // of @{...}. |
41 static template(List<lang.Node> selectors, CssWorld cssWorld) { | 41 static template(List<ASTNode> selectors, CssWorld cssWorld) { |
42 var errorSelector; // signal which selector didn't match. | 42 var errorSelector; // signal which selector didn't match. |
43 bool found = false; // signal if a selector is matched. | 43 bool found = false; // signal if a selector is matched. |
44 int matches = 0; // < 0 IdSelectors, > 0 ClassSelector | 44 int matches = 0; // < 0 IdSelectors, > 0 ClassSelector |
45 | 45 |
46 // At most one selector group (any number of simple selector sequences). | 46 // At most one selector group (any number of simple selector sequences). |
47 assert(selectors.length <= 1); | 47 assert(selectors.length <= 1); |
48 | 48 |
49 for (final sels in selectors) { | 49 for (final sels in selectors) { |
50 for (final selector in sels.simpleSelectorSequences) { | 50 for (final selector in sels.simpleSelectorSequences) { |
51 found = false; | 51 found = false; |
52 var simpleSelector = selector.simpleSelector; | 52 var simpleSelector = selector.simpleSelector; |
53 if (simpleSelector is ClassSelector) { | 53 if (simpleSelector is ClassSelector) { |
54 // Any class name starting with an underscore is a private class name | 54 // Any class name starting with an underscore is a private class name |
55 // that doesn't have to match the world of known classes. | 55 // that doesn't have to match the world of known classes. |
56 if (!simpleSelector.name.startsWith('_')) { | 56 if (!simpleSelector.name.startsWith('_')) { |
57 // TODO(terry): For now iterate through all classes look for faster | 57 // TODO(terry): For now iterate through all classes look for faster |
58 // mechanism hash map, etc. | 58 // mechanism hash map, etc. |
59 var className; | |
60 for (final className in cssWorld.classes) { | 59 for (final className in cssWorld.classes) { |
61 if (selector.simpleSelector.name == className) { | 60 if (selector.simpleSelector.name == className) { |
62 matches = _classNameCheck(selector, matches); | 61 matches = _classNameCheck(selector, matches); |
63 found = true; // .class found. | 62 found = true; // .class found. |
64 break; | 63 break; |
65 } | 64 } |
66 for (final className2 in cssWorld.classes) { | 65 for (final className2 in cssWorld.classes) { |
67 print(className2); | 66 print(className2); |
68 } | 67 } |
69 } | 68 } |
(...skipping 29 matching lines...) Expand all Loading... |
99 } | 98 } |
100 | 99 |
101 if (!found) { | 100 if (!found) { |
102 String unknownName = simpleSelector.toString(); | 101 String unknownName = simpleSelector.toString(); |
103 throw new CssSelectorException('Unknown selector name $unknownName'); | 102 throw new CssSelectorException('Unknown selector name $unknownName'); |
104 } | 103 } |
105 } | 104 } |
106 } | 105 } |
107 | 106 |
108 // Every selector must match. | 107 // Every selector must match. |
| 108 var selector = selectors[0]; |
109 assert((matches >= 0 ? matches : -matches) == | 109 assert((matches >= 0 ? matches : -matches) == |
110 selectors[0].simpleSelectorSequences.length); | 110 selector.simpleSelectorSequences.length); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
OLD | NEW |