Repository navigation
Blocks: Add innerContent support for static inner blocks, adopt it in the HTML block #79115
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3ee7936
Blocks: Add innerContent support for static inner blocks, adopt it in…
youknowriad bc90e92
Don't cascade the static inner content lock into nested containers
youknowriad ee2d7e7
HTML block: Use own class names for the inspector Edit code button
youknowriad 4900d91
Fix e2e tests for the HTML block's attribute-less content
youknowriad 6362673
HTML block: Skip no-op updates from the code modal
youknowriad 9fe50a5
Reword round-trip phrasing as parsing and serializing
youknowriad 09086d7
HTML block: Keep the content attribute as a back-compat shim
youknowriad 4d49003
HTML block: Simplify content migration and drop the save fallback
youknowriad 44ab01f
Block editor: Sanitize InnerContent static markup with safeHTML
youknowriad 44c0861
Collaboration: Cover innerContent sync for the HTML block
youknowriad e922e3b
HTML block: Reconstruct inner blocks when transforming from code
youknowriad 6cbeb42
HTML block: Return the parsed block directly from the code transform
youknowriad a823cd5
Blocks: Warn when createBlock gets innerContent without block support
youknowriad ae84bc8
Limit static inner content to the Custom HTML block
youknowriad 6caef11
Core data: Add innerContent to the CRDT Block type
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
108 changes: 108 additions & 0 deletions
108
packages/block-editor/src/components/inner-content/index.js
This file contains hidden or 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,108 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| createPortal, | ||
| useLayoutEffect, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from '@wordpress/element'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { safeHTML } from '@wordpress/dom'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../store'; | ||
| import BlockListBlock from '../block-list/block'; | ||
| import { LayoutProvider } from '../block-list/layout'; | ||
|
|
||
| const SLOT_TAG_NAME = 'wp-inner-block-slot'; | ||
|
|
||
| // The static markup surrounding the inner blocks is arbitrary HTML the block | ||
| // editor doesn't manage, so it provides no layout context: no alignments are | ||
| // available to the inner blocks. | ||
| const LAYOUT = { type: 'default', alignments: [] }; | ||
|
|
||
| /** | ||
| * Renders the static HTML fragments of a block with the `innerContent` | ||
| * support, mounting each inner block at the position of its `null` | ||
| * placeholder within the static markup. | ||
| * | ||
| * Unlike `InnerBlocks`, the rendered blocks are not a contiguous list: they | ||
| * live at arbitrary positions inside markup the block editor doesn't manage. | ||
| * The static fragments are sanitized with `safeHTML` before being injected | ||
| * into the canvas — the same treatment block save content receives elsewhere | ||
| * in the editor — and the inner blocks are portalled into placeholder | ||
| * elements. Inner blocks are locked: they can be edited in place but not | ||
| * moved, removed, or added to. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {string} props.clientId Client ID of the block whose inner content | ||
| * should be rendered. | ||
| */ | ||
| export default function InnerContent( { clientId } ) { | ||
| const { innerContent, order } = useSelect( | ||
| ( select ) => { | ||
| const { getBlock, getBlockOrder } = select( blockEditorStore ); | ||
| return { | ||
| innerContent: getBlock( clientId )?.innerContent, | ||
| order: getBlockOrder( clientId ), | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
| const html = useMemo( () => { | ||
| let slotIndex = 0; | ||
| return ( innerContent ?? [] ) | ||
| .map( ( item ) => | ||
| item === null | ||
| ? `<${ SLOT_TAG_NAME } data-slot-index="${ slotIndex++ }" style="display: contents"></${ SLOT_TAG_NAME }>` | ||
| : item | ||
| ) | ||
| .join( '' ); | ||
| }, [ innerContent ] ); | ||
|
|
||
| const containerRef = useRef(); | ||
| const [ slots, setSlots ] = useState( [] ); | ||
|
|
||
| useLayoutEffect( () => { | ||
| const container = containerRef.current; | ||
|
|
||
| // Sanitize before injecting into the canvas: `safeHTML` removes | ||
| // `<script>` elements and inline event handlers, matching how block | ||
| // save content is rendered elsewhere in the editor. | ||
| container.innerHTML = safeHTML( html ); | ||
|
|
||
| setSlots( | ||
| Array.from( container.querySelectorAll( SLOT_TAG_NAME ) ).sort( | ||
| ( a, b ) => | ||
| Number( a.dataset.slotIndex ) - | ||
| Number( b.dataset.slotIndex ) | ||
| ) | ||
| ); | ||
| }, [ html ] ); | ||
|
|
||
| return ( | ||
| <LayoutProvider value={ LAYOUT }> | ||
| <div | ||
| ref={ containerRef } | ||
| className="block-editor-inner-content" | ||
| style={ { display: 'contents' } } | ||
| /> | ||
| { order.map( ( childClientId, index ) => | ||
| slots[ index ] | ||
| ? createPortal( | ||
| <BlockListBlock | ||
| rootClientId={ clientId } | ||
| clientId={ childClientId } | ||
| />, | ||
| slots[ index ], | ||
| childClientId | ||
| ) | ||
| : null | ||
| ) } | ||
| </LayoutProvider> | ||
| ); | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
packages/block-editor/src/components/inner-content/test/index.js
This file contains hidden or 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,81 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { render } from '@testing-library/react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| createBlock, | ||
| registerBlockType, | ||
| unregisterBlockType, | ||
| } from '@wordpress/blocks'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import InnerContent from '../'; | ||
| import { BlockEditorProvider } from '../../provider'; | ||
|
|
||
| const BLOCK_NAME = 'core/html'; | ||
|
|
||
| // These tests inspect the raw injected DOM to verify the static markup is | ||
| // sanitized before being added to the editor canvas, so direct node access is | ||
| // intentional. | ||
| /* eslint-disable testing-library/no-node-access, testing-library/no-container, testing-library/render-result-naming-convention */ | ||
|
|
||
| function renderWithInnerContent( innerContent ) { | ||
| const block = createBlock( BLOCK_NAME, {}, [], innerContent ); | ||
| const { container } = render( | ||
| <BlockEditorProvider value={ [ block ] }> | ||
| <InnerContent clientId={ block.clientId } /> | ||
| </BlockEditorProvider> | ||
| ); | ||
| return container.querySelector( '.block-editor-inner-content' ); | ||
| } | ||
|
|
||
| describe( 'InnerContent', () => { | ||
| beforeAll( () => { | ||
| registerBlockType( BLOCK_NAME, { | ||
| apiVersion: 3, | ||
| title: 'Custom HTML', | ||
| category: 'text', | ||
| save: () => null, | ||
| } ); | ||
| } ); | ||
|
|
||
| afterAll( () => { | ||
| unregisterBlockType( BLOCK_NAME ); | ||
| } ); | ||
|
|
||
| it( 'renders the static fragments into the canvas', () => { | ||
| const root = renderWithInnerContent( [ | ||
| '<div class="banner"><h1>Static heading</h1></div>', | ||
| ] ); | ||
|
|
||
| expect( root.querySelector( 'h1' ) ).toHaveTextContent( | ||
| 'Static heading' | ||
| ); | ||
| } ); | ||
|
|
||
| it( 'strips script elements from the static fragments', () => { | ||
| const root = renderWithInnerContent( [ | ||
| '<div><script>window.__innerContentRan = true;</script>Safe</div>', | ||
| ] ); | ||
|
|
||
| expect( root.querySelector( 'script' ) ).toBeNull(); | ||
| expect( root ).toHaveTextContent( 'Safe' ); | ||
| } ); | ||
|
|
||
| it( 'strips inline event handlers from the static fragments', () => { | ||
| const root = renderWithInnerContent( [ | ||
| '<button onclick="alert(1)">Click</button>', | ||
| ] ); | ||
|
|
||
| expect( root.querySelector( '[onclick]' ) ).toBeNull(); | ||
| expect( root.querySelector( 'button' ) ).toHaveTextContent( 'Click' ); | ||
| } ); | ||
| } ); | ||
|
|
||
| /* eslint-enable testing-library/no-node-access, testing-library/no-container, testing-library/render-result-naming-convention */ |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heh, clever :)