From 01e5f838ac3d2bd0b7df14ed8b920ae73bd0f830 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 7 Sep 2020 17:12:51 +0100 Subject: [PATCH 1/2] Decode any entities within the innerHTML --- packages/blocks/src/api/matchers.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/blocks/src/api/matchers.js b/packages/blocks/src/api/matchers.js index 7a6ac84891658a..8deebfcc974740 100644 --- a/packages/blocks/src/api/matchers.js +++ b/packages/blocks/src/api/matchers.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { decodeEntities } from '@wordpress/html-entities'; + /** * External dependencies */ @@ -38,6 +43,6 @@ export function html( selector, multilineTag ) { return value; } - return match.innerHTML; + return decodeEntities( match.innerHTML ); }; } From bc5b16d2f4128d3da026860219d23695a5d5a01b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 14 Sep 2021 16:58:13 +0100 Subject: [PATCH 2/2] Convert subset of HTML entities --- packages/blocks/src/api/matchers.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/blocks/src/api/matchers.js b/packages/blocks/src/api/matchers.js index 8deebfcc974740..919502af208bdf 100644 --- a/packages/blocks/src/api/matchers.js +++ b/packages/blocks/src/api/matchers.js @@ -1,8 +1,3 @@ -/** - * WordPress dependencies - */ -import { decodeEntities } from '@wordpress/html-entities'; - /** * External dependencies */ @@ -14,6 +9,20 @@ export { attr, prop, text, query } from 'hpq'; export { matcher as node } from './node'; export { matcher as children } from './children'; +function replaceInnerHTMLEntities( innerHTML ) { + const entityRegEx = /&(amp|lt|gt);/g; + + // This subset of chars are returned as HTML entities from Element.innerHTML + const lookUp = { + amp: '&', + lt: '<', + gt: '>', + }; + + // Replace entity with text equivalent. + return innerHTML.replace( entityRegEx, ( match, p1 ) => lookUp[ p1 ] ); +} + export function html( selector, multilineTag ) { return ( domNode ) => { let match = domNode; @@ -43,6 +52,6 @@ export function html( selector, multilineTag ) { return value; } - return decodeEntities( match.innerHTML ); + return replaceInnerHTMLEntities( match.innerHTML ); }; }