<?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>277440</bug_id>
          
          <creation_ts>2024-07-31 12:11:31 -0700</creation_ts>
          <short_desc>XMLSerializer.serializeToString() not serializing child(s) of &lt;img&gt; and also not closing the &lt;img&gt; if it has child(s)</short_desc>
          <delta_ts>2024-08-26 01:59:24 -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 17</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://github.com/web-platform-tests/wpt/pull/47775</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Deepak Aravind">codesculptures</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>annevk</cc>
    
    <cc>cdumez</cc>
    
    <cc>karlcow</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2049893</commentid>
    <comment_count>0</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-07-31 12:11:31 -0700</bug_when>
    <thetext>&lt;img&gt; tag is not closed in serialization through XMLSerializer if &lt;img&gt; tag has child(s), this is different from chromium as it is serializing the &lt;img&gt;&apos;s children.


This can be reproducible 

const root = document.createElement(&quot;div&quot;);
const newImage = document.createElement(&quot;img&quot;);
const newStyle = document.createElement(&quot;style&quot;);

newImage.appendChild(newStyle);

root.appendChild(newImage);
document.body.appendChild(root);


const serializer = new XMLSerializer();

const serialized = serializer.serializeToString(root)

console.log(serialized);


Output:

Chrome: 
&lt;div xmlns=\&quot;http://www.w3.org/1999/xhtml\&quot;&gt;&lt;img&gt;&lt;style&gt;&lt;/style&gt;&lt;/img&gt;&lt;/div&gt;

Safari:
&lt;div xmlns=\&quot;http://www.w3.org/1999/xhtml\&quot;&gt;&lt;img&gt;&lt;/div&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2050470</commentid>
    <comment_count>1</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-02 23:09:47 -0700</bug_when>
    <thetext>Source/WebCore/editing/MarkupAccumulator.cpp#260

MarkupAccumulator::serializeNodesWithNamespaces, here if the &lt;img&gt; has child nodes the self tag would be skipped since shouldSelfClose will be false thus self close tag is not added,
```
void MarkupAccumulator::appendCloseTag(StringBuilder&amp; result, const Element&amp; element)
{
    if (shouldSelfClose(element, m_serializationSyntax)) {
        if (element.isHTMLElement())
            result.append(&apos; &apos;); // XHTML 1.0 &lt;-&gt; HTML compatibility.
        result.append(&apos;/&apos;);
    }
    result.append(&apos;&gt;&apos;);
}
```

but `elementCannotHaveEndTag` this been checked for to add close tag (&lt;/img&gt;) but this would always fails regardless of whether node has children.

So if `shouldSelfClose` fails we should add closeTag</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2050557</commentid>
    <comment_count>2</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-04 08:58:27 -0700</bug_when>
    <thetext>The shouldSelfClose and elementCannotHaveEndTag were contradicting each other where i believe both return opposite values but as per current implementation  producing same result

const myImg = document.createElement(&quot;img&quot;)
const childDiv = document.createElement(&quot;div&quot;)
img.appendChild(childDiv)


Consider current, targetNode = myImg
From the above case, 
shouldSelfClose returns false as it has child(s)
elementCannotHaveEndTag returns also false as it would always return false for void-elements



We have to make a decision clearly here,
1.Allow children in void elements (as per spec, it is incorrect, but chrome doing this)

2.shouldSelfClose would give priority for whether it is a void element immediately return true over hasChildNodes

3.Don&apos;t allow (or Ignore) appending node on void elements</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2050558</commentid>
    <comment_count>3</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-04 08:59:38 -0700</bug_when>
    <thetext>In the above am referring shouldSelfClose and elementCannotHaveEndTag are implementation which are located in Source/WebCore/editing/MarkupAccumulator.cpp</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2051214</commentid>
    <comment_count>4</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2024-08-07 12:12:32 -0700</bug_when>
    <thetext>&lt;rdar://problem/133404338&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2053648</commentid>
    <comment_count>5</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2024-08-18 23:42:29 -0700</bug_when>
    <thetext>Per https://w3c.github.io/DOM-Parsing/#the-xmlserializer-interface it should always produce an XML serialization. In the XML world concepts such as &quot;shouldSelfClose&quot; and &quot;elementCannotHaveEndTag&quot; are not applicable as all elements are equivalent when it comes to syntax.

So 1 is the correct option from the list in comment 2 except that it is per specification and not against the specification.

Hope that helps.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2053650</commentid>
    <comment_count>6</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-18 23:46:40 -0700</bug_when>
    <thetext>Just making sure, we planning to allow constructing serialization for children of void elements. And you are saying it is correct as per spec?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2053652</commentid>
    <comment_count>7</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2024-08-18 23:49:28 -0700</bug_when>
    <thetext>Yes, that is correct. XML (just like the DOM, but unlike HTML) does not have a concept of void elements. Every element can have child elements. So when you serialize as XML, you essentially have to ignore HTML-specific serialization aspects.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2053653</commentid>
    <comment_count>8</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-18 23:50:57 -0700</bug_when>
    <thetext>Agree with this ! 
Thanks a lot for the answer, i will try to raise a PR for this.
Thanks again</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2054426</commentid>
    <comment_count>9</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-21 09:44:10 -0700</bug_when>
    <thetext>Pull request: https://github.com/Webkit/WebKit/pull/32528</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2054924</commentid>
    <comment_count>10</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-23 09:41:14 -0700</bug_when>
    <thetext>Pull request: https://github.com/Webkit/WebKit/pull/32628</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2055134</commentid>
    <comment_count>11</comment_count>
    <who name="Deepak Aravind">codesculptures</who>
    <bug_when>2024-08-24 10:46:51 -0700</bug_when>
    <thetext>Submitted web-platform-tests pull request: https://github.com/web-platform-tests/wpt/pull/47775</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2055287</commentid>
    <comment_count>12</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2024-08-26 01:59:21 -0700</bug_when>
    <thetext>Committed 282725@main (0afcbcac01bd): &lt;https://commits.webkit.org/282725@main&gt;

Reviewed commits have been landed. Closing PR #32628 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>