| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict' |
| 2 | |
| 3 | const walk = ( node, parent, cb ) => { |
| 4 | cb( node, parent ) |
| 5 | |
| 6 | if( Array.isArray( node.children ) ) |
| 7 | node.children.forEach( child => walk( child, node, cb ) ) |
| 8 | } |
| 9 | |
| 10 | const data = { |
| 11 | name: 'div', |
| 12 | attribs: { |
| 13 | id: 'container', |
| 14 | class: 'message' |
| 15 | }, |
| 16 | children: [ |
| 17 | { |
| 18 | name: 'strong', |
| 19 | attribs: { |
| 20 | class: 'message' |
| 21 | }, |
| 22 | children: [ |
| 23 | { text: 'Hello' } |
| 24 | ] |
| 25 | }, |
| 26 | { text: ', World!' } |
| 27 | ] |
| 28 | } |
| 29 | |
| 30 | walk( data, null, ( node, parent ) => { |
| 31 | if( parent ) node.parent = parent |
| 32 | }) |
| 33 | |
| 34 | module.exports = [ data ] |