Created
April 14, 2015 14:58
-
-
Save mkoubik/13932afaf7ed9aaf203c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof HtmlFormControlsCollection == 'undefined') { | |
function HtmlFormControlsCollection( arr ) { | |
for ( var i = 0; i < arr.length; i += 1 ) { | |
this[i] = arr[i]; | |
} | |
// length is readonly | |
Object.defineProperty( this, 'length', { | |
get: function () { | |
return arr.length; | |
} | |
}); | |
// a HTMLCollection is immutable | |
Object.freeze( this ); | |
} | |
HtmlFormControlsCollection.prototype = { | |
item: function ( i ) { | |
return this[i] != null ? this[i] : null; | |
}, | |
namedItem: function ( name ) { | |
var ret = []; | |
for ( var i = 0; i < this.length; i += 1 ) { | |
if ( this[i].id === name || this[i].name === name ) { | |
ret.push(this[i]); | |
} | |
} | |
return ret.length ? ret : null; | |
} | |
}; | |
} | |
var oldElements = HTMLFormElement.prototype.__lookupGetter__('elements'); | |
HTMLFormElement.prototype.__defineGetter__('elements', function() { | |
var elems = oldElements.call(this); | |
if (typeof HtmlFormControlsCollection !== 'undefined' && elems instanceof HtmlFormControlsCollection) { | |
return elems; | |
} | |
return new HtmlFormControlsCollection(elems); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment