Global variables and id lookup for elements

Hey folks,

Context: http://krijnhoetmer.nl/irc-logs/whatwg/20110428#l-707

Current browsers disagree about how to handle <div 
id=x></div><script>var x;</script>. Webkit browsers leave x pointing to 
the div, whereas IE, Firefox and Opera make x undefined [1]. (There is 
content that depends on x being undefined, but I don't have any links 
handy right now.)

My reading of the relevant specs (es5 section 10, WebIDL 4.5.3, HTML 
6.2.4) supports the Webkit behavior. To summarize, a property x is 
defined on the global object pointing to the element before the script 
block, and when the script block is parsed for variable declarations 
there's already an x defined and therefore the variable is not 
initialized to undefined. I therefore propose a spec change is in order.

jgraham summarized the wanted behavior neatly as "id lookup should 
happen after all other property resolution". Conceptually I think this 
is like saying that the global environment actually has an outer 
environment, and this outer environment contains the properties for 
element lookups (which are currently defined on the global object). 
Another proposal was to say that properties for element lookup are 
special and should get overwritten in 10.8.c. Other, better options may 
also exist.

Open question: Should this be defined directly in HTML, or should WebIDL 
define it and let HTML annotate Window to enable the necessary 
exceptional behavior? My initial take was that since both id lookup and 
window=global is defined in HTML it would fit best there, but it also 
makes sense to contain finicky DOM-ES interactions to WebIDL.

[1] http://code.google.com/p/chromium/issues/detail?id=80591

-- 
Magnus Kristiansen
"Don't worry; the Universe IS out to get you."

Received on Thursday, 28 April 2011 20:31:33 UTC