From 76026cf8d9acce6a41f9291220feb79289006cdb Mon Sep 17 00:00:00 2001 From: Maxime Thirouin Date: Fri, 23 May 2014 06:54:20 +0200 Subject: [PATCH] Add forEachSelectors method --- src/scripts/lib/foreach-selectors.js | 8 ++++++++ tests/scripts/lib/foreach-selectors.js | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/scripts/lib/foreach-selectors.js create mode 100644 tests/scripts/lib/foreach-selectors.js diff --git a/src/scripts/lib/foreach-selectors.js b/src/scripts/lib/foreach-selectors.js new file mode 100644 index 0000000..7a8479c --- /dev/null +++ b/src/scripts/lib/foreach-selectors.js @@ -0,0 +1,8 @@ +var forEachEls = require("./foreach-els") + +module.exports = function(selectors, cb, context, DOMcontext) { + DOMcontext = DOMcontext || document + selectors.forEach(function(selector) { + forEachEls(DOMcontext.querySelectorAll(selector), cb, context) + }) +} diff --git a/tests/scripts/lib/foreach-selectors.js b/tests/scripts/lib/foreach-selectors.js new file mode 100644 index 0000000..5668a4e --- /dev/null +++ b/tests/scripts/lib/foreach-selectors.js @@ -0,0 +1,24 @@ +var tape = require("tape") + +var forEachEls = require("../../../src/scripts/lib/foreach-selectors.js") + +var cb = function(el) { + el.className = "modified" +} + +tape("test forEachSelector", function(t) { + forEachEls(["html", "body"], cb) + + t.equal(document.documentElement.className, "modified", "callback has been executed on first selector") + t.equal(document.body.className, "modified", "callback has been executed on first selector") + + document.documentElement.className = "" + document.body.className = "" + + forEachEls(["html", "body"], cb, null, document.documentElement) + + t.equal(document.documentElement.className, "", "callback has not been executed on first selector when context is used") + t.equal(document.body.className, "modified", "callback has been executed on first selector when context is used") + + t.end() +})