Search in sources :

Example 1 with CSSDataURL

use of com.helger.css.utils.CSSDataURL 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 2 with CSSDataURL

use of com.helger.css.utils.CSSDataURL in project ph-css by phax.

the class CSSURITest method testDataURL.

@Test
public void testDataURL() {
    final CSSURI aURI = new CSSURI("data:text/plain,foobar");
    assertEquals("data:text/plain,foobar", aURI.getURI());
    assertTrue(aURI.isDataURL());
    final CSSDataURL aDataURL = aURI.getAsDataURL();
    assertNotNull(aDataURL);
    assertEquals("foobar", aDataURL.getContentAsString());
}
Also used : CSSDataURL(com.helger.css.utils.CSSDataURL) Test(org.junit.Test)

Aggregations

CSSDataURL (com.helger.css.utils.CSSDataURL)2 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)1 CSSImportRule (com.helger.css.decl.CSSImportRule)1 CSSURI (com.helger.css.decl.CSSURI)1 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)1 Test (org.junit.Test)1