Skip to content

Commit

Permalink
Dom: Added ancestors() method, docs and unit tests [#28 state:review]
Browse files Browse the repository at this point in the history
  • Loading branch information
phae committed Sep 3, 2009
1 parent 039f4c0 commit b6e555c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/dom/dom.js
Expand Up @@ -2062,6 +2062,40 @@
for (; i < length; i++) {
ret[ri++] = this[i].parentNode;
}

return r.get(unique(ret));
},

/**
@name glow.dom.NodeList#ancestors
@function
@description Gets the unique ancestor nodes of each node as a new NodeList.
@returns {glow.dom.NodeList}
Returns a new NodeList containing the ancestor nodes, with
duplicates removed
@example
// get ancestory elements for anchor elements
var ancestors = glow.dom.get("a").ancestors();
*/
ancestors: function() {
var ret = [],
ri = 0,
i = 0,
length = this.length,
elm;

for (; i < length; i++) {
elm = this[i];

while (elm = elm.parentNode) {
ret[ri++] = elm;
}

}

return r.get(unique(ret));
},

Expand Down
18 changes: 18 additions & 0 deletions test/glow/dom/dom.js
Expand Up @@ -1009,6 +1009,24 @@ t.test("glow.dom.NodeList.parent()", function() {
t.ok(nodes instanceof glow.dom.NodeList, "Returns NodeList");
});

t.test("glow.dom.NodeList.ancestors()", function() {
t.expect(3);

var nodes = glow.dom.get("#simon1").ancestors();
var expectedNodes = glow.dom.get("#firstp, #main, dl, body, html");

t.isSet(nodes, expectedNodes, "Gets ancestors");

var nodes = glow.dom.get("#simon1, #foo").ancestors();
var expectedNodes = glow.dom.get("#firstp, #main, dl, body, html");

t.isSet(nodes, expectedNodes, "Gets only unique ancestors");

nodes = glow.dom.get("#foo, #ap").ancestors();

t.ok(nodes instanceof glow.dom.NodeList, "Returns NodeList");
});

t.test("glow.dom.NodeList.next()", function() {
t.expect(4);
var nodes = glow.dom.get("#foo, #google").next();
Expand Down

0 comments on commit b6e555c

Please sign in to comment.