Skip to content

Commit

Permalink
Element: Render RawHTML dangerously from serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 30, 2018
1 parent c0846d2 commit f496200
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
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

0 comments on commit f496200

Please sign in to comment.