Skip to content

Commit

Permalink
Skipping deprecated properties; #84
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jan 27, 2022
1 parent c97f125 commit 3aef4e6
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,12 @@ private CSSDeclaration _createDeclaration (@Nonnull final CSSNode aNode)
}

final String sProperty = aNode.jjtGetChild (0).getText ();
if (sProperty == null)
{
// Syntax error with deprecated property name (see #84)
return null;
}

final CSSExpression aExpression = _createExpression (aNode.jjtGetChild (1));
boolean bImportant = false;
if (nChildCount == 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public static CSSParseError createUnexpectedRule (@Nonnull final Token aCurrentT
return new CSSParseError (LoggingCSSParseErrorHandler.createLoggingStringUnexpectedRule (aCurrentToken, sRule, sMsg));
}

@Nonnull
public static CSSParseError createDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken)
{
return new CSSParseError (LoggingCSSParseErrorHandler.createLoggingStringDeprecatedProperty (aPrefixToken, aIdentifierToken));
}

@Nonnull
public static CSSParseError createBrowserCompliantSkip (@Nullable final ParseException ex,
@Nonnull final Token aFromToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,25 @@ public void onCSSUnexpectedRule (@Nonnull final Token aCurrentToken,
@Nonnull @Nonempty final String sRule,
@Nonnull @Nonempty final String sMsg) throws ParseException
{
m_aRWLock.writeLockedBoolean ( () -> m_aErrors.add (CSSParseError.createUnexpectedRule (aCurrentToken, sRule, sMsg)));
m_aRWLock.writeLocked ( () -> m_aErrors.add (CSSParseError.createUnexpectedRule (aCurrentToken, sRule, sMsg)));
}

public void onCSSDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken)
{
m_aRWLock.writeLocked ( () -> m_aErrors.add (CSSParseError.createDeprecatedProperty (aPrefixToken, aIdentifierToken)));
}

public void onCSSBrowserCompliantSkip (@Nullable final ParseException ex,
@Nonnull final Token aFromToken,
@Nonnull final Token aToToken) throws ParseException
{
m_aRWLock.writeLockedBoolean ( () -> m_aErrors.add (CSSParseError.createBrowserCompliantSkip (ex, aFromToken, aToToken)));
m_aRWLock.writeLocked ( () -> m_aErrors.add (CSSParseError.createBrowserCompliantSkip (ex, aFromToken, aToToken)));
}

@Override
public void onIllegalCharacter (final char cIllegalChar)
{
m_aRWLock.writeLockedBoolean ( () -> m_aErrors.add (CSSParseError.createIllegalCharacter (cIllegalChar)));
m_aRWLock.writeLocked ( () -> m_aErrors.add (CSSParseError.createIllegalCharacter (cIllegalChar)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ public class DoNothingCSSParseErrorHandler implements ICSSParseErrorHandler
public DoNothingCSSParseErrorHandler ()
{}

public void onCSSParseError (@Nonnull final ParseException aParseEx, @Nullable final Token aLastSkippedToken) throws ParseException
public void onCSSParseError (@Nonnull final ParseException aParseEx, @Nullable final Token aLastSkippedToken)
{
/* really do nothing :) */
}

public void onCSSUnexpectedRule (@Nonnull final Token aCurrentToken,
@Nonnull @Nonempty final String sRule,
@Nonnull @Nonempty final String sMsg) throws ParseException
@Nonnull @Nonempty final String sMsg)
{
/* really do nothing :) */
}

public void onCSSBrowserCompliantSkip (@Nullable final ParseException ex,
@Nonnull final Token aFromToken,
@Nonnull final Token aToToken) throws ParseException
public void onCSSDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken)
{
/* really do nothing :) */
}

public void onCSSBrowserCompliantSkip (@Nullable final ParseException ex, @Nonnull final Token aFromToken, @Nonnull final Token aToToken)
{
/* really do nothing :) */
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ void onCSSUnexpectedRule (@Nonnull Token aCurrentToken,
@Nonnull @Nonempty String sRule,
@Nonnull @Nonempty String sMsg) throws ParseException;

/**
* To be called, if a deprecated old IE 6/7 property is found.
*
* @param aPrefixToken
* The prefix token found (like '$' or '*'). Never <code>null</code>.
* @param aIdentifierToken
* The identifier token found. Never <code>null</code>.
* @throws ParseException
* In case the error is fatal and should be propagated.
* @since 6.4.5
*/
void onCSSDeprecatedProperty (@Nonnull Token aPrefixToken, @Nonnull Token aIdentifierToken) throws ParseException;

/**
* This method is only called in browser compliant mode if a certain part of
* the CSS is skipped.
Expand All @@ -75,7 +88,7 @@ void onCSSUnexpectedRule (@Nonnull Token aCurrentToken,
* Original token that caused the error and was skipped (inclusive).
* Never <code>null</code>.
* @param aToToken
* The end token until which was skipped(exclusive). Never
* The end token until which was skipped (exclusive). Never
* <code>null</code>.
* @throws ParseException
* In case the error is fatal and should be propagated.
Expand Down Expand Up @@ -124,6 +137,12 @@ public void onCSSUnexpectedRule (@Nonnull final Token aCurrentToken,
aOther.onCSSUnexpectedRule (aCurrentToken, sRule, sMsg);
}

public void onCSSDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken) throws ParseException
{
aThis.onCSSDeprecatedProperty (aPrefixToken, aIdentifierToken);
aOther.onCSSDeprecatedProperty (aPrefixToken, aIdentifierToken);
}

public void onCSSBrowserCompliantSkip (@Nullable final ParseException ex,
@Nonnull final Token aFromToken,
@Nonnull final Token aToToken) throws ParseException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@ public void onCSSUnexpectedRule (@Nonnull final Token aCurrentToken,
LOGGER.warn (createLoggingStringUnexpectedRule (aCurrentToken, sRule, sMsg));
}

/**
* Create a common string to be used for deprecated properties. To be called,
* if a deprecated old IE 6/7 property is found.
*
* @param aPrefixToken
* The prefix token found (like '$' or '*'). Never <code>null</code>.
* @param aIdentifierToken
* The identifier token found. Never <code>null</code>.
* @throws ParseException
* In case the error is fatal and should be propagated.
* @return The concatenated string with source location, etc. May neither be
* <code>null</code> nor empty.
*/
@Nonnull
@Nonempty
public static String createLoggingStringDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken)
{
return "[" +
aPrefixToken.beginLine +
":" +
aPrefixToken.beginColumn +
"] Deprecated property name '" +
aPrefixToken.image +
aIdentifierToken.image +
"'";
}

public void onCSSDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken)
{
LOGGER.warn (createLoggingStringDeprecatedProperty (aPrefixToken, aIdentifierToken));
}

@Nonnull
@Nonempty
public static String createLoggingStringBrowserCompliantSkip (@Nullable final ParseException ex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public void onCSSUnexpectedRule (@Nonnull final Token aCurrentToken,
throw new ParseException (LoggingCSSParseErrorHandler.createLoggingStringUnexpectedRule (aCurrentToken, sRule, sMsg));
}

public void onCSSDeprecatedProperty (@Nonnull final Token aPrefixToken, @Nonnull final Token aIdentifierToken) throws ParseException
{
throw new ParseException (LoggingCSSParseErrorHandler.createLoggingStringDeprecatedProperty (aPrefixToken, aIdentifierToken));
}

public void onCSSBrowserCompliantSkip (@Nullable final ParseException ex,
@Nonnull final Token aFromToken,
@Nonnull final Token aToToken) throws ParseException
Expand Down
64 changes: 44 additions & 20 deletions ph-css/src/main/jjtree/ParserCSS30.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ options {
// JavaCC options
// LOOKAHEAD = 2;
CHOICE_AMBIGUITY_CHECK = 5;
// DEBUG_PARSER = true;
// DEBUG_LOOKAHEAD = true;
// DEBUG_TOKEN_MANAGER = true;
// DEBUG_PARSER = true; DEBUG_LOOKAHEAD = true; DEBUG_TOKEN_MANAGER = true;
// ERROR_REPORTING = false;
JAVA_UNICODE_ESCAPE = true;
UNICODE_INPUT = true;
Expand Down Expand Up @@ -346,9 +344,24 @@ SPECIAL_TOKEN :
}

JAVACODE
private void errorDeprecated(final Token tok, final int... aKinds) throws ParseException #void {
int expectedSeq[][] = { aKinds };
throw new ParseException(tok, expectedSeq, tokenImage);
private void errorDeprecatedProperty(final Token aPrefixToken) throws ParseException #void
{
if (m_bBrowserCompliantMode)
{
if (m_aCustomErrorHandler != null)
m_aCustomErrorHandler.onCSSDeprecatedProperty (aPrefixToken, token);
else
LOGGER.warn("[" + aPrefixToken.beginLine + ":" + aPrefixToken.beginColumn + "] Deprecated property name '" + aPrefixToken.image + token.image + "'");
}
else
{
final ParseException ex = new ParseException (aPrefixToken, new int[][] { new int[] { IDENT } },
tokenImage, token_source == null ? null : ParserCSS30TokenManager.lexStateNames[token_source.curLexState]);
// javaSkipToClosingBraceOrSemicolon (1);
// LOGGER.warn ("Skipped now '"+s+"'");
// if (m_aCustomErrorHandler != null) m_aCustomErrorHandler.onCSSParseError (ex, token);
throw ex;
}
}

JAVACODE
Expand Down Expand Up @@ -510,7 +523,7 @@ JAVACODE
* @param ex The source ParseException. May not be <code>null</code>
* @throws ParseEOFException If EOF occurs while skipping
*/
private void browserCompliantSkip(final ParseException ex) throws ParseEOFException #void {
private void browserCompliantSkipInRule(final ParseException ex) throws ParseEOFException #void {
javaSkipToClosingBrace (1);
// push back last token (char count!!)
token_source.backup(1);
Expand All @@ -524,7 +537,7 @@ JAVACODE
* @param ex The source ParseException. May not be <code>null</code>
* @throws ParseEOFException If EOF occurs while skipping
*/
private void browserCompliantSkipSelector(final ParseException ex) throws ParseEOFException #void {
private void browserCompliantSkipInSelector(final ParseException ex) throws ParseEOFException #void {
javaSkipToClosingBrace (0);
// push back last token (char count!!)
token_source.backup(1);
Expand Down Expand Up @@ -1134,15 +1147,17 @@ void selector() : {}

void property() :
{
Token save;
Token aPrefixToken;
}
{
( <IDENT> { jjtThis.setText (token.image); }
( <S> )*
)
| LOOKAHEAD(2, (<ASTERISK> | <DOLLAR>) <IDENT>)
{ save = token; }
(<ASTERISK> | <DOLLAR>) { errorDeprecated(save, IDENT); }
| LOOKAHEAD(2, ( <ASTERISK> | <DOLLAR> ) <IDENT> )
( <ASTERISK> | <DOLLAR> ) { aPrefixToken = token; }
<IDENT> { errorDeprecatedProperty (aPrefixToken); }
/* leaving jjtThis.text null is handled inside the code */
( <S> )*
)
}

void important() : {}
Expand Down Expand Up @@ -1190,25 +1205,34 @@ void styleDeclarationOrRule() #void : {}

CSSNode styleDeclarationList() : {}
{
try {
( <S> )*
( styleDeclarationOrRule() )?
( <SEMICOLON>
( <S> )*
( styleDeclarationOrRule() )?
)*
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkipDecl (ex);
else {
errorSkipTo (ex, RBRACE);
token_source.backup(1);
}
}
{ return jjtThis; }
}

void styleDeclarationBlock() #void : {}
{
<LBRACE>
try{
try {
styleDeclarationList()
<RBRACE>
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkipDecl (ex);
else
else
errorSkipTo (ex, RBRACE);
}
}
Expand All @@ -1226,7 +1250,7 @@ try{
styleDeclarationBlock()
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkipSelector (ex);
browserCompliantSkipInSelector (ex);
else
throw ex;
}
Expand Down Expand Up @@ -1327,7 +1351,7 @@ try{
<RBRACE>
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkip (ex);
browserCompliantSkipInRule (ex);
else
errorSkipTo (ex, RBRACE);
}
Expand Down Expand Up @@ -1412,7 +1436,7 @@ try{
<RBRACE>
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkip (ex);
browserCompliantSkipInRule (ex);
else
errorSkipTo (ex, RBRACE);
}
Expand Down Expand Up @@ -1488,7 +1512,7 @@ try{
<RBRACE>
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkip (ex);
browserCompliantSkipInRule (ex);
else
errorSkipTo (ex, RBRACE);
}
Expand Down Expand Up @@ -1575,7 +1599,7 @@ try{
<RBRACE>
} catch (/*final*/ ParseException ex) {
if (m_bBrowserCompliantMode)
browserCompliantSkip (ex);
browserCompliantSkipInRule (ex);
else
errorSkipTo (ex, RBRACE);
}
Expand Down
Loading

0 comments on commit 3aef4e6

Please sign in to comment.