From a5b0824c853fa8146dee4e2528143dfff65971b1 Mon Sep 17 00:00:00 2001 From: Andy Peatling Date: Tue, 2 Mar 2021 02:19:32 -0800 Subject: [PATCH] Check the content of a button and file download button and strip anchor tags before saving. (#29273) --- packages/block-library/src/button/edit.js | 7 ++++++- packages/block-library/src/file/edit.js | 15 +++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index fe32f2ab5d837..096f785748472 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -230,6 +230,11 @@ function ButtonEdit( props ) { [ rel, setAttributes ] ); + const setButtonText = ( newText ) => { + // Remove anchor tags from button text content. + setAttributes( { text: newText.replace( /<\/?a[^>]*>/g, '' ) } ); + }; + const colorProps = getColorAndStyleProps( attributes, colors, true ); const ref = useRef(); const blockProps = useBlockProps( { ref } ); @@ -247,7 +252,7 @@ function ButtonEdit( props ) { aria-label={ __( 'Button text' ) } placeholder={ placeholder || __( 'Add text…' ) } value={ text } - onChange={ ( value ) => setAttributes( { text: value } ) } + onChange={ ( value ) => setButtonText( value ) } withoutInteractiveFormatting className={ classnames( className, diff --git a/packages/block-library/src/file/edit.js b/packages/block-library/src/file/edit.js index 25ca100381db4..4ece3165939f0 100644 --- a/packages/block-library/src/file/edit.js +++ b/packages/block-library/src/file/edit.js @@ -89,9 +89,7 @@ function FileEdit( { attributes, setAttributes, noticeUI, noticeOperations } ) { } if ( downloadButtonText === undefined ) { - setAttributes( { - downloadButtonText: _x( 'Download', 'button label' ), - } ); + changeDownloadButtonText( _x( 'Download', 'button label' ) ); } }, [] ); @@ -128,6 +126,13 @@ function FileEdit( { attributes, setAttributes, noticeUI, noticeOperations } ) { setAttributes( { showDownloadButton: newValue } ); } + function changeDownloadButtonText( newValue ) { + // Remove anchor tags from button text content. + setAttributes( { + downloadButtonText: newValue.replace( /<\/?a[^>]*>/g, '' ), + } ); + } + const attachmentPage = media && media.link; const blockProps = useBlockProps( { @@ -214,9 +219,7 @@ function FileEdit( { attributes, setAttributes, noticeUI, noticeOperations } ) { withoutInteractiveFormatting placeholder={ __( 'Add text…' ) } onChange={ ( text ) => - setAttributes( { - downloadButtonText: text, - } ) + changeDownloadButtonText( text ) } />