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