Search in sources :

Example 1 with CascadingStyleSheet

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

the class WikiCreateFontFaceRule method createFontFace.

/**
 * Create a single font-face rule.
 *
 * <pre>
 * &#64;font-face {
 *   font-family: "Your typeface";
 *   src: url("path/basename.eot");
 *   src: local("local font name"),
 *        url("path/basename.woff") format("woff"),
 *        url("path/basename.otf") format("opentype"),
 *        url("path/basename.svg#filename") format("svg");
 * }
 * </pre>
 *
 * @param sTypefaceName
 *        The name of the font-face in CSS. May neither be <code>null</code>
 *        nor empty.
 * @param sLocalName
 *        The name of the local font to be used. May be <code>null</code>.
 * @param sPath
 *        The server-relative path, where the font files reside. May not be
 *        <code>null</code>.
 * @param sBasename
 *        the base name of the font-files (without extension). May neither be
 *        <code>null</code> nor empty
 * @return The created {@link CascadingStyleSheet}.
 */
@Nonnull
public static CascadingStyleSheet createFontFace(@Nonnull @Nonempty final String sTypefaceName, @Nullable final String sLocalName, @Nonnull final String sPath, @Nonnull final String sBasename) {
    final CascadingStyleSheet aCSS = new CascadingStyleSheet();
    final CSSFontFaceRule aFFR = new CSSFontFaceRule();
    // The font-family
    aFFR.addDeclaration("font-family", CSSExpression.createString(sTypefaceName), false);
    // The special EOT file
    aFFR.addDeclaration("src", CSSExpression.createURI(sPath + sBasename + ".eot"), false);
    // The generic rules
    final CSSExpression aExpr = new CSSExpression();
    if (StringHelper.hasText(sLocalName))
        aExpr.addMember(new CSSExpressionMemberFunction("local", CSSExpression.createString(sLocalName))).addMember(ECSSExpressionOperator.COMMA);
    aExpr.addURI(sPath + sBasename + ".woff").addMember(_createFormatFct("woff")).addMember(ECSSExpressionOperator.COMMA).addURI(sPath + sBasename + ".otf").addMember(_createFormatFct("opentype")).addMember(ECSSExpressionOperator.COMMA).addURI(sPath + sBasename + ".svg#" + sBasename).addMember(_createFormatFct("svg"));
    aFFR.addDeclaration("src", aExpr, false);
    // Add the font-face rule to the main CSS
    aCSS.addRule(aFFR);
    return aCSS;
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSFontFaceRule(com.helger.css.decl.CSSFontFaceRule) CSSExpression(com.helger.css.decl.CSSExpression) CSSExpressionMemberFunction(com.helger.css.decl.CSSExpressionMemberFunction) Nonnull(javax.annotation.Nonnull)

Example 2 with CascadingStyleSheet

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

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

the class CSSReader30SpecialFuncTest method testReadWithBOM.

@Test
public void testReadWithBOM() {
    final String sCSSBase = "/* comment */.class{color:red}.class{color:blue}";
    for (final EUnicodeBOM eBOM : EUnicodeBOM.values()) {
        final Charset aDeterminedCharset = eBOM.getCharset();
        if (aDeterminedCharset != null) {
            final CascadingStyleSheet aCSS = CSSReader.readFromStream(new ByteArrayInputStreamProvider(ArrayHelper.getConcatenated(eBOM.getAllBytes(), sCSSBase.getBytes(aDeterminedCharset))), aDeterminedCharset, ECSSVersion.CSS30, new DoNothingCSSParseErrorHandler());
            assertNotNull("Failed to read with BOM " + eBOM, aCSS);
            assertEquals(".class{color:red}.class{color:blue}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
        }
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ByteArrayInputStreamProvider(com.helger.commons.io.streamprovider.ByteArrayInputStreamProvider) EUnicodeBOM(com.helger.commons.charset.EUnicodeBOM) DoNothingCSSParseErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSParseErrorHandler) Charset(java.nio.charset.Charset) CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

Example 4 with CascadingStyleSheet

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

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

the class CSSReader30SpecialFuncTest method testReadSingleLineComments.

@Test
public void testReadSingleLineComments() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-singleline-comments.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    assertEquals(13, aCSS.getRuleCount());
    assertEquals(13, aCSS.getStyleRuleCount());
    // #any1 - #any5
    assertEquals(2, aCSS.getStyleRuleAtIndex(1).getDeclarationCount());
    assertEquals(1, aCSS.getStyleRuleAtIndex(2).getDeclarationCount());
    assertEquals(1, aCSS.getStyleRuleAtIndex(3).getDeclarationCount());
    assertEquals(0, aCSS.getStyleRuleAtIndex(4).getDeclarationCount());
    assertEquals(0, aCSS.getStyleRuleAtIndex(5).getDeclarationCount());
    // .test1 - .test7
    assertEquals(2, aCSS.getStyleRuleAtIndex(6).getDeclarationCount());
    assertEquals(3, aCSS.getStyleRuleAtIndex(7).getDeclarationCount());
    assertEquals(1, aCSS.getStyleRuleAtIndex(8).getDeclarationCount());
    assertEquals(1, aCSS.getStyleRuleAtIndex(9).getDeclarationCount());
    assertEquals(2, aCSS.getStyleRuleAtIndex(10).getDeclarationCount());
    assertEquals(2, aCSS.getStyleRuleAtIndex(11).getDeclarationCount());
    assertEquals(1, aCSS.getStyleRuleAtIndex(12).getDeclarationCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) File(java.io.File) 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