W3C home > Mailing lists > Public > whatwg@whatwg.org > May 2013

[whatwg] [dom] attributes collection not fully defined?

From: Michael Day <mikeday@yeslogic.com>
Date: Thu, 30 May 2013 13:08:44 +1000
Message-ID: <51A6C2BC.7020903@yeslogic.com>
To: whatwg@lists.whatwg.org

In the definition of the Element.attributes collection here:


It doesn't seem to describe the behaviour for setting direct properties 
of the attributes collection, and how they map to attributes.

For example, setting an attribute will create a property with the same 
name as the attribute:

     div = document.createElement("div");
     div.setAttribute("foo", "bar");
     alert(div.attributes.foo); // [Object Attr]

Except for read-only properties like length, which will not be shadowed 
by attributes:

     div.setAttribute("length", "99");
     alert(div.attributes.length); // 2

So far so good. Things get weird, though:

     div.attributes.fruit = "apple";
     alert(div.attributes.fruit); // apple
     div.setAttribute("fruit", "orange");
     alert(div.attributes.fruit); // [object Attr]
     alert(div.attributes.fruit); // apple (!!!)

Firefox and Chrome seem to be inconsistent on this, but at least in some 
situations they will shadow the property with an attribute, then restore 
the original property when the attribute is removed.

You can have more fun by using Object.defineProperty to make the 
property read-only or unconfigurable, which Firefox and Chrome will 
again treat inconsistently.

The mind boggles. How are these pseudo-properties supposed to be 
implemented? What magic hook calls them to life?

The reason I ask is that jQuery >= 1.9 uses div.attributes in its 
feature detection code, and it's causing us problems.

Best regards,


Prince: Print with CSS!
Received on Thursday, 30 May 2013 03:09:25 UTC

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