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: Image block error #1614

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 2 additions & 11 deletions src/hoc/withHtmlAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { applyFilters } from '@wordpress/hooks';
import { useUpdateEffect } from 'react-use';

import { convertInlineStyleStringToObject } from '@utils/convertInlineStyleStringToObject';
import { sanitizeHtmlAttribute } from '@utils/sanitizeHtmlAttribute';

export const booleanAttributes = [
'allowfullscreen',
Expand Down Expand Up @@ -60,16 +61,6 @@ function shallowEqual( obj1, obj2 ) {
return true;
}

const sanitizeAttributeValue = ( value ) => {
// Replace characters like &, <, >, " with their HTML entity equivalents
return value
.replace( /&/g, '&amp;' )
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
.replace( /"/g, '&quot;' )
.replace( /'/g, '&#039;' );
};

export function withHtmlAttributes( WrappedComponent ) {
return ( ( props ) => {
const {
Expand All @@ -88,7 +79,7 @@ export function withHtmlAttributes( WrappedComponent ) {
const isSavingPost = useSelect( ( select ) => select( 'core/editor' ).isSavingPost() );
const { style = '', href, ...otherAttributes } = htmlAttributes;
const escapedAttributes = Object.keys( otherAttributes ).reduce( ( acc, key ) => {
acc[ key ] = sanitizeAttributeValue( otherAttributes[ key ] );
acc[ key ] = sanitizeHtmlAttribute( otherAttributes[ key ] );
return acc;
}, {} );
const [ processedStyle, setProcessedStyle ] = useState( style );
Expand Down
49 changes: 49 additions & 0 deletions src/tests/unit/sanitizeHtmlAttribute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { sanitizeHtmlAttribute } from '../../utils/sanitizeHtmlAttribute';

describe( 'sanitize HTML attribute', () => {
it( 'should convert a number to a string', () => {
const value = sanitizeHtmlAttribute( 500 );
expect( value ).toEqual( '500' );
} );

it( 'should convert an object to a string', () => {
const value = sanitizeHtmlAttribute( { foo: 'bar' } );
expect( value ).toEqual( '{&quot;foo&quot;:&quot;bar&quot;}' );
} );

it( 'should convert an array to a string', () => {
const value = sanitizeHtmlAttribute( [ 'foo', 'bar' ] );
expect( value ).toEqual( '[&quot;foo&quot;,&quot;bar&quot;]' );
} );

it( 'should handle undefined', () => {
const value = sanitizeHtmlAttribute( undefined );
expect( value ).toEqual( '' );
} );

it( 'should handle null', () => {
const value = sanitizeHtmlAttribute( null );
expect( value ).toEqual( '' );
} );

it( 'should escape HTML special characters', () => {
const value = sanitizeHtmlAttribute( 'foo & <bar> "quote" \'single\'' );
expect( value ).toEqual( 'foo &amp; &lt;bar&gt; &quot;quote&quot; &#039;single&#039;' );
} );

it( 'should handle boolean values', () => {
expect( sanitizeHtmlAttribute( true ) ).toEqual( 'true' );
expect( sanitizeHtmlAttribute( false ) ).toEqual( 'false' );
} );

it( 'should handle special number values', () => {
expect( sanitizeHtmlAttribute( NaN ) ).toEqual( 'NaN' );
expect( sanitizeHtmlAttribute( Infinity ) ).toEqual( 'Infinity' );
expect( sanitizeHtmlAttribute( -Infinity ) ).toEqual( '-Infinity' );
} );

it( 'should handle empty string', () => {
const value = sanitizeHtmlAttribute( '' );
expect( value ).toEqual( '' );
} );
} );
25 changes: 25 additions & 0 deletions src/utils/sanitizeHtmlAttribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const sanitizeHtmlAttribute = ( value ) => {
if ( null === value || undefined === value ) {
return '';
}

let stringValue = '';

if ( 'object' === typeof value ) {
try {
stringValue = JSON.stringify( value );
} catch ( e ) {
return '';
}
} else {
stringValue = String( value );
}

// Replace characters like &, <, >, " with their HTML entity equivalents
return stringValue
.replace( /&/g, '&amp;' )
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
.replace( /"/g, '&quot;' )
.replace( /'/g, '&#039;' );
};
Loading