Search in sources :

Example 1 with HtmlStyle

use of com.gargoylesoftware.htmlunit.html.HtmlStyle in project htmlunit by HtmlUnit.

the class HTMLStyleElement method setMedia.

/**
 * Sets the media of this style.
 * @param media the new media
 */
@JsxSetter
public void setMedia(final String media) {
    final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
    style.setAttribute("media", media);
}
Also used : HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 2 with HtmlStyle

use of com.gargoylesoftware.htmlunit.html.HtmlStyle in project htmlunit by HtmlUnit.

the class CSSStyleSheet2Test method selects_miscSelectors.

/**
 * @throws Exception if the test fails
 */
@Test
public void selects_miscSelectors() throws Exception {
    final String html = "<html><head><title>test</title>\n" + "</head><body><style></style>\n" + "<form name='f1' action='foo' class='yui-log'>\n" + "<div><div><input name='i1' id='m1'></div></div>\n" + "<input name='i2' class='yui-log'>\n" + "<button name='b1' class='yui-log'>\n" + "<button name='b2'>\n" + "</form>\n" + "</body></html>";
    final HtmlPage page = loadPage(html);
    final HtmlElement body = page.getBody();
    final HtmlForm form = page.getFormByName("f1");
    final HtmlInput input1 = (HtmlInput) page.getElementsByName("i1").get(0);
    final HtmlInput input2 = (HtmlInput) page.getElementsByName("i2").get(0);
    final DomElement button1 = page.getElementsByName("b1").get(0);
    final DomElement button2 = page.getElementsByName("b2").get(0);
    final BrowserVersion browserVersion = getBrowserVersion();
    final HtmlStyle node = (HtmlStyle) page.getElementsByTagName("style").item(0);
    final HTMLStyleElement host = (HTMLStyleElement) node.getScriptableObject();
    final CSSStyleSheet sheet = host.getSheet();
    Selector selector = parseSelector(sheet, "*.yui-log input");
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, body, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, form, null, false));
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, input1, null, false));
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, input2, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, button1, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, button2, null, false));
    selector = parseSelector(sheet, "#m1");
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, input1, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, input2, null, false));
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) Selector(com.gargoylesoftware.css.parser.selector.Selector) Test(org.junit.Test)

Example 3 with HtmlStyle

use of com.gargoylesoftware.htmlunit.html.HtmlStyle in project htmlunit by HtmlUnit.

the class CSSStyleSheet method getHref.

/**
 * Returns the URL of the stylesheet.
 * @return the URL of the stylesheet
 */
@JsxGetter
public String getHref() {
    if (ownerNode_ != null) {
        final DomNode node = ownerNode_.getDomNodeOrDie();
        if (node instanceof HtmlStyle) {
            return null;
        }
        if (node instanceof HtmlLink) {
            // <link rel="stylesheet" type="text/css" href="..." />
            final HtmlLink link = (HtmlLink) node;
            final String href = link.getHrefAttribute();
            if ("".equals(href) && getBrowserVersion().hasFeature(STYLESHEET_HREF_EMPTY_IS_NULL)) {
                return null;
            }
            // Expand relative URLs.
            try {
                final HtmlPage page = (HtmlPage) link.getPage();
                final URL url = page.getFullyQualifiedUrl(href);
                return url.toExternalForm();
            } catch (final MalformedURLException e) {
                // Log the error and fall through to the return values below.
                LOG.warn(e.getMessage(), e);
            }
        }
    }
    return getUri();
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) MalformedURLException(java.net.MalformedURLException) HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) URL(java.net.URL) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 4 with HtmlStyle

use of com.gargoylesoftware.htmlunit.html.HtmlStyle 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 5 with HtmlStyle

use of com.gargoylesoftware.htmlunit.html.HtmlStyle in project htmlunit by HtmlUnit.

the class HTMLStyleElement method setType.

/**
 * Sets the type of this style.
 * @param type the new type
 */
@JsxSetter
public void setType(final String type) {
    final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
    style.setTypeAttribute(type);
}
Also used : HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Aggregations

HtmlStyle (com.gargoylesoftware.htmlunit.html.HtmlStyle)8 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 Selector (com.gargoylesoftware.css.parser.selector.Selector)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 HTMLStyleElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)2 HtmlLink (com.gargoylesoftware.htmlunit.html.HtmlLink)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)2 Test (org.junit.Test)2 CSSStyleSheetImpl (com.gargoylesoftware.css.dom.CSSStyleSheetImpl)1 MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)1 Cache (com.gargoylesoftware.htmlunit.Cache)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1 CSSStyleSheet (com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet)1