dom4-elements
Version:
Polyfill for Elements class and query/queryAll additions in DOM4.
1 lines • 1.19 kB
JavaScript
(function(){var arrayProto=Array.prototype;if(typeof Document==="undefined"||!arrayProto.forEach||!document.querySelector){return}var query=function query(selector){return this.querySelector(selector)};var queryAll=function queryAll(selector){var matches=this.querySelectorAll(selector);var elements=new Elements(matches);return elements};["Document","DocumentFragment","Element"].forEach(function(ParentNode){if(!(ParentNode in this)){return}var prototype=this[ParentNode].prototype;if(!("query"in prototype)){prototype.query=query}if(!("queryAll"in prototype)){prototype.queryAll=queryAll}});if(!("Elements"in this)){var push_uniq=function(a,b){for(var i=0,mx=b.length;i<mx;++i){var el=b[i];if(a.indexOf(el)===-1){a.push(el)}}return a};this.Elements=Elements=function Elements(elements){if(elements){push_uniq(this,elements)}};Object.getOwnPropertyNames(arrayProto).forEach(function(fn){Elements.prototype[fn]=arrayProto[fn]});Elements.prototype.queryAll=function(selector){var mapped=this.map(function(el){return el.queryAll(selector)});var reduced=mapped.reduce(push_uniq,new Elements);return reduced};Elements.prototype.query=function(selector){return this.queryAll(selector)[0]}}})();