themummy
Version:
UNSTABLE. codecoverage married with integration testing. currently a work in progress
56 lines (46 loc) • 1.49 kB
JavaScript
// Base Browser stuff
window.w = window
w.D = Document
w.d = document
Element.prototype.qs = Element.prototype.querySelector
Element.prototype.qsa = Element.prototype.querySelectorAll
D.prototype.qs = Document.prototype.querySelector
D.prototype.qsa = Document.prototype.querySelectorAll
EventTarget.prototype.on = EventTarget.prototype.addEventListener
EventTarget.prototype.off = EventTarget.prototype.removeEventListener
EventTarget.prototype.emit = EventTarget.prototype.dispatchEvent
// http://stackoverflow.com/questions/11761881/javascript-dom-find-element-index-in-container
Element.prototype.getNodeIndex = function() {
var node = this
var index = 0;
while ( (node = node.previousSibling) ) {
if (node.nodeType != 3 || !/^\s*$/.test(node.data)) {
index++;
}
}
return index;
}
NodeList.prototype.toArray = function() {
return Array.prototype.map.call(this, function(item){
return item
})
}
HTMLCollection.prototype.toArray = function() {
return NodeList.prototype.toArray.call(this)
}
Node.prototype.prependChild = function(el) {
var parentNode = this
parentNode.insertBefore(el, parentNode.firstChild)
}
Element.prototype.qsP = function(query) {
// qsP: querySelectorParent
var el = this
var do_break = false
while (do_break === false) {
parent = el.parentElement
do_break = parent === null || parent.matches(query)
// parentElement of html is null
}
if (parent === null) {return undefined}
return parent
}