Skip to content

Commit

Permalink
fix: parse CSS custom properties
Browse files Browse the repository at this point in the history
This change only fixes the `CssScanner` to parse CSS custom properties
as IDENT tokens.

Additional work may be needed to actually make the built-in CSS Grammar
aware of custom properties, but at this time this is not needed to
prevent EPUBCheck from reporting custom properties as errors.

Fix #790
  • Loading branch information
rdeltour committed Mar 13, 2019
1 parent cab45e6 commit 0036e93
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/main/java/org/idpf/epubcheck/util/css/CssScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ && matchesOrEOF(reader.at(2), URANGESTART))
{
_urange();
}
else if (NMSTART.matches(cur) || cur == '-'
&& matches(next, NMSTART) || escapes.get(0).isPresent()
|| (cur == '-' && escapes.get(1).isPresent()))
else if (NMSTART.matches(cur)
|| cur == '-' &&
(matches(next, NMSTART) || reader.peek() == '-' || escapes.get(1).isPresent())
|| escapes.get(0).isPresent())
{
_ident();
if (reader.peek() == '(')
Expand Down Expand Up @@ -406,7 +407,7 @@ private void _atkeyword() throws
}

/**
* IDENT [-]?{nmstart}{nmchar}*
* IDENT ([-]?{nmstart}|[--]){nmchar}*
*/
private void _ident() throws
IOException,
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/idpf/epubcheck/util/css/CssParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ public void testParser014() throws Exception {
assertTrue(((CssQuantity)cc).subType != CssQuantity.Unit.LENGTH); // '1m' is invalid length
}
}

@Test
public void testParser015() throws Exception {
String s = "E { --my-property: 16px; } ";
HandlerImpl handler = checkBasics(exec(s));
assertEquals(0, handler.errors.size());
}

@Test
public void testParserAtRule001() throws Exception {
Expand Down Expand Up @@ -884,6 +891,11 @@ public void testParserFile004() throws Exception {
checkBasics(execFile("counter-styles.css", false));
}

@Test
public void testParserFile005() throws Exception {
checkBasics(execFile("custom-properties.css", false));
}

@Test
public void testGrammarToCssString001() throws Exception {
String s = "E {color:black}";
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/idpf/epubcheck/util/css/CssScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,17 @@ public void testLexerIdent_23() throws Exception {
assertEquals("-a", tokens.get(0).getChars());
}

@Test
public void testLexerIdent_30() throws Exception {
String s = "--a";

List<CssToken> tokens = execScan(s);
assertEquals(1, tokens.size());
assertEquals(0, exceptions.size());
assertEquals(CssToken.Type.IDENT, tokens.get(0).getType());
assertEquals("--a", tokens.get(0).getChars());
}

@Test
public void testLexerAtKeyword_10() throws Exception {
String s = "@ident";
Expand Down
37 changes: 37 additions & 0 deletions src/test/resources/css/custom-properties.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*Examples taken from MDN "Using custom properties"*/

/* Basics */

:root {
--main-bg-color: brown;
}

.one {
color: white;
background-color: var(--main-bg-color);
margin: 10px;
width: 50px;
height: 50px;
display: inline-block;
}


/* Fallbacks */

.two {
color: var(--my-var, red); /* Red if --my-var is not defined */
}

.three {
background-color: var(--my-var, var(--my-background, pink)); /* pink if my-var and --my-background are not defined */
}

.three {
background-color: var(--my-var, --my-background, pink); /* Invalid: "--my-background, pink" */
}

/* Invalid variable */

:root { --text-color: 16px; }
p { color: blue; }
p { color: var(--text-color); }

0 comments on commit 0036e93

Please sign in to comment.