W3C home > Mailing lists > Public > whatwg@whatwg.org > September 2011

[whatwg] [html5] scope chain for event handlers specified via content attributes

From: David Flanagan <dflanagan@mozilla.com>
Date: Thu, 08 Sep 2011 17:23:09 -0700
Message-ID: <4E695C6D.1030000@mozilla.com>
I've always assumed that if I do e.setAttribute("onclick", "alert(x)"), 
the resulting event handler function is (or works like) this:

function(event) {
     with(event.target.ownerDocument) {
         with(event.target.form || {}) {
             with(event.target) {
                 alert(x);
             }
         }
     }
}

That is, I'd expect the scope chain to be created dynamically for each 
invocation of the function.

But ?7.1.6.1 says that the scope chain should be initialized statically 
when the content attribute string is converted to a function.  I'd like 
to check that that is intentional, since it causes counter-intiuitive 
(to me) behavior if an element moves between forms or moves between 
documents after the event handler attribute is set.

I've put some test code here:  http://pastebin.mozilla.org/1326758

My results:  Firefox and Safari create the scope chain statically: when 
an element moves between forms, the scope chain remains the same.  
Chrome's scope chain is dynamic and resolves identifiers against the 
element's new form.  Chrome's behavior seems more sensible to me. Is it 
correct?

(When an element moves from one document to another (via adoptNode()) 
firefox uses dynamic scope (perhaps because it is re-creating the 
function?).  In Chrome and Safari, the event handler stops working when 
the element is moved from one document to another, so the test doesn't 
succeed there.)

     David
Received on Thursday, 8 September 2011 17:23:09 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:36 UTC