Add forEachSelectors method
This commit is contained in:
8
src/scripts/lib/foreach-selectors.js
Normal file
8
src/scripts/lib/foreach-selectors.js
Normal file
@@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
24
tests/scripts/lib/foreach-selectors.js
Normal file
24
tests/scripts/lib/foreach-selectors.js
Normal file
@@ -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()
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user