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));
}
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());
}
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(""");
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);
}
}
}
}
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));
}
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));
}
Aggregations