Search in sources :

Example 1 with AbstractCSSRuleImpl

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

the class CSSStyleSheet method refreshCssRules.

private void refreshCssRules() {
    if (cssRules_ == null) {
        return;
    }
    cssRules_.clearRules();
    cssRulesIndexFix_.clear();
    final CSSRuleListImpl ruleList = getWrappedSheet().getCssRules();
    final List<AbstractCSSRuleImpl> rules = ruleList.getRules();
    int pos = 0;
    for (final AbstractCSSRuleImpl rule : rules) {
        if (rule instanceof CSSCharsetRuleImpl) {
            cssRulesIndexFix_.add(pos);
            continue;
        }
        final com.gargoylesoftware.htmlunit.javascript.host.css.CSSRule cssRule = com.gargoylesoftware.htmlunit.javascript.host.css.CSSRule.create(this, rule);
        if (null == cssRule) {
            cssRulesIndexFix_.add(pos);
        } else {
            cssRules_.addRule(cssRule);
        }
        pos++;
    }
    // reset our index also
    getWrappedSheet().resetRuleIndex();
}
Also used : AbstractCSSRuleImpl(com.gargoylesoftware.css.dom.AbstractCSSRuleImpl) CSSCharsetRuleImpl(com.gargoylesoftware.css.dom.CSSCharsetRuleImpl) CSSRuleListImpl(com.gargoylesoftware.css.dom.CSSRuleListImpl)

Example 2 with AbstractCSSRuleImpl

use of com.gargoylesoftware.css.dom.AbstractCSSRuleImpl 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 3 with AbstractCSSRuleImpl

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

the class CSSGroupingRule method refreshCssRules.

private void refreshCssRules() {
    if (cssRules_ == null) {
        return;
    }
    cssRules_.clearRules();
    cssRulesIndexFix_.clear();
    final CSSRuleListImpl ruleList = getGroupingRule().getCssRules();
    final List<AbstractCSSRuleImpl> rules = ruleList.getRules();
    int pos = 0;
    for (final AbstractCSSRuleImpl rule : rules) {
        if (rule instanceof CSSCharsetRuleImpl) {
            cssRulesIndexFix_.add(pos);
            continue;
        }
        final CSSRule cssRule = CSSRule.create(getParentStyleSheet(), rule);
        if (null == cssRule) {
            cssRulesIndexFix_.add(pos);
        } else {
            cssRules_.addRule(cssRule);
        }
        pos++;
    }
}
Also used : AbstractCSSRuleImpl(com.gargoylesoftware.css.dom.AbstractCSSRuleImpl) CSSCharsetRuleImpl(com.gargoylesoftware.css.dom.CSSCharsetRuleImpl) CSSRuleListImpl(com.gargoylesoftware.css.dom.CSSRuleListImpl)

Example 4 with AbstractCSSRuleImpl

use of com.gargoylesoftware.css.dom.AbstractCSSRuleImpl in project LoboEvolution by LoboEvolution.

the class BackgroundImageSetter method changeValue.

/**
 * {@inheritDoc}
 */
@Override
public void changeValue(AbstractCSSProperties properties, String newValue, CSSStyleDeclarationImpl declaration, boolean important) {
    String baseHref = null;
    String finalValue;
    if (declaration != null) {
        final AbstractCSSRuleImpl rule = declaration.getParentRule();
        if (rule != null) {
            final CSSStyleSheetImpl sheet = rule.getParentStyleSheet();
            final CSSStyleSheetImpl ssheet = sheet;
            baseHref = ssheet.getHref();
        }
    }
    if (baseHref == null) {
        baseHref = properties.getContext().getDocumentBaseURI();
    }
    final String start = "url(";
    if (newValue == null || !newValue.toLowerCase().startsWith(start)) {
        finalValue = newValue;
    } else {
        final int startIdx = start.length();
        final int closingIdx = newValue.lastIndexOf(')');
        if (closingIdx == -1) {
            finalValue = newValue;
        } else {
            final String quotedUri = newValue.substring(startIdx, closingIdx);
            final String tentativeUri = HtmlValues.unquoteAndUnescape(quotedUri);
            if (baseHref == null) {
                finalValue = newValue;
            } else {
                try {
                    final URL styleUrl = Urls.createURL(null, baseHref);
                    if (tentativeUri.contains("data:image")) {
                        finalValue = tentativeUri;
                    } else {
                        finalValue = "url(" + HtmlValues.quoteAndEscape(Urls.createURL(styleUrl, tentativeUri).toExternalForm()) + ")";
                    }
                } catch (final Exception mfu) {
                    logger.log(Level.WARNING, "Unable to create URL for URI=[" + tentativeUri + "], with base=[" + baseHref + "].", mfu);
                    finalValue = newValue;
                }
            }
        }
    }
    properties.setPropertyValueLCAlt(BACKGROUND_IMAGE, finalValue, important);
}
Also used : AbstractCSSRuleImpl(com.gargoylesoftware.css.dom.AbstractCSSRuleImpl) CSSStyleSheetImpl(com.gargoylesoftware.css.dom.CSSStyleSheetImpl) URL(java.net.URL)

Example 5 with AbstractCSSRuleImpl

use of com.gargoylesoftware.css.dom.AbstractCSSRuleImpl in project LoboEvolution by LoboEvolution.

the class CSSOMParser method parseRule.

/**
 * Parses a string into a CSSRule.
 *
 * @param rule the input string
 * @return the css rule
 * @throws IOException if the underlying SAC parser throws an IOException
 */
public AbstractCSSRuleImpl parseRule(final String rule) throws IOException {
    try (InputSource source = new InputSource(new StringReader(rule))) {
        final CSSOMHandler handler = new CSSOMHandler();
        parser_.setDocumentHandler(handler);
        parser_.parseRule(source);
        return (AbstractCSSRuleImpl) handler.getRoot();
    }
}
Also used : AbstractCSSRuleImpl(com.gargoylesoftware.css.dom.AbstractCSSRuleImpl) StringReader(java.io.StringReader)

Aggregations

AbstractCSSRuleImpl (com.gargoylesoftware.css.dom.AbstractCSSRuleImpl)5 CSSRuleListImpl (com.gargoylesoftware.css.dom.CSSRuleListImpl)3 CSSCharsetRuleImpl (com.gargoylesoftware.css.dom.CSSCharsetRuleImpl)2 CSSImportRuleImpl (com.gargoylesoftware.css.dom.CSSImportRuleImpl)1 CSSMediaRuleImpl (com.gargoylesoftware.css.dom.CSSMediaRuleImpl)1 CSSStyleRuleImpl (com.gargoylesoftware.css.dom.CSSStyleRuleImpl)1 CSSStyleSheetImpl (com.gargoylesoftware.css.dom.CSSStyleSheetImpl)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 StringReader (java.io.StringReader)1 URL (java.net.URL)1