Search in sources :

Example 6 with CSSDeclarationList

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

Example 7 with CSSDeclarationList

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

the class WikiVisitFromHtml method readFromStyleAttributeWithAPI.

public static void readFromStyleAttributeWithAPI() {
    final String sStyle = "color:red; background:fixed !important";
    final CSSDeclarationList aDeclList = CSSReaderDeclarationList.readFromString(sStyle, ECSSVersion.CSS30);
    if (aDeclList == null)
        throw new IllegalStateException("Failed to parse CSS: " + sStyle);
    // For all contained declarations
    for (final CSSDeclaration aDeclaration : aDeclList.getAllDeclarations()) System.out.println(aDeclaration.getProperty() + ": " + aDeclaration.getExpression().getAsCSSString(new CSSWriterSettings(ECSSVersion.CSS30)) + (aDeclaration.isImportant() ? " (important)" : " (not important)"));
}
Also used : CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList)

Example 8 with CSSDeclarationList

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

the class WikiVisitFromHtml method readFromStyleAttributeWithVisitor.

public static void readFromStyleAttributeWithVisitor() {
    final String sStyle = "color:red; background:fixed !important";
    final CSSDeclarationList aDeclList = CSSReaderDeclarationList.readFromString(sStyle, ECSSVersion.CSS30);
    if (aDeclList == null)
        throw new IllegalStateException("Failed to parse CSS: " + sStyle);
    // Create a custom visitor
    final ICSSVisitor aVisitor = new DefaultCSSVisitor() {

        @Override
        public void onDeclaration(@Nonnull final CSSDeclaration aDeclaration) {
            System.out.println(aDeclaration.getProperty() + ": " + aDeclaration.getExpression().getAsCSSString(new CSSWriterSettings(ECSSVersion.CSS30)) + (aDeclaration.isImportant() ? " (important)" : " (not important)"));
        }
    };
    CSSVisitor.visitAllDeclarations(aDeclList, aVisitor);
}
Also used : CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) Nonnull(javax.annotation.Nonnull) DefaultCSSVisitor(com.helger.css.decl.visit.DefaultCSSVisitor) CSSDeclaration(com.helger.css.decl.CSSDeclaration) CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) ICSSVisitor(com.helger.css.decl.visit.ICSSVisitor)

Aggregations

CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)8 CSSDeclaration (com.helger.css.decl.CSSDeclaration)5 Test (org.junit.Test)4 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)3 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)1 CSSImportRule (com.helger.css.decl.CSSImportRule)1 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)1 DefaultCSSVisitor (com.helger.css.decl.visit.DefaultCSSVisitor)1 ICSSVisitor (com.helger.css.decl.visit.ICSSVisitor)1 DoNothingCSSParseExceptionCallback (com.helger.css.handler.DoNothingCSSParseExceptionCallback)1 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)1 CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)1 CSSWriter (com.helger.css.writer.CSSWriter)1 LinkedHashMap (java.util.LinkedHashMap)1 Nonnull (javax.annotation.Nonnull)1