[whatwg/dom] Browsers don't throw when using replaceChild() to replace existing doctype (Issue #1327)

### What is the issue with the DOM Standard?

The spec for [replaceChild](https://dom.spec.whatwg.org/#concept-node-replace) currently has this:

> If parent is a [document](https://dom.spec.whatwg.org/#concept-document), and any of the statements below, switched on the interface node [implements](https://webidl.spec.whatwg.org/#implements), are true, then [throw](https://webidl.spec.whatwg.org/#dfn-throw) a "[HierarchyRequestError](https://webidl.spec.whatwg.org/#hierarchyrequesterror)" [DOMException](https://webidl.spec.whatwg.org/#idl-DOMException).
> ...
> --> [DocumentType](https://dom.spec.whatwg.org/#documenttype)
parent has a [doctype](https://dom.spec.whatwg.org/#concept-doctype) [child](https://dom.spec.whatwg.org/#concept-tree-child) that is not child, or an [element](https://dom.spec.whatwg.org/#concept-element) is [preceding](https://dom.spec.whatwg.org/#concept-tree-preceding) child.

However, all major browsers appear to allow replacement of an existing doctype node by another doctype node like so:

```html
<script>
    let doc1 = document.implementation.createHTMLDocument("doc1");
    let doc2 = document.implementation.createHTMLDocument("doc2");
    doc1.replaceChild(doc2.doctype, doc1.doctype);
</script>
```

Based on the spec, I would have expected this to throw. Chrome, Firefox and Safari all happily yoink the doctype from `doc2` to `doc1`.

WPT has at least one test that relies on the actual behavior of browsers (subtest "If the context node is a document, inserting a new doctype should work."):
http://wpt.live/dom/nodes/Node-replaceChild.html



-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1327
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1327@github.com>

Received on Wednesday, 20 November 2024 11:15:29 UTC