From: <rb...@us...> - 2013-01-31 18:32:14
|
Revision: 8069 http://sourceforge.net/p/htmlunit/code/8069 Author: rbri Date: 2013-01-31 18:32:08 +0000 (Thu, 31 Jan 2013) Log Message: ----------- css selector parser fixes 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-01-31 14:44:26 UTC (rev 8068) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-01-31 18:32:08 UTC (rev 8069) @@ -43,6 +43,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.css.sac.AttributeCondition; @@ -724,22 +725,20 @@ // an+b final int nIndex = nth.indexOf('n'); - final int a; + int a = 0; if (nIndex != -1) { String value = nth.substring(0, nIndex).trim(); if (value.startsWith("+")) { value = value.substring(1); } - a = Integer.parseInt(value); + a = NumberUtils.toInt(value, 1); } - else { - a = 0; - } + String value = nth.substring(nIndex + 1).trim(); if (value.startsWith("+")) { value = value.substring(1); } - final int b = Integer.parseInt(value); + final int b = NumberUtils.toInt(value, 0); if (a == 0) { return index == b; } 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-01-31 14:44:26 UTC (rev 8068) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-01-31 18:32:08 UTC (rev 8069) @@ -41,7 +41,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = "li2", IE8 = "exception") + @Alerts(DEFAULT = { "li2", "li1", "li2", "li1", "li3", "li1" }, IE8 = "exception") public void nth_child() throws Exception { final String html = "<html><head><title>First</title>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" @@ -50,6 +50,11 @@ + " if (document.querySelectorAll) {\n" + " try {\n" + " alert(document.querySelectorAll('li:nth-child(2)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(n)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(2n)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(2n+1)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(2n+1)')[1].id);\n" + + " alert(document.querySelectorAll('li:nth-child(2n-1)')[0].id);\n" + " } catch(e) {alert('exception')}\n" + " }\n" + "}\n" @@ -69,6 +74,38 @@ * @throws Exception if an error occurs */ @Test + @Alerts(DEFAULT = { "1", "li2", "2", "li1", "li3" }, IE8 = "exception") + public void nth_child_even_odd() 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" + + " alert(document.querySelectorAll('li:nth-child(even)').length);\n" + + " alert(document.querySelectorAll('li:nth-child(eVen)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(odd)').length);\n" + + " alert(document.querySelectorAll('li:nth-child(OdD)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(ODD)')[1].id);\n" + + " } catch(e) {alert('exception')}\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + "<ul>\n" + + " <li id='li1'></li>\n" + + " <li id='li2'></li>\n" + + " <li id='li3'></li>\n" + + "</ul>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test @Alerts("exception") public void nth_child_no_argument() throws Exception { final String html |