Skip to content

Commit 2000403

Browse files
committed
Pin parser behaviour for !important, value lists, and at-rules
Extend StyleSheetStructureTest with value- and at-rule-level cases that lock in the parser output the CSS engine rework's replacement parser must reproduce: !important priority, optional trailing and stray semicolons, multi-value lists, length and percentage units, unquoted @import URLs, and @media / @font-face tolerance.
1 parent bacf493 commit 2000403

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/parser/StyleSheetStructureTest.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.w3c.dom.css.CSSStyleRule;
3737
import org.w3c.dom.css.CSSStyleSheet;
3838
import org.w3c.dom.css.CSSValue;
39+
import org.w3c.dom.css.CSSValueList;
3940
import org.w3c.dom.css.RGBColor;
4041

4142
/**
@@ -247,6 +248,121 @@ void testInvalidInputErrorReported() throws Exception {
247248
"expected the parser to throw or invoke the error handler for invalid input");
248249
}
249250

251+
@Test
252+
void testImportantPriorityPreserved() throws Exception {
253+
CSSStyleSheet sheet = ParserTestUtil.parseCss("Button { color: red !important; }");
254+
255+
CSSStyleDeclaration style = ((CSSStyleRule) sheet.getCssRules().item(0)).getStyle();
256+
assertEquals("red", style.getPropertyCSSValue("color").getCssText());
257+
assertEquals("important", style.getPropertyPriority("color"));
258+
}
259+
260+
@Test
261+
void testImportantOnlyMarksItsOwnDeclaration() throws Exception {
262+
CSSStyleSheet sheet = ParserTestUtil
263+
.parseCss("Button { color: red !important; background-color: blue; }");
264+
265+
CSSStyleDeclaration style = ((CSSStyleRule) sheet.getCssRules().item(0)).getStyle();
266+
assertEquals("important", style.getPropertyPriority("color"));
267+
assertEquals("", style.getPropertyPriority("background-color"));
268+
}
269+
270+
@Test
271+
void testTrailingSemicolonOptional() throws Exception {
272+
CSSStyleSheet sheet = ParserTestUtil.parseCss("Button { color: red }");
273+
274+
CSSStyleDeclaration style = ((CSSStyleRule) sheet.getCssRules().item(0)).getStyle();
275+
assertEquals(1, style.getLength());
276+
assertEquals("red", style.getPropertyCSSValue("color").getCssText());
277+
assertEquals("", style.getPropertyPriority("color"));
278+
}
279+
280+
@Test
281+
void testStraySemicolonsTolerated() throws Exception {
282+
CSSStyleSheet sheet = ParserTestUtil.parseCss("Button { ; color: red;; }");
283+
284+
CSSStyleDeclaration style = ((CSSStyleRule) sheet.getCssRules().item(0)).getStyle();
285+
assertEquals(1, style.getLength());
286+
assertEquals("red", style.getPropertyCSSValue("color").getCssText());
287+
}
288+
289+
@Test
290+
void testMultiValuePropertyIsValueList() throws Exception {
291+
CSSStyleSheet sheet = ParserTestUtil.parseCss("Button { margin: 1px 2px 3px 4px; }");
292+
293+
CSSValue value = ((CSSStyleRule) sheet.getCssRules().item(0)).getStyle().getPropertyCSSValue("margin");
294+
assertEquals(CSSValue.CSS_VALUE_LIST, value.getCssValueType());
295+
296+
CSSValueList list = (CSSValueList) value;
297+
assertEquals(4, list.getLength());
298+
for (int i = 0; i < list.getLength(); i++) {
299+
CSSPrimitiveValue item = (CSSPrimitiveValue) list.item(i);
300+
assertEquals(CSSPrimitiveValue.CSS_PX, item.getPrimitiveType());
301+
assertEquals(i + 1.0f, item.getFloatValue(CSSPrimitiveValue.CSS_PX));
302+
}
303+
}
304+
305+
@Test
306+
void testLengthAndPercentageUnits() throws Exception {
307+
CSSStyleSheet sheet = ParserTestUtil.parseCss("""
308+
A { width: 10px; }
309+
B { width: 50%; }
310+
""");
311+
312+
CSSPrimitiveValue px = (CSSPrimitiveValue) ((CSSStyleRule) sheet.getCssRules().item(0)).getStyle()
313+
.getPropertyCSSValue("width");
314+
assertEquals(CSSPrimitiveValue.CSS_PX, px.getPrimitiveType());
315+
assertEquals(10.0f, px.getFloatValue(CSSPrimitiveValue.CSS_PX));
316+
317+
CSSPrimitiveValue percent = (CSSPrimitiveValue) ((CSSStyleRule) sheet.getCssRules().item(1)).getStyle()
318+
.getPropertyCSSValue("width");
319+
assertEquals(CSSPrimitiveValue.CSS_PERCENTAGE, percent.getPrimitiveType());
320+
assertEquals(50.0f, percent.getFloatValue(CSSPrimitiveValue.CSS_PERCENTAGE));
321+
}
322+
323+
@Test
324+
void testUnquotedImportUrlHref() throws Exception {
325+
CSSStyleSheet sheet = ParserTestUtil.parseCssWithoutImports("@import url(other.css);");
326+
327+
CSSImportRule rule = (CSSImportRule) sheet.getCssRules().item(0);
328+
assertEquals("other.css", rule.getHref());
329+
}
330+
331+
@Test
332+
void testFontFaceRuleDiscarded() throws Exception {
333+
CSSStyleSheet sheet = ParserTestUtil.parseCss("""
334+
@font-face { font-family: x; }
335+
Label { color: blue; }
336+
""");
337+
338+
// The engine parses but does not retain @font-face; the regular rule that
339+
// follows it must still be present and intact.
340+
assertEquals(1, sheet.getCssRules().getLength());
341+
CSSStyleRule label = (CSSStyleRule) sheet.getCssRules().item(0);
342+
assertEquals("Label", label.getSelectorText());
343+
assertEquals("blue", label.getStyle().getPropertyCSSValue("color").getCssText());
344+
}
345+
346+
@Test
347+
void testMediaRuleToleratedAndFollowingRuleParsed() throws Exception {
348+
CSSStyleSheet sheet = ParserTestUtil.parseCss("""
349+
@media screen { Button { color: red; } }
350+
Label { color: blue; }
351+
""");
352+
353+
// @media is accepted without applying its block; the top-level rule after
354+
// it must remain reachable with its declarations intact.
355+
boolean labelFound = false;
356+
CSSRuleList rules = sheet.getCssRules();
357+
for (int i = 0; i < rules.getLength(); i++) {
358+
if (rules.item(i) instanceof CSSStyleRule rule && "Label".equals(rule.getSelectorText())) {
359+
assertEquals("blue", rule.getStyle().getPropertyCSSValue("color").getCssText());
360+
labelFound = true;
361+
}
362+
}
363+
assertTrue(labelFound, "expected the Label rule following the @media block to be parsed");
364+
}
365+
250366
private static void assertWhiteRgb(CSSPrimitiveValue value) {
251367
assertEquals(CSSPrimitiveValue.CSS_RGBCOLOR, value.getPrimitiveType());
252368
RGBColor rgb = value.getRGBColorValue();

0 commit comments

Comments
 (0)