File tree Expand file tree Collapse file tree 1 file changed +9
-10
lines changed Expand file tree Collapse file tree 1 file changed +9
-10
lines changed Original file line number Diff line number Diff line change 11/*
22Removes all elements except for the trees rooted
3- in the given selectors.
3+ in the given selectors. Selectors are queried using querySelectorAll
44
55For example, given a document
66
2020 hello
2121*/
2222( 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 ) ;
2726 if ( ! selectors . length ) {
2827 throw new Error ( 'Need at least one selector to leave' ) ;
2928 }
3029
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+ } , [ ] ) ;
3433
3534 function shouldKeep ( el ) {
3635 return keep . some ( function ( keepElement ) {
3736 return keepElement . contains ( el ) || el . contains ( keepElement ) ;
3837 } ) ;
3938 }
4039
41- const all = toArray ( document . body . querySelectorAll ( '*' ) , 0 ) ;
40+ const all = Array . from ( document . body . querySelectorAll ( '*' ) ) ;
4241 var removed = 0 ;
4342
4443 all . forEach ( function ( el ) {
4948 } ) ;
5049
5150 console . log ( 'removed %d elements' , removed ) ;
52- } ( '.foo' , '#baz' ) ) ;
51+ } ( '.foo' ) ) ;
You can’t perform that action at this time.
0 commit comments