From: <mgu...@us...> - 2013-03-21 15:17:51
|
Revision: 8181 http://sourceforge.net/p/htmlunit/code/8181 Author: mguillem Date: 2013-03-21 15:17:47 +0000 (Thu, 21 Mar 2013) Log Message: ----------- CSS3 pseudo selector "empty" should select nodes containing only a comment Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-03-21 11:55:44 UTC (rev 8180) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-03-21 15:17:47 UTC (rev 8181) @@ -79,6 +79,7 @@ import com.gargoylesoftware.htmlunit.html.DisabledElement; import com.gargoylesoftware.htmlunit.html.DomElement; import com.gargoylesoftware.htmlunit.html.DomNode; +import com.gargoylesoftware.htmlunit.html.DomText; import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlHtml; @@ -692,7 +693,7 @@ return true; } else if ("empty".equals(value)) { - return element.getFirstChild() == null; + return isEmpty(element); } else if ("target".equals(value)) { final String ref = element.getPage().getUrl().getRef(); @@ -733,6 +734,15 @@ return false; } + private static boolean isEmpty(final DomElement element) { + for (DomNode n = element.getFirstChild(); n != null; n = n.getNextSibling()) { + if (n instanceof DomElement || n instanceof DomText) { + return false; + } + } + return true; + } + private static boolean getNth(final String nth, final int index) { if ("odd".equalsIgnoreCase(nth)) { return index % 2 != 0; Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-03-21 11:55:44 UTC (rev 8180) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-03-21 15:17:47 UTC (rev 8181) @@ -862,7 +862,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = "id2", IE8 = "exception") + @Alerts(DEFAULT = { "id2", "span1" }, IE8 = "exception") public void empty() throws Exception { final String html = "<html><head><title>First</title>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" @@ -871,6 +871,7 @@ + " if (document.querySelectorAll) {\n" + " try {\n" + " alert(document.querySelectorAll('p:empty')[0].id);\n" + + " alert(document.querySelectorAll('span:empty')[0].id);\n" + " } catch(e) {alert('exception')}\n" + " }\n" + "}\n" @@ -878,6 +879,8 @@ + "<body onload='test()'>\n" + " <p id='id1'>Hello, World!</p>\n" + " <p id='id2'></p>\n" + + " <span id='span1'><!-- a comment --></span>\n" + + " <span id='span2'>a text</span>\n" + "</body></html>"; loadPageWithAlerts2(html); |