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

Michael Day mikeday at yeslogic.com
Wed May 29 20:08:44 PDT 2013


Hi,

In the definition of the Element.attributes collection here:

http://dom.spec.whatwg.org/#dom-element-attributes

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]
     div.removeAttribute("fruit");
     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,

Michael

-- 
Prince: Print with CSS!
http://www.princexml.com


More information about the whatwg mailing list