Search in sources :

Example 1 with DoNothingCSSInterpretErrorHandler

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

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

the class Issue33Test method testIssue.

@Test
public void testIssue() {
    // No log message may be issued in this test!
    final String css = "@media \\0screen\\,screen\\9 {.test {margin-left: 0px}}";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true).setInterpretErrorHandler(new DoNothingCSSInterpretErrorHandler());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    assertEquals("@media \\0screen\\,screen\\9 {.test{margin-left:0}}", writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DoNothingCSSInterpretErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) Test(org.junit.Test)

Aggregations

CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)2 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)2 DoNothingCSSInterpretErrorHandler (com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler)2 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)1 CSSImportRule (com.helger.css.decl.CSSImportRule)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)1 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)1 CSSWriter (com.helger.css.writer.CSSWriter)1 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)1 URL (java.net.URL)1 Charset (java.nio.charset.Charset)1 Test (org.junit.Test)1