Created
May 7, 2010 15:46
-
-
Save bga/393621 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
var _nodeUnique = function (nodeArray) { | |
var a = -1, len = nodeArray.length, b = -1, ret = new Array(); | |
while (++a < len) { | |
while (++b < len && nodeArray[b] === nodeArray[a]) | |
; | |
ret[b] = nodeArray[a]; | |
} | |
return ret; | |
}; | |
var _nodeSortFn = (document.documentElement.sourceIndex != null) ? | |
function (a, b) { | |
return a.sourceIndex - b.sourceIndex; | |
} : | |
function (a, b) { | |
return 3 - (a.compareDocumentPosition(b) & 6); | |
}; | |
var _nodeSort = function (nodeArray) { | |
return _nodeUnique(nodeArray.sort(_nodeSortFn)); | |
}; | |
/* add */ | |
Y.Node.prototype.add = function (elem, context) { | |
return Y.all(this).add(elem, context); | |
}; | |
Y.NodeList.prototype.add = function (elem, context) { | |
var collection = this._nodes; | |
if (context && Y.Lang.isString(elem)) { | |
((context._nodes) ? context : Y.Node.all(context)).each(function (node) { | |
collection = collection.concat(node.all(elem)._nodes); | |
}); | |
} | |
else if (elem) { | |
collection = collection.concat(elem._nodes || Y.Node.all(elem)._nodes); | |
} | |
return Y.Node.all(_nodeSort(collection)); | |
}; | |
/* children */ | |
Y.Node.prototype.children = function (selector) { | |
return this.get('children').filter(selector); | |
}; | |
Y.NodeList.prototype.children = function (selector) { | |
var collections = new Array(); | |
var nodes = this._nodes, i = -1, node, j = -1; | |
while((node = nodes[++i])) | |
{ | |
collections[++j] = node.children().filter(selector)._nodes; | |
} | |
return Y.Node.all(Array.prototype.concat.apply(new Array(), collections)); | |
}; | |
/* parent */ | |
Y.Node.prototype.parent = function (selector) { | |
return this.get('parentNode').filter(selector); | |
}; | |
Y.NodeList.prototype.parent = function (selector) { | |
var collection = new Array(); | |
var nodes = this._nodes, i = -1, node, j = -1; | |
var parent; | |
while((node = nodes[++i])) | |
{ | |
if (parent = node.parent(selector)) { | |
collection[++j] = node.parent(selector)._node; | |
} | |
} | |
return Y.Node.all(_nodeSort(collection)); | |
}; | |
/* empty */ | |
Y.Node.prototype.empty = function () { | |
this.get('children').remove(true); | |
this.get('childNodes').remove(); | |
return this; | |
}; | |
Y.NodeList.importMethod(Y.Node.prototype, ['empty']); | |
/* eq */ | |
Y.NodeList.prototype.eq = function (index) { | |
return this.item((index < 0) ? this.size() + index : index); | |
}; | |
/* filter */ | |
Y.NodeList.prototype.filterByString = Y.NodeList.prototype.filter; | |
Y.Node.prototype.filter = function (selector) { | |
return Y.all(this).filter(selector).item(0); | |
}; | |
Y.NodeList.prototype.filter = function (selector) { | |
var collection = new Array(); | |
if (Y.Lang.isString(selector)) { | |
return this.filterByString(selector); | |
} | |
else if (typeof selector == 'function') { | |
var nodes = this._nodes, i = -1, node, j = -1; | |
while((node = nodes[++i])) | |
{ | |
if (selector(i) !== false) { | |
collection[++j] = node; | |
} | |
return Y.Node.all(collection); | |
} | |
else { | |
return this; | |
} | |
}; | |
/* find */ | |
Y.NodeList.prototype.find = function (selector) { | |
var collections = new Array(); | |
var nodes = this._nodes, i = -1, node, j = -1; | |
while((node = nodes[++i])) | |
collections[++j] = node.all(selector)._nodes; | |
return Y.Node.all(_nodeSort(Array.prototype.concat.apply(new Array(), collections))); | |
}; | |
/* get */ | |
Y.NodeList.prototype.get = function (index) { | |
return (!Y.Lang.isUndefined(index)) ? this._nodes[index] : this._nodes; | |
}; | |
/* is */ | |
Y.Node.prototype.is = Y.Node.prototype.test; | |
Y.NodeList.prototype.is = function (selector) { | |
var boolean = true; | |
var nodes = this._nodes, i = -1, node; | |
while((node = nodes[++i]) && node.is(selector)) | |
; | |
return node == null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment