diff --git a/remove-all-but.js b/remove-all-but.js index eb6194c..45723ab 100644 --- a/remove-all-but.js +++ b/remove-all-but.js @@ -1,6 +1,6 @@ /* Removes all elements except for the trees rooted -in the given selectors. +in the given selectors. Selectors are queried using querySelectorAll For example, given a document @@ -20,17 +20,16 @@ body hello */ (function hideAllBut() { - function toArray(what) { - return Array.prototype.slice.call(what, 0); - } - const selectors = toArray(arguments); + 'use strict'; + + const selectors = Array.from(arguments); if (!selectors.length) { throw new Error('Need at least one selector to leave'); } - const keep = selectors.map(function (selector) { - return document.querySelector(selector); - }); + const keep = selectors.reduce(function (all, selector) { + return all.concat(Array.from(document.querySelectorAll(selector))); + }, []); function shouldKeep(el) { return keep.some(function (keepElement) { @@ -38,7 +37,7 @@ body }); } - const all = toArray(document.body.querySelectorAll('*'), 0); + const all = Array.from(document.body.querySelectorAll('*')); var removed = 0; all.forEach(function (el) { @@ -49,4 +48,4 @@ body }); console.log('removed %d elements', removed); -}('.foo', '#baz')); +}('.foo'));