Search in sources :

Example 6 with HtmlStyle

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

the class HTMLStyleElement method getSheet.

/**
 * Gets the associated sheet.
 * @see <a href="http://www.xulplanet.com/references/objref/HTMLStyleElement.html">Mozilla doc</a>
 * @return the sheet
 */
@JsxGetter
public CSSStyleSheet getSheet() {
    if (sheet_ != null) {
        return sheet_;
    }
    final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
    final String css = style.getTextContent();
    final Window window = getWindow();
    final Cache cache = window.getWebWindow().getWebClient().getCache();
    final CSSStyleSheetImpl cached = cache.getCachedStyleSheet(css);
    final String uri = getDomNodeOrDie().getPage().getWebResponse().getWebRequest().getUrl().toExternalForm();
    if (cached != null) {
        sheet_ = new CSSStyleSheet(this, window, cached, uri);
    } else {
        sheet_ = new CSSStyleSheet(this, css, uri);
        cache.cache(css, sheet_.getWrappedSheet());
    }
    return sheet_;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) CSSStyleSheetImpl(com.gargoylesoftware.css.dom.CSSStyleSheetImpl) CSSStyleSheet(com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet) Cache(com.gargoylesoftware.htmlunit.Cache) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 7 with HtmlStyle

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

the class CSSStyleSheet2Test method testSelects.

private void testSelects(final String css, final boolean selectBody, final boolean selectDivD, final boolean selectSpanS) throws Exception {
    final String html = "<html>\n" + "  <body id='b'>\n" + "    <style></style>\n" + "    <div id='d' class='foo bar' lang='en-GB'>\n" + "      <span>x</span>\n" + "      <span id='s'>a</span>b\n" + "    </div>\n" + "  </body>\n" + "</html>";
    final HtmlPage page = loadPage(html);
    final BrowserVersion browserVersion = getBrowserVersion();
    final HtmlStyle node = (HtmlStyle) page.getElementsByTagName("style").item(0);
    final HTMLStyleElement host = (HTMLStyleElement) node.getScriptableObject();
    final CSSStyleSheet sheet = host.getSheet();
    final Selector selector = sheet.parseSelectors(css).get(0);
    assertEquals(selectBody, CSSStyleSheet.selects(browserVersion, selector, page.getHtmlElementById("b"), null, false));
    assertEquals(selectDivD, CSSStyleSheet.selects(browserVersion, selector, page.getHtmlElementById("d"), null, false));
    assertEquals(selectSpanS, CSSStyleSheet.selects(browserVersion, selector, page.getHtmlElementById("s"), null, false));
}
Also used : HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 8 with HtmlStyle

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

the class CSSStyleSheet2Test method selectsIdConditionWithSpecialChars.

/**
 * @throws Exception if an error occurs
 */
@Test
public void selectsIdConditionWithSpecialChars() throws Exception {
    final String html = "<html><body><style></style>\n" + "<div id='d:e'></div>\n" + "<div id='d-e'></div>\n" + "</body></html>";
    final HtmlPage page = loadPage(html);
    final HtmlStyle node = (HtmlStyle) page.getElementsByTagName("style").item(0);
    final HTMLStyleElement host = (HTMLStyleElement) node.getScriptableObject();
    final BrowserVersion browserVersion = getBrowserVersion();
    final CSSStyleSheet sheet = host.getSheet();
    Selector selector = sheet.parseSelectors("#d\\:e").get(0);
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, page.getHtmlElementById("d:e"), null, false));
    selector = sheet.parseSelectors("#d-e").get(0);
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, page.getHtmlElementById("d-e"), null, false));
}
Also used : HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) Selector(com.gargoylesoftware.css.parser.selector.Selector) Test(org.junit.Test)

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