document.write from event handlers (was: Re: Black-box equivalence of parsing fragments directly into context node)

On Nov 27, 2008, at 17:15, Boris Zbarsky wrote:

>> Speaking of the load event for SVG subtrees: During normal parse,  
>> when the load event for an <svg> element fires, can a handler react  
>> to it before there has been an event loop spin?
>
> Yes, absolutely.  The event fires synchronously, as required by the  
> spec.
>
>> That is, should I treat the firing of that event as a potential  
>> trigger of document.write() entering the parser?
>
> Yes.


This brings up a spec-relevant issue:

Should document.write() tokenize synchronously when called from a  
event handler?

I tested document.write() from a mutation event handler by mutating  
the tree from script during parse. It looks like Gecko might in some  
cases tokenize the argument of document.write() from mutation event  
handler before the mutation that fired the event is complete. I  
couldn't figure out when exactly that happens. However, when it did,  
WebKit and Opera behaved differently, so there's no interop.

As a preliminary opinion, I'd like to suggest that if at all feasible  
considering legacy, document.write() from an event handler should  
write to the stream but not tokenize before the method returns.  
Otherwise, the ways in which the parser needs to be re-entrant become  
complicated for very little practical gain. With simple testing, this  
*seems* to be what WebKit and Opera do.

More generally, if document.write() occurs for any reason other than  
the parser kicking off the evaluation of a script element, it would be  
simpler if document.write() returned without tokenizing.

-- 
Henri Sivonen
hsivonen@iki.fi
http://hsivonen.iki.fi/

Received on Thursday, 27 November 2008 16:59:48 UTC