Safari and HTMLElement.prototype
Saturday, 21. April 2007, 05:13:51
As you all know, Safari does not support HTMLElement, which makes it hard to add stuff to all newly created HTML-Elements, Nodes, whatever you might call it.
Fortunately, it supports the semi-standard __proto__ property. Exploiting another "bug", that all HTMLElement(s) are produced from the same prototype, I came up with a neat solution that even works in Safari 1.3 and you can replace functions such as "getAttribute" (go figure!).
This piece uses perfect object detection, i.e. every browser that works this way will be fixed (i.e. all supporting Safaris, maybe KHTML?), while leaving browsers that work differently untouched.
Remember: You saw it here first!
PS: Best used inside a closure, e.g. (function(){ [your code here] })().
Fortunately, it supports the semi-standard __proto__ property. Exploiting another "bug", that all HTMLElement(s) are produced from the same prototype, I came up with a neat solution that even works in Safari 1.3 and you can replace functions such as "getAttribute" (go figure!).
var w = window;
if(!w.HTMLElement&&(typeof document.createElement)=="function" // check basics
&&(var t=document.createElement('a').__proto__) // '__proto__' supported?
&&t==document.createElement('p').__proto__){ // all HTMLElements the same?
w.HTMLElement={}; // prevent people from constructing
w.HTMLElement.prototype=t;
}
This piece uses perfect object detection, i.e. every browser that works this way will be fixed (i.e. all supporting Safaris, maybe KHTML?), while leaving browsers that work differently untouched.
Remember: You saw it here first!
PS: Best used inside a closure, e.g. (function(){ [your code here] })().

