From: <asa...@us...> - 2012-12-12 10:13:48
|
Revision: 7860 http://sourceforge.net/p/htmlunit/code/7860 Author: asashour Date: 2012-12-12 10:13:44 +0000 (Wed, 12 Dec 2012) Log Message: ----------- JavaScript: fix the value of "(i in x)" for NodeList, HTMLCollection and CSSRuleList. Issue 1456 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NodeList.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleList.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollection.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/NodeListTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleListTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollectionTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/changes/changes.xml 2012-12-12 10:13:44 UTC (rev 7860) @@ -8,6 +8,9 @@ <body> <release version="2.12" date="???" description="Bugfixes"> + <action type="fix" dev="asashour" issue="1456 "> + JavaScript: fix the value of "(i in x)" for NodeList, HTMLCollection and CSSRuleList. + </action> <action type="update" dev="asashour"> HtmlElement: deprecate getElementById() and hasHtmlElementWithId(). </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -579,7 +579,6 @@ } } - /** * Adds the new node to the right parent that is not necessary the currentNode in case * of malformed HTML code. Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NodeList.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NodeList.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NodeList.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -416,6 +416,18 @@ * {@inheritDoc} */ @Override + public boolean has(final int index, final Scriptable start) { + final List<Object> elements = getElements(); + if (index >= 0 && index < elements.size()) { + return true; + } + return false; + } + + /** + * {@inheritDoc} + */ + @Override public boolean has(final String name, final Scriptable start) { // let's Rhino work normally if current instance is the prototype if (isPrototype()) { @@ -423,11 +435,7 @@ } try { - final int index = Integer.parseInt(name); - final List<Object> elements = getElements(); - if (index >= 0 && index < elements.size()) { - return true; - } + return has(Integer.parseInt(name), start); } catch (final NumberFormatException e) { // Ignore. Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleList.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleList.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleList.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -109,6 +109,18 @@ } /** + * {@inheritDoc} + */ + @Override + public boolean has(final int index, final Scriptable start) { + final int length = getLength(); + if (index >= 0 && index < length) { + return true; + } + return false; + } + + /** * {@inheritDoc}. */ @Override @@ -117,11 +129,7 @@ return true; } try { - final int index = Integer.parseInt(name); - final int length = getLength(); - if (index >= 0 && index < length) { - return true; - } + return has(Integer.parseInt(name), start); } catch (final Exception e) { //ignore Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollection.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollection.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollection.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -15,7 +15,6 @@ package com.gargoylesoftware.htmlunit.javascript.host.html; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.GENERATED_48; -import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.GENERATED_49; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.GENERATED_50; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.HTMLCOLLECTION_COMMENT_IS_ELEMENT; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.HTMLCOLLECTION_IDENTICAL_IDS; @@ -347,43 +346,6 @@ } /** - * {@inheritDoc} - */ - @Override - public boolean has(final String name, final Scriptable start) { - // let's Rhino work normally if current instance is the prototype - if (isPrototype()) { - return super.has(name, start); - } - - try { - final int index = Integer.parseInt(name); - final List<Object> elements = getElements(); - if (index >= 0 && index < elements.size()) { - return true; - } - } - catch (final NumberFormatException e) { - // Ignore. - } - - if ("length".equals(name)) { - return true; - } - if (!getBrowserVersion().hasFeature(GENERATED_49)) { - final JavaScriptConfiguration jsConfig = getWindow().getWebWindow().getWebClient() - .getJavaScriptEngine().getJavaScriptConfiguration(); - for (final String functionName : jsConfig.getClassConfiguration(getClassName()).functionKeys()) { - if (name.equals(functionName)) { - return true; - } - } - return false; - } - return getWithPreemption(name) != NOT_FOUND; - } - - /** * {@inheritDoc}. */ @Override Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/NodeListTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/NodeListTest.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/NodeListTest.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -49,4 +49,22 @@ loadPageWithAlerts2(html); } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts("true") + public void has() throws Exception { + final String html = "<html><head><title>test</title>\n" + + "<script>\n" + + " function test(){\n" + + " alert(0 in document.body.parentNode.childNodes);\n" + + " }\n" + + "</script>\n" + + "</head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleListTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleListTest.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSRuleListTest.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -34,7 +34,7 @@ * @throws Exception on test failure */ @Test - @Alerts(FF = "[object CSSStyleRule]", + @Alerts(DEFAULT = "[object CSSStyleRule]", IE = "[object]") public void testRuleList() throws Exception { final String html = "<html><head><title>First</title>\n" @@ -82,4 +82,30 @@ + "</body></html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts("true") + public void has() throws Exception { + final String html = "<html><head><title>First</title>\n" + + "<style>\n" + + " BODY { font-size: 1234px; }\n" + + "</style>\n" + + "<script>\n" + + " function test(){\n" + + " var rules;\n" + + " if (document.styleSheets[0].cssRules)\n" + + " rules = document.styleSheets[0].cssRules;\n" + + " else\n" + + " rules = document.styleSheets[0].rules;\n" + + " alert(0 in rules);\n" + + " }\n" + + "</script>\n" + + "</head><body onload='test()'>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } + } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollectionTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollectionTest.java 2012-12-12 08:40:33 UTC (rev 7859) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLCollectionTest.java 2012-12-12 10:13:44 UTC (rev 7860) @@ -318,4 +318,20 @@ loadPageWithAlerts2(html); } + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("true") + public void has() throws Exception { + final String html = "<html><head><title>foo</title><script>\n" + + " function test() {\n" + + " alert(0 in document.forms);\n" + + " }\n" + + "</script></head><body onload='test()'>\n" + + "<form name='myForm'></form>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } } |