use of com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler in project ph-css by phax.
the class CSSReader30SpecialFuncTest method testReadSpecialBadButRecoverable.
@Test
public void testReadSpecialBadButRecoverable() {
final CollectingCSSParseErrorHandler aErrors = new CollectingCSSParseErrorHandler();
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()));
assertNotNull(aFile.getAbsolutePath(), aCSS);
}
use of com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler in project ph-css by phax.
the class AbstractFuncTestCSSReader method testReadBadButRecoverable.
protected final void testReadBadButRecoverable(@Nonnull final String sBaseDir) {
final File aBaseDir = new File(sBaseDir);
if (!aBaseDir.exists())
throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
final String sKey = aFile.getAbsolutePath();
if (m_bDebug)
m_aLogger.info(sKey);
// Handle each error as a fatal error!
final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
assertNotNull(sKey, aCSS);
assertTrue(sKey, aErrorHdl.hasParseErrors());
assertTrue(sKey, aErrorHdl.getParseErrorCount() > 0);
if (m_bDebug)
m_aLogger.info(aErrorHdl.getAllParseErrors().toString());
// Write optimized version and re-read it
final String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
if (m_bDebug)
m_aLogger.info(sCSS);
final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
assertNotNull("Failed to parse:\n" + sCSS, aCSSReRead);
assertEquals(sKey, aCSS, aCSSReRead);
}
}
use of com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler in project ph-css by phax.
the class AbstractFuncTestCSSReader method testReadGood.
protected final void testReadGood(@Nonnull final String sBaseDir) {
final File aBaseDir = new File(sBaseDir);
if (!aBaseDir.exists())
throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
final String sKey = aFile.getAbsolutePath();
if (m_bDebug)
m_aLogger.info("Filename: " + sKey);
final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
assertNotNull(sKey, aCSS);
// May have errors or not
if (m_bDebug)
m_aLogger.info("Parse errors: " + aErrorHdl.getAllParseErrors().toString());
CommonsTestHelper.testDefaultSerialization(aCSS);
// Write optimized version and compare it
String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
if (m_bDebug)
m_aLogger.info("Created CSS: " + sCSS);
final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
assertNotNull("Failed to parse " + sKey + ":\n" + sCSS, aCSSReRead);
assertEquals(sKey + "\n" + sCSS, aCSS, aCSSReRead);
// Write non-optimized version and compare it
sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
if (m_bDebug)
m_aLogger.info("Read and re-created CSS: " + sCSS);
assertEquals(sKey, aCSS, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
// Write non-optimized and code-removed version and ensure it is not
// null
sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false).setRemoveUnnecessaryCode(true)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
assertNotNull(sKey, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
// Restore value :)
m_aWriterSettings.setRemoveUnnecessaryCode(false);
}
}
use of com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler in project flow by vaadin.
the class StyleAttributeHandler method parseStyles.
/**
* Parses the given style string and populates the given style object with
* the found styles.
*
* @param styleString
* the string to parse
* @return a map containing the found style rules
*/
public static LinkedHashMap<String, String> parseStyles(String styleString) {
CollectingCSSParseErrorHandler errorCollector = new CollectingCSSParseErrorHandler();
CSSDeclarationList parsed = CSSReaderDeclarationList.readFromString(styleString, ECSSVersion.LATEST, errorCollector);
if (errorCollector.hasParseErrors()) {
throw new IllegalArgumentException(String.format(ERROR_PARSING_STYLE, styleString, errorCollector.getAllParseErrors().get(0).getErrorMessage()));
}
if (parsed == null) {
// Did not find any styles
throw new IllegalArgumentException(String.format(ERROR_PARSING_STYLE, styleString, "No styles found"));
}
LinkedHashMap<String, String> parsedStyles = new LinkedHashMap<>();
for (CSSDeclaration declaration : parsed.getAllDeclarations()) {
String key = declaration.getProperty();
String value = declaration.getExpression().getAsCSSString(new CSSWriterSettings(ECSSVersion.LATEST).setOptimizedOutput(true), 0);
parsedStyles.put(StyleUtil.styleAttributeToProperty(key), value);
}
return parsedStyles;
}
Aggregations