Search in sources :

Example 36 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project jmeter by apache.

the class CssParserCacheLoader method load.

@Override
public URLCollection load(Triple<String, URL, Charset> triple) throws Exception {
    final String cssContent = triple.getLeft();
    final URL baseUrl = triple.getMiddle();
    final Charset charset = triple.getRight();
    final CSSReaderSettings readerSettings = new CSSReaderSettings().setBrowserCompliantMode(true).setFallbackCharset(charset).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setUseSourceLocation(false).setCustomExceptionHandler(new CSSParseExceptionCallback(baseUrl));
    if (IGNORE_ALL_CSS_ERRORS) {
        readerSettings.setInterpretErrorHandler(new DoNothingCSSInterpretErrorHandler());
    }
    final CascadingStyleSheet aCSS = CSSReader.readFromStringReader(cssContent, readerSettings);
    final URLCollection urls = new URLCollection(new ArrayList<>());
    if (aCSS == null) {
        LOG.warn("Failed parsing CSS: {}, got null CascadingStyleSheet", baseUrl);
        return urls;
    }
    CSSVisitor.visitCSSUrl(aCSS, new DefaultCSSUrlVisitor() {

        @Override
        public void onImport(CSSImportRule rule) {
            final String location = rule.getLocationString();
            if (!StringUtils.isEmpty(location)) {
                urls.addURL(location, baseUrl);
            }
        }

        // Call for URLs outside of URLs
        @Override
        public void onUrlDeclaration(final ICSSTopLevelRule aTopLevelRule, final CSSDeclaration aDeclaration, final CSSExpressionMemberTermURI aURITerm) {
        // NOOP
        // Browser fetch such urls only when CSS rule matches
        // so we disable this code
        }
    });
    return urls;
}
Also used : CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) CSSImportRule(com.helger.css.decl.CSSImportRule) Charset(java.nio.charset.Charset) DoNothingCSSInterpretErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler) URL(java.net.URL) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) DefaultCSSUrlVisitor(com.helger.css.decl.visit.DefaultCSSUrlVisitor)

Example 37 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet 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 38 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet 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 39 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet 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 40 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class Issue36Test method testIssue.

@Test
public void testIssue() {
    final String css = "@media screen and (min-width: 768px) {.section {.\r\n" + "    padding: 40px\r\n" + "}\r\n" + "\r\n" + "}";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true).setCustomErrorHandler(new DoNothingCSSParseErrorHandler());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    assertEquals("@media screen and (min-width:768px){.section{}}", writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DoNothingCSSParseErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSParseErrorHandler) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) Test(org.junit.Test)

Aggregations

CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)64 Test (org.junit.Test)50 CSSWriter (com.helger.css.writer.CSSWriter)25 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)19 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)14 File (java.io.File)14 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)13 IReadableResource (com.helger.commons.io.resource.IReadableResource)12 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)10 Charset (java.nio.charset.Charset)10 ECSSVersion (com.helger.css.ECSSVersion)8 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)6 CSSDeclaration (com.helger.css.decl.CSSDeclaration)5 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)5 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)5 CSSImportRule (com.helger.css.decl.CSSImportRule)4 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)3 CSSStyleRule (com.helger.css.decl.CSSStyleRule)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)3