Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added getChildren function to replace it.
- Loading branch information
April Barrett
committed
Aug 13, 2014
1 parent
a84ee65
commit a08eaaa
Showing
1 changed file
with
38 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,52 @@ | ||
var matches = require('matches-selector') | ||
var getChildren = require('children') | ||
var matches = require("matches-selector") | ||
|
||
module.exports = Siblings | ||
|
||
function Siblings(el, selector) { | ||
return getChildren(el.parentNode, selector) | ||
.filter(function (sib) { | ||
return sib !== el | ||
}) | ||
return getChildren(el.parentNode, selector).filter(function(sibling) { | ||
return sibling !== el; | ||
}) | ||
} | ||
|
||
Siblings.next = traverse('next') | ||
Siblings.prev = traverse('previous') | ||
function getChildren(el, selector) { | ||
var selection; | ||
|
||
if (selector) { | ||
selection = Array.prototype.slice.call(el.querySelectorAll(selector), 0); | ||
} else { | ||
selection = Array.prototype.slice.call(el.children, 0); | ||
} | ||
return selection; | ||
} | ||
|
||
Siblings.next = traverse("next") | ||
Siblings.prev = traverse("previous") | ||
|
||
function traverse(dir) { | ||
var prop = dir + 'ElementSibling' | ||
var prop = dir + "ElementSibling" | ||
|
||
return function (el, selector, limit) { | ||
if (typeof selector === 'number') { | ||
limit = selector | ||
selector = null | ||
} | ||
return function(el, selector, limit) { | ||
if (typeof selector === "number") { | ||
limit = selector | ||
selector = null | ||
} | ||
|
||
var siblings = [] | ||
var sibling = el | ||
var siblings = [] | ||
var sibling = el | ||
|
||
while (true) { | ||
if (!(sibling = sibling[prop])) | ||
break | ||
while (true) { | ||
if (!(sibling = sibling[prop])) | ||
break | ||
|
||
if (selector && !matches(sibling, selector)) | ||
continue | ||
if (selector && !matches(sibling, selector)) | ||
continue | ||
|
||
siblings.push(sibling) | ||
if (limit && siblings.length >= limit) | ||
break | ||
} | ||
siblings.push(sibling) | ||
|
||
if (limit && siblings.length >= limit) | ||
break | ||
} | ||
|
||
return limit === 1 ? siblings.shift() : siblings | ||
} | ||
} | ||
return limit === 1 ? siblings.shift() : siblings | ||
} | ||
} |