Search in sources :

Example 1 with HTMLStyleElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement 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 2 with HTMLStyleElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement in project htmlunit by HtmlUnit.

the class WebClientTest method cssErrorHandler.

/**
 * @throws Exception if an error occurs
 */
@Test
public void cssErrorHandler() throws Exception {
    final WebClient client = getWebClient();
    assertTrue(client.getCssErrorHandler() instanceof DefaultCssErrorHandler);
    final MutableInt fatals = new MutableInt();
    final MutableInt errors = new MutableInt();
    final MutableInt warnings = new MutableInt();
    final StringBuilder errorUri = new StringBuilder();
    final CSSErrorHandler handler = new CSSErrorHandler() {

        @Override
        public void warning(final CSSParseException exception) throws CSSException {
            warnings.increment();
        }

        @Override
        public void fatalError(final CSSParseException exception) throws CSSException {
            fatals.increment();
        }

        @Override
        public void error(final CSSParseException exception) throws CSSException {
            errors.increment();
            errorUri.append(exception.getURI());
        }
    };
    client.setCssErrorHandler(handler);
    assertEquals(handler, client.getCssErrorHandler());
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, "<html><body><style></style></body></html>");
    conn.setResponse(URL_SECOND, "<html><body><style>.x{color:red;}</style></body></html>");
    conn.setResponse(URL_THIRD, "<html><body><style>.x{color{}}}</style></body></html>");
    client.setWebConnection(conn);
    final HtmlPage page1 = client.getPage(URL_FIRST);
    ((HTMLStyleElement) page1.getBody().getFirstChild().getScriptableObject()).getSheet();
    assertEquals(0, warnings.intValue());
    assertEquals(0, errors.intValue());
    assertEquals(0, fatals.intValue());
    final HtmlPage page2 = client.getPage(URL_SECOND);
    ((HTMLStyleElement) page2.getBody().getFirstChild().getScriptableObject()).getSheet();
    assertEquals(0, warnings.intValue());
    assertEquals(0, errors.intValue());
    assertEquals(0, fatals.intValue());
    final HtmlPage page3 = client.getPage(URL_THIRD);
    ((HTMLStyleElement) page3.getBody().getFirstChild().getScriptableObject()).getSheet();
    assertEquals(1, warnings.intValue());
    assertEquals(2, errors.intValue());
    assertEquals(0, fatals.intValue());
    assertEquals("http://127.0.0.1:" + PORT + "/third/http://127.0.0.1:" + PORT + "/third/", errorUri.toString());
}
Also used : CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Test(org.junit.Test)

Example 3 with HTMLStyleElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement in project htmlunit by HtmlUnit.

the class Element method printNode.

protected void printNode(final StringBuilder builder, final DomNode node, final boolean html) {
    if (node instanceof DomComment) {
        if (html) {
            // Remove whitespace sequences.
            final String s = PRINT_NODE_PATTERN.matcher(node.getNodeValue()).replaceAll(" ");
            builder.append("<!--").append(s).append("-->");
        }
    } else if (node instanceof DomCharacterData) {
        // Remove whitespace sequences, possibly escape XML characters.
        String s = node.getNodeValue();
        if (html) {
            s = com.gargoylesoftware.htmlunit.util.StringUtils.escapeXmlChars(s);
        }
        builder.append(s);
    } else if (html) {
        final DomElement element = (DomElement) node;
        final Element scriptObject = node.getScriptableObject();
        final String tag = element.getTagName();
        Element htmlElement = null;
        if (scriptObject instanceof HTMLElement) {
            htmlElement = scriptObject;
        }
        builder.append('<').append(tag);
        // Add the attributes. IE does not use quotes, FF does.
        for (final DomAttr attr : element.getAttributesMap().values()) {
            if (!attr.getSpecified()) {
                continue;
            }
            final String name = attr.getName();
            final String value = PRINT_NODE_QUOTE_PATTERN.matcher(attr.getValue()).replaceAll("&quot;");
            builder.append(' ').append(name).append('=');
            builder.append('\"');
            builder.append(value);
            builder.append('\"');
        }
        builder.append('>');
        // Add the children.
        final boolean isHtml = html && !(scriptObject instanceof HTMLScriptElement) && !(scriptObject instanceof HTMLStyleElement);
        printChildren(builder, node, isHtml);
        if (null == htmlElement || !htmlElement.isEndTagForbidden()) {
            builder.append("</").append(tag).append('>');
        }
    } else {
        if (node instanceof HtmlElement) {
            final HtmlElement element = (HtmlElement) node;
            if ("p".equals(element.getTagName())) {
                if (getBrowserVersion().hasFeature(JS_INNER_HTML_LF)) {
                    // \r\n because it's to implement something IE specific
                    builder.append('\n');
                } else {
                    int i = builder.length() - 1;
                    while (i >= 0 && Character.isWhitespace(builder.charAt(i))) {
                        i--;
                    }
                    builder.setLength(i + 1);
                    builder.append('\n');
                }
            }
            if (!"script".equals(element.getTagName())) {
                printChildren(builder, node, html);
            }
        }
    }
}
Also used : DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLTemplateElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData)

Example 4 with HTMLStyleElement

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

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement 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

HTMLStyleElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 Selector (com.gargoylesoftware.css.parser.selector.Selector)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 HtmlStyle (com.gargoylesoftware.htmlunit.html.HtmlStyle)3 Test (org.junit.Test)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)2 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)1 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)1 DomCharacterData (com.gargoylesoftware.htmlunit.html.DomCharacterData)1 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)1 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)1 HTMLScriptElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement)1 HTMLTemplateElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1