Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: RichText: Format content from tree value #7583

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions editor/components/rich-text/format.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* External dependencies
* WordPress dependencies
*/
import { omitBy } from 'lodash';
import { nodeListToReact } from 'dom-react';
import { createElement, renderToString } from '@wordpress/element';

/**
* WordPress dependencies
* Browser dependencies
*/
import { createElement, renderToString } from '@wordpress/element';
const { Node } = window;

/**
* Transforms a WP Element to its corresponding HTML string.
Expand Down Expand Up @@ -37,29 +36,30 @@ export function valueToString( value, format ) {
}
}

/**
* Strips out TinyMCE specific attributes and nodes from a WPElement
*
* @param {string} type Element type
* @param {Object} props Element Props
* @param {Array} children Element Children
*
* @return {Element} WPElement.
*/
export function createTinyMCEElement( type, props, ...children ) {
if ( props[ 'data-mce-bogus' ] === 'all' ) {
return null;
}
function walkChildren( node, mapFn ) {
const result = [];

if ( props.hasOwnProperty( 'data-mce-bogus' ) ) {
return children;
let child = node.firstChild;
while ( child ) {
result.push( mapFn( child ) );
child = child.next;
}

return createElement(
type,
omitBy( props, ( _, key ) => key.indexOf( 'data-mce-' ) === 0 ),
...children
);
return result;
}

export function treeNodeToElement( node ) {
switch ( node.type ) {
case Node.TEXT_NODE:
return node.value;

default:
return createElement(
node.name,
null,
...walkChildren( node, treeNodeToElement )
);
}
}

/**
Expand All @@ -69,8 +69,12 @@ export function createTinyMCEElement( type, props, ...children ) {
*
* @return {WPElement} WP Element.
*/
export function domToElement( value ) {
return nodeListToReact( value || [], createTinyMCEElement );
export function treeToElement( node ) {
if ( ! node ) {
return [];
}

return walkChildren( node, treeNodeToElement );
}

/**
Expand All @@ -81,14 +85,8 @@ export function domToElement( value ) {
*
* @return {string} HTML.
*/
export function domToString( value, editor ) {
const doc = document.implementation.createHTMLDocument( '' );

Array.from( value ).forEach( ( child ) => {
doc.body.appendChild( child );
} );

return editor ? editor.serializer.serialize( doc.body ) : doc.body.innerHTML;
export function treeToString( tree ) {
return renderToString( treeToElement( tree ) );
}

/**
Expand All @@ -100,11 +98,11 @@ export function domToString( value, editor ) {
*
* @return {*} Output.
*/
export function domToFormat( value, format, editor ) {
export function treeToFormat( tree, format ) {
switch ( format ) {
case 'string':
return domToString( value, editor );
return treeToString( tree );
default:
return domToElement( value );
return treeToElement( tree );
}
}
Loading