Search in sources :

Example 1 with CSSStyleDeclarationImpl

use of com.steadystate.css.dom.CSSStyleDeclarationImpl 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)

Aggregations

CSSStyleDeclarationImpl (com.steadystate.css.dom.CSSStyleDeclarationImpl)1 CSSStyleRuleImpl (com.steadystate.css.dom.CSSStyleRuleImpl)1 CSSOMParser (com.steadystate.css.parser.CSSOMParser)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DataNode (org.jsoup.nodes.DataNode)1 Element (org.jsoup.nodes.Element)1 Node (org.jsoup.nodes.Node)1 TextNode (org.jsoup.nodes.TextNode)1 Elements (org.jsoup.select.Elements)1 NodeVisitor (org.jsoup.select.NodeVisitor)1 ConditionalSelector (org.w3c.css.sac.ConditionalSelector)1 DescendantSelector (org.w3c.css.sac.DescendantSelector)1 InputSource (org.w3c.css.sac.InputSource)1 Selector (org.w3c.css.sac.Selector)1 SimpleSelector (org.w3c.css.sac.SimpleSelector)1 CSSRuleList (org.w3c.dom.css.CSSRuleList)1