From: <rb...@us...> - 2017-06-12 17:45:32
|
Revision: 14596 http://sourceforge.net/p/htmlunit/code/14596 Author: rbri Date: 2017-06-12 17:45:29 +0000 (Mon, 12 Jun 2017) Log Message: ----------- fix Array out of bounds Exception in insertBefore implementation when calling without any parameters. Issue 1891 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-06-11 14:26:06 UTC (rev 14595) +++ trunk/htmlunit/src/changes/changes.xml 2017-06-12 17:45:29 UTC (rev 14596) @@ -14,6 +14,9 @@ </action> </release> <release version="2.27" date="June 4, 2017" description="FF52, Bugfixes"> + <action type="fix" dev="rbri" issue="1891"> + JavaScript: fix Array out of bounds Exception in insertBefore implementation when calling without any parameters. + </action> <action type="add" dev="asashour" issue="44336828" system="stackoverflow"> JavaScript: implement Array.from(). </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node.java 2017-06-11 14:26:06 UTC (rev 14595) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node.java 2017-06-12 17:45:29 UTC (rev 14596) @@ -49,6 +49,7 @@ import net.sourceforge.htmlunit.corejs.javascript.Interpreter; import net.sourceforge.htmlunit.corejs.javascript.JavaScriptException; import net.sourceforge.htmlunit.corejs.javascript.RhinoException; +import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime; import net.sourceforge.htmlunit.corejs.javascript.Scriptable; import net.sourceforge.htmlunit.corejs.javascript.Undefined; @@ -294,6 +295,11 @@ * @return the newly added child node */ protected Object insertBeforeImpl(final Object[] args) { + if (args.length < 1) { + throw ScriptRuntime.constructError("TypeError", + "Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 0 present."); + } + final Object newChildObject = args[0]; final Object refChildObject; if (args.length > 1) { @@ -310,17 +316,15 @@ // is the node allowed here? if (!isNodeInsertable(newChild)) { - throw asJavaScriptException( - new DOMException("Node cannot be inserted at the specified point in the hierarchy", - DOMException.HIERARCHY_REQUEST_ERR)); + throw ScriptRuntime.constructError("ReferenceError", + "Node cannot be inserted at the specified point in the hierarchy"); } if (newChildNode instanceof DomDocumentFragment) { final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode; for (final DomNode child : fragment.getChildren()) { if (!isNodeInsertable((Node) child.getScriptableObject())) { - throw asJavaScriptException( - new DOMException("Node cannot be inserted at the specified point in the hierarchy", - DOMException.HIERARCHY_REQUEST_ERR)); + throw ScriptRuntime.constructError("ReferenceError", + "Node cannot be inserted at the specified point in the hierarchy"); } } } @@ -332,7 +336,8 @@ refChildNode = null; } else { - throw Context.reportRuntimeError("insertBefore: not enough arguments"); + throw ScriptRuntime.typeError( + "Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 1 present."); } } else if (refChildObject != null) { @@ -348,9 +353,7 @@ domNode.insertBefore(newChildNode, refChildNode); } catch (final org.w3c.dom.DOMException e) { - throw asJavaScriptException( - new DOMException(e.getMessage(), - DOMException.HIERARCHY_REQUEST_ERR)); + throw ScriptRuntime.constructError("ReferenceError", e.getMessage()); } insertedChild = newChild; } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java 2017-06-11 14:26:06 UTC (rev 14595) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java 2017-06-12 17:45:29 UTC (rev 14596) @@ -762,6 +762,17 @@ * @throws Exception if the test fails */ @Test + @Alerts("exception") + public void insertBefore_noArgs() throws Exception { + insertBefore("aNode.insertBefore();"); + } + + /** + * Regression test to verify that insertBefore correctly appends + * the new child object when the reference child object is null. + * @throws Exception if the test fails + */ + @Test @Alerts(DEFAULT = "exception", IE = {"3", "SPAN"}) public void insertBefore_noSecondArg() throws Exception { |