File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed
Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change 11'use strict' ;
22
33
4- module . exports = function ( ast , selector ) {
4+ function select ( ast , selector ) {
55 var result = [ ] ;
66
77 ( function walk ( node ) {
8- if ( node . type == selector ) {
9- result . push ( node ) ;
8+ if ( node . type == selector [ 0 ] ) {
9+ if ( selector . length == 1 ) {
10+ result . push ( node ) ;
11+ }
12+ else if ( node . children ) {
13+ node . children . forEach ( function ( child ) {
14+ [ ] . push . apply ( result , select ( child , selector . slice ( 1 ) ) ) ;
15+ } ) ;
16+ }
1017 }
1118 if ( node . children ) {
1219 node . children . forEach ( walk ) ;
1320 }
1421 } ( ast ) ) ;
1522
1623 return result ;
24+ }
25+
26+
27+ module . exports = function ( ast , selector ) {
28+ return select ( ast , selector . split ( / \s + / g) ) ;
1729} ;
Original file line number Diff line number Diff line change @@ -12,5 +12,18 @@ test('type selector', function (t) {
1212 t . equal ( select ( ast , 'text' ) . length , 39 ) ;
1313 t . equal ( select ( ast , 'text' ) [ 1 ] , ast . children [ 1 ] . children [ 0 ] ) ;
1414 t . equal ( select ( ast , 'tableRow' ) . length , 2 ) ;
15+ t . equal ( select ( ast , 'heading' ) . length , 5 ) ;
16+ t . end ( ) ;
17+ } ) ;
18+
19+
20+ test ( 'nesting' , function ( t ) {
21+ t . deepEqual ( select ( ast , 'root heading' ) , select ( ast , 'heading' ) ) ;
22+ t . deepEqual ( select ( ast , 'paragraph emphasis' ) , [
23+ ast . children [ 2 ] . children [ 0 ] . children [ 1 ] ,
24+ ast . children [ 3 ] . children [ 1 ] ,
25+ ast . children [ 4 ] . children [ 1 ] . children [ 1 ] . children [ 1 ]
26+ . children [ 0 ] . children [ 0 ] . children [ 1 ]
27+ ] ) ;
1528 t . end ( ) ;
1629} ) ;
You can’t perform that action at this time.
0 commit comments