Search in sources :

Example 1 with LoggingCSSParseErrorHandler

use of com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler 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()).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: " + baseUrl + ", got null CascadingStyleSheet");
        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 2 with LoggingCSSParseErrorHandler

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

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

the class Issue17Test method testIssue.

@Test
public void testIssue() {
    // Multiple errors contained
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue17.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);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) 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 4 with LoggingCSSParseErrorHandler

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

the class Issue21Test method testIssue.

@Test
public void testIssue() {
    // Multiple errors contained
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue21.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
    assertEquals(2, aCSS.getRuleCount());
    assertEquals(2, 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 5 with LoggingCSSParseErrorHandler

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

the class Issue9Test method testIssue9b.

@Test
public void testIssue9b() {
    // File only ends with an invalid comment
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad/issue9b.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, StandardCharsets.UTF_8, ECSSVersion.CSS30, new LoggingCSSParseErrorHandler());
    assertNull(aCSS);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

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