Example usage for org.w3c.dom.css CSSRule IMPORT_RULE

List of usage examples for org.w3c.dom.css CSSRule IMPORT_RULE

Introduction

In this page you can find the example usage for org.w3c.dom.css CSSRule IMPORT_RULE.

Prototype

short IMPORT_RULE

To view the source code for org.w3c.dom.css CSSRule IMPORT_RULE.

Click Source Link

Document

The rule is a CSSImportRule.

Usage

From source file:com.gargoylesoftware.htmlunit.javascript.host.Stylesheet.java

/**
 * Modifies the specified style object by adding any style rules which apply to the specified
 * element./*from  w  ww.jav  a  2  s .c o m*/
 *
 * @param style the style to modify
 * @param element the element to which style rules must apply in order for them to be added to
 *        the specified style
 */
void modifyIfNecessary(final ComputedCSSStyleDeclaration style, final HTMLElement element) {
    final HtmlElement e = element.getDomNodeOrDie();
    final CSSRuleList rules = getWrappedSheet().getCssRules();
    if (rules == null) {
        return;
    }
    for (int i = 0; i < rules.getLength(); i++) {
        final CSSRule rule = rules.item(i);
        if (rule.getType() == CSSRule.STYLE_RULE) {
            final CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule;
            final SelectorList selectors = styleRule.getSelectors();
            for (int j = 0; j < selectors.getLength(); j++) {
                final Selector selector = selectors.item(j);
                final boolean selected = selects(selector, e);
                if (selected) {
                    final org.w3c.dom.css.CSSStyleDeclaration dec = styleRule.getStyle();
                    style.applyStyleFromSelector(dec, selector);
                }
            }
        } else if (rule.getType() == CSSRule.IMPORT_RULE) {
            final CSSImportRuleImpl importRule = (CSSImportRuleImpl) rule;
            Stylesheet sheet = imports_.get(importRule);
            if (sheet == null) {
                // TODO: surely wrong: in which case is it null and why?
                final String uri = (uri_ != null) ? uri_
                        : e.getPage().getWebResponse().getRequestSettings().getUrl().toExternalForm();
                final String href = importRule.getHref();
                final String url = UrlUtils.resolveUrl(uri, href);
                sheet = loadStylesheet(getWindow(), ownerNode_, null, url);
                imports_.put(importRule, sheet);
            }
            sheet.modifyIfNecessary(style, element);
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.java

private void modifyIfNecessary(final ComputedCSSStyleDeclaration style, final Element element,
        final CSSRuleList rules, final Set<String> alreadyProcessing) {
    if (rules == null) {
        return;/*  w w w .  ja v  a2  s.  c o m*/
    }

    final BrowserVersion browser = getBrowserVersion();
    final DomElement e = element.getDomNodeOrDie();
    final int rulesLength = rules.getLength();
    for (int i = 0; i < rulesLength; i++) {
        final CSSRule rule = rules.item(i);

        final short ruleType = rule.getType();
        if (CSSRule.STYLE_RULE == ruleType) {
            final CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule;
            final SelectorList selectors = styleRule.getSelectors();
            for (int j = 0; j < selectors.getLength(); j++) {
                final Selector selector = selectors.item(j);
                final boolean selected = selects(browser, selector, e);
                if (selected) {
                    final org.w3c.dom.css.CSSStyleDeclaration dec = styleRule.getStyle();
                    style.applyStyleFromSelector(dec, selector);
                }
            }
        } else if (CSSRule.IMPORT_RULE == ruleType) {
            final CSSImportRuleImpl importRule = (CSSImportRuleImpl) rule;
            final MediaList mediaList = importRule.getMedia();
            if (isActive(this, mediaList)) {
                CSSStyleSheet sheet = imports_.get(importRule);
                if (sheet == null) {
                    // TODO: surely wrong: in which case is it null and why?
                    final String uri = (uri_ != null) ? uri_ : e.getPage().getUrl().toExternalForm();
                    final String href = importRule.getHref();
                    final String url = UrlUtils.resolveUrl(uri, href);
                    sheet = loadStylesheet(getWindow(), ownerNode_, null, url);
                    imports_.put(importRule, sheet);
                }

                if (!alreadyProcessing.contains(sheet.getUri())) {
                    final CSSRuleList sheetRules = sheet.getWrappedSheet().getCssRules();
                    alreadyProcessing.add(getUri());
                    sheet.modifyIfNecessary(style, element, sheetRules, alreadyProcessing);
                }
            }
        } else if (CSSRule.MEDIA_RULE == ruleType) {
            final CSSMediaRuleImpl mediaRule = (CSSMediaRuleImpl) rule;
            final MediaList mediaList = mediaRule.getMedia();
            if (isActive(this, mediaList)) {
                final CSSRuleList internalRules = mediaRule.getCssRules();
                modifyIfNecessary(style, element, internalRules, alreadyProcessing);
            }
        }
    }
}