Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Element: Render RawHTML dangerously from serializer
  • Loading branch information
aduth committed Mar 30, 2018
commit f49620053f19861b27366be3af47349a23b656cb
9 changes: 1 addition & 8 deletions element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,7 @@ export { createPortal };
*
* @return {string} HTML.
*/
export function renderToString( element ) {
let rendered = serialize( element );

// Drop raw HTML wrappers (support dangerous inner HTML without wrapper)
rendered = rendered.replace( /<\/?wp-raw-html>/g, '' );

return rendered;
}
export { serialize as renderToString };

/**
* Concatenate two or more React children objects.
Expand Down
36 changes: 21 additions & 15 deletions element/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { castArray, omit, kebabCase } from 'lodash';
/**
* Internal dependencies
*/
import { Fragment } from './';
import { Fragment, RawHTML } from './';

/**
* Valid attribute types.
Expand Down Expand Up @@ -348,28 +348,34 @@ export function renderElement( element, context = {} ) {
return renderChildren( element, context );
}

if ( typeof element === 'string' ) {
return escapeHTML( element );
}
switch ( typeof element ) {
case 'string':
return escapeHTML( element );

if ( typeof element === 'number' ) {
return element.toString();
case 'number':
return element.toString();
}

const { type: tagName, props } = element;

if ( tagName === Fragment ) {
return renderChildren( props.children, context );
switch ( tagName ) {
case Fragment:
return renderChildren( props.children, context );

case RawHTML:
return props.children;
}

if ( typeof tagName === 'string' ) {
return renderNativeComponent( tagName, props, context );
} else if ( typeof tagName === 'function' ) {
if ( tagName.prototype && typeof tagName.prototype.render === 'function' ) {
return renderComponent( tagName, props, context );
}
switch ( typeof tagName ) {
case 'string':
return renderNativeComponent( tagName, props, context );

case 'function':
if ( tagName.prototype && typeof tagName.prototype.render === 'function' ) {
return renderComponent( tagName, props, context );
}

return renderElement( tagName( props, context ), context );
return renderElement( tagName( props, context ), context );
}

return '';
Expand Down
10 changes: 8 additions & 2 deletions element/test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { noop } from 'lodash';
/**
* Internal dependencies
*/
import { Component, Fragment } from '../';
import { Component, Fragment, RawHTML } from '../';
import serialize, {
hasPrefix,
renderElement,
Expand Down Expand Up @@ -193,11 +193,17 @@ describe( 'renderElement()', () => {
expect( result ).toBe( '' );
} );

it( 'renders fragment as its inner children', () => {
it( 'renders Fragment as its inner children', () => {
const result = renderElement( <Fragment>Hello</Fragment> );

expect( result ).toBe( 'Hello' );
} );

it( 'renders RawHTML as its unescaped children', () => {
const result = renderElement( <RawHTML>{ '<img/>' }</RawHTML> );

expect( result ).toBe( '<img/>' );
} );
} );

describe( 'renderNativeComponent()', () => {
Expand Down