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 #import("../../../css/css.dart"); |
5 #import('../../../../frog/lang.dart', prefix:'lang'); | 6 #import('../../../../frog/lang.dart', prefix:'lang'); |
6 #import("../../../css/css.dart"); | |
7 | 7 |
8 class SelectorLiteralTest { | 8 class ExpressionTest { |
9 | 9 |
10 static testMain() { | 10 static testMain() { |
11 initCssWorld(); | 11 initCssWorld(); |
12 | 12 |
13 testClass(); | 13 testClass(); |
14 testId(); | 14 testId(); |
15 testElement(); | 15 testElement(); |
16 testNamespace(); | 16 testNamespace(); |
17 testSelectorGroups(); | 17 testSelectorGroups(); |
18 testCombinator(); | 18 testCombinator(); |
19 testWildcard(); | 19 testWildcard(); |
20 testPseudo(); | 20 testPseudo(); |
21 testAttribute(); | 21 testAttribute(); |
22 testNegation(); | 22 testNegation(); |
23 } | 23 } |
24 | 24 |
25 static void testClass() { | 25 static void testClass() { |
26 Parser parser = new Parser(new lang.SourceFile( | 26 Parser parser = new Parser(new lang.SourceFile( |
27 lang.SourceFile.IN_MEMORY_FILE, ".foobar")); | 27 lang.SourceFile.IN_MEMORY_FILE, ".foobar {}")); |
28 | 28 |
29 List<SelectorGroup> exprTree = parser.parse(); | 29 Stylesheet stylesheet = parser.parse(); |
30 Expect.isNotNull(exprTree); | 30 Expect.isNotNull(stylesheet); |
31 Expect.equals(exprTree.length, 1); | 31 Expect.equals(stylesheet.topLevels.length, 1); |
32 for (selectorGroup in exprTree) { | 32 |
33 Expect.equals(selectorGroup.selectors.length, 1); | 33 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
34 for (selector in selectorGroup.selectors) { | 34 RuleSet ruleset = stylesheet.topLevels[0]; |
35 Expect.isTrue(selector is ClassSelector); | 35 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 36 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 37 |
| 38 List<SimpleSelectorSequence> simpleSeqs = |
| 39 ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 40 Expect.equals(simpleSeqs.length, 1); |
| 41 for (final selector in simpleSeqs) { |
| 42 final simpSelector = selector.simpleSelector; |
| 43 Expect.isTrue(simpSelector is ClassSelector); |
| 44 Expect.isTrue(selector.isCombinatorNone()); |
| 45 Expect.equals(simpSelector.name, "foobar"); |
| 46 } |
| 47 |
| 48 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 49 ".foobar .bar .no-story {}")); |
| 50 |
| 51 stylesheet = parser.parse(); |
| 52 Expect.isNotNull(stylesheet); |
| 53 Expect.equals(stylesheet.topLevels.length, 1); |
| 54 |
| 55 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 56 ruleset = stylesheet.topLevels[0]; |
| 57 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 58 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 59 |
| 60 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 61 |
| 62 var idx = 0; |
| 63 for (final selector in simpleSeqs) { |
| 64 final simpSelector = selector.simpleSelector; |
| 65 if (idx == 0) { |
| 66 Expect.isTrue(simpSelector is ClassSelector); |
36 Expect.isTrue(selector.isCombinatorNone()); | 67 Expect.isTrue(selector.isCombinatorNone()); |
37 Expect.equals(selector.name, "foobar"); | 68 Expect.equals(simpSelector.name, "foobar"); |
| 69 } else if (idx == 1) { |
| 70 Expect.isTrue(simpSelector is ClassSelector); |
| 71 Expect.isTrue(selector.isCombinatorDescendant()); |
| 72 Expect.equals(simpSelector.name, "bar"); |
| 73 } else if (idx == 2) { |
| 74 Expect.isTrue(simpSelector is ClassSelector); |
| 75 Expect.isTrue(selector.isCombinatorDescendant()); |
| 76 Expect.equals(simpSelector.name, "no-story"); |
| 77 } else { |
| 78 Expect.fail("unexpected expression"); |
38 } | 79 } |
| 80 |
| 81 idx++; |
| 82 } |
| 83 |
| 84 Expect.equals(simpleSeqs.length, idx); |
| 85 } |
| 86 |
| 87 static void testId() { |
| 88 Parser parser = new Parser(new lang.SourceFile( |
| 89 lang.SourceFile.IN_MEMORY_FILE, "#elemId {}")); |
| 90 |
| 91 Stylesheet stylesheet = parser.parse(); |
| 92 Expect.isNotNull(stylesheet); |
| 93 Expect.equals(stylesheet.topLevels.length, 1); |
| 94 |
| 95 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 96 RuleSet ruleset = stylesheet.topLevels[0]; |
| 97 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 98 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 99 |
| 100 List<SimpleSelectorSequence> simpleSeqs = |
| 101 ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 102 |
| 103 for (final selector in simpleSeqs) { |
| 104 final simpSelector = selector.simpleSelector; |
| 105 Expect.isTrue(simpSelector is IdSelector); |
| 106 Expect.isTrue(selector.isCombinatorNone()); |
| 107 Expect.equals(simpSelector.name, "elemId"); |
| 108 } |
| 109 } |
| 110 |
| 111 static void testElement() { |
| 112 Parser parser = new Parser(new lang.SourceFile( |
| 113 lang.SourceFile.IN_MEMORY_FILE, "div {}")); |
| 114 Stylesheet stylesheet = parser.parse(); |
| 115 Expect.isNotNull(stylesheet); |
| 116 Expect.equals(stylesheet.topLevels.length, 1); |
| 117 |
| 118 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 119 RuleSet ruleset = stylesheet.topLevels[0]; |
| 120 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 121 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 122 |
| 123 List<SimpleSelectorSequence> simpleSeqs = |
| 124 ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 125 |
| 126 for (final selector in simpleSeqs) { |
| 127 final simpSelector = selector.simpleSelector; |
| 128 Expect.isTrue(simpSelector is ElementSelector); |
| 129 Expect.isTrue(selector.isCombinatorNone()); |
| 130 Expect.equals(simpSelector.name, "div"); |
39 } | 131 } |
40 | 132 |
41 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, | 133 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
42 ".foobar .bar .no-story")); | 134 "div div span {}")); |
43 | 135 stylesheet = parser.parse(); |
44 exprTree = parser.parse(); | 136 Expect.isNotNull(stylesheet); |
45 Expect.isNotNull(exprTree); | 137 Expect.equals(stylesheet.topLevels.length, 1); |
46 Expect.equals(exprTree.length, 1); | 138 |
47 for (selectorGroup in exprTree) { | 139 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
48 var idx = 0; | 140 ruleset = stylesheet.topLevels[0]; |
49 for (selector in selectorGroup.selectors) { | 141 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
50 if (idx == 0) { | 142 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
51 Expect.isTrue(selector is ClassSelector); | 143 |
52 Expect.isTrue(selector.isCombinatorNone()); | 144 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
53 Expect.equals(selector.name, "foobar"); | 145 |
54 } else if (idx == 1) { | 146 var idx = 0; |
55 Expect.isTrue(selector is ClassSelector); | 147 for (final selector in simpleSeqs) { |
56 Expect.isTrue(selector.isCombinatorDescendant()); | 148 final simpSelector = selector.simpleSelector; |
57 Expect.equals(selector.name, "bar"); | 149 if (idx == 0) { |
58 } else if (idx == 2) { | 150 Expect.isTrue(simpSelector is ElementSelector); |
59 Expect.isTrue(selector is ClassSelector); | 151 Expect.isTrue(selector.isCombinatorNone()); |
60 Expect.isTrue(selector.isCombinatorDescendant()); | 152 Expect.equals(simpSelector.name, "div"); |
61 Expect.equals(selector.name, "no-story"); | 153 } else if (idx == 1) { |
62 } else { | 154 Expect.isTrue(simpSelector is ElementSelector); |
63 Expect.fail("unexpected expression"); | 155 Expect.isTrue(selector.isCombinatorDescendant()); |
64 } | 156 Expect.equals(simpSelector.name, "div"); |
65 | 157 } else if (idx == 2) { |
66 idx++; | 158 Expect.isTrue(simpSelector is ElementSelector); |
| 159 Expect.isTrue(selector.isCombinatorDescendant()); |
| 160 Expect.equals(simpSelector.name, "span"); |
| 161 } else { |
| 162 Expect.fail("unexpected expression"); |
67 } | 163 } |
68 } | 164 |
69 } | 165 idx++; |
70 | 166 } |
71 static void testId() { | 167 Expect.equals(simpleSeqs.length, idx); |
72 Parser parser = new Parser(new lang.SourceFile( | 168 } |
73 lang.SourceFile.IN_MEMORY_FILE, "#elemId")); | 169 |
74 | 170 static void testNamespace() { |
75 List<SelectorGroup> exprTree = parser.parse(); | 171 Parser parser = new Parser(new lang.SourceFile( |
76 Expect.isNotNull(exprTree); | 172 lang.SourceFile.IN_MEMORY_FILE, "ns1|div {}")); |
77 Expect.equals(exprTree.length, 1); | 173 Stylesheet stylesheet = parser.parse(); |
78 Expect.isNotNull(exprTree); | 174 Expect.isNotNull(stylesheet); |
79 for (selectorGroup in exprTree) { | 175 Expect.equals(stylesheet.topLevels.length, 1); |
80 Expect.equals(selectorGroup.selectors.length, 1); | 176 |
81 for (selector in selectorGroup.selectors) { | 177 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
82 Expect.isTrue(selector is IdSelector); | 178 RuleSet ruleset = stylesheet.topLevels[0]; |
| 179 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 180 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 181 |
| 182 List<SimpleSelectorSequence> simpleSeqs = |
| 183 ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 184 |
| 185 for (final selector in simpleSeqs) { |
| 186 final simpSelector = selector.simpleSelector; |
| 187 Expect.isTrue(simpSelector is NamespaceSelector); |
| 188 Expect.isTrue(selector.isCombinatorNone()); |
| 189 Expect.isFalse(simpSelector.isNamespaceWildcard()); |
| 190 Expect.equals(simpSelector.namespace, "ns1"); |
| 191 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector; |
| 192 Expect.isTrue(elementSelector is ElementSelector); |
| 193 Expect.isFalse(elementSelector.isWildcard()); |
| 194 Expect.equals(elementSelector.name, "div"); |
| 195 } |
| 196 |
| 197 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 198 "ns1|div div ns2|span .foobar {}")); |
| 199 stylesheet = parser.parse(); |
| 200 Expect.isNotNull(stylesheet); |
| 201 |
| 202 Expect.equals(stylesheet.topLevels.length, 1); |
| 203 |
| 204 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 205 ruleset = stylesheet.topLevels[0]; |
| 206 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 207 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 208 |
| 209 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 210 |
| 211 var idx = 0; |
| 212 for (final selector in simpleSeqs) { |
| 213 final simpSelector = selector.simpleSelector; |
| 214 if (idx == 0) { |
| 215 Expect.isTrue(simpSelector is NamespaceSelector); |
83 Expect.isTrue(selector.isCombinatorNone()); | 216 Expect.isTrue(selector.isCombinatorNone()); |
84 Expect.equals(selector.name, "elemId"); | 217 Expect.equals(simpSelector.namespace, "ns1"); |
85 } | 218 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector; |
86 } | |
87 } | |
88 | |
89 static void testElement() { | |
90 Parser parser = new Parser(new lang.SourceFile( | |
91 lang.SourceFile.IN_MEMORY_FILE, "div")); | |
92 List<SelectorGroup> exprTree = parser.parse(); | |
93 Expect.isNotNull(exprTree); | |
94 Expect.equals(exprTree.length, 1); | |
95 for (selectorGroup in exprTree) { | |
96 Expect.equals(selectorGroup.selectors.length, 1); | |
97 for (selector in selectorGroup.selectors) { | |
98 Expect.isTrue(selector is ElementSelector); | |
99 Expect.isTrue(selector.isCombinatorNone()); | |
100 Expect.equals(selector.name, "div"); | |
101 } | |
102 } | |
103 | |
104 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, | |
105 "div div span")); | |
106 exprTree = parser.parse(); | |
107 Expect.isNotNull(exprTree); | |
108 Expect.equals(exprTree.length, 1); | |
109 for (selectorGroup in exprTree) { | |
110 var idx = 0; | |
111 for (selector in selectorGroup.selectors) { | |
112 if (idx == 0) { | |
113 Expect.isTrue(selector is ElementSelector); | |
114 Expect.isTrue(selector.isCombinatorNone()); | |
115 Expect.equals(selector.name, "div"); | |
116 } else if (idx == 1) { | |
117 Expect.isTrue(selector is ElementSelector); | |
118 Expect.isTrue(selector.isCombinatorDescendant()); | |
119 Expect.equals(selector.name, "div"); | |
120 } else if (idx == 2) { | |
121 Expect.isTrue(selector is ElementSelector); | |
122 Expect.isTrue(selector.isCombinatorDescendant()); | |
123 Expect.equals(selector.name, "span"); | |
124 } else { | |
125 Expect.fail("unexpected expression"); | |
126 } | |
127 | |
128 idx++; | |
129 } | |
130 } | |
131 } | |
132 | |
133 static void testNamespace() { | |
134 Parser parser = new Parser(new lang.SourceFile( | |
135 lang.SourceFile.IN_MEMORY_FILE, "ns1|div")); | |
136 List<SelectorGroup> exprTree = parser.parse(); | |
137 Expect.isNotNull(exprTree); | |
138 Expect.equals(exprTree.length, 1); | |
139 for (selectorGroup in exprTree) { | |
140 Expect.equals(selectorGroup.selectors.length, 1); | |
141 for (selector in selectorGroup.selectors) { | |
142 Expect.isTrue(selector is NamespaceSelector); | |
143 Expect.isTrue(selector.isCombinatorNone()); | |
144 Expect.isFalse(selector.isNamespaceWildcard()); | |
145 Expect.equals(selector.namespace, "ns1"); | |
146 ElementSelector elementSelector = selector.nameAsSimpleSelector; | |
147 Expect.isTrue(elementSelector is ElementSelector); | 219 Expect.isTrue(elementSelector is ElementSelector); |
148 Expect.isFalse(elementSelector.isWildcard()); | 220 Expect.isFalse(elementSelector.isWildcard()); |
149 Expect.equals(elementSelector.name, "div"); | 221 Expect.equals(elementSelector.name, "div"); |
| 222 } else if (idx == 1) { |
| 223 Expect.isTrue(simpSelector is ElementSelector); |
| 224 Expect.isTrue(selector.isCombinatorDescendant()); |
| 225 Expect.equals(simpSelector.name, "div"); |
| 226 } else if (idx == 2) { |
| 227 Expect.isTrue(simpSelector is NamespaceSelector); |
| 228 Expect.isTrue(selector.isCombinatorDescendant()); |
| 229 Expect.equals(simpSelector.namespace, "ns2"); |
| 230 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector; |
| 231 Expect.isTrue(elementSelector is ElementSelector); |
| 232 Expect.isFalse(elementSelector.isWildcard()); |
| 233 Expect.equals(elementSelector.name, "span"); |
| 234 } else if (idx == 3) { |
| 235 Expect.isTrue(simpSelector is ClassSelector); |
| 236 Expect.isTrue(selector.isCombinatorDescendant()); |
| 237 Expect.equals(simpSelector.name, "foobar"); |
| 238 } else { |
| 239 Expect.fail("unexpected expression"); |
150 } | 240 } |
151 } | 241 |
152 | 242 idx++; |
153 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, | 243 } |
154 "ns1|div div ns2|span .foobar")); | 244 |
155 exprTree = parser.parse(); | 245 Expect.equals(simpleSeqs.length, idx); |
156 Expect.isNotNull(exprTree); | 246 } |
157 Expect.equals(exprTree.length, 1); | 247 |
158 for (selectorGroup in exprTree) { | 248 static void testSelectorGroups() { |
| 249 Parser parser = new Parser(new lang.SourceFile( |
| 250 lang.SourceFile.IN_MEMORY_FILE, |
| 251 "div, .foobar ,#elemId, .xyzzy .test, ns1|div div #elemId .foobar {}")); |
| 252 Stylesheet stylesheet = parser.parse(); |
| 253 Expect.isNotNull(stylesheet); |
| 254 |
| 255 Expect.equals(stylesheet.topLevels.length, 1); |
| 256 |
| 257 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 258 RuleSet ruleset = stylesheet.topLevels[0]; |
| 259 Expect.equals(ruleset.selectorGroup.selectors.length, 5); |
| 260 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 261 |
| 262 var groupIdx = 0; |
| 263 for (final selectorGroup in ruleset.selectorGroup.selectors) { |
159 var idx = 0; | 264 var idx = 0; |
160 for (selector in selectorGroup.selectors) { | 265 for (final selector in selectorGroup.simpleSelectorSequences) { |
161 if (idx == 0) { | 266 final simpSelector = selector.simpleSelector; |
162 Expect.isTrue(selector is NamespaceSelector); | |
163 Expect.isTrue(selector.isCombinatorNone()); | |
164 Expect.equals(selector.namespace, "ns1"); | |
165 ElementSelector elementSelector = selector.nameAsSimpleSelector; | |
166 Expect.isTrue(elementSelector is ElementSelector); | |
167 Expect.isFalse(elementSelector.isWildcard()); | |
168 Expect.equals(elementSelector.name, "div"); | |
169 } else if (idx == 1) { | |
170 Expect.isTrue(selector is ElementSelector); | |
171 Expect.isTrue(selector.isCombinatorDescendant()); | |
172 Expect.equals(selector.name, "div"); | |
173 } else if (idx == 2) { | |
174 Expect.isTrue(selector is NamespaceSelector); | |
175 Expect.isTrue(selector.isCombinatorDescendant()); | |
176 Expect.equals(selector.namespace, "ns2"); | |
177 ElementSelector elementSelector = selector.nameAsSimpleSelector; | |
178 Expect.isTrue(elementSelector is ElementSelector); | |
179 Expect.isFalse(elementSelector.isWildcard()); | |
180 Expect.equals(elementSelector.name, "span"); | |
181 } else if (idx == 3) { | |
182 Expect.isTrue(selector is ClassSelector); | |
183 Expect.isTrue(selector.isCombinatorDescendant()); | |
184 Expect.equals(selector.name, "foobar"); | |
185 } else { | |
186 Expect.fail("unexpected expression"); | |
187 } | |
188 | |
189 idx++; | |
190 } | |
191 } | |
192 } | |
193 | |
194 static void testSelectorGroups() { | |
195 Parser parser = new Parser(new lang.SourceFile( | |
196 lang.SourceFile.IN_MEMORY_FILE, | |
197 "div, .foobar ,#elemId, .xyzzy .test, ns1|div div #elemId .foobar")); | |
198 List<SelectorGroup> exprTree = parser.parse(); | |
199 Expect.isNotNull(exprTree); | |
200 Expect.equals(exprTree.length, 5); | |
201 var groupIdx = 0; | |
202 for (selectorGroup in exprTree) { | |
203 var idx = 0; | |
204 for (selector in selectorGroup.selectors) { | |
205 switch (groupIdx) { | 267 switch (groupIdx) { |
206 case 0: // First selector group. | 268 case 0: // First selector group. |
207 Expect.equals(selectorGroup.selectors.length, 1); | 269 Expect.isTrue(simpSelector is ElementSelector); |
208 Expect.isTrue(selector is ElementSelector); | |
209 Expect.isTrue(selector.isCombinatorNone()); | 270 Expect.isTrue(selector.isCombinatorNone()); |
210 Expect.equals(selector.name, "div"); | 271 Expect.equals(simpSelector.name, "div"); |
211 break; | 272 break; |
212 case 1: // Second selector group. | 273 case 1: // Second selector group. |
213 Expect.equals(selectorGroup.selectors.length, 1); | 274 Expect.isTrue(simpSelector is ClassSelector); |
214 Expect.isTrue(selector is ClassSelector); | |
215 Expect.isTrue(selector.isCombinatorNone()); | 275 Expect.isTrue(selector.isCombinatorNone()); |
216 Expect.equals(selector.name, "foobar"); | 276 Expect.equals(simpSelector.name, "foobar"); |
217 break; | 277 break; |
218 case 2: // Third selector group. | 278 case 2: // Third selector group. |
219 Expect.equals(selectorGroup.selectors.length, 1); | 279 Expect.isTrue(simpSelector is IdSelector); |
220 Expect.isTrue(selector is IdSelector); | |
221 Expect.isTrue(selector.isCombinatorNone()); | 280 Expect.isTrue(selector.isCombinatorNone()); |
222 Expect.equals(selector.name, "elemId"); | 281 Expect.equals(simpSelector.name, "elemId"); |
223 break; | 282 break; |
224 case 3: // Fourth selector group. | 283 case 3: // Fourth selector group. |
225 Expect.equals(selectorGroup.selectors.length, 2); | 284 Expect.equals(selectorGroup.simpleSelectorSequences.length, 2); |
226 if (idx == 0) { | 285 if (idx == 0) { |
227 Expect.isTrue(selector is ClassSelector); | 286 Expect.isTrue(simpSelector is ClassSelector); |
228 Expect.isTrue(selector.isCombinatorNone()); | 287 Expect.isTrue(selector.isCombinatorNone()); |
229 Expect.equals(selector.name, "xyzzy"); | 288 Expect.equals(simpSelector.name, "xyzzy"); |
230 } else if (idx == 1) { | 289 } else if (idx == 1) { |
231 Expect.isTrue(selector is ClassSelector); | 290 Expect.isTrue(simpSelector is ClassSelector); |
232 Expect.isTrue(selector.isCombinatorDescendant()); | 291 Expect.isTrue(selector.isCombinatorDescendant()); |
233 Expect.equals(selector.name, "test"); | 292 Expect.equals(simpSelector.name, "test"); |
234 } else { | 293 } else { |
235 Expect.fail("unexpected expression"); | 294 Expect.fail("unexpected expression"); |
236 } | 295 } |
237 break; | 296 break; |
238 case 4: // Fifth selector group. | 297 case 4: // Fifth selector group. |
239 Expect.equals(selectorGroup.selectors.length, 4); | 298 Expect.equals(selectorGroup.simpleSelectorSequences.length, 4); |
240 if (idx == 0) { | 299 if (idx == 0) { |
241 Expect.isTrue(selector is NamespaceSelector); | 300 Expect.isTrue(simpSelector is NamespaceSelector); |
242 Expect.isTrue(selector.isCombinatorNone()); | 301 Expect.isTrue(selector.isCombinatorNone()); |
243 Expect.equals(selector.namespace, "ns1"); | 302 Expect.equals(simpSelector.namespace, "ns1"); |
244 ElementSelector elementSelector = selector.nameAsSimpleSelector; | 303 ElementSelector elementSelector = simpSelector.nameAsSimpleSelecto
r; |
245 Expect.isTrue(elementSelector is ElementSelector); | 304 Expect.isTrue(elementSelector is ElementSelector); |
246 Expect.isFalse(elementSelector.isWildcard()); | 305 Expect.isFalse(elementSelector.isWildcard()); |
247 Expect.equals(elementSelector.name, "div"); | 306 Expect.equals(elementSelector.name, "div"); |
248 } else if (idx == 1) { | 307 } else if (idx == 1) { |
249 Expect.isTrue(selector is ElementSelector); | 308 Expect.isTrue(simpSelector is ElementSelector); |
250 Expect.isTrue(selector.isCombinatorDescendant()); | 309 Expect.isTrue(selector.isCombinatorDescendant()); |
251 Expect.equals(selector.name, "div"); | 310 Expect.equals(simpSelector.name, "div"); |
252 } else if (idx == 2) { | 311 } else if (idx == 2) { |
253 Expect.isTrue(selector is IdSelector); | 312 Expect.isTrue(simpSelector is IdSelector); |
254 Expect.isTrue(selector.isCombinatorDescendant()); | 313 Expect.isTrue(selector.isCombinatorDescendant()); |
255 Expect.equals(selector.name, "elemId"); | 314 Expect.equals(simpSelector.name, "elemId"); |
256 } else if (idx == 3) { | 315 } else if (idx == 3) { |
257 Expect.isTrue(selector is ClassSelector); | 316 Expect.isTrue(simpSelector is ClassSelector); |
258 Expect.isTrue(selector.isCombinatorDescendant()); | 317 Expect.isTrue(selector.isCombinatorDescendant()); |
259 Expect.equals(selector.name, "foobar"); | 318 Expect.equals(simpSelector.name, "foobar"); |
260 } else { | 319 } else { |
261 Expect.fail("unexpected expression"); | 320 Expect.fail("unexpected expression"); |
262 } | 321 } |
263 break; | 322 break; |
264 } | 323 } |
265 idx++; | 324 idx++; |
266 } | 325 } |
267 groupIdx++; | 326 groupIdx++; |
268 } | 327 } |
269 } | 328 } |
270 | 329 |
271 static void testCombinator() { | 330 static void testCombinator() { |
272 Parser parser = new Parser(new lang.SourceFile( | 331 Parser parser = new Parser(new lang.SourceFile( |
273 lang.SourceFile.IN_MEMORY_FILE, | 332 lang.SourceFile.IN_MEMORY_FILE, |
274 ".foobar > .bar + .no-story ~ myNs|div #elemId")); | 333 ".foobar > .bar + .no-story ~ myNs|div #elemId {}")); |
275 | 334 |
276 List<SelectorGroup> exprTree = parser.parse(); | 335 Stylesheet stylesheet = parser.parse(); |
277 Expect.isNotNull(exprTree); | 336 Expect.isNotNull(stylesheet); |
278 Expect.equals(exprTree.length, 1); | 337 Expect.equals(stylesheet.topLevels.length, 1); |
279 for (selectorGroup in exprTree) { | |
280 var idx = 0; | |
281 Expect.equals(selectorGroup.selectors.length, 5); | |
282 for (selector in selectorGroup.selectors) { | |
283 if (idx == 0) { | |
284 Expect.isTrue(selector is ClassSelector); | |
285 Expect.isTrue(selector.isCombinatorNone()); | |
286 Expect.equals(selector.name, "foobar"); | |
287 } else if (idx == 1) { | |
288 Expect.isTrue(selector is ClassSelector); | |
289 Expect.isTrue(selector.isCombinatorGreater()); | |
290 Expect.equals(selector.name, "bar"); | |
291 } else if (idx == 2) { | |
292 Expect.isTrue(selector is ClassSelector); | |
293 Expect.isTrue(selector.isCombinatorPlus()); | |
294 Expect.equals(selector.name, "no-story"); | |
295 } else if (idx == 3) { | |
296 Expect.isTrue(selector is NamespaceSelector); | |
297 Expect.isTrue(selector.isCombinatorTilde()); | |
298 Expect.equals(selector.namespace, "myNs"); | |
299 ElementSelector elementSelector = selector.nameAsSimpleSelector; | |
300 Expect.isTrue(elementSelector is ElementSelector); | |
301 Expect.isFalse(elementSelector.isWildcard()); | |
302 Expect.equals(elementSelector.name, "div"); | |
303 } else if (idx == 4) { | |
304 Expect.isTrue(selector is IdSelector); | |
305 Expect.isTrue(selector.isCombinatorDescendant()); | |
306 Expect.equals(selector.name, "elemId"); | |
307 } else { | |
308 Expect.fail("unexpected expression"); | |
309 } | |
310 | 338 |
311 idx++; | 339 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 340 RuleSet ruleset = stylesheet.topLevels[0]; |
| 341 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 342 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 343 |
| 344 List<SimpleSelectorSequence> simpleSeqs = |
| 345 ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 346 |
| 347 Expect.equals(simpleSeqs.length, 5); |
| 348 var idx = 0; |
| 349 for (final selector in simpleSeqs) { |
| 350 final simpSelector = selector.simpleSelector; |
| 351 if (idx == 0) { |
| 352 Expect.isTrue(simpSelector is ClassSelector); |
| 353 Expect.isTrue(selector.isCombinatorNone()); |
| 354 Expect.equals(simpSelector.name, "foobar"); |
| 355 } else if (idx == 1) { |
| 356 Expect.isTrue(simpSelector is ClassSelector); |
| 357 Expect.isTrue(selector.isCombinatorGreater()); |
| 358 Expect.equals(simpSelector.name, "bar"); |
| 359 } else if (idx == 2) { |
| 360 Expect.isTrue(simpSelector is ClassSelector); |
| 361 Expect.isTrue(selector.isCombinatorPlus()); |
| 362 Expect.equals(simpSelector.name, "no-story"); |
| 363 } else if (idx == 3) { |
| 364 Expect.isTrue(simpSelector is NamespaceSelector); |
| 365 Expect.isTrue(selector.isCombinatorTilde()); |
| 366 Expect.equals(simpSelector.namespace, "myNs"); |
| 367 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector; |
| 368 Expect.isTrue(elementSelector is ElementSelector); |
| 369 Expect.isFalse(elementSelector.isWildcard()); |
| 370 Expect.equals(elementSelector.name, "div"); |
| 371 } else if (idx == 4) { |
| 372 Expect.isTrue(simpSelector is IdSelector); |
| 373 Expect.isTrue(selector.isCombinatorDescendant()); |
| 374 Expect.equals(simpSelector.name, "elemId"); |
| 375 } else { |
| 376 Expect.fail("unexpected expression"); |
312 } | 377 } |
| 378 |
| 379 idx++; |
313 } | 380 } |
314 } | 381 } |
315 | 382 |
316 static void testWildcard() { | 383 static void testWildcard() { |
317 Parser parser = new Parser(new lang.SourceFile( | 384 Parser parser = new Parser(new lang.SourceFile( |
318 lang.SourceFile.IN_MEMORY_FILE, "*")); | 385 lang.SourceFile.IN_MEMORY_FILE, "* {}")); |
319 | 386 |
320 List<SelectorGroup> exprTree = parser.parse(); | 387 Stylesheet stylesheet = parser.parse(); |
321 Expect.isNotNull(exprTree); | 388 Expect.isNotNull(stylesheet); |
322 Expect.equals(exprTree.length, 1); | 389 Expect.equals(stylesheet.topLevels.length, 1); |
323 for (selectorGroup in exprTree) { | 390 |
324 Expect.equals(selectorGroup.selectors.length, 1); | 391 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
325 for (selector in selectorGroup.selectors) { | 392 RuleSet ruleset = stylesheet.topLevels[0]; |
326 Expect.isTrue(selector is ElementSelector); | 393 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
327 Expect.isTrue(selector.isCombinatorNone()); | 394 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
328 Expect.isTrue(selector.isWildcard()); | 395 |
329 Expect.equals(selector.name, "*"); | 396 List<SimpleSelectorSequence> simpleSeqs = |
330 } | 397 ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 398 |
| 399 for (final selector in simpleSeqs) { |
| 400 final simpSelector = selector.simpleSelector; |
| 401 Expect.isTrue(simpSelector is ElementSelector); |
| 402 Expect.isTrue(selector.isCombinatorNone()); |
| 403 Expect.isTrue(simpSelector.isWildcard()); |
| 404 Expect.equals(simpSelector.name, "*"); |
331 } | 405 } |
332 | 406 |
333 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, | 407 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
334 "*.foobar")); | 408 "*.foobar {}")); |
335 | 409 |
336 exprTree = parser.parse(); | 410 stylesheet = parser.parse(); |
337 Expect.isNotNull(exprTree); | 411 Expect.isNotNull(stylesheet); |
338 Expect.equals(exprTree.length, 1); | 412 Expect.equals(stylesheet.topLevels.length, 1); |
339 for (selectorGroup in exprTree) { | |
340 var idx = 0; | |
341 for (selector in selectorGroup.selectors) { | |
342 Expect.equals(selectorGroup.selectors.length, 2); | |
343 if (idx == 0) { | |
344 Expect.isTrue(selector is ElementSelector); | |
345 Expect.isTrue(selector.isCombinatorNone()); | |
346 Expect.isTrue(selector.isWildcard()); | |
347 Expect.equals(selector.name, "*"); | |
348 } else if (idx == 1) { | |
349 Expect.isTrue(selector is ClassSelector); | |
350 Expect.isTrue(selector.isCombinatorNone()); | |
351 Expect.equals(selector.name, "foobar"); | |
352 } else { | |
353 Expect.fail("unexpected expression"); | |
354 } | |
355 | 413 |
356 idx++; | 414 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 415 ruleset = stylesheet.topLevels[0]; |
| 416 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 417 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 418 |
| 419 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 420 |
| 421 Expect.equals(simpleSeqs.length, 2); |
| 422 var idx = 0; |
| 423 for (final selector in simpleSeqs) { |
| 424 final simpSelector = selector.simpleSelector; |
| 425 if (idx == 0) { |
| 426 Expect.isTrue(simpSelector is ElementSelector); |
| 427 Expect.isTrue(selector.isCombinatorNone()); |
| 428 Expect.isTrue(simpSelector.isWildcard()); |
| 429 Expect.equals(simpSelector.name, "*"); |
| 430 } else if (idx == 1) { |
| 431 Expect.isTrue(simpSelector is ClassSelector); |
| 432 Expect.isTrue(selector.isCombinatorNone()); |
| 433 Expect.equals(simpSelector.name, "foobar"); |
| 434 } else { |
| 435 Expect.fail("unexpected expression"); |
357 } | 436 } |
| 437 |
| 438 idx++; |
358 } | 439 } |
359 | 440 |
360 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, | 441 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
361 "myNs|*.foobar")); | 442 "myNs|*.foobar {}")); |
362 | 443 |
363 exprTree = parser.parse(); | 444 stylesheet = parser.parse(); |
364 Expect.isNotNull(exprTree); | 445 Expect.isNotNull(stylesheet); |
365 Expect.equals(exprTree.length, 1); | 446 Expect.equals(stylesheet.topLevels.length, 1); |
366 for (selectorGroup in exprTree) { | |
367 var idx = 0; | |
368 for (selector in selectorGroup.selectors) { | |
369 Expect.equals(selectorGroup.selectors.length, 2); | |
370 if (idx == 0) { | |
371 Expect.isTrue(selector is NamespaceSelector); | |
372 Expect.isTrue(selector.isCombinatorNone()); | |
373 Expect.isFalse(selector.isNamespaceWildcard()); | |
374 ElementSelector elementSelector = selector.nameAsSimpleSelector; | |
375 Expect.equals("myNs", selector.namespace); | |
376 Expect.isTrue(elementSelector.isWildcard()); | |
377 Expect.equals("*", elementSelector.name); | |
378 } else if (idx == 1) { | |
379 Expect.isTrue(selector is ClassSelector); | |
380 Expect.isTrue(selector.isCombinatorNone()); | |
381 Expect.equals("foobar", selector.name); | |
382 } else { | |
383 Expect.fail("unexpected expression"); | |
384 } | |
385 | 447 |
386 idx++; | 448 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 449 ruleset = stylesheet.topLevels[0]; |
| 450 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 451 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 452 |
| 453 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 454 |
| 455 Expect.equals(simpleSeqs.length, 2); |
| 456 idx = 0; |
| 457 for (final selector in simpleSeqs) { |
| 458 final simpSelector = selector.simpleSelector; |
| 459 if (idx == 0) { |
| 460 Expect.isTrue(simpSelector is NamespaceSelector); |
| 461 Expect.isTrue(selector.isCombinatorNone()); |
| 462 Expect.isFalse(simpSelector.isNamespaceWildcard()); |
| 463 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector; |
| 464 Expect.equals("myNs", simpSelector.namespace); |
| 465 Expect.isTrue(elementSelector.isWildcard()); |
| 466 Expect.equals("*", elementSelector.name); |
| 467 } else if (idx == 1) { |
| 468 Expect.isTrue(simpSelector is ClassSelector); |
| 469 Expect.isTrue(selector.isCombinatorNone()); |
| 470 Expect.equals("foobar", simpSelector.name); |
| 471 } else { |
| 472 Expect.fail("unexpected expression"); |
387 } | 473 } |
| 474 |
| 475 idx++; |
388 } | 476 } |
389 | 477 |
390 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, | 478 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
391 "*|*.foobar")); | 479 "*|*.foobar {}")); |
392 | 480 |
393 exprTree = parser.parse(); | 481 stylesheet = parser.parse(); |
394 Expect.isNotNull(exprTree); | 482 Expect.isNotNull(stylesheet); |
395 Expect.equals(exprTree.length, 1); | |
396 for (selectorGroup in exprTree) { | |
397 var idx = 0; | |
398 for (selector in selectorGroup.selectors) { | |
399 Expect.equals(selectorGroup.selectors.length, 2); | |
400 if (idx == 0) { | |
401 Expect.isTrue(selector is NamespaceSelector); | |
402 Expect.isTrue(selector.isCombinatorNone()); | |
403 Expect.isTrue(selector.isNamespaceWildcard()); | |
404 Expect.equals("*", selector.namespace); | |
405 ElementSelector elementSelector = selector.nameAsSimpleSelector; | |
406 Expect.isTrue(elementSelector.isWildcard()); | |
407 Expect.equals("*", elementSelector.name); | |
408 } else if (idx == 1) { | |
409 Expect.isTrue(selector is ClassSelector); | |
410 Expect.isTrue(selector.isCombinatorNone()); | |
411 Expect.equals("foobar", selector.name); | |
412 } else { | |
413 Expect.fail("unexpected expression"); | |
414 } | |
415 | 483 |
416 idx++; | 484 Expect.isTrue(stylesheet.topLevels[0] is RuleSet); |
| 485 ruleset = stylesheet.topLevels[0]; |
| 486 Expect.equals(ruleset.selectorGroup.selectors.length, 1); |
| 487 Expect.equals(ruleset.declarationGroup.declarations.length, 0); |
| 488 |
| 489 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences; |
| 490 |
| 491 Expect.equals(simpleSeqs.length, 2); |
| 492 idx = 0; |
| 493 for (final selector in simpleSeqs) { |
| 494 final simpSelector = selector.simpleSelector; |
| 495 if (idx == 0) { |
| 496 Expect.isTrue(simpSelector is NamespaceSelector); |
| 497 Expect.isTrue(selector.isCombinatorNone()); |
| 498 Expect.isTrue(simpSelector.isNamespaceWildcard()); |
| 499 Expect.equals("*", simpSelector.namespace); |
| 500 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector; |
| 501 Expect.isTrue(elementSelector.isWildcard()); |
| 502 Expect.equals("*", elementSelector.name); |
| 503 } else if (idx == 1) { |
| 504 Expect.isTrue(simpSelector is ClassSelector); |
| 505 Expect.isTrue(selector.isCombinatorNone()); |
| 506 Expect.equals("foobar", simpSelector.name); |
| 507 } else { |
| 508 Expect.fail("unexpected expression"); |
417 } | 509 } |
| 510 |
| 511 idx++; |
418 } | 512 } |
419 | 513 |
420 } | 514 } |
421 | 515 |
422 static void testPseudo() { | 516 static void testPseudo() { |
423 // TODO(terry): Implement | 517 // TODO(terry): Implement |
424 } | 518 } |
425 | 519 |
426 static void testAttribute() { | 520 static void testAttribute() { |
427 // TODO(terry): Implement | 521 // TODO(terry): Implement |
428 } | 522 } |
429 | 523 |
430 static void testNegation() { | 524 static void testNegation() { |
431 // TODO(terry): Implement | 525 // TODO(terry): Implement |
432 } | 526 } |
433 | 527 |
434 } | 528 } |
435 | 529 |
436 | 530 |
437 main() { | 531 main() { |
438 SelectorLiteralTest.testMain(); | 532 ExpressionTest.testMain(); |
439 } | 533 } |
OLD | NEW |