[whatwg] Inserting a DocumentFragment of multiple text nodes into a script element

David Flanagan dflanagan at mozilla.com
Fri Oct 28 14:20:47 PDT 2011


Here's another ambiguity about the "child nodes are changed" trigger for 
executing a script element.
What is the correct behavior for the following code?

<script>
window.onload = test;

function test() {
     var s = document.createElement("script");
     document.head.appendChild(s);

     var f = document.createDocumentFragment();
     
f.appendChild(document.createTextNode("alert(document.scripts[1].text);"));
     f.appendChild(document.createTextNode("alert(2);"));
     f.appendChild(document.createTextNode("alert(3);"));

     s.appendChild(f);

     alert(s.text);
}
</script>

In Firefox, the code in all three text nodes runs, so there are 4 alerts 
in total, and the first and the fourth display the same text: the 
concatenation of the three text nodes.

In Chrome, Safari and Opera (I can't test on IE), only the first text 
node is run as a script. There are two alerts.  The first displays the 
content of the first text node, and the second alert displays the 
concatenation of all three text nodes.

I would guess that Firefox's behavior is correct here, because DOM4 
specifies the algorithm for DocumentFragment insertion without using 
recursion. But its not really specified clearly there either.  Does the 
HTML spec need a clarifying note on this point?  (I also plan to raise 
this issue on the www-dom mailing list)

     David


On 10/28/11 12:07 PM, David Flanagan wrote:
> On 10/28/11 12:03 PM, Bjoern Hoehrmann wrote:
>> * David Flanagan wrote:
>>> All browsers do that correctly.  The case I'm interested in is this 
>>> one:
>>>
>>>      var s1 = document.createElement("script");
>>>      var t1 = document.createTextNode("");
>>>      s1.appendChild(t1);
>>>      document.head.appendChild(s1);
>>>      t1.appendData("alert('changed text node data');");
>>>
>>> Firefox runs this script and Chrome, Safari and Opera do not. (I don't
>>> have a windows installation, so I haven't tested IE)
>> In "IE9 standards" mode IE9 displays the alert.
>>
> Thanks, Bjoern. That makes it a lot harder for me to argue that the 
> spec should change to match Chrome, Safari and Opera... But can we at 
> least change  "when child nodes change" to something like "when the 
> text IDL attribute changes from the empty string to a non-empty string"?
>
>     David



More information about the whatwg mailing list