Skip to content

Commit 844e3e5

Browse files
Fix outerHTML setter for DocumentFragment parent per spec
Setting outerHTML on an element whose parent is a DocumentFragment was incorrectly throwing a NoModificationAllowedError. Per the HTML spec[0], the outerHTML setter should only throw when the parent is a Document. When the parent is a DocumentFragment, the spec requires using a temporary body element as the context for fragment parsing. Added web tests for DocumentFragment, Document, and no-parent cases. [0] https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-outerhtml-property Fixed: 40885158 Change-Id: I79169dfbef86fbf4b6dc4d133fe90c13ca938857 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7530588 Reviewed-by: Fernando Fiori <ffiori@microsoft.com> Reviewed-by: Joey Arhar <jarhar@chromium.org> Commit-Queue: Stephanie Zhang <stephanie.zhang@microsoft.com> Cr-Commit-Position: refs/heads/main@{#1611702}
1 parent a3cfe4c commit 844e3e5

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<title>outerHTML: child of DocumentFragment</title>
3+
<link rel="help" href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-outerhtml-property">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script>
7+
test(() => {
8+
const fragment = new DocumentFragment();
9+
const div = document.createElement("div");
10+
div.textContent = "original";
11+
fragment.appendChild(div);
12+
13+
div.outerHTML = "<span>replaced</span>";
14+
15+
assert_equals(fragment.childNodes.length, 1, "Fragment should have one child");
16+
assert_equals(fragment.firstChild.tagName, "SPAN", "Child should be a SPAN");
17+
assert_equals(fragment.firstChild.textContent, "replaced", "Content should be 'replaced'");
18+
}, "outerHTML replaces element within DocumentFragment parent.");
19+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<title>outerHTML: element with no parent</title>
3+
<link rel="help" href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-outerhtml-property">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script>
7+
test(() => {
8+
const el = document.createElement("div");
9+
el.textContent = "original";
10+
el.outerHTML = "<p>replacement</p>";
11+
assert_equals(el.tagName, "DIV");
12+
assert_equals(el.textContent, "original");
13+
}, "Setting outerHTML on element with no parent should be a no-op.");
14+
</script>

0 commit comments

Comments
 (0)