commit e39f53ab8f3fb0fb0e1d643a011896235bea36b9
parent 0c46c249c9591b714a1b5fce7ef873abe27aa466
Author: Ryan Wolf <rwolf@borderstylo.com>
Date: Wed, 15 Dec 2010 16:04:49 -0800
added .parent()
Diffstat:
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/windex.js b/windex.js
@@ -130,8 +130,8 @@ WindexNodes.prototype.filter = function (f) {
// See: http://api.jquery.com/children/
WindexNodes.prototype.children = function (selector) {
- var children = [];
if (selector) { return this._childrenMatching(selector); }
+ var children = [];
this.forEach(function (node) {
var nodeList = node.childNodes;
for (i = 0; i < nodeList.length; i++) {
@@ -153,6 +153,25 @@ WindexNodes.prototype._childrenMatching = function (selector) {
return new WindexNodes(children, selector, this[0]);
};
+// See: http://api.jquery.com/parent/
+WindexNodes.prototype.parent = function (selector) {
+ if (selector) { return this._parentMatching(selector); }
+ return new WindexNodes(this.map(function (node) { return node.parentNode; }));
+};
+
+WindexNodes.prototype._parentMatching = function (selector) {
+ var parents = [];
+ this.forEach(function (node) {
+ var parent = node.parentNode;
+ Windex(selector, parent.parentNode).forEach(function (node) {
+ if (node.wrappedJSObject === parent.wrappedJSObject) {
+ parents.push(parent);
+ }
+ });
+ });
+ return new WindexNodes(parents, selector, parents[0]);
+};
+
// See: http://api.jquery.com/addClass/
WindexNodes.prototype.addClass = function (className) {
var classesToAdd = className.split(" ");