From a08eaaa2609a2f1eadb5b6f47ecec90135a5351d Mon Sep 17 00:00:00 2001 From: April Barrett Date: Wed, 13 Aug 2014 15:43:25 -0700 Subject: [PATCH] remove discore-children dependency Added getChildren function to replace it. --- index.js | 67 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 98f1a46..9da5970 100644 --- a/index.js +++ b/index.js @@ -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 - } -} \ No newline at end of file + return limit === 1 ? siblings.shift() : siblings + } +}