Search in sources :

Example 1 with MediaListImpl

use of com.gargoylesoftware.css.dom.MediaListImpl 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 MediaListImpl

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

the class CSSStyleSheet method isActive.

/**
 * Returns {@code true} if this stylesheet is active, based on the media types it is associated with (if any).
 * @return {@code true} if this stylesheet is active, based on the media types it is associated with (if any)
 */
public boolean isActive() {
    final String media;
    final HtmlElement e = ownerNode_.getDomNodeOrNull();
    if (e instanceof HtmlStyle) {
        final HtmlStyle style = (HtmlStyle) e;
        media = style.getMediaAttribute();
    } else if (e instanceof HtmlLink) {
        final HtmlLink link = (HtmlLink) e;
        media = link.getMediaAttribute();
    } else {
        return true;
    }
    if (StringUtils.isBlank(media)) {
        return true;
    }
    final WebClient webClient = getWindow().getWebWindow().getWebClient();
    final MediaListImpl mediaList = parseMedia(webClient.getCssErrorHandler(), media);
    return isActive(this, mediaList);
}
Also used : HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) WebClient(com.gargoylesoftware.htmlunit.WebClient) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl)

Example 3 with MediaListImpl

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

the class MediaQueryList method isMatches.

/**
 * Returns whether the document currently matches the media query list or not.
 * @return whether the document currently matches the media query list or not
 */
@JsxGetter
public boolean isMatches() {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media_);
    return CSSStyleSheet.isActive(this, mediaList);
}
Also used : CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 4 with MediaListImpl

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

the class StyleMedia method matchMedium.

/**
 * Returns whether the specified media is supported by the object that displays the document object.
 * @param media the media query
 * @return whether the specified media is supported or not
 */
@JsxFunction
public boolean matchMedium(final String media) {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
    return CSSStyleSheet.isActive(this, mediaList);
}
Also used : CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 5 with MediaListImpl

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

the class StyleSheetList method isActiveStyleSheetLink.

/**
 * Verifies if the provided node is a link node pointing to an active stylesheet.
 *
 * @param domNode the mode to check
 * @return true if the provided node is a stylesheet link
 */
boolean isActiveStyleSheetLink(final DomNode domNode) {
    if (domNode instanceof HtmlLink) {
        final HtmlLink link = (HtmlLink) domNode;
        if (link.isStyleSheetLink()) {
            final String media = link.getMediaAttribute();
            if (StringUtils.isBlank(media)) {
                return true;
            }
            final WebClient webClient = getWindow().getWebWindow().getWebClient();
            final MediaListImpl mediaList = CSSStyleSheet.parseMedia(webClient.getCssErrorHandler(), media);
            return CSSStyleSheet.isActive(this, mediaList);
        }
    }
    return false;
}
Also used : HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) WebClient(com.gargoylesoftware.htmlunit.WebClient) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl)

Aggregations

MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)9 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)3 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 HtmlLink (com.gargoylesoftware.htmlunit.html.HtmlLink)2 MediaList (com.gargoylesoftware.htmlunit.javascript.host.dom.MediaList)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 CSSStyleRuleImpl (com.gargoylesoftware.css.dom.CSSStyleRuleImpl)1 CSSException (com.gargoylesoftware.css.parser.CSSException)1 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)1 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 Condition (com.gargoylesoftware.css.parser.condition.Condition)1 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)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