From 97eb4a4db1b0f42ca77a1eff335b632584d11226 Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Wed, 7 Dec 2022 12:19:03 +0200 Subject: [PATCH] HTML block: fix parsing (#27268) --- packages/block-library/src/html/block.json | 2 +- .../src/api/parser/get-block-attributes.js | 16 +++++++++++----- schemas/json/block.json | 1 + test/e2e/specs/editor/blocks/html.spec.js | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/html/block.json b/packages/block-library/src/html/block.json index e72a674373e14..c1e1e94b87496 100644 --- a/packages/block-library/src/html/block.json +++ b/packages/block-library/src/html/block.json @@ -10,7 +10,7 @@ "attributes": { "content": { "type": "string", - "source": "html" + "source": "raw" } }, "supports": { diff --git a/packages/blocks/src/api/parser/get-block-attributes.js b/packages/blocks/src/api/parser/get-block-attributes.js index b9fd3dcf67508..c8d7aa88204cc 100644 --- a/packages/blocks/src/api/parser/get-block-attributes.js +++ b/packages/blocks/src/api/parser/get-block-attributes.js @@ -104,16 +104,18 @@ export function isOfTypes( value, types ) { * * @param {string} attributeKey Attribute key. * @param {Object} attributeSchema Attribute's schema. - * @param {string|Node} innerHTML Block's raw content. + * @param {Node} innerDOM Parsed DOM of block's inner HTML. * @param {Object} commentAttributes Block's comment attributes. + * @param {string} innerHTML Raw HTML from block node's innerHTML property. * * @return {*} Attribute value. */ export function getBlockAttribute( attributeKey, attributeSchema, - innerHTML, - commentAttributes + innerDOM, + commentAttributes, + innerHTML ) { let value; @@ -125,6 +127,10 @@ export function getBlockAttribute( ? commentAttributes[ attributeKey ] : undefined; break; + // raw source means that it's the original raw block content. + case 'raw': + value = innerHTML; + break; case 'attribute': case 'property': case 'html': @@ -133,7 +139,7 @@ export function getBlockAttribute( case 'node': case 'query': case 'tag': - value = parseWithAttributeSchema( innerHTML, attributeSchema ); + value = parseWithAttributeSchema( innerDOM, attributeSchema ); break; } @@ -270,7 +276,7 @@ export function getBlockAttributes( const blockType = normalizeBlockType( blockTypeOrName ); const blockAttributes = mapValues( blockType.attributes, ( schema, key ) => - getBlockAttribute( key, schema, doc, attributes ) + getBlockAttribute( key, schema, doc, attributes, innerHTML ) ); return applyFilters( diff --git a/schemas/json/block.json b/schemas/json/block.json index 93ed4e5a12b94..f23ef249577bd 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -160,6 +160,7 @@ "attribute", "text", "html", + "raw", "query", "meta" ] diff --git a/test/e2e/specs/editor/blocks/html.spec.js b/test/e2e/specs/editor/blocks/html.spec.js index dedb9197984fc..77e9d2a9186a7 100644 --- a/test/e2e/specs/editor/blocks/html.spec.js +++ b/test/e2e/specs/editor/blocks/html.spec.js @@ -30,4 +30,20 @@ test.describe( 'HTML block', () => { ` ); } ); + + test( 'should not encode <', async ( { editor, page } ) => { + // Create a Custom HTML block with the slash shortcut. + await page.click( 'role=button[name="Add default block"i]' ); + await page.keyboard.type( '/html' ); + await expect( + page.locator( 'role=option[name="Custom HTML"i][selected]' ) + ).toBeVisible(); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '1 < 2' ); + await editor.publishPost(); + await page.reload(); + await expect( + page.locator( '[data-type="core/html"] textarea' ) + ).toBeVisible(); + } ); } );