Search in sources :

Example 1 with Selector

use of org.w3c.css.sac.Selector in project java-to-graphviz by randomnoun.

the class StylesheetApplier method apply.

/**
 * actually inline the styles
 */
public void apply() {
    final Multimap<Element, StyleApplication> elementMatches = ArrayListMultimap.create();
    final CSSOMParser inlineParser = new CSSOMParser();
    inlineParser.setErrorHandler(new ExceptionErrorHandler());
    // factor in elements' style attributes
    NodeTraversor.traverse(new NodeVisitor() {

        @Override
        public void head(Node node, int depth) {
            if (node instanceof Element && node.hasAttr("style")) {
                // parse the CSS into a CSSStyleDeclaration
                InputSource input = new InputSource(new StringReader(node.attr("style")));
                CSSStyleDeclaration declaration = null;
                try {
                    declaration = inlineParser.parseStyleDeclaration(input);
                } catch (IOException e) {
                    // again, this should never happen, cuz we're just reading from a string
                    e.printStackTrace();
                }
                node.removeAttr("style");
                elementMatches.put(((Element) node), new InlineStyleApplication(declaration));
            }
        }

        @Override
        public void tail(Node node, int depth) {
        }
    }, htmlDocument.body());
    // compute which rules match which elements
    CSSRuleList rules = styleSheet.getCssRules();
    for (int i = 0; i < rules.getLength(); i++) {
        if (rules.item(i) instanceof CSSStyleRule) {
            CSSStyleRuleImpl rule = (CSSStyleRuleImpl) rules.item(i);
            // for each selector in the rule... (separated by commas)
            for (int j = 0; j < rule.getSelectors().getLength(); j++) {
                Selector selector = rule.getSelectors().item(j);
                Elements matches = null;
                // selector.toString() converts a~=b to a~="b" which then includes the double quotes in the regex
                // could remove this but the jsoup impl to ~= is wrong anyway ( should be performing a word search, not a regex )
                matches = htmlDocument.select(selector.toString());
                // for each matched element....
                for (Element match : matches) {
                    elementMatches.put(match, new RuleStyleApplication(selector, rule.getStyle()));
                }
            }
        }
    }
    Map<Element, CSSStyleDeclaration> inlinedStyles = new HashMap<Element, CSSStyleDeclaration>();
    // calculate overrides
    for (Element element : elementMatches.keySet()) {
        CSSStyleDeclaration properties = new CSSStyleDeclarationImpl();
        List<StyleApplication> matchedRules = new ArrayList<StyleApplication>(elementMatches.get(element));
        Collections.sort(matchedRules, orderBySpecificity);
        for (StyleApplication matchedRule : matchedRules) {
            CSSStyleDeclaration cssBlock = matchedRule.getCssBlock();
            for (int i = 0; i < cssBlock.getLength(); i++) {
                String propertyKey = cssBlock.item(i);
                CSSValue propertyValue = cssBlock.getPropertyCSSValue(propertyKey);
                // TODO: !important
                properties.setProperty(propertyKey, propertyValue.getCssText(), "");
            }
        }
        inlinedStyles.put(element, properties);
    }
    // apply to DOM
    for (Map.Entry<Element, CSSStyleDeclaration> entry : inlinedStyles.entrySet()) {
        entry.getKey().attr("style", entry.getValue().getCssText());
    }
}
Also used : InputSource(org.w3c.css.sac.InputSource) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) DataNode(org.jsoup.nodes.DataNode) ArrayList(java.util.ArrayList) CSSStyleDeclarationImpl(com.steadystate.css.dom.CSSStyleDeclarationImpl) Elements(org.jsoup.select.Elements) NodeVisitor(org.jsoup.select.NodeVisitor) CSSRuleList(org.w3c.dom.css.CSSRuleList) CSSValue(org.w3c.dom.css.CSSValue) CSSStyleRule(org.w3c.dom.css.CSSStyleRule) StringReader(java.io.StringReader) DescendantSelector(org.w3c.css.sac.DescendantSelector) ConditionalSelector(org.w3c.css.sac.ConditionalSelector) Selector(org.w3c.css.sac.Selector) SimpleSelector(org.w3c.css.sac.SimpleSelector) CSSOMParser(com.steadystate.css.parser.CSSOMParser) IOException(java.io.IOException) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) CSSStyleRuleImpl(com.steadystate.css.dom.CSSStyleRuleImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Selector

use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.

the class StyleSheet method createSelectorWrappers.

private void createSelectorWrappers() {
    List<SelectorWrapper> selectorWrappersList = new LinkedList<>();
    for (int pos = 0; pos < styleRules.length; pos++) {
        StyleRule styleRule = styleRules[pos];
        SelectorList selectors = styleRule.getSelectors();
        StylePropertyMap properties = styleRule.getProperties();
        int length = selectors.getLength();
        for (int i = 0; i < length; i++) {
            Selector selector = selectors.item(i);
            SelectorWrapper selectorWrapper = new SelectorWrapper(selector, properties, pos);
            selectorWrappersList.add(selectorWrapper);
        }
    }
    Collections.sort(selectorWrappersList, COMPARATOR);
    Collections.reverse(selectorWrappersList);
    selectorWrappers = new SelectorWrapper[selectorWrappersList.size()];
    selectorWrappersList.toArray(selectorWrappers);
}
Also used : SelectorList(org.w3c.css.sac.SelectorList) LinkedList(java.util.LinkedList) Selector(org.w3c.css.sac.Selector)

Example 3 with Selector

use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.

the class DocumentHandlerImpl method toString.

private static String toString(SelectorList patterns) {
    StringBuilder buffer = new StringBuilder();
    buffer.append("[");
    int length = patterns.getLength();
    for (int i = 0; i < length; i++) {
        buffer.append(" ");
        Selector selector = patterns.item(i);
        buffer.append(selector.toString());
    }
    buffer.append(" ]");
    return buffer.toString();
}
Also used : Selector(org.w3c.css.sac.Selector)

Example 4 with Selector

use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.

the class Parser method parseSelector.

/**
 * Parses a selector.
 */
protected Selector parseSelector() {
    SimpleSelector ss = parseSimpleSelector();
    Selector result = ss;
    pseudoElement = null;
    loop: for (; ; ) {
        switch(current) {
            default:
                break loop;
            case LexicalUnits.IDENTIFIER:
            case LexicalUnits.ANY:
            case LexicalUnits.HASH:
            case LexicalUnits.DOT:
            case LexicalUnits.LEFT_BRACKET:
            case LexicalUnits.COLON:
                result = selectorFactory.createDescendantSelector(result, parseSimpleSelector());
                break;
            case LexicalUnits.PLUS:
                nextIgnoreSpaces();
                result = selectorFactory.createDirectAdjacentSelector((short) 1, result, parseSimpleSelector());
                break;
            case LexicalUnits.PRECEDE:
                nextIgnoreSpaces();
                result = selectorFactory.createChildSelector(result, parseSimpleSelector());
        }
    }
    if (pseudoElement != null) {
        result = selectorFactory.createChildSelector(result, selectorFactory.createPseudoElementSelector(null, pseudoElement));
    }
    return result;
}
Also used : SimpleSelector(org.w3c.css.sac.SimpleSelector) Selector(org.w3c.css.sac.Selector) SimpleSelector(org.w3c.css.sac.SimpleSelector)

Example 5 with Selector

use of org.w3c.css.sac.Selector in project org.eclipse.rap by eclipse-rap.

the class ThemeTestBase method processTestStyleRule.

private static void processTestStyleRule(StyleRule styleRule) {
    SelectorList selectors = styleRule.getSelectors();
    StylePropertyMap properties = styleRule.getProperties();
    int length = selectors.getLength();
    for (int i = 0; i < length; i++) {
        Selector selector = selectors.item(i);
        SelectorExt selectorExt = (SelectorExt) selector;
        checkProperties(selectorExt, properties);
    }
}
Also used : SelectorExt(org.eclipse.rap.rwt.internal.theme.css.SelectorExt) SelectorList(org.w3c.css.sac.SelectorList) StylePropertyMap(org.eclipse.rap.rwt.internal.theme.css.StylePropertyMap) SimpleSelector(org.eclipse.rap.rwt.internal.theme.SimpleSelector) Selector(org.w3c.css.sac.Selector)

Aggregations

Selector (org.w3c.css.sac.Selector)22 SelectorList (org.w3c.css.sac.SelectorList)7 ConditionalSelector (org.w3c.css.sac.ConditionalSelector)5 SimpleSelector (org.w3c.css.sac.SimpleSelector)5 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 ExtendedCSSRule (org.eclipse.e4.ui.css.core.dom.ExtendedCSSRule)2 ExtendedSelector (org.eclipse.e4.ui.css.core.impl.sac.ExtendedSelector)2 AttributeCondition (org.w3c.css.sac.AttributeCondition)2 CSSParseException (org.w3c.css.sac.CSSParseException)2 Condition (org.w3c.css.sac.Condition)2 DescendantSelector (org.w3c.css.sac.DescendantSelector)2 InputSource (org.w3c.css.sac.InputSource)2 JSONParser (com.codename1.io.JSONParser)1 CSSStyleDeclarationImpl (com.steadystate.css.dom.CSSStyleDeclarationImpl)1 CSSStyleRuleImpl (com.steadystate.css.dom.CSSStyleRuleImpl)1 CSSOMParser (com.steadystate.css.parser.CSSOMParser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CharArrayReader (java.io.CharArrayReader)1 FileInputStream (java.io.FileInputStream)1