Search in sources :

Example 1 with CSSDeclarationList

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

the class CSSReaderDeclarationListTest method testRead21.

@Test
public void testRead21() {
    final ICSSParseExceptionCallback aHdl = new DoNothingCSSParseExceptionCallback();
    for (final String sCSS : VALID) {
        final CSSDeclarationList aDL = CSSReaderDeclarationList.readFromString(sCSS, ECSSVersion.CSS30, aHdl);
        assertNotNull(aDL);
    }
    for (final String sCSS : INVALID) assertNull(sCSS, CSSReaderDeclarationList.readFromString(sCSS, ECSSVersion.CSS30, aHdl));
}
Also used : CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) DoNothingCSSParseExceptionCallback(com.helger.css.handler.DoNothingCSSParseExceptionCallback) Test(org.junit.Test)

Example 2 with CSSDeclarationList

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

the class WikiReadFromHtml method readFromStyleAttribute.

public static CSSDeclarationList readFromStyleAttribute() {
    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);
    return aDeclList;
}
Also used : CSSDeclarationList(com.helger.css.decl.CSSDeclarationList)

Example 3 with CSSDeclarationList

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

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

the class CSSVisitorDeclarationListFuncTest method testVisitConstantCSS.

@Test
public void testVisitConstantCSS() {
    // CSS 1
    CSSDeclarationList aCSS = CSSReaderDeclarationList.readFromString("color:red;", ECSSVersion.CSS30);
    assertNotNull(aCSS);
    MockCountingUrlVisitor aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor);
    CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor());
    CSSVisitor.visitAllDeclarations(aCSS, new DefaultCSSVisitor());
    assertEquals(0, aVisitor.getCount());
    // CSS 2
    aCSS = CSSReaderDeclarationList.readFromString("background:url(a.gif)", ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor);
    CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor());
    CSSVisitor.visitAllDeclarations(aCSS, new DefaultCSSVisitor());
    assertEquals(1, aVisitor.getCount());
    // CSS 3
    aCSS = CSSReaderDeclarationList.readFromString("background:url(a.gif);background:url(b.gif);", ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor);
    CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor());
    CSSVisitor.visitAllDeclarations(aCSS, new DefaultCSSVisitor());
    assertEquals(2, aVisitor.getCount());
}
Also used : CSSDeclarationList(com.helger.css.decl.CSSDeclarationList) Test(org.junit.Test)

Example 5 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)

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