Search in sources :

Example 11 with LoggingCSSParseErrorHandler

use of com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler in project ph-css by phax.

the class CSSVisitor30FuncTest method testVisitContent30.

@Test
public void testVisitContent30() {
    for (final File aFile : new FileSystemRecursiveIterator(new File("src/test/resources/testfiles/css30/good")).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (true)
            s_aLogger.info(sKey);
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
        assertNotNull(sKey, aCSS);
        CSSVisitor.visitCSSUrl(aCSS, new MockUrlVisitor(sKey));
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 12 with LoggingCSSParseErrorHandler

use of com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler in project ph-css by phax.

the class CSSReader30FuncTest method testSpecialCasesAsString.

@Test
public void testSpecialCasesAsString() {
    final boolean bBrowserCompliantMode = isBrowserCompliantMode();
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(bBrowserCompliantMode);
    // Parsing problem
    String sCSS = ".class{color:red;.class{color:green}.class{color:blue}";
    CascadingStyleSheet aCSS;
    CascadingStyleSheet aCSS2;
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals(bBrowserCompliantMode ? "" : ".class{color:red}.class{color:blue}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
    sCSS = "  \n/* comment */\n  \n.class{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals(".class{color:red}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
    // With Umlauts
    sCSS = "div { colör: räd; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("div{colör:räd}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    aCSS2 = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS2);
    assertEquals("div{colör:räd}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS2));
    assertEquals(aCSS, aCSS2);
    // With masking
    sCSS = "#mask\\26{ color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\26{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\26 { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\26 {color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\26   { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\26 {color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // With masking
    sCSS = "#mask\\x{ color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\x{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\x { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\x{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\x   { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\x{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // With charset rule defined
    sCSS = "@charset \"iso-8859-1\"; div{color:red ; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("div{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // Invalid identifier 1
    sCSS = "-0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
    // Invalid identifier 2
    sCSS = "$0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
    // Invalid identifier 3
    sCSS = "*0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    if (bBrowserCompliantMode) {
        assertNotNull(aCSS);
        assertEquals(1, aCSS.getRuleCount());
    } else
        assertNull(aCSS);
    // Valid version of previous variant
    sCSS = "*abc{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("*abc{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // Invalid identifier 4
    sCSS = "0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
    // Invalid identifier 5
    sCSS = "--{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CSSWriter(com.helger.css.writer.CSSWriter) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 13 with LoggingCSSParseErrorHandler

use of com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler in project ph-css by phax.

the class Issue19Test method testIssue.

@Test
public void testIssue() {
    // Multiple errors contained
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue19.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
    assertEquals(1, aCSS.getRuleCount());
    assertEquals(1, aCSS.getStyleRuleCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 14 with LoggingCSSParseErrorHandler

use of com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler in project ph-css by phax.

the class Issue34Test method testIssue.

@Test
@Ignore("TODO")
public void testIssue() {
    final String css = ".pen {background-color:red} {* some incorrect block *} .pen {background-color: blue}";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setCustomExceptionHandler(new LoggingCSSParseExceptionCallback());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    assertNotNull(cascadingStyleSheet);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    s_aLogger.info(writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) LoggingCSSParseExceptionCallback(com.helger.css.handler.LoggingCSSParseExceptionCallback) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with LoggingCSSParseErrorHandler

use of com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler 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)

Aggregations

CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)19 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)19 Test (org.junit.Test)16 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)12 IReadableResource (com.helger.commons.io.resource.IReadableResource)12 CSSWriter (com.helger.css.writer.CSSWriter)12 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)9 File (java.io.File)4 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)3 CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)3 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)2 Charset (java.nio.charset.Charset)2 ECSSVersion (com.helger.css.ECSSVersion)1 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)1 CSSImportRule (com.helger.css.decl.CSSImportRule)1 CSSStyleRule (com.helger.css.decl.CSSStyleRule)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)1 LoggingCSSParseExceptionCallback (com.helger.css.handler.LoggingCSSParseExceptionCallback)1