Skip to content

Commit

Permalink
HTML block: fix parsing (#27268)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 7, 2022
1 parent 1d4be43 commit 97eb4a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/html/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"attributes": {
"content": {
"type": "string",
"source": "html"
"source": "raw"
}
},
"supports": {
Expand Down
16 changes: 11 additions & 5 deletions packages/blocks/src/api/parser/get-block-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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':
Expand All @@ -133,7 +139,7 @@ export function getBlockAttribute(
case 'node':
case 'query':
case 'tag':
value = parseWithAttributeSchema( innerHTML, attributeSchema );
value = parseWithAttributeSchema( innerDOM, attributeSchema );
break;
}

Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions schemas/json/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"attribute",
"text",
"html",
"raw",
"query",
"meta"
]
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/specs/editor/blocks/html.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ test.describe( 'HTML block', () => {
<!-- /wp:html -->`
);
} );

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();
} );
} );

0 comments on commit 97eb4a4

Please sign in to comment.