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

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>14288</bug_id>
          
          <creation_ts>2011-09-26 10:39:38 +0000</creation_ts>
          <short_desc>document.documentElement.insertAdjacentHTML specs WebKit behavior instead of IE behavior</short_desc>
          <delta_ts>2011-10-03 15:33:51 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebAppsWG</product>
          <component>DOM Parsing and Serialization</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugzilla.mozilla.org/show_bug.cgi?id=681052</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Henri Sivonen">hsivonen</reporter>
          <assigned_to name="Ms2ger">Ms2ger</assigned_to>
          <cc>gavin.sharp</cc>
    
    <cc>jonas</cc>
    
    <cc>mike</cc>
    
    <cc>public-webapps</cc>
          
          <qa_contact>public-webapps-bugzilla</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>57340</commentid>
    <comment_count>0</comment_count>
    <who name="Henri Sivonen">hsivonen</who>
    <bug_when>2011-09-26 10:39:38 +0000</bug_when>
    <thetext>Consider
http://software.hixie.ch/utilities/js/live-dom-viewer/saved/1148

It throws in Chrome and per spec. It doesn&apos;t throw in IE9 or in Opera 11.51. Given that insertAdjacentHTML is originally an IE API, shouldn&apos;t the spec describe IE behavior instead of WebKit behavior? Is there a good reason to throw?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>57341</commentid>
    <comment_count>1</comment_count>
    <who name="Jonas Sicking (Not reading bugmail)">jonas</who>
    <bug_when>2011-09-26 11:00:35 +0000</bug_when>
    <thetext>&quot;beforebegin&quot;/&quot;afterend&quot; must absolutely throw if you try to insert text or element. But inserting comments or using &quot;afterbegin&quot;/&quot;beforeend&quot; seems ok.

In other words, we should remove the explicit throwing if the context is a document, but we should reword the actual insertion of the fragment in terms of calling insertBefore and rethrow any exceptions that that throws.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>57342</commentid>
    <comment_count>2</comment_count>
    <who name="Henri Sivonen">hsivonen</who>
    <bug_when>2011-09-26 11:27:51 +0000</bug_when>
    <thetext>(In reply to comment #1)
&gt; &quot;beforebegin&quot;/&quot;afterend&quot; must absolutely throw if you try to insert text or
&gt; element. 

The current spec doesn&apos;t throw if the parent is null, either.

&gt; But inserting comments or using &quot;afterbegin&quot;/&quot;beforeend&quot; seems ok.

WebKit doesn&apos;t support it.

&gt; In other words, we should remove the explicit throwing if the context is a
&gt; document, but we should reword the actual insertion of the fragment in terms of
&gt; calling insertBefore and rethrow any exceptions that that throws.

There&apos;s little value in being able to insert comments or whitespace using insertAdjacentHTML, but supporting that case would add complexity. We&apos;d need to deoptimize the &quot;afterend&quot; case in Gecko when insertAdjacentHTML is invoked on the root element. Is being able to insert comments really worth the trouble compared to keeping the spec as-is or making the parent-is-document case return silently without doing anything?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>57365</commentid>
    <comment_count>3</comment_count>
    <who name="Jonas Sicking (Not reading bugmail)">jonas</who>
    <bug_when>2011-09-26 20:25:15 +0000</bug_when>
    <thetext>(In reply to comment #2)
&gt; (In reply to comment #1)
&gt; &gt; &quot;beforebegin&quot;/&quot;afterend&quot; must absolutely throw if you try to insert text
&gt; &gt; or element. 
&gt; 
&gt; The current spec doesn&apos;t throw if the parent is null, either.

That seems like a bug in the spec.

&gt; &gt; But inserting comments or using &quot;afterbegin&quot;/&quot;beforeend&quot; seems ok.
&gt; 
&gt; WebKit doesn&apos;t support it.

Which &quot;it&quot;? And it&apos;s not immediately clear why webkit not supporting it matters?

&gt; &gt; In other words, we should remove the explicit throwing if the context is a
&gt; &gt; document, but we should reword the actual insertion of the fragment in
&gt; &gt; terms of calling insertBefore and rethrow any exceptions that that throws.
&gt; 
&gt; There&apos;s little value in being able to insert comments or whitespace using
&gt; insertAdjacentHTML, but supporting that case would add complexity. We&apos;d need
&gt; to deoptimize the &quot;afterend&quot; case in Gecko when insertAdjacentHTML is
&gt; invoked on the root element. Is being able to insert comments really worth
&gt; the trouble compared to keeping the spec as-is or making the
&gt; parent-is-document case return silently without doing anything?

I&apos;m not sure what you mean by &quot;deoptimize&quot;.

A good implementation strategy seems to be to detect when parsing into the document node (which can happen for innerHTML, outerHTML and insertAdjecentHTML). In gecko this requires a single inlined flag check, so not a performance problem for the common case.

When parsing into the document node, use a fragment as parsing target and then insert the fragment using insertBefore or other appropriate DOM method and rethrow any exceptions that that throws.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>57393</commentid>
    <comment_count>4</comment_count>
    <who name="Henri Sivonen">hsivonen</who>
    <bug_when>2011-09-27 07:31:00 +0000</bug_when>
    <thetext>(In reply to comment #3)
&gt; (In reply to comment #2)
&gt; &gt; (In reply to comment #1)
&gt; &gt; &gt; &quot;beforebegin&quot;/&quot;afterend&quot; must absolutely throw if you try to insert text
&gt; &gt; &gt; or element. 
&gt; &gt; 
&gt; &gt; The current spec doesn&apos;t throw if the parent is null, either.
&gt; 
&gt; That seems like a bug in the spec.

The spec is describing an existing feature and implementations that predated the spec didn&apos;t throw in that case.

&gt; &gt; &gt; But inserting comments or using &quot;afterbegin&quot;/&quot;beforeend&quot; seems ok.
&gt; &gt; 
&gt; &gt; WebKit doesn&apos;t support it.
&gt; 
&gt; Which &quot;it&quot;? 

Inserting comments outside the root element using insertAdjacentHTML.

&gt; And it&apos;s not immediately clear why webkit not supporting it
&gt; matters?

It&apos;s an indicator that sites probably don&apos;t try to insert comments above the root element too much using insertAdjacentHTML().

&gt; &gt; &gt; In other words, we should remove the explicit throwing if the context is a
&gt; &gt; &gt; document, but we should reword the actual insertion of the fragment in
&gt; &gt; &gt; terms of calling insertBefore and rethrow any exceptions that that throws.
&gt; &gt; 
&gt; &gt; There&apos;s little value in being able to insert comments or whitespace using
&gt; &gt; insertAdjacentHTML, but supporting that case would add complexity. We&apos;d need
&gt; &gt; to deoptimize the &quot;afterend&quot; case in Gecko when insertAdjacentHTML is
&gt; &gt; invoked on the root element. Is being able to insert comments really worth
&gt; &gt; the trouble compared to keeping the spec as-is or making the
&gt; &gt; parent-is-document case return silently without doing anything?
&gt; 
&gt; I&apos;m not sure what you mean by &quot;deoptimize&quot;.

I mean having to have yet another check before we decide whether we can parse directly into the context node.

&gt; A good implementation strategy seems to be to detect when parsing into the
&gt; document node (which can happen for innerHTML, outerHTML and
&gt; insertAdjecentHTML). In gecko this requires a single inlined flag check, so not
&gt; a performance problem for the common case.
&gt; 
&gt; When parsing into the document node, use a fragment as parsing target and then
&gt; insert the fragment using insertBefore or other appropriate DOM method and
&gt; rethrow any exceptions that that throws.

But why bother doing any of this compared to not letting anything be inserted before or after the root element? Is there a use case that Web authors really need addressed here?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>57703</commentid>
    <comment_count>5</comment_count>
    <who name="Ms2ger">Ms2ger</who>
    <bug_when>2011-10-03 15:33:51 +0000</bug_when>
    <thetext>I am told that

  document.documentElement.insertAdjacentHTML(&quot;beforebegin&quot;, &quot;&lt;div&gt;&lt;/div&gt;&quot;)

adds a second element child to the document in IE8 and IE9, and throws (per spec) in IE10, and that it throws in the null-parent case as well. That is, IE appears to match Gecko.

After discussion with Henri, I went with throwing in both cases.

https://bitbucket.org/ms2ger/dom-parsing-and-serialization/changeset/ee0ba6d66250</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>