OLD | NEW |
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 library csslib.src.validate; | 5 library csslib.src.validate; |
6 | 6 |
7 import 'package:csslib/visitor.dart'; | 7 import 'package:csslib/visitor.dart'; |
8 import 'package:source_span/source_span.dart'; | 8 import 'package:source_span/source_span.dart'; |
9 | 9 |
10 /** Can be thrown on any Css runtime problem includes source location. */ | 10 /** Can be thrown on any Css runtime problem includes source location. */ |
(...skipping 22 matching lines...) Expand all Loading... |
33 throw new CssSelectorException( | 33 throw new CssSelectorException( |
34 'Selectors can not have combinators (>, +, or ~) before $error'); | 34 'Selectors can not have combinators (>, +, or ~) before $error'); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 static int _elementIdCheck(var selector, int matches) { | 38 static int _elementIdCheck(var selector, int matches) { |
39 if (selector.isCombinatorNone() && matches == 0) { | 39 if (selector.isCombinatorNone() && matches == 0) { |
40 // Perfect just one element id returns matches of -1. | 40 // Perfect just one element id returns matches of -1. |
41 return -1; | 41 return -1; |
42 } else if (selector.isCombinatorDescendant()) { | 42 } else if (selector.isCombinatorDescendant()) { |
43 String tooMany = selector.simpleSelector.toString(); | 43 String tooMany = selector.simpleSelector.toString(); |
44 throw new CssSelectorException( | 44 throw new CssSelectorException( |
45 'Use of Id selector must be singleton starting at $tooMany'); | 45 'Use of Id selector must be singleton starting at $tooMany'); |
46 } else { | 46 } else { |
47 String error = selector.simpleSelector.toString(); | 47 String error = selector.simpleSelector.toString(); |
48 throw new CssSelectorException( | 48 throw new CssSelectorException( |
49 'Selectors can not have combinators (>, +, or ~) before $error'); | 49 'Selectors can not have combinators (>, +, or ~) before $error'); |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
53 // Validate the @{css expression} only .class and #elementId are valid inside | 53 // Validate the @{css expression} only .class and #elementId are valid inside |
54 // of @{...}. | 54 // of @{...}. |
55 static template(List<Selector> selectors) { | 55 static template(List<Selector> selectors) { |
56 var errorSelector; // signal which selector didn't match. | 56 var errorSelector; // signal which selector didn't match. |
57 bool found = false; // signal if a selector is matched. | 57 bool found = false; // signal if a selector is matched. |
58 int matches = 0; // < 0 IdSelectors, > 0 ClassSelector | 58 int matches = 0; // < 0 IdSelectors, > 0 ClassSelector |
59 | 59 |
60 // At most one selector group (any number of simple selector sequences). | 60 // At most one selector group (any number of simple selector sequences). |
61 assert(selectors.length <= 1); | 61 assert(selectors.length <= 1); |
62 | 62 |
63 for (final sels in selectors) { | 63 for (final sels in selectors) { |
64 for (final selector in sels.simpleSelectorSequences) { | 64 for (final selector in sels.simpleSelectorSequences) { |
65 found = false; | 65 found = false; |
66 var simpleSelector = selector.simpleSelector; | 66 var simpleSelector = selector.simpleSelector; |
67 if (simpleSelector is ClassSelector) { | 67 if (simpleSelector is ClassSelector) { |
68 // Any class name starting with an underscore is a private class name | 68 // Any class name starting with an underscore is a private class name |
69 // that doesn't have to match the world of known classes. | 69 // that doesn't have to match the world of known classes. |
70 if (!simpleSelector.name.startsWith('_')) { | 70 if (!simpleSelector.name.startsWith('_')) { |
71 // TODO(terry): For now iterate through all classes look for faster | 71 // TODO(terry): For now iterate through all classes look for faster |
72 // mechanism hash map, etc. | 72 // mechanism hash map, etc. |
73 for (final className in classes) { | 73 for (final className in classes) { |
74 if (selector.simpleSelector.name == className) { | 74 if (selector.simpleSelector.name == className) { |
75 matches = _classNameCheck(selector, matches); | 75 matches = _classNameCheck(selector, matches); |
76 found = true; // .class found. | 76 found = true; // .class found. |
77 break; | 77 break; |
78 } | 78 } |
79 for (final className2 in classes) { | 79 for (final className2 in classes) { |
80 print(className2); | 80 print(className2); |
81 } | 81 } |
82 } | 82 } |
83 | |
84 } else { | 83 } else { |
85 // Don't check any class name that is prefixed with an underscore. | 84 // Don't check any class name that is prefixed with an underscore. |
86 // However, signal as found and bump up matches; it's a valid class | 85 // However, signal as found and bump up matches; it's a valid class |
87 // name. | 86 // name. |
88 matches = _classNameCheck(selector, matches); | 87 matches = _classNameCheck(selector, matches); |
89 found = true; // ._class are always okay. | 88 found = true; // ._class are always okay. |
90 } | 89 } |
91 } else if (simpleSelector is IdSelector) { | 90 } else if (simpleSelector is IdSelector) { |
92 // Any element id starting with an underscore is a private element id | 91 // Any element id starting with an underscore is a private element id |
93 // that doesn't have to match the world of known elemtn ids. | 92 // that doesn't have to match the world of known elemtn ids. |
94 if (!simpleSelector.name.startsWith('_')) { | 93 if (!simpleSelector.name.startsWith('_')) { |
95 for (final id in ids) { | 94 for (final id in ids) { |
96 if (simpleSelector.name == id) { | 95 if (simpleSelector.name == id) { |
97 matches = _elementIdCheck(selector, matches); | 96 matches = _elementIdCheck(selector, matches); |
98 found = true; // #id found. | 97 found = true; // #id found. |
99 break; | 98 break; |
100 } | 99 } |
101 } | 100 } |
102 } else { | 101 } else { |
103 // Don't check any element ID that is prefixed with an underscore. | 102 // Don't check any element ID that is prefixed with an underscore. |
104 // Signal as found and bump up matches; it's a valid element ID. | 103 // Signal as found and bump up matches; it's a valid element ID. |
105 matches = _elementIdCheck(selector, matches); | 104 matches = _elementIdCheck(selector, matches); |
106 found = true; // #_id are always okay | 105 found = true; // #_id are always okay |
107 } | 106 } |
108 } else { | 107 } else { |
109 String badSelector = simpleSelector.toString(); | 108 String badSelector = simpleSelector.toString(); |
110 throw new CssSelectorException( | 109 throw new CssSelectorException( |
111 'Invalid template selector $badSelector'); | 110 'Invalid template selector $badSelector'); |
112 } | 111 } |
113 | 112 |
114 if (!found) { | 113 if (!found) { |
115 String unknownName = simpleSelector.toString(); | 114 String unknownName = simpleSelector.toString(); |
116 throw new CssSelectorException('Unknown selector name $unknownName'); | 115 throw new CssSelectorException('Unknown selector name $unknownName'); |
117 } | 116 } |
118 } | 117 } |
119 } | 118 } |
120 | 119 |
121 // Every selector must match. | 120 // Every selector must match. |
122 Selector selector = selectors[0]; | 121 Selector selector = selectors[0]; |
123 assert((matches >= 0 ? matches : -matches) == | 122 assert((matches >= 0 ? matches : -matches) == |
124 selector.simpleSelectorSequences.length); | 123 selector.simpleSelectorSequences.length); |
125 } | 124 } |
126 } | 125 } |
127 | |
OLD | NEW |