-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blocks: Represent children as array of node objects
- Loading branch information
Showing
7 changed files
with
115 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { reduce, map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createElement } from '@wordpress/element'; | ||
|
||
export function namedNodeMapToObject( namedNodeMap ) { | ||
return reduce( namedNodeMap, ( result, entry ) => { | ||
result[ entry.name ] = entry.value; | ||
return result; | ||
}, {} ); | ||
} | ||
|
||
export function toText( node ) { | ||
return { | ||
type: 'text', | ||
text: node.nodeValue, | ||
}; | ||
} | ||
|
||
export function toNode( node ) { | ||
const { nodeName, attributes, childNodes } = node; | ||
|
||
return { | ||
type: 'node', | ||
name: nodeName.toLowerCase(), | ||
attributes: namedNodeMapToObject( attributes ), | ||
children: children( childNodes ), | ||
}; | ||
} | ||
|
||
export function children( childNodes ) { | ||
return reduce( childNodes, ( result, childNode ) => { | ||
switch ( childNode.nodeType ) { | ||
case window.Node.ELEMENT_NODE: | ||
result.push( toNode( childNode ) ); | ||
break; | ||
|
||
case window.Node.TEXT_NODE: | ||
result.push( toText( childNode ) ); | ||
break; | ||
} | ||
|
||
return result; | ||
}, [] ); | ||
} | ||
|
||
export function toElement( value ) { | ||
if ( value === null || value === undefined ) { | ||
return; | ||
} | ||
|
||
if ( Array.isArray( value ) ) { | ||
return map( toElement, value ); | ||
} | ||
|
||
switch ( value.type ) { | ||
case 'text': | ||
return value.text; | ||
|
||
case 'element': | ||
return createElement( | ||
value.name, | ||
value.attributes, | ||
toElement( value.children ), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters