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

fix: only escape HTML if invalid #3399

Merged
merged 1 commit into from
Jan 27, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/block-components/typography/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ShadowControl,
} from '~stackable/components'
import { getAttributeName, getAttrNameFunction } from '~stackable/util'
import { escapeHTMLIfInvalid } from './util'

/**
* WordPress dependencies
Expand All @@ -28,7 +29,6 @@ import {
useEffect, useState, useCallback, memo,
} from '@wordpress/element'
import { __ } from '@wordpress/i18n'
import { escapeHTML } from '@wordpress/escape-html'
import { applyFilters } from '@wordpress/hooks'

const TYPOGRAPHY_SHADOWS = [
Expand Down Expand Up @@ -98,7 +98,7 @@ export const Controls = props => {
return () => clearTimeout( timeout )
}, [ updateAttribute, debouncedText, text ] )

const onChangeContent = useCallback( text => setDebouncedText( escapeHTML( text ) ), [] )
const onChangeContent = useCallback( text => setDebouncedText( escapeHTMLIfInvalid( text ) ), [] )

return (
<>
Expand Down
14 changes: 14 additions & 0 deletions src/block-components/typography/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { escapeHTML } from '@wordpress/escape-html'

export const escapeHTMLIfInvalid = html => {
const parser = new DOMParser()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any performance issues because we're doing this every keypress?

const doc = parser.parseFromString( html, 'text/html' )
const parseError = doc.querySelector( 'parsererror' )
const serialized = doc.body.innerHTML.trim()

// If valid, return the raw HTML
if ( ! parseError && serialized === html.trim() ) {
return html
}
return escapeHTML( html )
}
Loading