From: <rb...@us...> - 2013-02-02 08:20:44
|
Revision: 8082 http://sourceforge.net/p/htmlunit/code/8082 Author: rbri Date: 2013-02-02 08:20:41 +0000 (Sat, 02 Feb 2013) Log Message: ----------- another nth selector fix 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-01 19:13:04 UTC (rev 8081) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-02-02 08:20:41 UTC (rev 8082) @@ -728,10 +728,15 @@ int a = 0; if (nIndex != -1) { String value = nth.substring(0, nIndex).trim(); - if (value.startsWith("+")) { - value = value.substring(1); + if ("-".equals(value)) { + a = -1; } - a = NumberUtils.toInt(value, 1); + else { + if (value.startsWith("+")) { + value = value.substring(1); + } + a = NumberUtils.toInt(value, 1); + } } String value = nth.substring(nIndex + 1).trim(); @@ -740,9 +745,11 @@ } final int b = NumberUtils.toInt(value, 0); if (a == 0) { - return index == b; + return index == b && b > 0; } - return (index - b) % a == 0; + + final double n = (index - b) / (double)a; + return (n >= 0) && (n % 1 == 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-02-01 19:13:04 UTC (rev 8081) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-02-02 08:20:41 UTC (rev 8082) @@ -105,7 +105,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "li2", "li1", "li2", "li1", "li3", "li1" }, IE8 = "exception") + @Alerts(DEFAULT = { "li2", "li1", "li2", "li1", "li3", "li1", "2", "li1", "li2" }, 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" @@ -119,6 +119,10 @@ + " 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" + + + " alert(document.querySelectorAll('li:nth-child(-n+2)').length);\n" + + " alert(document.querySelectorAll('li:nth-child(-n+2)')[0].id);\n" + + " alert(document.querySelectorAll('li:nth-child(-n+2)')[1].id);\n" + " } catch(e) {alert('exception')}\n" + " }\n" + "}\n" |