-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block API: Add hook for deprecated raw HTML support
- Loading branch information
Showing
4 changed files
with
84 additions
and
5 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,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { includes } from 'lodash'; | ||
|
||
/** | ||
* 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: https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/#save' | ||
); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
|
||
return <DangerousHTML>{ children }</DangerousHTML>; | ||
} | ||
} | ||
|
||
/** | ||
* Override save element for a block, providing support for deprecated HTML | ||
* return value, logging a warning advising the developer to use the preferred | ||
* DangerousHTML component instead. | ||
* | ||
* @param {(string|Object|Array)} element Original block save return. | ||
* | ||
* @returns {(string|Object|Array)} Dangerously shimmed block save. | ||
*/ | ||
export function shimDangerousHTML( element ) { | ||
// Still support string return from save, but in the same way any component | ||
// could render a string, it should be escaped. Therefore, only shim usage | ||
// which had included some HTML expected to be unescaped. | ||
if ( typeof element === 'string' && includes( '<', element ) ) { | ||
element = <DangerousHTMLWithWarning children={ element } />; | ||
} | ||
|
||
return element; | ||
} | ||
|
||
addFilter( 'blocks.getSaveContent.saveElement', 'core/deprecated/save-props', shimDangerousHTML ); |
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