From: <rb...@us...> - 2013-02-02 09:49:13
|
Revision: 8083 http://sourceforge.net/p/htmlunit/code/8083 Author: rbri Date: 2013-02-02 09:49:10 +0000 (Sat, 02 Feb 2013) Log Message: ----------- fixes for handling of empty prefix/postfix/substring selector 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-02-02 08:20:41 UTC (rev 8082) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-02-02 09:49:10 UTC (rev 8083) @@ -436,15 +436,18 @@ static boolean selects(final BrowserVersion browserVersion, final Condition condition, final DomElement element) { if (condition instanceof PrefixAttributeConditionImpl) { final AttributeCondition ac = (AttributeCondition) condition; - return element.getAttribute(ac.getLocalName()).startsWith(ac.getValue()); + final String value = ac.getValue(); + return !"".equals(value) && element.getAttribute(ac.getLocalName()).startsWith(value); } if (condition instanceof SuffixAttributeConditionImpl) { final AttributeCondition ac = (AttributeCondition) condition; - return element.getAttribute(ac.getLocalName()).endsWith(ac.getValue()); + final String value = ac.getValue(); + return !"".equals(value) && element.getAttribute(ac.getLocalName()).endsWith(value); } if (condition instanceof SubstringAttributeConditionImpl) { final AttributeCondition ac = (AttributeCondition) condition; - return element.getAttribute(ac.getLocalName()).contains(ac.getValue()); + final String value = ac.getValue(); + return !"".equals(value) && element.getAttribute(ac.getLocalName()).contains(value); } switch (condition.getConditionType()) { case Condition.SAC_ID_CONDITION: 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 08:20:41 UTC (rev 8082) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-02-02 09:49:10 UTC (rev 8083) @@ -340,6 +340,32 @@ * @throws Exception if an error occurs */ @Test + @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" + + "<script>\n" + + "function test() {\n" + + " if (document.querySelectorAll) {\n" + + " var list = document.querySelectorAll('[id^=\"\"]');\n" + + " alert(list.length);\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + " <div></div>\n" + + " <ul id='something'></ul>\n" + + " <p></p>\n" + + " <ul id='thing1'></ul>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test @Alerts({ "1", "something" }) public void suffixAttribute() throws Exception { final String html = "<html><head><title>First</title>\n" @@ -367,6 +393,32 @@ * @throws Exception if an error occurs */ @Test + @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" + + "<script>\n" + + "function test() {\n" + + " if (document.querySelectorAll) {\n" + + " var list = document.querySelectorAll('[id$=\"\"]');\n" + + " alert(list.length);\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + " <div></div>\n" + + " <ul id='something'></ul>\n" + + " <p></p>\n" + + " <ul id='thing2'></ul>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test @Alerts({ "2", "something", "thing2" }) public void substringAttribute() throws Exception { final String html = "<html><head><title>First</title>\n" @@ -391,10 +443,36 @@ loadPageWithAlerts2(html); } + /** * @throws Exception if an error occurs */ @Test + @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" + + "<script>\n" + + "function test() {\n" + + " if (document.querySelectorAll) {\n" + + " var list = document.querySelectorAll('[id*=\"\"]');\n" + + " alert(list.length);\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + " <div></div>\n" + + " <ul id='something'></ul>\n" + + " <p></p>\n" + + " <ul id='thing2'></ul>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + /** + * @throws Exception if an error occurs + */ + @Test @Alerts({ "1", "ul2" }) public void generalAdjacentSelector() throws Exception { final String html = "<html><head><title>First</title>\n" |