Skip to content
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

Blocks: optimise parser by batch parsing HTML to DOM #57180

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ _Parameters_
- _blockTypeOrName_ `string|Object`: Block type or name.
- _innerHTML_ `string|Node`: Raw block content.
- _attributes_ `?Object`: Known block attributes (from delimiters).
- _innerDom_ `?Node`: Raw block content parsed into DOM.

_Returns_

Expand Down
7 changes: 4 additions & 3 deletions packages/blocks/src/api/parser/get-block-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,16 @@ export function parseWithAttributeSchema( innerHTML, attributeSchema ) {
* @param {string|Object} blockTypeOrName Block type or name.
* @param {string|Node} innerHTML Raw block content.
* @param {?Object} attributes Known block attributes (from delimiters).
*
* @param {?Node} innerDom Raw block content parsed into DOM.
* @return {Object} All block attributes.
*/
export function getBlockAttributes(
blockTypeOrName,
innerHTML,
attributes = {}
attributes = {},
innerDom
) {
const doc = parseHtml( innerHTML );
const doc = innerDom ? innerDom : parseHtml( innerHTML );
const blockType = normalizeBlockType( blockTypeOrName );

const blockAttributes = Object.fromEntries(
Expand Down
41 changes: 39 additions & 2 deletions packages/blocks/src/api/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ export function parseRawBlock( rawBlock, options ) {
getBlockAttributes(
blockType,
normalizedBlock.innerHTML,
normalizedBlock.attrs
normalizedBlock.attrs,
normalizedBlock.innerDom
),
parsedInnerBlocks
);
Expand Down Expand Up @@ -308,7 +309,43 @@ export function parseRawBlock( rawBlock, options ) {
* @return {Array} Block list.
*/
export default function parse( content, options ) {
return grammarParse( content ).reduce( ( accumulator, rawBlock ) => {
const rawBlocks = grammarParse( content ).filter(
// Down the road, we trim content, so let's ignore empty sections
// between blocks early.
( rawBlock ) => rawBlock.innerHTML.trim() || rawBlock.blockName
);

let allInnerHtml = '';

function addInnerHtml( _rawBlocks ) {
for ( const rawBlock of _rawBlocks ) {
allInnerHtml += '<div>' + rawBlock.innerHTML + `</div>`;
if ( rawBlock.innerBlocks.length ) {
addInnerHtml( rawBlock.innerBlocks );
}
}
}

addInnerHtml( rawBlocks );

const doc = document.implementation.createHTMLDocument( '' );

doc.body.innerHTML = allInnerHtml;

allInnerHtml = Array.from( doc.body.children );

function addInnerDom( _rawBlocks ) {
for ( const rawBlock of _rawBlocks ) {
rawBlock.innerDom = allInnerHtml.shift();
if ( rawBlock.innerBlocks.length ) {
addInnerDom( rawBlock.innerBlocks );
}
}
}

addInnerDom( rawBlocks );

return rawBlocks.reduce( ( accumulator, rawBlock ) => {
const block = parseRawBlock( rawBlock, options );
if ( block ) {
accumulator.push( block );
Expand Down
Loading