<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>249737</bug_id>
          
          <creation_ts>2022-12-21 12:15:41 -0800</creation_ts>
          <short_desc>Setting outerHTML on child of DocumentFragment throws error</short_desc>
          <delta_ts>2023-07-15 15:06:53 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>DOM</component>
          <version>Safari Technology Preview</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=226808</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>BrowserCompat, InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Nolan Lawson">nlawson</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ahmad.saleem792</cc>
    
    <cc>cdumez</cc>
    
    <cc>jarhar</cc>
    
    <cc>karlcow</cc>
    
    <cc>rniwa</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1921178</commentid>
    <comment_count>0</comment_count>
      <attachid>464152</attachid>
    <who name="Nolan Lawson">nlawson</who>
    <bug_when>2022-12-21 12:15:41 -0800</bug_when>
    <thetext>Created attachment 464152
Repro HTML

Steps to repro:

1. Go to https://codepen.io/nolanlawson-the-selector/pen/oNMgmXe
2. Notice that it says an error was thrown

Minimal repro:

    const fragment = new DocumentFragment()
    fragment.appendChild(document.createElement(&apos;div&apos;))
    fragment.firstChild.outerHTML = &apos;&apos;

In Firefox this does not throw an Error. In Safari Technology Preview Release 146 (Safari 15.4, WebKit 16614.1.14.10.6), it throws an error:

&gt; Cannot set outerHTML on element because its parent is not an Element

According to the spec [1]:

&gt; If parent is a DocumentFragment, let parent be a new Element […]

So it appears that Firefox is following the spec here, but Safari is not.

Note that my repro tests both a DocumentFragment and a ShadowRoot. Since a ShadowRoot is an instance of DocumentFragment, neither test should throw an error.

Same bug filed on Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=1403060

[1]: https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1921614</commentid>
    <comment_count>1</comment_count>
    <who name="Karl Dubost">karlcow</who>
    <bug_when>2022-12-22 22:57:04 -0800</bug_when>
    <thetext>Blink error:

&gt; Uncaught DOMException: Failed to set the &apos;outerHTML&apos; property on &apos;Element&apos;: This element&apos;s parent is of type &apos;#document-fragment&apos;, which is not an element node.
&gt;    at &lt;anonymous&gt;:1:31



WebKit error:
&gt; nomodificationallowederror: Cannot set outerHTML on element because its parent is not an Element


Firefox returns:
&apos;&apos;


https://searchfox.org/wubkat/rev/9ced63bbda6ac9231451b1ca549a16f397e28c78/Source/WebCore/dom/Element.cpp#3554-3589

```
ExceptionOr&lt;void&gt; Element::setOuterHTML(const String&amp; html)
{
    // The specification allows setting outerHTML on an Element whose parent is a DocumentFragment and Gecko supports this.
    // However, as of June 2021, Blink matches our behavior and throws a NoModificationAllowedError for non-Element parents.
    RefPtr parent = parentElement();
    if (UNLIKELY(!parent)) {
        if (!parentNode())
            return Exception { NoModificationAllowedError, &quot;Cannot set outerHTML on element because it doesn&apos;t have a parent&quot;_s };
        return Exception { NoModificationAllowedError, &quot;Cannot set outerHTML on element because its parent is not an Element&quot;_s };
    }


// … cut for brevity

}

```

This was added by Chris Dumez in June 2021
https://bugs.webkit.org/show_bug.cgi?id=226808
https://github.com/WebKit/WebKit/commit/dc33e397532ef8140d9e3fa80b7723e16b863746</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1922090</commentid>
    <comment_count>2</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-12-28 12:16:16 -0800</bug_when>
    <thetext>&lt;rdar://problem/103746193&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1922187</commentid>
    <comment_count>3</comment_count>
    <who name="Joey Arhar">jarhar</who>
    <bug_when>2022-12-29 08:59:33 -0800</bug_when>
    <thetext>I opened a spec issue: https://github.com/whatwg/html/issues/8657</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1965002</commentid>
    <comment_count>4</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2023-07-04 09:41:13 -0700</bug_when>
    <thetext>(In reply to Joey Arhar from comment #3)
&gt; I opened a spec issue: https://github.com/whatwg/html/issues/8657

As per following, Safari should align with Gecko behavior and should not throw.

So I think we might need to revert Chris&apos;s patch, which made us match Blink.

https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1965138</commentid>
    <comment_count>5</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2023-07-05 09:21:52 -0700</bug_when>
    <thetext>(In reply to Ahmad Saleem from comment #4)
&gt; (In reply to Joey Arhar from comment #3)
&gt; &gt; I opened a spec issue: https://github.com/whatwg/html/issues/8657
&gt; 
&gt; As per following, Safari should align with Gecko behavior and should not
&gt; throw.
&gt; 
&gt; So I think we might need to revert Chris&apos;s patch, which made us match Blink.
&gt; 
&gt; https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml

Yes, looks like Blink agreed on the spec issue to stop throwing so we should do the same. Do you want to do it or should I?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1965159</commentid>
    <comment_count>6</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2023-07-05 10:54:04 -0700</bug_when>
    <thetext>PR with revert + not throw error: https://github.com/WebKit/WebKit/pull/15575</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1966924</commentid>
    <comment_count>7</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2023-07-15 15:06:51 -0700</bug_when>
    <thetext>Committed 266086@main (b41af45ee409): &lt;https://commits.webkit.org/266086@main&gt;

Reviewed commits have been landed. Closing PR #15575 and removing active labels.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>464152</attachid>
            <date>2022-12-21 12:15:41 -0800</date>
            <delta_ts>2022-12-21 12:15:41 -0800</delta_ts>
            <desc>Repro HTML</desc>
            <filename>repro.html</filename>
            <type>text/html</type>
            <size>720</size>
            <attacher name="Nolan Lawson">nlawson</attacher>
            
              <data encoding="base64">PCFkb2N0eXBlIGh0bWw+CjxodG1sPgo8cHJlIGlkPWRpc3BsYXk+PC9wcmU+CjxzY3JpcHQgdHlw
ZT1tb2R1bGU+CgpmdW5jdGlvbiB0ZXN0KGNhbGxiYWNrKSB7CiAgdHJ5IHsKICAgIGNhbGxiYWNr
KCkKICAgIHJldHVybiAnTm8gZXJyb3InCiAgfSBjYXRjaCAoZXJyKSB7CiAgICByZXR1cm4gZXJy
Lm1lc3NhZ2UKICB9Cn0KCmNvbnN0IHJlczEgPSB0ZXN0KCgpID0+IHsKICBjb25zdCBmcmFnbWVu
dCA9IG5ldyBEb2N1bWVudEZyYWdtZW50KCkKICBmcmFnbWVudC5hcHBlbmRDaGlsZChkb2N1bWVu
dC5jcmVhdGVFbGVtZW50KCdkaXYnKSkKICBmcmFnbWVudC5maXJzdENoaWxkLm91dGVySFRNTCA9
ICcnCn0pCgpjb25zdCByZXMyID0gdGVzdCgoKSA9PiB7CiAgY29uc3QgY29udGFpbmVyID0gZG9j
dW1lbnQuY3JlYXRlRWxlbWVudCgnZGl2JykKICBjb250YWluZXIuYXR0YWNoU2hhZG93KHttb2Rl
OiAnb3Blbid9KS5hcHBlbmRDaGlsZChkb2N1bWVudC5jcmVhdGVFbGVtZW50KCdkaXYnKSkKICBj
b250YWluZXIuc2hhZG93Um9vdC5maXJzdENoaWxkLm91dGVySFRNTCA9ICcnCn0pCgoKZGlzcGxh
eS5pbm5lckhUTUwgPSBgClNldHRpbmcgb3V0ZXJIVE1MIG9uIGNoaWxkIG9mIERvY3VtZW50RnJh
Z21lbnQ6ICR7cmVzMX0KU2V0dGluZyBvdXRlckhUTUwgb24gY2hpbGQgb2YgU2hhZG93Um9vdDog
JHtyZXMyfQpgLnRyaW0oKQoKPC9zY3JpcHQ+CjwvaHRtbD4K
</data>

          </attachment>
      

    </bug>

</bugzilla>