Search in sources :

Example 1 with DefaultCSSVisitor

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

the class WikiVisitSelectors method readAllSelectors.

public void readAllSelectors() {
    final String sStyle = "blockquote p,\r\n" + "blockquote ul,\r\n" + "blockquote ol {\r\n" + "  line-height:normal;\r\n" + "  font-style:italic;\r\n" + "}\r\n" + "\r\n" + "a { color:#FFEA6F; }\r\n" + "\r\n" + "a:hover { text-decoration:none; }\r\n" + "\r\n" + "img { border:none; }";
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
    final ICommonsList<String> aAllSelectors = new CommonsArrayList<>();
    CSSVisitor.visitCSS(aCSS, new DefaultCSSVisitor() {

        @Override
        public void onStyleRuleSelector(@Nonnull final CSSSelector aSelector) {
            aAllSelectors.add(aSelector.getAsCSSString(new CSSWriterSettings(ECSSVersion.CSS30)));
        }
    });
    System.out.println(aAllSelectors);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DefaultCSSVisitor(com.helger.css.decl.visit.DefaultCSSVisitor) CSSSelector(com.helger.css.decl.CSSSelector) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

Example 2 with DefaultCSSVisitor

use of com.helger.css.decl.visit.DefaultCSSVisitor 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

DefaultCSSVisitor (com.helger.css.decl.visit.DefaultCSSVisitor)2 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CSSDeclaration (com.helger.css.decl.CSSDeclaration)1 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)1 CSSSelector (com.helger.css.decl.CSSSelector)1 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)1 ICSSVisitor (com.helger.css.decl.visit.ICSSVisitor)1 Nonnull (javax.annotation.Nonnull)1