Search in sources :

Example 11 with CSSWriterSettings

use of com.helger.css.writer.CSSWriterSettings in project ph-css by phax.

the class CSSURITest method testBasic.

@Test
public void testBasic() {
    final CSSURI aURI = new CSSURI("a.gif");
    assertEquals("a.gif", aURI.getURI());
    final CSSWriterSettings aSettings = new CSSWriterSettings(ECSSVersion.CSS30, false);
    assertEquals("url(a.gif)", aURI.getAsCSSString(aSettings));
    aSettings.setQuoteURLs(true);
    assertEquals("url('a.gif')", aURI.getAsCSSString(aSettings));
    assertFalse(aURI.isDataURL());
    assertNull(aURI.getAsDataURL());
    CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aURI, new CSSURI("a.gif"));
    CommonsTestHelper.testDefaultImplementationWithDifferentContentObject(aURI, new CSSURI("b.gif"));
}
Also used : CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) Test(org.junit.Test)

Example 12 with CSSWriterSettings

use of com.helger.css.writer.CSSWriterSettings in project flow by vaadin.

the class StyleAttributeHandler method parseStyles.

/**
 * Parses the given style string and populates the given style object with
 * the found styles.
 *
 * @param styleString
 *            the string to parse
 * @return a map containing the found style rules
 */
public static LinkedHashMap<String, String> parseStyles(String styleString) {
    CollectingCSSParseErrorHandler errorCollector = new CollectingCSSParseErrorHandler();
    CSSDeclarationList parsed = CSSReaderDeclarationList.readFromString(styleString, ECSSVersion.LATEST, errorCollector);
    if (errorCollector.hasParseErrors()) {
        throw new IllegalArgumentException(String.format(ERROR_PARSING_STYLE, styleString, errorCollector.getAllParseErrors().get(0).getErrorMessage()));
    }
    if (parsed == null) {
        // Did not find any styles
        throw new IllegalArgumentException(String.format(ERROR_PARSING_STYLE, styleString, "No styles found"));
    }
    LinkedHashMap<String, String> parsedStyles = new LinkedHashMap<>();
    for (CSSDeclaration declaration : parsed.getAllDeclarations()) {
        String key = declaration.getProperty();
        String value = declaration.getExpression().getAsCSSString(new CSSWriterSettings(ECSSVersion.LATEST).setOptimizedOutput(true), 0);
        parsedStyles.put(StyleUtil.styleAttributeToProperty(key), value);
    }
    return parsedStyles;
}
Also used : CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with CSSWriterSettings

use of com.helger.css.writer.CSSWriterSettings in project ph-css by phax.

the class CSSReader30FuncTest method testSpecialCasesAsString.

@Test
public void testSpecialCasesAsString() {
    final boolean bBrowserCompliantMode = isBrowserCompliantMode();
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(bBrowserCompliantMode);
    // Parsing problem
    String sCSS = ".class{color:red;.class{color:green}.class{color:blue}";
    CascadingStyleSheet aCSS;
    CascadingStyleSheet aCSS2;
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals(bBrowserCompliantMode ? "" : ".class{color:red}.class{color:blue}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
    sCSS = "  \n/* comment */\n  \n.class{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals(".class{color:red}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
    // With Umlauts
    sCSS = "div { colör: räd; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("div{colör:räd}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    aCSS2 = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS2);
    assertEquals("div{colör:räd}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS2));
    assertEquals(aCSS, aCSS2);
    // With masking
    sCSS = "#mask\\26{ color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\26{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\26 { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\26 {color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\26   { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\26 {color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // With masking
    sCSS = "#mask\\x{ color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\x{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\x { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\x{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    sCSS = "#mask\\x   { color: red; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("#mask\\x{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // With charset rule defined
    sCSS = "@charset \"iso-8859-1\"; div{color:red ; }";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("div{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // Invalid identifier 1
    sCSS = "-0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
    // Invalid identifier 2
    sCSS = "$0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
    // Invalid identifier 3
    sCSS = "*0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    if (bBrowserCompliantMode) {
        assertNotNull(aCSS);
        assertEquals(1, aCSS.getRuleCount());
    } else
        assertNull(aCSS);
    // Valid version of previous variant
    sCSS = "*abc{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNotNull(aCSS);
    assertEquals("*abc{color:red}", new CSSWriter(new CSSWriterSettings(ECSSVersion.CSS30).setOptimizedOutput(true)).getCSSAsString(aCSS));
    // Invalid identifier 4
    sCSS = "0{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
    // Invalid identifier 5
    sCSS = "--{color:red;}";
    aCSS = CSSReader.readFromStringReader(sCSS, aSettings);
    assertNull(aCSS);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CSSWriter(com.helger.css.writer.CSSWriter) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 14 with CSSWriterSettings

use of com.helger.css.writer.CSSWriterSettings in project ph-css by phax.

the class Issue34Test method testIssue.

@Test
@Ignore("TODO")
public void testIssue() {
    final String css = ".pen {background-color:red} {* some incorrect block *} .pen {background-color: blue}";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setCustomExceptionHandler(new LoggingCSSParseExceptionCallback());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    assertNotNull(cascadingStyleSheet);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    s_aLogger.info(writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) LoggingCSSParseExceptionCallback(com.helger.css.handler.LoggingCSSParseExceptionCallback) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with CSSWriterSettings

use of com.helger.css.writer.CSSWriterSettings in project ph-css by phax.

the class Issue36Test method testIssue.

@Test
public void testIssue() {
    final String css = "@media screen and (min-width: 768px) {.section {.\r\n" + "    padding: 40px\r\n" + "}\r\n" + "\r\n" + "}";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true).setCustomErrorHandler(new DoNothingCSSParseErrorHandler());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    assertEquals("@media screen and (min-width:768px){.section{}}", writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DoNothingCSSParseErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSParseErrorHandler) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) Test(org.junit.Test)

Aggregations

CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)21 Test (org.junit.Test)14 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)10 CSSWriter (com.helger.css.writer.CSSWriter)10 CSSDeclaration (com.helger.css.decl.CSSDeclaration)6 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)6 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)3 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)3 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)3 File (java.io.File)3 Charset (java.nio.charset.Charset)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)2 ECSSVersion (com.helger.css.ECSSVersion)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2 DefaultCSSVisitor (com.helger.css.decl.visit.DefaultCSSVisitor)2 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)2