Search in sources :

Example 1 with CollectingCSSParseErrorHandler

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);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 2 with CollectingCSSParseErrorHandler

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);
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)

Example 3 with CollectingCSSParseErrorHandler

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);
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)

Example 4 with CollectingCSSParseErrorHandler

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;
}
Also used : CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)4 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)3 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)3 File (java.io.File)3 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)2 CSSWriter (com.helger.css.writer.CSSWriter)2 ECSSVersion (com.helger.css.ECSSVersion)1 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)1 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)1 Charset (java.nio.charset.Charset)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1