Search in sources :

Example 11 with CSSDeclaration

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

the class CSSShortHandDescriptorTest method testBackground1.

@Test
public void testBackground1() {
    final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.BACKGROUND);
    assertNotNull(aSHD);
    final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("background: #ff0000 url('grafik.png') left top / 180px 100px no-repeat", ECSSVersion.CSS30).getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
    assertNotNull(aSplittedDecls);
    assertEquals(8, aSplittedDecls.size());
    assertEquals("background-color:#ff0000", aSplittedDecls.get(0).getAsCSSString(CWS));
    assertEquals("background-image:url(grafik.png)", aSplittedDecls.get(1).getAsCSSString(CWS));
    assertEquals("background-position:left top", aSplittedDecls.get(2).getAsCSSString(CWS));
    assertEquals("background-size:180px 100px", aSplittedDecls.get(3).getAsCSSString(CWS));
    assertEquals("background-repeat:no-repeat", aSplittedDecls.get(4).getAsCSSString(CWS));
    assertEquals("background-attachment:scroll", aSplittedDecls.get(5).getAsCSSString(CWS));
    assertEquals("background-clip:border-box", aSplittedDecls.get(6).getAsCSSString(CWS));
    assertEquals("background-origin:padding-box", aSplittedDecls.get(7).getAsCSSString(CWS));
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) Test(org.junit.Test)

Example 12 with CSSDeclaration

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

the class CSSShortHandDescriptorTest method testBorderColor2.

@Test
public void testBorderColor2() {
    final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.BORDER_COLOR);
    assertNotNull(aSHD);
    final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("border-color: red blue", ECSSVersion.CSS30).getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
    assertNotNull(aSplittedDecls);
    assertEquals(4, aSplittedDecls.size());
    assertEquals("border-top-color:red", aSplittedDecls.get(0).getAsCSSString(CWS));
    assertEquals("border-right-color:blue", aSplittedDecls.get(1).getAsCSSString(CWS));
    assertEquals("border-bottom-color:red", aSplittedDecls.get(2).getAsCSSString(CWS));
    assertEquals("border-left-color:blue", aSplittedDecls.get(3).getAsCSSString(CWS));
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) Test(org.junit.Test)

Example 13 with CSSDeclaration

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

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

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

the class CSSReaderDeclarationListTest method testReadAndValidate.

@Test
public void testReadAndValidate() {
    final CSSDeclarationList aList = CSSReaderDeclarationList.readFromString("color:red; background:fixed;", ECSSVersion.CSS30);
    assertNotNull(aList);
    assertEquals(2, aList.getDeclarationCount());
    CSSDeclaration aDecl = aList.getDeclarationAtIndex(0);
    assertNotNull(aDecl);
    assertEquals("color", aDecl.getProperty());
    assertEquals(1, aDecl.getExpression().getMemberCount());
    assertEquals("red", ((CSSExpressionMemberTermSimple) aDecl.getExpression().getMemberAtIndex(0)).getValue());
    aDecl = aList.getDeclarationAtIndex(1);
    assertNotNull(aDecl);
    assertEquals("background", aDecl.getProperty());
}
Also used : CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) Test(org.junit.Test)

Aggregations

CSSDeclaration (com.helger.css.decl.CSSDeclaration)26 Test (org.junit.Test)19 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)6 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)6 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)5 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)5 CSSImportRule (com.helger.css.decl.CSSImportRule)4 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)4 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)3 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CSSWriter (com.helger.css.writer.CSSWriter)3 Charset (java.nio.charset.Charset)3 ECSSVersion (com.helger.css.ECSSVersion)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2 File (java.io.File)2 Nonnull (javax.annotation.Nonnull)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1