Skip to content

Commit 7903c8b

Browse files
committed
more :is() selector tests
1 parent fe92dc7 commit 7903c8b

File tree

2 files changed

+199
-4
lines changed

2 files changed

+199
-4
lines changed

src/test/java/org/htmlunit/cssparser/parser/AbstractCSSParserTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,27 @@ protected MediaQueryList parseMedia(final String media,
162162
return mediaList;
163163
}
164164

165+
protected SelectorList parseSelectors(final String cssText,
166+
final int err, final int fatal, final int warn) throws Exception {
167+
final CSSOMParser parser = new CSSOMParser();
168+
final ErrorHandler errorHandler = new ErrorHandler();
169+
parser.setErrorHandler(errorHandler);
170+
171+
final SelectorList selectorList = parser.parseSelectors(cssText);
172+
173+
assertEquals(err, errorHandler.getErrorCount());
174+
assertEquals(fatal, errorHandler.getFatalErrorCount());
175+
assertEquals(warn, errorHandler.getWarningCount());
176+
177+
return selectorList;
178+
}
179+
165180
protected SelectorList createSelectors(final String cssText) throws Exception {
166181
return new CSSOMParser().parseSelectors(cssText);
167182
}
168183

169184
protected List<Condition> createConditions(final String cssText) throws Exception {
170-
final SelectorList selectors = createSelectors(cssText);
185+
final SelectorList selectors = parseSelectors(cssText, 0, 0, 0);
171186
final Selector selector = selectors.get(0);
172187
final ElementSelector elementSelector = (ElementSelector) selector;
173188
return elementSelector.getConditions();
@@ -243,12 +258,12 @@ protected void conditionAssert(final String cssText, final String name,
243258
}
244259

245260
protected void selectorList(final String cssText, final int length) throws Exception {
246-
final SelectorList selectors = createSelectors(cssText);
261+
final SelectorList selectors = parseSelectors(cssText, 0, 0, 0);
247262
assertEquals(length, selectors.size());
248263
}
249264

250265
protected void selectorType(final String cssText, final SelectorType... selectorTypes) throws Exception {
251-
final SelectorList selectors = createSelectors(cssText);
266+
final SelectorList selectors = parseSelectors(cssText, 0, 0, 0);
252267
final Selector selector = selectors.get(0);
253268
assertEquals(selectorTypes[0], selector.getSelectorType());
254269
if (selectorTypes[0] == SelectorType.DESCENDANT_SELECTOR) {

src/test/java/org/htmlunit/cssparser/parser/CSS3ParserIsSelectorTest.java

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CSS3ParserIsSelectorTest extends AbstractCSSParserTest {
3636
@Test
3737
public void isElementType() throws Exception {
3838
// element name
39-
final SelectorList selectors = createSelectors(":is(ol, ul)");
39+
final SelectorList selectors = parseSelectors(":is(ol, ul)", 0, 0, 0);
4040
assertEquals("*:is(ol, ul)", selectors.get(0).toString());
4141

4242
assertEquals(1, selectors.size());
@@ -60,4 +60,184 @@ public void isElementType() throws Exception {
6060
final ElementSelector conditionElemSelector = (ElementSelector) conditionSelector;
6161
assertEquals("ol", conditionElemSelector.getElementName());
6262
}
63+
64+
/**
65+
* @throws Exception if any error occurs
66+
*/
67+
@Test
68+
public void basic() throws Exception {
69+
parseSelectors(":is(h1, h2, h3)", 0, 0, 0);
70+
parseSelectors(":is(.class1, .class2)", 0, 0, 0);
71+
parseSelectors(":is(#id1, #id2, #id3)", 0, 0, 0);
72+
parseSelectors(":is(div, span, p)", 0, 0, 0);
73+
parseSelectors(":is([data-attr], [title])", 0, 0, 0);
74+
parseSelectors(":is(input, textarea, select)", 0, 0, 0);
75+
}
76+
77+
/**
78+
* @throws Exception if any error occurs
79+
*/
80+
@Test
81+
public void pseudoClassCombinations() throws Exception {
82+
parseSelectors(":is(a:hover, a:focus)", 0, 0, 0);
83+
parseSelectors(":is(button:disabled, input:disabled)", 0, 0, 0);
84+
parseSelectors(":is(li:first-child, li:last-child)", 0, 0, 0);
85+
parseSelectors(":is(tr:nth-child(odd), tr:nth-child(even))", 0, 0, 0);
86+
}
87+
88+
/**
89+
* @throws Exception if any error occurs
90+
*/
91+
@Test
92+
public void complex() throws Exception {
93+
parseSelectors(":is(.nav > li, .menu > li)", 0, 0, 0);
94+
parseSelectors(":is(article h1, section h1)", 0, 0, 0);
95+
parseSelectors(":is(.sidebar .widget, .footer .widget)", 0, 0, 0);
96+
parseSelectors(":is(form input[type=\"text\"], form input[type=\"email\"])", 0, 0, 0);
97+
}
98+
99+
/**
100+
* @throws Exception if any error occurs
101+
*/
102+
@Test
103+
public void nested() throws Exception {
104+
parseSelectors(":is(:is(h1, h2), :is(h3, h4))", 0, 0, 0);
105+
parseSelectors(":is(article h1, section h1)", 0, 0, 0);
106+
parseSelectors(":is(.sidebar .widget, .footer .widget)", 0, 0, 0);
107+
parseSelectors(":is(.container :is(.item, .element), .wrapper :is(.item, .element))", 0, 0, 0);
108+
}
109+
110+
/**
111+
* @throws Exception if any error occurs
112+
*/
113+
@Test
114+
public void descendant() throws Exception {
115+
parseSelectors("div :is(p, span)", 0, 0, 0);
116+
parseSelectors(".container :is(.item, .box)", 0, 0, 0);
117+
parseSelectors("article :is(h1, h2, h3)", 0, 0, 0);
118+
}
119+
120+
/**
121+
* @throws Exception if any error occurs
122+
*/
123+
@Test
124+
public void adjacentGeneralSibling() throws Exception {
125+
parseSelectors(":is(h1, h2) + p", 0, 0, 0);
126+
parseSelectors(":is(.alert, .warning) ~ div", 0, 0, 0);
127+
parseSelectors(":is(img, video) + figcaption", 0, 0, 0);
128+
}
129+
130+
/**
131+
* @throws Exception if any error occurs
132+
*/
133+
@Test
134+
public void multiple() throws Exception {
135+
parseSelectors(":is(main, aside) > :is(section, article)", 0, 0, 0);
136+
parseSelectors(":is(.header, .footer) .nav :is(a, button)", 0, 0, 0);
137+
}
138+
139+
/**
140+
* @throws Exception if any error occurs
141+
*/
142+
@Test
143+
public void attribute() throws Exception {
144+
parseSelectors(":is(input[required], textarea[required])", 0, 0, 0);
145+
parseSelectors(":is([data-theme=\"dark\"], [data-theme=\"night\"])", 0, 0, 0);
146+
parseSelectors(":is(a[href^=\"http\"], a[href^=\"mailto\"])", 0, 0, 0);
147+
}
148+
149+
/**
150+
* @throws Exception if any error occurs
151+
*/
152+
@Test
153+
public void pseudo() throws Exception {
154+
parseSelectors(":is(h1, h2, h3)::before", 0, 0, 0);
155+
parseSelectors(":is(blockquote, q)::after", 0, 0, 0);
156+
}
157+
158+
/**
159+
* @throws Exception if any error occurs
160+
*/
161+
@Test
162+
public void whitespace() throws Exception {
163+
parseSelectors(":is(h1,h2,h3)", 0, 0, 0);
164+
parseSelectors(":is( h1 , h2 , h3 )", 0, 0, 0);
165+
parseSelectors(":is( h1,\n h2,\n h3\n )", 0, 0, 0);
166+
}
167+
168+
/**
169+
* @throws Exception if any error occurs
170+
*/
171+
@Test
172+
public void single() throws Exception {
173+
parseSelectors(":is(div)", 0, 0, 0);
174+
parseSelectors(":is(.single-class)", 0, 0, 0);
175+
}
176+
177+
/**
178+
* @throws Exception if any error occurs
179+
*/
180+
@Test
181+
public void emptyAndWhitespace() throws Exception {
182+
parseSelectors(":is()", 1, 0, 0);
183+
parseSelectors(":is( )", 1, 0, 0);
184+
parseSelectors(":is(,)", 1, 0, 0);
185+
parseSelectors(":is(h1,)", 1, 0, 0);
186+
parseSelectors(":is(h1,,h2)", 1, 0, 0);
187+
}
188+
189+
/**
190+
* @throws Exception if any error occurs
191+
*/
192+
@Test
193+
public void syntaxErrors() throws Exception {
194+
// parseSelectors(":is(h1 h2)", 1, 0, 0);
195+
// parseSelectors(":is h1, h2", 1, 0, 0);
196+
parseSelectors("is(h1, h2)", 1, 0, 0);
197+
parseSelectors(":is((h1, h2))", 1, 0, 0);
198+
parseSelectors(":is[h1, h2]", 1, 0, 0);
199+
200+
parseSelectors(":is(h1, h2", 1, 0, 0);
201+
parseSelectors(":is h1, h2)", 1, 0, 0);
202+
}
203+
204+
/**
205+
* @throws Exception if any error occurs
206+
*/
207+
@Test
208+
public void pseudoElementsInside() throws Exception {
209+
parseSelectors(":is(p::before, p::after)", 0, 0, 0);
210+
parseSelectors(":is(::first-line, ::first-letter)", 0, 0, 0);
211+
parseSelectors(":is(div::placeholder, input::placeholder)", 0, 0, 0);
212+
}
213+
214+
/**
215+
* @throws Exception if any error occurs
216+
*/
217+
@Test
218+
public void invalidSelectorsWithin() throws Exception {
219+
parseSelectors(":is(123, h2)", 1, 0, 0);
220+
//parseSelectors(":is(.class--, h2)", 1, 0, 0);
221+
parseSelectors(":is(#, h2)", 1, 0, 0);
222+
parseSelectors(":is([attr=], h2)", 1, 0, 0);
223+
}
224+
225+
/**
226+
* @throws Exception if any error occurs
227+
*/
228+
@Test
229+
public void caseSensitive() throws Exception {
230+
parseSelectors(":IS(h2)", 0, 0, 0);
231+
parseSelectors(":iS(h2)", 0, 0, 0);
232+
parseSelectors(":Is(h2)", 0, 0, 0);
233+
}
234+
235+
/**
236+
* @throws Exception if any error occurs
237+
*/
238+
@Test
239+
public void comment() throws Exception {
240+
parseSelectors(":is(h1 /* comment */, h2)", 0, 0, 0);
241+
parseSelectors(":is(/* comment */ h1, h2)", 0, 0, 0);
242+
}
63243
}

0 commit comments

Comments
 (0)