WebIDL attribute reflection

Claude Pache claude.pache at gmail.com
Sun Dec 30 13:20:16 PST 2012


Le 30 déc. 2012 à 19:20, David Bruant <bruant.d at gmail.com> a écrit :
> Interesting point.
> Unrelated, but it's making me realize that innerHTML may stay a getter/setter (not sure about inherited or not).
> I guess the divide I'd like to put is between object state and what is conveniently put as an accessor

FWIW, I used the fact that, in IE 8 & 9, innerHTML is conveniently a configurable accessor found at Element.prototype (in IE8) or HTMLElement.prototype (in IE9), for correcting a quirk of its setter, by extracting it and replacing it with a corrected version. I don't know if and how it would be possible if it was an own property on each instance, or a magical property. Here is the code:

if (!window.HTMLElement)
	window.HTMLElement = window.Element
;(function() {
    var wrapMarkup = {
            'SELECT': [1, '<select multiple>', '</select>']
          , 'TABLE' : [1, '<table>', '</table>']
          , 'TBODY' : [2, '<table><tbody>', '</tbody></table>']
          , 'TFOOT' : [2, '<table><tfoot>', '</tfoot></table>']
          , 'THEAD' : [2, '<table><thead>', '</thead><table>']
          , 'TR'    : [3, '<table><tbody><tr>', '</tr></tbody></table>']
        }
      , setInnerHTML = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'innerHTML').set
    Object.defineProperty(HTMLElement.prototype, 'innerHTML', {'set': function(content) {
        var result
          , i
        if (wrapMarkup.hasOwnProperty(this.nodeName)) {
            result =  document.createElement('div')
            setInnerHTML.call(result,  wrapMarkup[this.nodeName][1] + content + wrapMarkup[this.nodeName][2])
            for (i = 0; i < wrapMarkup[this.nodeName][0]; i++)
                result = result.firstChild
            setInnerHTML.call(this, '')
            while (result.firstChild)
                this.appendChild(result.firstChild)
        }
        else {
           setInnerHTML.call(this, content)
        }
    }})
})()

Claude




More information about the es-discuss mailing list