Skip to content

Commit

Permalink
Return node when it contains the other node - with unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kapouer committed Jul 30, 2017
1 parent 8c5c1e8 commit 031f6e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -10,8 +10,10 @@ function getCommonAncestor(node) {
}

var nodes = slice.call(arguments, 1)
var list = parents(node)
list.unshift(node)

return parents(node).filter(function (parent) {
return list.filter(function (parent) {
return nodes.every(function (node) {
return contains(parent, node)
})
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Expand Up @@ -17,6 +17,20 @@ test("is correct", function (assert) {
assert.end()
})

test("is correct with inner child", function (assert) {
var one = elem()
var two = elem()
var three = elem()

one.appendChild(two)
two.appendChild(three)

var ancestor = commonAncestors(two, three)

assert.equal(ancestor, two)
assert.end()
})

test("doesn't return false positives", function (assert) {
var one = elem()
var two = elem()
Expand Down

0 comments on commit 031f6e0

Please sign in to comment.