Search in sources :

Example 26 with CascadingStyleSheet

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

the class CSSWriterTest method testRemoveUnnecessaryCode1.

@Test
public void testRemoveUnnecessaryCode1() {
    final CascadingStyleSheet aCSS = CSSReader.readFromString(CSS3, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    final CSSWriterSettings aSettings = new CSSWriterSettings(ECSSVersion.CSS30, false).setRemoveUnnecessaryCode(true);
    final CSSWriter aWriter = new CSSWriter(aSettings).setWriteHeaderText(false);
    assertEquals("h1 {\n" + "  color:red;\n" + "  margin:1px;\n" + "}\n" + "\n" + "h2 { color:rgb(1,2,3); }\n" + "\n" + "@keyframes x {\n" + "  from {\n" + "    align:left;\n" + "    color:#123;\n" + "  }\n" + "  to { x:y; }\n" + "}\n" + "\n" + "@page {\n" + "  margin:1in;\n" + "  marks:none;\n" + "}\n" + "\n" + "@page :first { margin:2in; }\n" + "\n" + "@media print {\n" + "  div {\n" + "    width:100%;\n" + "    min-height:0;\n" + "  }\n" + "}\n" + "\n" + "@media all {\n" + "  div { width:90%; }\n" + "}\n" + "\n" + "@font-face {\n" + "  font-family:'Soho';\n" + "  src:url(Soho.eot);\n" + "}\n" + "\n" + "@font-face { src:local('Soho Gothic Pro'); }\n", aWriter.getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 27 with CascadingStyleSheet

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

the class CSSWriterTest method testNewLineModeMac.

@Test
public void testNewLineModeMac() {
    final CascadingStyleSheet aCSS = CSSReader.readFromString(CSS5, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    // Non-optimized version
    CSSWriter aWriter = new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(false).setNewLineMode(ENewLineMode.MAC)).setWriteHeaderText(true).setHeaderText("Unit test");
    assertEquals("/*\r" + " * Unit test\r" + " */\r" + "h1 {\r" + "  color:red;\r" + "  margin:1px;\r" + "}\r" + "\r" + "h2 {\r" + "  color:red;\r" + "  margin:1px;\r" + "}\r", aWriter.getCSSAsString(aCSS));
    // Optimized version
    aWriter = new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true).setNewLineMode(ENewLineMode.MAC)).setWriteHeaderText(true).setHeaderText("Unit test2");
    assertEquals("/*\r" + " * Unit test2\r" + " */\r" + "h1{color:red;margin:1px}h2{color:red;margin:1px}", aWriter.getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 28 with CascadingStyleSheet

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

the class CSSWriterTest method testWriteCertainRules.

@Test
public void testWriteCertainRules() {
    final CSSWriterSettings aSettings = new CSSWriterSettings(ECSSVersion.CSS30, true);
    aSettings.setWriteFontFaceRules(false);
    aSettings.setWriteKeyframesRules(false);
    aSettings.setWriteMediaRules(false);
    aSettings.setWritePageRules(false);
    final CSSWriter aWriter = new CSSWriter(aSettings).setWriteHeaderText(false);
    // Some non-special rules
    CascadingStyleSheet aCSS = CSSReader.readFromString(CSS3, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    assertEquals("h1{color:red;margin:1px}h2{color:rgb(1,2,3)}h3{}", aWriter.getCSSAsString(aCSS));
    // Only @media rule
    aCSS = CSSReader.readFromString(CSS4, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    assertEquals("", aWriter.getCSSAsString(aCSS));
    // Nothing special
    aCSS = CSSReader.readFromString(CSS5, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    assertEquals("h1{color:red;margin:1px}h2{color:red;margin:1px}", aWriter.getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 29 with CascadingStyleSheet

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

the class CSSWriterTest method testNewLineModeWindows.

@Test
public void testNewLineModeWindows() {
    final CascadingStyleSheet aCSS = CSSReader.readFromString(CSS5, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    // Non-optimized version
    CSSWriter aWriter = new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(false).setNewLineMode(ENewLineMode.WINDOWS)).setWriteHeaderText(true).setHeaderText("Unit test");
    assertEquals("/*\r\n" + " * Unit test\r\n" + " */\r\n" + "h1 {\r\n" + "  color:red;\r\n" + "  margin:1px;\r\n" + "}\r\n" + "\r\n" + "h2 {\r\n" + "  color:red;\r\n" + "  margin:1px;\r\n" + "}\r\n", aWriter.getCSSAsString(aCSS));
    // Optimized version
    aWriter = new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true).setNewLineMode(ENewLineMode.WINDOWS)).setWriteHeaderText(true).setHeaderText("Unit test2");
    assertEquals("/*\r\n" + " * Unit test2\r\n" + " */\r\n" + "h1{color:red;margin:1px}h2{color:red;margin:1px}", aWriter.getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 30 with CascadingStyleSheet

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

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