Open Bug 1713492 Opened 5 years ago Updated 1 year ago

Setting element.outerHTML doesn't throw a proper exception when the element's parent is null

Categories

(Core :: DOM: Core & HTML, enhancement)

Firefox 88
enhancement

Tracking

()

People

(Reporter: zyscoder, Unassigned)

References

()

Details

(Keywords: spec-needed)

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Steps to reproduce:

(1) Open a tab and navigate any URL (In my test is https://www.un.org/);
(2) Run the following code in the Console of Devtools:

a = document.getElementsByTagName('head')[0];
a.replaceWith([]);
a.parentNode;
a.outerHTML = "";

(3) Then this code would be evaluated successfully without throwing any exceptions.

Actual results:

This code would be evaluated successfully without throwing any exceptions.

Expected results:

In this code, the result of a.parentNode is null. As https://w3c.github.io/DOM-Parsing/#dfn-context-object says, If parent is null, terminate these steps. There would be no way to obtain a reference to the nodes created even if the remaining steps were run. Thus proper exception should be thrown for Firefox to running this code.
In fact, Chrome and Webkit would throw exceptions when evaluating this code:
Chrome: VM1851:1 Uncaught DOMException: Failed to set the 'outerHTML' property on 'Element': This element has no parent node.
Webkit: NoModificationAllowedError: The object can not be modified.

Setting a component for this enhancement in order to get the dev team involved.
If you feel it's an incorrect one please feel free to change it to a more appropriate one.

Status: UNCONFIRMED → NEW
Component: Untriaged → DOM: Core & HTML
Ever confirmed: true
Product: Firefox → Core

(In reply to zyscoder from comment #0)

In this code, the result of a.parentNode is null. As https://w3c.github.io/DOM-Parsing/#dfn-context-object says, If parent is null, terminate these steps. There would be no way to obtain a reference to the nodes created even if the remaining steps were run. Thus proper exception should be thrown for Firefox to running this code.

No, the spec says to terminate the steps. That means not throwing. Therefore, the other engines don't comply.

However, given that 2 out of three engines throw, it might be worthwhile to change the spec (and Gecko here).

Anne, what do you think?

Flags: needinfo?(annevk)

Yeah, that sounds good to me. Per https://github.com/w3c/DOM-Parsing/issues/70 we might also have to merge Text nodes. (That can probably share code with outerText?)

Flags: needinfo?(annevk)

Henri, want to take this?

Flags: needinfo?(hsivonen)

(In reply to Anne (:annevk) from comment #4)

Henri, want to take this?

Not right away. I'm leaving myself needinfoed in order to come back to this in a couple of months, but if someone wants to take this sooner, please do so.

There was work on this recently on webkit
https://trac.webkit.org/changeset/278821/webkit/

You need to log in before you can comment on or make changes to this bug.