Skip to content

Commit 950d714

Browse files
authored
fix (content attribute): only escape HTML if invalid (#3399)
1 parent 37327cc commit 950d714

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/block-components/typography/edit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
ShadowControl,
2121
} from '~stackable/components'
2222
import { getAttributeName, getAttrNameFunction } from '~stackable/util'
23+
import { escapeHTMLIfInvalid } from './util'
2324

2425
/**
2526
* WordPress dependencies
@@ -28,7 +29,6 @@ import {
2829
useEffect, useState, useCallback, memo,
2930
} from '@wordpress/element'
3031
import { __ } from '@wordpress/i18n'
31-
import { escapeHTML } from '@wordpress/escape-html'
3232
import { applyFilters } from '@wordpress/hooks'
3333

3434
const TYPOGRAPHY_SHADOWS = [
@@ -98,7 +98,7 @@ export const Controls = props => {
9898
return () => clearTimeout( timeout )
9999
}, [ updateAttribute, debouncedText, text ] )
100100

101-
const onChangeContent = useCallback( text => setDebouncedText( escapeHTML( text ) ), [] )
101+
const onChangeContent = useCallback( text => setDebouncedText( escapeHTMLIfInvalid( text ) ), [] )
102102

103103
return (
104104
<>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { escapeHTML } from '@wordpress/escape-html'
2+
3+
export const escapeHTMLIfInvalid = html => {
4+
const parser = new DOMParser()
5+
const doc = parser.parseFromString( html, 'text/html' )
6+
const parseError = doc.querySelector( 'parsererror' )
7+
const serialized = doc.body.innerHTML.trim()
8+
9+
// If valid, return the raw HTML
10+
if ( ! parseError && serialized === html.trim() ) {
11+
return html
12+
}
13+
return escapeHTML( html )
14+
}

0 commit comments

Comments
 (0)