From: <rb...@us...> - 2013-02-02 18:06:58
|
Revision: 8085 http://sourceforge.net/p/htmlunit/code/8085 Author: rbri Date: 2013-02-02 18:06:54 +0000 (Sat, 02 Feb 2013) Log Message: ----------- Support for CSS pseudo selector ':target' added. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml 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/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-02-02 17:14:13 UTC (rev 8084) +++ trunk/htmlunit/src/changes/changes.xml 2013-02-02 18:06:54 UTC (rev 8085) @@ -8,6 +8,9 @@ <body> <release version="2.12" date="???" description="Bugfixes, CSS3 Selectors"> + <action type="fix" dev="rbri"> + Support for CSS pseudo selector ':target' added. + </action> <action type="add" dev="rbri"> JavaScript: add DocumentFragment.querySelectorAll()/DocumentFragment.querySelector(). </action> 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-02-02 17:14:13 UTC (rev 8084) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-02-02 18:06:54 UTC (rev 8085) @@ -686,6 +686,10 @@ else if ("empty".equals(value)) { return element.getFirstChild() == null; } + else if ("target".equals(value)) { + final String ref = element.getPage().getUrl().getRef(); + return StringUtils.isNotBlank(ref) && ref.equals(element.getId()); + } else if (value.startsWith("not(")) { final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1); final AtomicBoolean errorOccured = new AtomicBoolean(false); 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-02-02 17:14:13 UTC (rev 8084) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-02-02 18:06:54 UTC (rev 8085) @@ -24,6 +24,7 @@ import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented; import com.gargoylesoftware.htmlunit.WebDriverTestCase; import com.gargoylesoftware.htmlunit.html.HtmlPageTest; +import com.gargoylesoftware.htmlunit.util.UrlUtils; /** * Tests for CSS selectors. @@ -340,7 +341,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({ "0" }) + @Alerts("0") public void prefixAttributeEmpty() throws Exception { final String html = "<html><head><title>First</title>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=8'>\n" @@ -393,7 +394,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({ "0" }) + @Alerts("0") public void suffixAttributeEmpty() throws Exception { final String html = "<html><head><title>First</title>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=8'>\n" @@ -447,7 +448,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({ "0" }) + @Alerts("0") public void substringAttributeEmpty() throws Exception { final String html = "<html><head><title>First</title>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=8'>\n" @@ -501,7 +502,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({ "0" }) + @Alerts("0") public void oneOfAttributeEmpty() throws Exception { final String html = "<html><head><title>First</title>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=8'>\n" @@ -1000,4 +1001,86 @@ loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "1", "id2" }, IE = "exception") + public void target() throws Exception { + final String html = "<html><head><title>First</title>\n" + + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" + + "<script>\n" + + "function test() {\n" + + " if (document.querySelectorAll) {\n" + + " try {\n" + + " found = document.querySelectorAll(':target');\n" + + " alert(found.length);\n" + + " alert(found[0].id);\n" + + " } catch(e) {alert('exception')}\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + " <input id='id1' >\n" + + " <input id='id2'>\n" + + "</body></html>"; + + getMockWebConnection().setDefaultResponse(html); + loadPageWithAlerts2(UrlUtils.getUrlWithNewRef(URL_FIRST, "id2")); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "0" }, IE = "exception") + public void targetNoHash() throws Exception { + final String html = "<html><head><title>First</title>\n" + + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" + + "<script>\n" + + "function test() {\n" + + " if (document.querySelectorAll) {\n" + + " try {\n" + + " found = document.querySelectorAll(':target');\n" + + " alert(found.length);\n" + + " } catch(e) {alert('exception')}\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + " <input id='id1' >\n" + + " <input id='id2'>\n" + + "</body></html>"; + + getMockWebConnection().setDefaultResponse(html); + loadPageWithAlerts2(URL_FIRST); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "0" }, IE = "exception") + public void targetUnknown() throws Exception { + final String html = "<html><head><title>First</title>\n" + + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" + + "<script>\n" + + "function test() {\n" + + " if (document.querySelectorAll) {\n" + + " try {\n" + + " found = document.querySelectorAll(':target');\n" + + " alert(found.length);\n" + + " } catch(e) {alert('exception')}\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + " <input id='id1' >\n" + + " <input id='id2'>\n" + + "</body></html>"; + + getMockWebConnection().setDefaultResponse(html); + loadPageWithAlerts2(UrlUtils.getUrlWithNewRef(URL_FIRST, "id3")); + } } |