Skip to content

Commit

Permalink
Grammar changes for #101
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Sep 22, 2024
1 parent 4e0ba1f commit 31e9390
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private CSSImportRule _createImportRule (@Nonnull final CSSNode aNode)
}
else
if (!ECSSNodeType.MEDIALIST.isNode (aURINode, m_eVersion))
throw new IllegalStateException ("Expected an URI or MEDIALIST node but got " + ECSSNodeType.getNodeName (aURINode, m_eVersion));
throw new IllegalStateException ("Expected an URI or MEDIALIST node but got " +
ECSSNodeType.getNodeName (aURINode, m_eVersion));
}

if (aImportURI == null)
Expand Down Expand Up @@ -177,7 +178,11 @@ private CSSSelectorAttribute _createSelectorAttribute (@Nonnull final CSSNode aN
{
final int nExpectedChildCount = nOperatorIndex + 2;
if (nChildren != nExpectedChildCount)
_throwUnexpectedChildrenCount (aNode, "Illegal number of children present (" + nChildren + ") - expected " + nExpectedChildCount);
_throwUnexpectedChildrenCount (aNode,
"Illegal number of children present (" +
nChildren +
") - expected " +
nExpectedChildCount);

// With operator...
final CSSNode aOperator = aNode.jjtGetChild (nOperatorIndex);
Expand Down Expand Up @@ -267,7 +272,9 @@ private ICSSSelectorMember _createSelectorMember (final CSSNode aNode)
if (ECSSNodeType.NTH.isNode (aChildNode, m_eVersion))
{
// Handle nth. E.g. ":nth-child(even)" or ":nth-child(3n+1)"
final CSSSelectorSimpleMember ret = new CSSSelectorSimpleMember (aNode.getText () + aChildNode.getText () + ")");
final CSSSelectorSimpleMember ret = new CSSSelectorSimpleMember (aNode.getText () +
aChildNode.getText () +
")");
if (m_bUseSourceLocation)
ret.setSourceLocation (aNode.getSourceLocation ());
return ret;
Expand Down Expand Up @@ -323,7 +330,8 @@ private ICSSSelectorMember _createSelectorMember (final CSSNode aNode)
aNode.toString ());
}

m_aErrorHandler.onCSSInterpretationError ("Unsupported selector child: " + ECSSNodeType.getNodeName (aNode, m_eVersion));
m_aErrorHandler.onCSSInterpretationError ("Unsupported selector child: " +
ECSSNodeType.getNodeName (aNode, m_eVersion));
return null;
}

Expand Down Expand Up @@ -381,7 +389,8 @@ private CSSExpressionMemberMathProduct _createExpressionCalcProduct (@Nonnull fi
{
// Must be even child count
if ((nChildCount % 2) == 0)
_throwUnexpectedChildrenCount (aChildNode, "CSS math unit expected odd child count and got " + nChildCount);
_throwUnexpectedChildrenCount (aChildNode,
"CSS math unit expected odd child count and got " + nChildCount);

final CSSExpressionMemberMathProduct aNestedProduct = new CSSExpressionMemberMathProduct ();
for (int i = 0; i < nChildCount; ++i)
Expand Down Expand Up @@ -588,7 +597,8 @@ private ICSSExpressionMember _createExpressionTerm (@Nonnull final CSSNode aNode
return _createExpressionLineNamesTerm (aChildNode);
}
else
throw new IllegalStateException ("Expected an expression term but got " + ECSSNodeType.getNodeName (aChildNode, m_eVersion));
throw new IllegalStateException ("Expected an expression term but got " +
ECSSNodeType.getNodeName (aChildNode, m_eVersion));
}

@Nonnull
Expand Down Expand Up @@ -671,7 +681,8 @@ private CSSDeclaration _createDeclaration (@Nonnull final CSSNode aNode)
return ret;
}

private void _readStyleDeclarationList (@Nonnull final CSSNode aNode, @Nonnull final Consumer <CSSDeclaration> aConsumer)
private void _readStyleDeclarationList (@Nonnull final CSSNode aNode,
@Nonnull final Consumer <CSSDeclaration> aConsumer)
{
_expectNodeType (aNode, ECSSNodeType.STYLEDECLARATIONLIST);
// Read all contained declarations
Expand Down Expand Up @@ -823,7 +834,8 @@ private CSSPageRule _createPageRule (@Nonnull final CSSNode aNode)
}
else
if (!ECSSNodeType.isErrorNode (aChildNode, m_eVersion))
m_aErrorHandler.onCSSInterpretationError ("Unsupported page rule child: " + ECSSNodeType.getNodeName (aChildNode, m_eVersion));
m_aErrorHandler.onCSSInterpretationError ("Unsupported page rule child: " +
ECSSNodeType.getNodeName (aChildNode, m_eVersion));
}
return ret;
}
Expand Down Expand Up @@ -963,7 +975,8 @@ private CSSMediaQuery _createMediaQuery (@Nonnull final CSSNode aNode)
ret.addMediaExpression (_createMediaExpr (aChildNode));
else
if (!ECSSNodeType.isErrorNode (aChildNode, m_eVersion))
m_aErrorHandler.onCSSInterpretationError ("Unsupported media query child: " + ECSSNodeType.getNodeName (aChildNode, m_eVersion));
m_aErrorHandler.onCSSInterpretationError ("Unsupported media query child: " +
ECSSNodeType.getNodeName (aChildNode, m_eVersion));
}
return ret;
}
Expand All @@ -978,7 +991,8 @@ private CSSMediaExpression _createMediaExpr (@Nonnull final CSSNode aNode)

final CSSNode aFeatureNode = aNode.jjtGetChild (0);
if (!ECSSNodeType.MEDIAFEATURE.isNode (aFeatureNode, m_eVersion))
throw new IllegalStateException ("Expected a media feature but got " + ECSSNodeType.getNodeName (aFeatureNode, m_eVersion));
throw new IllegalStateException ("Expected a media feature but got " +
ECSSNodeType.getNodeName (aFeatureNode, m_eVersion));
final String sFeature = aFeatureNode.getText ();
if (ECSSMediaExpressionFeature.getFromNameOrNull (sFeature) == null)
m_aErrorHandler.onCSSInterpretationWarning ("Media expression uses unknown feature '" + sFeature + "'");
Expand Down Expand Up @@ -1118,7 +1132,8 @@ private CSSNamespaceRule _createNamespaceRule (@Nonnull final CSSNode aNode)
_expectNodeType (aNode, ECSSNodeType.NAMESPACERULE);
final int nChildCount = aNode.jjtGetNumChildren ();
if (nChildCount < 1 || nChildCount > 2)
_throwUnexpectedChildrenCount (aNode, "Expected at least 1 child and at last 2 children but got " + nChildCount + "!");
_throwUnexpectedChildrenCount (aNode,
"Expected at least 1 child and at last 2 children but got " + nChildCount + "!");

String sPrefix = null;
int nURLIndex = 0;
Expand Down Expand Up @@ -1204,7 +1219,8 @@ private ICSSSupportsConditionMember _createSupportsConditionMemberRecursive (@No
}

if (!ECSSNodeType.isErrorNode (aNode, m_eVersion))
m_aErrorHandler.onCSSInterpretationError ("Unsupported supports-condition child: " + ECSSNodeType.getNodeName (aNode, m_eVersion));
m_aErrorHandler.onCSSInterpretationError ("Unsupported supports-condition child: " +
ECSSNodeType.getNodeName (aNode, m_eVersion));

return null;
}
Expand Down Expand Up @@ -1286,7 +1302,8 @@ private CSSUnknownRule _createUnknownRule (@Nonnull final CSSNode aNode)
return ret;
}

private void _recursiveFillCascadingStyleSheetFromNode (@Nonnull final CSSNode aNode, @Nonnull final CascadingStyleSheet ret)
private void _recursiveFillCascadingStyleSheetFromNode (@Nonnull final CSSNode aNode,
@Nonnull final CascadingStyleSheet ret)
{
_expectNodeType (aNode, ECSSNodeType.ROOT);
if (m_bUseSourceLocation)
Expand Down Expand Up @@ -1354,7 +1371,8 @@ private void _recursiveFillCascadingStyleSheetFromNode (@Nonnull final CSSNode a
m_aErrorHandler.onCSSInterpretationError ("Unsupported child of " +
ECSSNodeType.getNodeName (aNode, m_eVersion) +
": " +
ECSSNodeType.getNodeName (aChildNode, m_eVersion));
ECSSNodeType.getNodeName (aChildNode,
m_eVersion));
}
}

Expand Down
8 changes: 4 additions & 4 deletions ph-css/src/main/java/com/helger/css/handler/ECSSNodeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public enum ECSSNodeType
// style rule -- selector
NAMESPACEPREFIX (ParserCSS30TreeConstants.JJTNAMESPACEPREFIX),
ELEMENTNAME (ParserCSS30TreeConstants.JJTELEMENTNAME),
HASH (ParserCSS30TreeConstants.JJTHASH),
HASH (ParserCSS30TreeConstants.JJTIDSELECTOR),
CLASS (ParserCSS30TreeConstants.JJTCLASS),
PSEUDO (ParserCSS30TreeConstants.JJTPSEUDO),
PSEUDO (ParserCSS30TreeConstants.JJTPSEUDOCLASSSELECTOR),
HOST (ParserCSS30TreeConstants.JJTHOST),
HOSTCONTEXT (ParserCSS30TreeConstants.JJTHOSTCONTEXT),
SLOTTED (ParserCSS30TreeConstants.JJTSLOTTED),
NEGATION (ParserCSS30TreeConstants.JJTNEGATION),
ATTRIB (ParserCSS30TreeConstants.JJTATTRIB),
NEGATION (ParserCSS30TreeConstants.JJTFUNCNOT),
ATTRIB (ParserCSS30TreeConstants.JJTATTRIBUTESELECTOR),
ATTRIBOPERATOR (ParserCSS30TreeConstants.JJTATTRIBOPERATOR),
ATTRIBVALUE (ParserCSS30TreeConstants.JJTATTRIBVALUE),
SELECTORCOMBINATOR (ParserCSS30TreeConstants.JJTSELECTORCOMBINATOR),
Expand Down
101 changes: 83 additions & 18 deletions ph-css/src/main/jjtree/ParserCSS30.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ TOKEN :
| < FUNCTION_CALC: "calc("
| "-" <IDENT> "-calc(" >
| < FUNCTION_NOT: ":not(" >
| < FUNCTION_IS: "is(" >
| < FUNCTION_HAS: "has(" >
| < FUNCTION_HOST: "host(" >
| < FUNCTION_WHERE: "where(" >
| < FUNCTION_HOSTCONTEXT: "host-context(" >
| < FUNCTION_SLOTTED: "slotted(" >
| < FUNCTION_NTH: "nth-child("
Expand Down Expand Up @@ -961,12 +964,12 @@ void typeSelector() #void : {}
elementName()
}

void hash() : {}
void idSelector() : {}
{
<HASH> { jjtThis.setText (token.image); }
}

void _class() #Class :
void classSelector() #Class :
{ String sIdentifier; }
{
<DOT>
Expand All @@ -992,7 +995,7 @@ void attribValue() :
)
}

void attrib() : {}
void attributeSelector() : {}
{
<LSQUARE>
( <S> )*
Expand Down Expand Up @@ -1058,11 +1061,52 @@ void pseudoSlotted () #slotted : {}
)*
}

void pseudo() : {}
void relativeSelector() : {}
{
( selectorCombinator()
( <S> )*
)?
selector ()
( <S> )*
( <COMMA>
( <S> )*
selector()
( <S> )*
)*
}

void relativeSelectorList() #void : {}
{
( relativeSelector() )*
}

void pseudoHas() #has : {}
{
( <S> )*
relativeSelectorList()
}

void pseudoIs() #is : {}
{
( <S> )*
selector()
}

void pseudoWhere() #where : {}
{
( <S> )*
selector()
}

void pseudoElementSelector() : {}
{
<COLON> { jjtThis.setText (":"); }
pseudoClassSelector()
}

void pseudoClassSelector() : {}
{
<COLON> { jjtThis.setText (":"); }
/* Extension for "::-moz-selection */
( <COLON> { jjtThis.appendText (":"); } )?
( <FUNCTION_NTH> { jjtThis.appendText (token.image); }
pseudoNth()
<RROUND> // do not append because of expression!
Expand All @@ -1075,6 +1119,15 @@ void pseudo() : {}
| <FUNCTION_SLOTTED> { jjtThis.appendText (token.image); }
pseudoSlotted()
<RROUND> // do not append because of expression!
| <FUNCTION_HAS> { jjtThis.setText (token.image); }
pseudoHas()
<RROUND> // do not append because of expression!
| <FUNCTION_IS> { jjtThis.setText (token.image); }
pseudoIs()
<RROUND> // do not append because of expression!
| <FUNCTION_WHERE> { jjtThis.setText (token.image); }
pseudoWhere()
<RROUND> // do not append because of expression!
| LOOKAHEAD( <FUNCTION> )
<FUNCTION> { jjtThis.appendText (token.image); }
( <S> )*
Expand All @@ -1084,7 +1137,7 @@ void pseudo() : {}
)
}

void negation() : {}
void funcNot() : {}
{
<FUNCTION_NOT> { jjtThis.setText (":not("); }
( <S> )*
Expand All @@ -1103,19 +1156,31 @@ void simpleSelectorSequence() #void : {}
{
LOOKAHEAD(2)
( typeSelector()
( hash()
| _class()
| attrib()
| pseudo()
| negation()
( idSelector()
| classSelector()
| attributeSelector()
| pseudoClassSelector()
| funcNot()
)*
// ( LOOKAHEAD(2)
// pseudoElementSelector()
// ( pseudoClassSelector() )*
// )*
)
| ( hash()
| _class()
| attrib()
| pseudo()
| negation()
| ( idSelector()
| classSelector()
| attributeSelector()
| pseudoClassSelector()
| funcNot()
)+
// ( LOOKAHEAD(2)
// pseudoElementSelector()
// ( pseudoClassSelector() )*
// )*
// | LOOKAHEAD(2)
// ( pseudoElementSelector()
// ( pseudoClassSelector() )*
// )+
// Extension for CSS animations (e.g. 50%)
| <PERCENTAGE>
}
Expand Down Expand Up @@ -1157,7 +1222,7 @@ void property() :
<IDENT> { errorDeprecatedProperty (aPrefixToken); }
/* leaving jjtThis.text null is handled inside the code */
( <S> )*
)
)
}

void important() : {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ public void testReadSpecialGood ()
assertNotNull (aCSS);

assertEquals ("--a", aCSS.getStyleRuleAtIndex (0).getDeclarationAtIndex (0).getProperty ());
assertEquals ("background-color:var(--A)", aCSS.getStyleRuleAtIndex (0).getDeclarationAtIndex (1).getAsCSSString ());
assertEquals ("background-color:var(--A)",
aCSS.getStyleRuleAtIndex (0).getDeclarationAtIndex (1).getAsCSSString ());

assertEquals ("--A", aCSS.getStyleRuleAtIndex (1).getDeclarationAtIndex (0).getProperty ());
assertEquals ("background-color:var(--A)", aCSS.getStyleRuleAtIndex (1).getDeclarationAtIndex (1).getAsCSSString ());
assertEquals ("background-color:var(--A)",
aCSS.getStyleRuleAtIndex (1).getDeclarationAtIndex (1).getAsCSSString ());

final String sCSS = new CSSWriter (eVersion, false).getCSSAsString (aCSS);
assertNotNull (sCSS);
Expand Down Expand Up @@ -306,7 +308,10 @@ public void testReadSpecialBadButRecoverable ()
final ECSSVersion eVersion = ECSSVersion.CSS30;
final Charset aCharset = StandardCharsets.UTF_8;
final File aFile = new File ("src/test/resources/testfiles/css30/bad_but_recoverable_and_browsercompliant/test-string.css");
final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, aCharset, eVersion, aErrors.and (new LoggingCSSParseErrorHandler ()));
final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile,
aCharset,
eVersion,
aErrors.and (new LoggingCSSParseErrorHandler ()));
assertNotNull (aFile.getAbsolutePath (), aCSS);
}

Expand All @@ -325,7 +330,8 @@ public void testReadWithBOM ()
ECSSVersion.CSS30,
new DoNothingCSSParseErrorHandler ());
assertNotNull ("Failed to read with BOM " + eBOM, aCSS);
assertEquals (".class{color:red}.class{color:blue}", new CSSWriter (ECSSVersion.CSS30, true).getCSSAsString (aCSS));
assertEquals (".class{color:red}.class{color:blue}",
new CSSWriter (ECSSVersion.CSS30, true).getCSSAsString (aCSS));
}
}
}
Expand Down Expand Up @@ -376,4 +382,16 @@ public void testReadFootnote ()
assertEquals ("@footnote", footnoteBlock.getPageMarginSymbol ());
assertEquals (1, footnoteBlock.getDeclarationCount ());
}

@Test
public void testIssue101 ()
{
final ECSSVersion eVersion = ECSSVersion.CSS30;
final Charset aCharset = StandardCharsets.UTF_8;
final File aFile = new File ("src/test/resources/testfiles/css30/good/issue101.css");
final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, aCharset, eVersion);
assertNotNull (aCSS);
assertEquals (2, aCSS.getRuleCount ());
assertEquals (2, aCSS.getStyleRuleCount ());
}
}
Loading

0 comments on commit 31e9390

Please sign in to comment.