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

Decode HTML entities created by block parser matcher reliance on innerHTML #25120

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 15 additions & 1 deletion packages/blocks/src/api/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,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;
Expand Down Expand Up @@ -38,6 +52,6 @@ export function html( selector, multilineTag ) {
return value;
}

return match.innerHTML;
return replaceInnerHTMLEntities( match.innerHTML );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can change this behaviour at this point. That's why I added a new type. Otherwise we could just return the raw html value here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played with this a bit more today. You're correct we cannot change the behaviour. It even breaks Core block let alone any third party blocks.

I'm now more convinced than ever that having a raw source type such as you propose is the best solution.

};
}