|
1 | 1 | /*
|
2 | 2 | Removes all elements except for the trees rooted
|
3 | | -in the given selectors. |
| 3 | +in the given selectors. Selectors are queried using querySelectorAll |
4 | 4 |
|
5 | 5 | For example, given a document
|
6 | 6 |
|
|
20 | 20 | hello
|
21 | 21 | */
|
22 | 22 | (function hideAllBut() {
|
23 | | - function toArray(what) { |
24 | | - return Array.prototype.slice.call(what, 0); |
25 | | - } |
26 | | - const selectors = toArray(arguments); |
| 23 | + 'use strict'; |
| 24 | + |
| 25 | + const selectors = Array.from(arguments); |
27 | 26 | if (!selectors.length) {
|
28 | 27 | throw new Error('Need at least one selector to leave');
|
29 | 28 | }
|
30 | 29 |
|
31 | | - const keep = selectors.map(function (selector) { |
32 | | - return document.querySelector(selector); |
33 | | - }); |
| 30 | + const keep = selectors.reduce(function (all,selector) { |
| 31 | + return all.concat(Array.from(document.querySelectorAll(selector))); |
| 32 | + },[]); |
34 | 33 |
|
35 | 34 | function shouldKeep(el) {
|
36 | 35 | return keep.some(function (keepElement) {
|
37 | 36 | return keepElement.contains(el) || el.contains(keepElement);
|
38 | 37 | });
|
39 | 38 | }
|
40 | 39 |
|
41 | | - const all = toArray(document.body.querySelectorAll('*'),0); |
| 40 | + const all = Array.from(document.body.querySelectorAll('*')); |
42 | 41 | var removed = 0;
|
43 | 42 |
|
44 | 43 | all.forEach(function (el) {
|
|
49 | 48 | });
|
50 | 49 |
|
51 | 50 | console.log('removed %d elements', removed);
|
52 | | -}('.foo','#baz')); |
| 51 | +}('.foo')); |
0 commit comments