Search in sources :

Example 1 with CSSImportRule

use of com.helger.css.decl.CSSImportRule 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 CSSImportRule

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

the class WikiVisitDataUrls method readFromStyleAttributeWithAPI.

public void readFromStyleAttributeWithAPI() {
    final String sStyle = "@import '/folder/foobar.css';\n" + "div{background:fixed url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB9EFBAoYMhVvMQIAAAAtSURBVHicY/z//z8DHoBH+v///yy4FDEyMjIwMDDhM3lgpaEuh7gTEzDiDxYA9HEPDF90e5YAAAAASUVORK5CYII=) !important;}\n" + "span { background-image:url('/my/folder/b.gif');}";
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
    CSSVisitor.visitCSSUrl(aCSS, new DefaultCSSUrlVisitor() {

        // Called for each import
        @Override
        public void onImport(@Nonnull final CSSImportRule aImportRule) {
            System.out.println("Import: " + aImportRule.getLocationString());
        }

        // Call for URLs outside of URLs
        @Override
        public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
            final CSSURI aURI = aURITerm.getURI();
            if (aURI.isDataURL()) {
                final CSSDataURL aDataURL = aURI.getAsDataURL();
                System.out.println(aDeclaration.getProperty() + " - references data URL with " + aDataURL.getContentLength() + " bytes of content");
            } else
                System.out.println(aDeclaration.getProperty() + " - references regular URL: " + aURI.getURI());
        }
    });
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSURI(com.helger.css.decl.CSSURI) CSSDataURL(com.helger.css.utils.CSSDataURL) CSSDeclaration(com.helger.css.decl.CSSDeclaration) DefaultCSSUrlVisitor(com.helger.css.decl.visit.DefaultCSSUrlVisitor)

Example 3 with CSSImportRule

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

the class WikiVisitUrls method readFromStyleAttributeWithAPI.

public void readFromStyleAttributeWithAPI() {
    final String sStyle = "@import 'foobar.css';\n" + "div{background:fixed url(a.gif) !important;}\n" + "span { background-image:url('/my/folder/b.gif');}";
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
    CSSVisitor.visitCSSUrl(aCSS, new DefaultCSSUrlVisitor() {

        // Called for each import
        @Override
        public void onImport(@Nonnull final CSSImportRule aImportRule) {
            System.out.println("Import: " + aImportRule.getLocationString() + " - " + getSourceLocationString(aImportRule.getSourceLocation()));
        }

        // Call for URLs outside of URLs
        @Override
        public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
            System.out.println(aDeclaration.getProperty() + " - references: " + aURITerm.getURIString() + " - " + getSourceLocationString(aURITerm.getSourceLocation()));
        }
    });
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSDeclaration(com.helger.css.decl.CSSDeclaration) DefaultCSSUrlVisitor(com.helger.css.decl.visit.DefaultCSSUrlVisitor)

Example 4 with CSSImportRule

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

the class CSSVisitorDeclarationListFuncTest method testModifyingCSSUrlVisitor.

@Test
public void testModifyingCSSUrlVisitor() {
    final CSSDeclarationList aCSS = CSSReaderDeclarationList.readFromString("background:url(a.gif);background:url(b.gif);", ECSSVersion.CSS30);
    assertNotNull(aCSS);
    // Append ".modified" to all URLs
    final MockModifyingCSSUrlVisitor aVisitor2 = new MockModifyingCSSUrlVisitor();
    CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor2);
    // Check the result
    assertEquals("background:url(a.gif.modified);background:url(b.gif.modified)", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
    // Re-iterate to check twice
    CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor() {

        @Override
        public void onImport(@Nonnull final CSSImportRule aImportRule) {
            assertTrue(aImportRule.getLocationString().endsWith(".modified"));
        }

        @Override
        public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
            assertTrue(aURITerm.getURIString().endsWith(".modified"));
        }
    });
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

Example 5 with CSSImportRule

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

the class MediaQueryTools method getWrappedInMediaQuery.

/**
 * Get the CSS wrapped in the specified media query. Note: all existing rule
 * objects are reused, so modifying them also modifies the original CSS!
 *
 * @param aCSS
 *        The CSS to be wrapped. May not be <code>null</code>.
 * @param aMediaQueries
 *        The media queries to use. May neither be <code>null</code> nor empty
 *        nor may it contain <code>null</code> elements.
 * @param bAllowNestedMediaQueries
 *        if <code>true</code> nested media queries are allowed,
 *        <code>false</code> if they are prohibited.
 * @return <code>null</code> if out CSS cannot be wrapped, the newly created
 *         {@link CascadingStyleSheet} object otherwise.
 */
@Nullable
public static CascadingStyleSheet getWrappedInMediaQuery(@Nonnull final CascadingStyleSheet aCSS, @Nonnull @Nonempty final Iterable<? extends CSSMediaQuery> aMediaQueries, final boolean bAllowNestedMediaQueries) {
    ValueEnforcer.notNull(aCSS, "CSS");
    ValueEnforcer.notEmpty(aMediaQueries, "MediaQueries");
    if (!canWrapInMediaQuery(aCSS, bAllowNestedMediaQueries))
        return null;
    final CascadingStyleSheet ret = new CascadingStyleSheet();
    // Copy all import rules
    for (final CSSImportRule aImportRule : aCSS.getAllImportRules()) {
        if (aImportRule.hasMediaQueries()) {
            // import rule already has a media query - do not alter
            ret.addImportRule(aImportRule);
        } else {
            // Create a new rule and add the passed media queries
            final CSSImportRule aNewImportRule = new CSSImportRule(aImportRule.getLocation());
            for (final CSSMediaQuery aMediaQuery : aMediaQueries) aNewImportRule.addMediaQuery(aMediaQuery);
            ret.addImportRule(aNewImportRule);
        }
    }
    // Copy all namespace rules
    for (final CSSNamespaceRule aNamespaceRule : aCSS.getAllNamespaceRules()) ret.addNamespaceRule(aNamespaceRule);
    // Create a single top-level media rule ...
    // into this media rule
    final CSSMediaRule aNewMediaRule = new CSSMediaRule();
    for (final CSSMediaQuery aMediaQuery : aMediaQueries) aNewMediaRule.addMediaQuery(aMediaQuery);
    // ... and add the existing top-level rules into this media rule
    for (final ICSSTopLevelRule aRule : aCSS.getAllRules()) aNewMediaRule.addRule(aRule);
    // Finally add the resulting media rule into the new CSS
    ret.addRule(aNewMediaRule);
    return ret;
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSMediaQuery(com.helger.css.decl.CSSMediaQuery) ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSMediaRule(com.helger.css.decl.CSSMediaRule) CSSNamespaceRule(com.helger.css.decl.CSSNamespaceRule) Nullable(javax.annotation.Nullable)

Aggregations

CSSImportRule (com.helger.css.decl.CSSImportRule)7 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)7 CSSDeclaration (com.helger.css.decl.CSSDeclaration)4 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)4 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)4 CSSNamespaceRule (com.helger.css.decl.CSSNamespaceRule)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)1 CSSMediaQuery (com.helger.css.decl.CSSMediaQuery)1 CSSMediaRule (com.helger.css.decl.CSSMediaRule)1 CSSURI (com.helger.css.decl.CSSURI)1 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)1 DoNothingCSSInterpretErrorHandler (com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler)1 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)1 CSSDataURL (com.helger.css.utils.CSSDataURL)1 CSSWriter (com.helger.css.writer.CSSWriter)1 URL (java.net.URL)1 Charset (java.nio.charset.Charset)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1