Skip to content

Commit

Permalink
feat(remove): remove elements using querySelectorAll
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Mar 1, 2016
1 parent cb3463c commit b380bf7
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions remove-all-but.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,25 +20,24 @@ 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) {
return keepElement.contains(el) || el.contains(keepElement);
});
}

const all = toArray(document.body.querySelectorAll('*'), 0);
const all = Array.from(document.body.querySelectorAll('*'));
var removed = 0;

all.forEach(function (el) {
Expand All @@ -49,4 +48,4 @@ body
});

console.log('removed %d elements', removed);
}('.foo', '#baz'));
}('.foo'));

0 comments on commit b380bf7

Please sign in to comment.