From: <asa...@us...> - 2012-11-17 13:13:30
|
Revision: 7733 http://sourceforge.net/p/htmlunit/code/7733 Author: asashour Date: 2012-11-17 13:13:26 +0000 (Sat, 17 Nov 2012) Log Message: ----------- JavaScript: prevent infinite loop during adding a DomNode to itself. Issue 1253 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/DomNodeTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2012-11-16 10:30:59 UTC (rev 7732) +++ trunk/htmlunit/src/changes/changes.xml 2012-11-17 13:13:26 UTC (rev 7733) @@ -8,6 +8,9 @@ <body> <release version="2.12" date="???" description="Bugfixes"> + <action type="fix" dev="asashour" issue="1253"> + JavaScript: prevent infinite loop during adding a DomNode to itself. + </action> <action type="fix" dev="mguillem"> JavaScript: don't enumerate function properties "arguments" and "caller". </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2012-11-16 10:30:59 UTC (rev 7732) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2012-11-17 13:13:26 UTC (rev 7733) @@ -1073,6 +1073,10 @@ @BrowserFeature(@WebBrowser(FF)) KEYBOARD_EVENT_SPECIAL_KEYPRESS, + /** If true, then silently ignore element.appendChild(element); */ + @BrowserFeature(@WebBrowser(IE)) + NODE_APPEND_CHILD_SELF_IGNORE, + /** Body of a <noscript> tag is not totally ignored but considered as a (not displayed) text node. */ @BrowserFeature({ @WebBrowser(FF), @WebBrowser(CHROME) }) NOSCRIPT_BODY_AS_TEXT, Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java 2012-11-16 10:30:59 UTC (rev 7732) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java 2012-11-17 13:13:26 UTC (rev 7733) @@ -16,6 +16,7 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.DISPLAYED_COLLAPSE; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.DOM_NORMALIZE_REMOVE_CHILDREN; +import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.NODE_APPEND_CHILD_SELF_IGNORE; import java.io.IOException; import java.io.PrintWriter; @@ -30,6 +31,7 @@ import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicBoolean; +import net.sourceforge.htmlunit.corejs.javascript.Context; import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject; import org.w3c.css.sac.CSSException; @@ -875,6 +877,12 @@ * {@inheritDoc} */ public DomNode appendChild(final Node node) { + if (node == this) { + if (!hasFeature(NODE_APPEND_CHILD_SELF_IGNORE)) { + Context.throwAsScriptRuntimeEx(new Exception("Can not add not to itself " + this)); + } + return this; + } final DomNode domNode = (DomNode) node; if (domNode instanceof DomDocumentFragment) { Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/DomNodeTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/DomNodeTest.java 2012-11-16 10:30:59 UTC (rev 7732) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/DomNodeTest.java 2012-11-17 13:13:26 UTC (rev 7733) @@ -764,4 +764,28 @@ assertTrue(page.getElementById("d3").isDisplayed()); } + /** + * Test for Bug #1253. + * + * @throws Exception on test failure + */ + @Test + @Alerts(FF = { "exception", "0" }, IE = {"true", "0" }) + public void appendChild_recursive() throws Exception { + final String html = "<html><head><title>foo</title>\n" + + "<script>\n" + + "function test(){\n" + + " var e = document.createElement('div');\n" + + " try {\n" + + " alert(e.appendChild(e) === e);\n" + + " } catch(e) {alert('exception');}\n" + + " alert(e.childNodes.length);" + + "}\n" + + "</script>\n" + + "</head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts(html); + } + } |