Skip to content

Commit

Permalink
Added new property "setKeepDeprecatedProperties(boolean)"; #107
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Feb 10, 2025
1 parent 0d0efe1 commit aac25b6
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 31 deletions.
50 changes: 39 additions & 11 deletions ph-css/src/main/java/com/helger/css/parser/AbstractParserCSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.css.reader.CSSReaderSettings;
import com.helger.css.reader.errorhandler.ICSSParseErrorHandler;

/**
Expand All @@ -35,7 +36,18 @@ public abstract class AbstractParserCSS
private static final Logger LOGGER = LoggerFactory.getLogger (AbstractParserCSS.class);

protected ICSSParseErrorHandler m_aCustomErrorHandler;
protected boolean m_bBrowserCompliantMode = false;
protected boolean m_bBrowserCompliantMode = CSSReaderSettings.DEFAULT_BROWSER_COMPLIANT_MODE;
protected boolean m_bKeepDeprecatedProperties = CSSReaderSettings.DEFAULT_KEEP_DEPRECATED_PROPERTIES;

/**
* @return The custom error handler to be used for this parser. May be
* <code>null</code>.
*/
@Nullable
public final ICSSParseErrorHandler getCustomErrorHandler ()
{
return m_aCustomErrorHandler;
}

/**
* Set a custom error handler to use.
Expand All @@ -49,13 +61,13 @@ public final void setCustomErrorHandler (@Nullable final ICSSParseErrorHandler a
}

/**
* @return The custom error handler to be used for this parser. May be
* <code>null</code>.
* @return <code>true</code> if browser compliant mode is active,
* <code>false</code> if not. By default browser compliant mode is
* disabled.
*/
@Nullable
public final ICSSParseErrorHandler getCustomErrorHandler ()
public final boolean isBrowserCompliantMode ()
{
return m_aCustomErrorHandler;
return m_bBrowserCompliantMode;
}

/**
Expand All @@ -71,13 +83,29 @@ public final void setBrowserCompliantMode (final boolean bBrowserCompliantMode)
}

/**
* @return <code>true</code> if browser compliant mode is active,
* <code>false</code> if not. By default browser compliant mode is
* disabled.
* @return <code>true</code> if deprecated properties (e.g.
* <code>*zoom</code>) should be kept while reading,
* <code>false</code> if they should be discarded. The default is
* {@link #DEFAULT_KEEP_DEPRECATED_PROPERTIES}.
* @since 7.0.4
*/
public final boolean isBrowserCompliantMode ()
public final boolean isKeepDeprecatedProperties ()
{
return m_bBrowserCompliantMode;
return m_bKeepDeprecatedProperties;
}

/**
* Define, whether deprecated properties (e.g. <code>*zoom</code>) should be
* kept or not.
*
* @param bKeepDeprecatedProperties
* <code>true</code> to keep them, <code>false</code> to discard them
* on reading.
* @since 7.0.4
*/
public final void setKeepDeprecatedProperties (final boolean bKeepDeprecatedProperties)
{
m_bKeepDeprecatedProperties = bKeepDeprecatedProperties;
}

// Used when NODE_SCOPE_HOOK is true - for debugging only
Expand Down
65 changes: 49 additions & 16 deletions ph-css/src/main/java/com/helger/css/reader/CSSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ public static void setDefaultInterpretErrorHandler (@Nonnull final ICSSInterpret
* @param bBrowserCompliantMode
* <code>true</code> for browser compliant parsing, <code>false</code>
* for default parsing.
* @param bKeepDeprecatedProperties
* <code>true</code> to keep deprecated properties (e.g.
* <code>*zoom</code>) or <code>false</code> to ignore them.
* @return <code>null</code> if parsing failed with an unrecoverable error
* (and no throwing exception handler is used), or <code>null</code>
* if a recoverable error occurred and no
Expand All @@ -198,7 +201,8 @@ private static CSSNode _readStyleSheet (@Nonnull final CharStream aCharStream,
@Nonnull final ECSSVersion eVersion,
@Nullable final ICSSParseErrorHandler aCustomErrorHandler,
@Nonnull final ICSSParseExceptionCallback aCustomExceptionHandler,
final boolean bBrowserCompliantMode)
final boolean bBrowserCompliantMode,
final boolean bKeepDeprecatedProperties)
{
try
{
Expand All @@ -212,6 +216,7 @@ private static CSSNode _readStyleSheet (@Nonnull final CharStream aCharStream,
final ParserCSS30 aParser = new ParserCSS30 (aTokenHdl);
aParser.setCustomErrorHandler (aCustomErrorHandler);
aParser.setBrowserCompliantMode (bBrowserCompliantMode);
aParser.setKeepDeprecatedProperties (bKeepDeprecatedProperties);
// Main parsing
return aParser.styleSheet ();
}
Expand Down Expand Up @@ -248,7 +253,9 @@ private static CSSNode _readStyleSheet (@Nonnull final CharStream aCharStream,
* @return <code>true</code> if the file can be parsed without error,
* <code>false</code> if not
*/
public static boolean isValidCSS (@Nonnull final File aFile, @Nonnull final Charset aFallbackCharset, @Nonnull final ECSSVersion eVersion)
public static boolean isValidCSS (@Nonnull final File aFile,
@Nonnull final Charset aFallbackCharset,
@Nonnull final ECSSVersion eVersion)
{
return isValidCSS (new FileSystemResource (aFile), aFallbackCharset, eVersion);
}
Expand Down Expand Up @@ -356,11 +363,16 @@ public static boolean isValidCSS (@Nonnull @WillClose final Reader aReader, @Non
try
{
final CSSCharStream aCharStream = new CSSCharStream (aReader);

final boolean bBrowserCompliantMode = false;
final boolean bKeepDeprecatedProperties = false;

final CSSNode aNode = _readStyleSheet (aCharStream,
eVersion,
getDefaultParseErrorHandler (),
new DoNothingCSSParseExceptionCallback (),
false);
bBrowserCompliantMode,
bKeepDeprecatedProperties);
return aNode != null;
}
finally
Expand Down Expand Up @@ -388,7 +400,9 @@ public static CascadingStyleSheet readFromString (@Nonnull final String sCSS,
@Nonnull final Charset aFallbackCharset,
@Nonnull final ECSSVersion eVersion)
{
return readFromStringStream (sCSS, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion));
return readFromStringStream (sCSS,
new CSSReaderSettings ().setFallbackCharset (aFallbackCharset)
.setCSSVersion (eVersion));
}

/**
Expand Down Expand Up @@ -498,7 +512,8 @@ public static CascadingStyleSheet readFromString (@Nonnull final String sCSS,
* @since 3.8.2
*/
@Nullable
public static CascadingStyleSheet readFromStringStream (@Nonnull final String sCSS, @Nonnull final CSSReaderSettings aSettings)
public static CascadingStyleSheet readFromStringStream (@Nonnull final String sCSS,
@Nonnull final CSSReaderSettings aSettings)
{
return readFromStream (new StringInputStreamProvider (sCSS, aSettings.getFallbackCharset ()), aSettings);
}
Expand Down Expand Up @@ -543,7 +558,9 @@ public static CascadingStyleSheet readFromString (@Nonnull final String sCSS,
@Nonnull final ECSSVersion eVersion,
@Nullable final ICSSParseErrorHandler aCustomErrorHandler)
{
return readFromStringReader (sCSS, new CSSReaderSettings ().setCSSVersion (eVersion).setCustomErrorHandler (aCustomErrorHandler));
return readFromStringReader (sCSS,
new CSSReaderSettings ().setCSSVersion (eVersion)
.setCustomErrorHandler (aCustomErrorHandler));
}

/**
Expand All @@ -568,7 +585,8 @@ public static CascadingStyleSheet readFromString (@Nonnull final String sCSS,
@Nullable final ICSSParseExceptionCallback aCustomExceptionHandler)
{
return readFromStringReader (sCSS,
new CSSReaderSettings ().setCSSVersion (eVersion).setCustomExceptionHandler (aCustomExceptionHandler));
new CSSReaderSettings ().setCSSVersion (eVersion)
.setCustomExceptionHandler (aCustomExceptionHandler));
}

/**
Expand Down Expand Up @@ -617,7 +635,8 @@ public static CascadingStyleSheet readFromString (@Nonnull final String sCSS,
* @since 3.8.2
*/
@Nullable
public static CascadingStyleSheet readFromStringReader (@Nonnull final String sCSS, @Nonnull final CSSReaderSettings aSettings)
public static CascadingStyleSheet readFromStringReader (@Nonnull final String sCSS,
@Nonnull final CSSReaderSettings aSettings)
{
return readFromReader (new StringReaderProvider (sCSS), aSettings);
}
Expand All @@ -641,7 +660,8 @@ public static CascadingStyleSheet readFromFile (@Nonnull final File aFile,
@Nonnull final Charset aFallbackCharset,
@Nonnull final ECSSVersion eVersion)
{
return readFromFile (aFile, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion));
return readFromFile (aFile,
new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion));
}

/**
Expand Down Expand Up @@ -778,7 +798,8 @@ public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream
@Nonnull final Charset aFallbackCharset,
@Nonnull final ECSSVersion eVersion)
{
return readFromStream (aISP, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion));
return readFromStream (aISP,
new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion));
}

/**
Expand Down Expand Up @@ -987,7 +1008,8 @@ public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream
* @since 3.8.2
*/
@Nullable
public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream aISP, @Nonnull final CSSReaderSettings aSettings)
public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream aISP,
@Nonnull final CSSReaderSettings aSettings)
{
ValueEnforcer.notNull (aISP, "InputStreamProvider");
ValueEnforcer.notNull (aSettings, "Settings");
Expand Down Expand Up @@ -1045,12 +1067,14 @@ public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream
aRealParseExceptionHandler = getDefaultParseExceptionHandler ();

final boolean bBrowserCompliantMode = aSettings.isBrowserCompliantMode ();
final boolean bKeepDeprecatedProperties = aSettings.isKeepDeprecatedProperties ();

final CSSNode aNode = _readStyleSheet (aCharStream,
eVersion,
aRealParseErrorHandler,
aRealParseExceptionHandler,
bBrowserCompliantMode);
bBrowserCompliantMode,
bKeepDeprecatedProperties);

// Failed to parse content as CSS?
if (aNode == null)
Expand All @@ -1064,7 +1088,10 @@ public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream
final boolean bUseSourceLocation = aSettings.isUseSourceLocation ();

// Convert the AST to a domain object
return CSSHandler.readCascadingStyleSheetFromNode (eVersion, aRealInterpretErrorHandler, bUseSourceLocation, aNode);
return CSSHandler.readCascadingStyleSheetFromNode (eVersion,
aRealInterpretErrorHandler,
bUseSourceLocation,
aNode);
}
finally
{
Expand Down Expand Up @@ -1121,7 +1148,8 @@ public static CascadingStyleSheet readFromReader (@Nonnull final IHasReader aRP,
* @since 3.8.2
*/
@Nullable
public static CascadingStyleSheet readFromReader (@Nonnull final IHasReader aRP, @Nonnull final CSSReaderSettings aSettings)
public static CascadingStyleSheet readFromReader (@Nonnull final IHasReader aRP,
@Nonnull final CSSReaderSettings aSettings)
{
ValueEnforcer.notNull (aRP, "ReaderProvider");
ValueEnforcer.notNull (aSettings, "Settings");
Expand Down Expand Up @@ -1153,12 +1181,14 @@ public static CascadingStyleSheet readFromReader (@Nonnull final IHasReader aRP,
aRealParseExceptionHandler = getDefaultParseExceptionHandler ();

final boolean bBrowserCompliantMode = aSettings.isBrowserCompliantMode ();
final boolean bKeepDeprecatedProperties = aSettings.isKeepDeprecatedProperties ();

final CSSNode aNode = _readStyleSheet (aCharStream,
eVersion,
aRealParseErrorHandler,
aRealParseExceptionHandler,
bBrowserCompliantMode);
bBrowserCompliantMode,
bKeepDeprecatedProperties);

// Failed to parse content as CSS?
if (aNode == null)
Expand All @@ -1172,7 +1202,10 @@ public static CascadingStyleSheet readFromReader (@Nonnull final IHasReader aRP,
final boolean bUseSourceLocation = aSettings.isUseSourceLocation ();

// Convert the AST to a domain object
return CSSHandler.readCascadingStyleSheetFromNode (eVersion, aRealInterpretErrorHandler, bUseSourceLocation, aNode);
return CSSHandler.readCascadingStyleSheetFromNode (eVersion,
aRealInterpretErrorHandler,
bUseSourceLocation,
aNode);
}
finally
{
Expand Down
32 changes: 32 additions & 0 deletions ph-css/src/main/java/com/helger/css/reader/CSSReaderSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class CSSReaderSettings implements ICloneable <CSSReaderSettings>
public static final ECSSVersion DEFAULT_VERSION = ECSSVersion.CSS30;
public static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
public static final boolean DEFAULT_BROWSER_COMPLIANT_MODE = false;
public static final boolean DEFAULT_KEEP_DEPRECATED_PROPERTIES = false;
public static final boolean DEFAULT_USE_SOURCE_LOCATION = true;
public static final int DEFAULT_TAB_SIZE = 8;

Expand All @@ -51,6 +52,7 @@ public class CSSReaderSettings implements ICloneable <CSSReaderSettings>
private ICSSParseErrorHandler m_aCustomErrorHandler;
private ICSSParseExceptionCallback m_aCustomExceptionHandler;
private boolean m_bBrowserCompliantMode = DEFAULT_BROWSER_COMPLIANT_MODE;
private boolean m_bKeepDeprecatedProperties = DEFAULT_KEEP_DEPRECATED_PROPERTIES;
private boolean m_bUseSourceLocation = DEFAULT_USE_SOURCE_LOCATION;
private int m_nTabSize = DEFAULT_TAB_SIZE;
private ICSSInterpretErrorHandler m_aInterpretErrorHandler;
Expand All @@ -66,6 +68,7 @@ public CSSReaderSettings (@Nonnull final CSSReaderSettings aOther)
m_aCustomErrorHandler = aOther.m_aCustomErrorHandler;
m_aCustomExceptionHandler = aOther.m_aCustomExceptionHandler;
m_bBrowserCompliantMode = aOther.m_bBrowserCompliantMode;
m_bKeepDeprecatedProperties = aOther.m_bKeepDeprecatedProperties;
m_bUseSourceLocation = aOther.m_bUseSourceLocation;
m_nTabSize = aOther.m_nTabSize;
m_aInterpretErrorHandler = aOther.m_aInterpretErrorHandler;
Expand Down Expand Up @@ -205,6 +208,35 @@ public CSSReaderSettings setBrowserCompliantMode (final boolean bBrowserComplian
return this;
}

/**
* @return <code>true</code> if deprecated properties (e.g.
* <code>*zoom</code>) should be kept while reading,
* <code>false</code> if they should be discarded. The default is
* {@link #DEFAULT_KEEP_DEPRECATED_PROPERTIES}.
* @since 7.0.4
*/
public boolean isKeepDeprecatedProperties ()
{
return m_bKeepDeprecatedProperties;
}

/**
* Define, whether deprecated properties (e.g. <code>*zoom</code>) should be
* kept or not.
*
* @param bKeepDeprecatedProperties
* <code>true</code> to keep them, <code>false</code> to discard them
* on reading.
* @return this
* @since 7.0.4
*/
@Nonnull
public CSSReaderSettings setKeepDeprecatedProperties (final boolean bKeepDeprecatedProperties)
{
m_bKeepDeprecatedProperties = bKeepDeprecatedProperties;
return this;
}

/**
* @return <code>true</code> if the source location should be stored,
* <code>false</code> to ignore them. The default is
Expand Down
8 changes: 5 additions & 3 deletions ph-css/src/main/jjtree/ParserCSS30.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ TOKEN_MGR_DECLS :
private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger (ParserCSS30TokenManager.class);

protected com.helger.css.reader.errorhandler.ICSSParseErrorHandler m_aCustomErrorHandler;
protected boolean m_bBrowserCompliantMode = false;

/**
* Set a custom error handler to use.
Expand Down Expand Up @@ -1203,8 +1202,11 @@ void property() :
( <S> )*
| LOOKAHEAD(2, ( <ASTERISK> | <DOLLAR> ) <IDENT> )
( <ASTERISK> | <DOLLAR> ) { aPrefixToken = token; }
<IDENT> { errorDeprecatedProperty (aPrefixToken); }
/* leaving jjtThis.text null is handled inside the code */
<IDENT> { if (m_bKeepDeprecatedProperties)
jjtThis.setText (aPrefixToken.image + token.image);
else
errorDeprecatedProperty (aPrefixToken);
}
( <S> )*
)
}
Expand Down
Loading

0 comments on commit aac25b6

Please sign in to comment.