Skip to content

Commit

Permalink
DomNode.appendChild() throws a HierarchyRequstError
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 2, 2025
1 parent c0cfd79 commit 1086364
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="4.10.0" date="February xx, 2025" description="Bugfixes">
<action type="fix" dev="rbri">
DomNode.appendChild() throws a HierarchyRequstError.
</action>
<action type="fix" dev="rbri">
AbstractRange.ctor() throws a TypeError.
</action>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/htmlunit/html/DomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.htmlunit.html.serializer.HtmlSerializerVisibleText;
import org.htmlunit.html.xpath.XPathHelper;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.host.event.Event;
import org.htmlunit.xpath.xml.utils.PrefixResolver;
import org.w3c.dom.DOMException;
Expand Down Expand Up @@ -908,11 +907,11 @@ public <T extends HtmlUnitScriptable> T getScriptableObject() {
@Override
public DomNode appendChild(final Node node) {
if (node == this) {
throw JavaScriptEngine.throwAsScriptRuntimeEx(new Exception("Can not add not to itself " + this));
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Can not add not to itself " + this);
}
final DomNode domNode = (DomNode) node;
if (domNode.isAncestorOf(this)) {
throw JavaScriptEngine.throwAsScriptRuntimeEx(new Exception("Can not add (grand)parent to itself " + this));
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Can not add (grand)parent to itself " + this);
}

if (domNode instanceof DomDocumentFragment) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/htmlunit/javascript/host/dom/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,14 @@ public Node appendChild(final Object childObject) {
final DomNode parentNode = getDomNodeOrDie();

// Append the child to the parent node
parentNode.appendChild(childDomNode);
try {
parentNode.appendChild(childDomNode);
}
catch (final org.w3c.dom.DOMException e) {
throw JavaScriptEngine.asJavaScriptException(
getWindow(),
new DOMException(e.getMessage(), e.code));
}

initInlineFrameIfNeeded(childDomNode);
for (final HtmlElement htmlElement : childDomNode.getHtmlElementDescendants()) {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/htmlunit/html/DomNode2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @author Chris Erskine
* @author Ahmed Ashour
* @author Ronald Brill
*/
@RunWith(BrowserRunner.class)
public class DomNode2Test extends WebDriverTestCase {
Expand Down

0 comments on commit 1086364

Please sign in to comment.