Search in sources :

Example 1 with CSSStyleRuleImpl

use of com.gargoylesoftware.css.dom.CSSStyleRuleImpl in project htmlunit by HtmlUnit.

the class CSSStyleSheet method index.

private void index(final CSSStyleSheetImpl.CSSStyleSheetRuleIndex index, final CSSRuleListImpl ruleList, final Set<String> alreadyProcessing) {
    for (final AbstractCSSRuleImpl rule : ruleList.getRules()) {
        if (rule instanceof CSSStyleRuleImpl) {
            final CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule;
            final SelectorList selectors = styleRule.getSelectors();
            for (final Selector selector : selectors) {
                final SimpleSelector simpleSel = selector.getSimpleSelector();
                if (SelectorType.ELEMENT_NODE_SELECTOR == simpleSel.getSelectorType()) {
                    final ElementSelector es = (ElementSelector) simpleSel;
                    boolean wasClass = false;
                    final List<Condition> conds = es.getConditions();
                    if (conds != null && conds.size() == 1) {
                        final Condition c = conds.get(0);
                        if (ConditionType.CLASS_CONDITION == c.getConditionType()) {
                            index.addClassSelector(es, c.getValue(), selector, styleRule);
                            wasClass = true;
                        }
                    }
                    if (!wasClass) {
                        index.addElementSelector(es, selector, styleRule);
                    }
                } else {
                    index.addOtherSelector(selector, styleRule);
                }
            }
        } else if (rule instanceof CSSImportRuleImpl) {
            final CSSImportRuleImpl importRule = (CSSImportRuleImpl) rule;
            final CSSStyleSheet sheet = getImportedStyleSheet(importRule);
            if (!alreadyProcessing.contains(sheet.getUri())) {
                final CSSRuleListImpl sheetRuleList = sheet.getWrappedSheet().getCssRules();
                alreadyProcessing.add(sheet.getUri());
                final MediaListImpl mediaList = importRule.getMedia();
                if (mediaList.getLength() == 0 && index.getMediaList().getLength() == 0) {
                    index(index, sheetRuleList, alreadyProcessing);
                } else {
                    index(index.addMedia(mediaList), sheetRuleList, alreadyProcessing);
                }
            }
        } else if (rule instanceof CSSMediaRuleImpl) {
            final CSSMediaRuleImpl mediaRule = (CSSMediaRuleImpl) rule;
            final MediaListImpl mediaList = mediaRule.getMediaList();
            if (mediaList.getLength() == 0 && index.getMediaList().getLength() == 0) {
                index(index, mediaRule.getCssRules(), alreadyProcessing);
            } else {
                index(index.addMedia(mediaList), mediaRule.getCssRules(), alreadyProcessing);
            }
        }
    }
}
Also used : AbstractCSSRuleImpl(com.gargoylesoftware.css.dom.AbstractCSSRuleImpl) Condition(com.gargoylesoftware.css.parser.condition.Condition) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) CSSMediaRuleImpl(com.gargoylesoftware.css.dom.CSSMediaRuleImpl) CSSStyleRuleImpl(com.gargoylesoftware.css.dom.CSSStyleRuleImpl) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) DirectAdjacentSelector(com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector) GeneralAdjacentSelector(com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) DescendantSelector(com.gargoylesoftware.css.parser.selector.DescendantSelector) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) Selector(com.gargoylesoftware.css.parser.selector.Selector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) ChildSelector(com.gargoylesoftware.css.parser.selector.ChildSelector) CSSImportRuleImpl(com.gargoylesoftware.css.dom.CSSImportRuleImpl) CSSRuleListImpl(com.gargoylesoftware.css.dom.CSSRuleListImpl)

Example 2 with CSSStyleRuleImpl

use of com.gargoylesoftware.css.dom.CSSStyleRuleImpl in project htmlunit by HtmlUnit.

the class CSSStyleRule method getSelectorText.

/**
 * Returns the textual representation of the selector for the rule set.
 * @return the textual representation of the selector for the rule set
 */
@JsxGetter
public String getSelectorText() {
    String selectorText = ((CSSStyleRuleImpl) getRule()).getSelectorText();
    final Matcher m = SELECTOR_PARTS_PATTERN.matcher(selectorText);
    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String fixedName = m.group();
        // this should be handled with the right regex but...
        if (getBrowserVersion().hasFeature(JS_SELECTOR_TEXT_LOWERCASE) && !fixedName.isEmpty() && '.' != fixedName.charAt(0) && '#' != fixedName.charAt(0)) {
            fixedName = fixedName.toLowerCase(Locale.ROOT);
        }
        fixedName = StringUtils.sanitizeForAppendReplacement(fixedName);
        m.appendReplacement(sb, fixedName);
    }
    m.appendTail(sb);
    // ".foo" and not "*.foo"
    selectorText = SELECTOR_REPLACE_PATTERN.matcher(sb.toString()).replaceAll("$1");
    return selectorText;
}
Also used : Matcher(java.util.regex.Matcher) CSSStyleRuleImpl(com.gargoylesoftware.css.dom.CSSStyleRuleImpl) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Aggregations

CSSStyleRuleImpl (com.gargoylesoftware.css.dom.CSSStyleRuleImpl)2 AbstractCSSRuleImpl (com.gargoylesoftware.css.dom.AbstractCSSRuleImpl)1 CSSImportRuleImpl (com.gargoylesoftware.css.dom.CSSImportRuleImpl)1 CSSMediaRuleImpl (com.gargoylesoftware.css.dom.CSSMediaRuleImpl)1 CSSRuleListImpl (com.gargoylesoftware.css.dom.CSSRuleListImpl)1 MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)1 Condition (com.gargoylesoftware.css.parser.condition.Condition)1 ChildSelector (com.gargoylesoftware.css.parser.selector.ChildSelector)1 DescendantSelector (com.gargoylesoftware.css.parser.selector.DescendantSelector)1 DirectAdjacentSelector (com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector)1 ElementSelector (com.gargoylesoftware.css.parser.selector.ElementSelector)1 GeneralAdjacentSelector (com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector)1 PseudoElementSelector (com.gargoylesoftware.css.parser.selector.PseudoElementSelector)1 Selector (com.gargoylesoftware.css.parser.selector.Selector)1 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)1 SimpleSelector (com.gargoylesoftware.css.parser.selector.SimpleSelector)1 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)1 Matcher (java.util.regex.Matcher)1