Skip to content

Commit

Permalink
Block API: Add hook for deprecated raw HTML support
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 31, 2018
1 parent de27fa1 commit b310d0e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function getSaveElement( blockType, attributes ) {
saveElement = save( { attributes } );
}

saveElement = applyFilters( 'blocks.getSaveContent.saveElement', saveElement, blockType, attributes );

const addExtraContainerProps = ( element ) => {
if ( ! element || ! isObject( element ) ) {
return element;
Expand Down
46 changes: 46 additions & 0 deletions blocks/hooks/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { Component, DangerousHTML } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

class DangerousHTMLWithWarning extends Component {
constructor() {
super( ...arguments );

// Disable reason: We're intentionally logging a console warning
// advising the developer to upgrade usage.

// eslint-disable-next-line no-console
console.warn(
'Deprecated: String returns from block `save` is unsupported. ' +
'Use `wp.element.DangerousHTML` component instead.\n\n' +
'See: '
);
}

render() {
const { children } = this.props;

return <DangerousHTML>{ children }</DangerousHTML>;
}
}

/**
* Override save element for a block, providing support for deprecated string
* return value, logging a warning advising the developer to use the preferred
* DangerousHTML component instead.
*
* @param {(string|Object)} element Original block save return.
*
* @returns {Object} Filtered props applied to save element.
*/
export function shimDangerousHTML( element ) {
if ( typeof element !== 'string' ) {
return element;
}

return <DangerousHTMLWithWarning children={ element } />;
}

addFilter( 'blocks.getSaveContent.saveElement', 'core/deprecated/save-props', shimDangerousHTML );
1 change: 1 addition & 0 deletions blocks/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
*/
import './anchor';
import './custom-class-name';
import './deprecated';
import './generated-class-name';
import './matchers';

0 comments on commit b310d0e

Please sign in to comment.