Skip to content

Commit

Permalink
Check the content of a button and file download button and strip anch…
Browse files Browse the repository at this point in the history
…or tags before saving. (#29273)
  • Loading branch information
apeatling committed Mar 2, 2021
1 parent f3327b1 commit a5b0824
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } );
Expand All @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ function FileEdit( { attributes, setAttributes, noticeUI, noticeOperations } ) {
}

if ( downloadButtonText === undefined ) {
setAttributes( {
downloadButtonText: _x( 'Download', 'button label' ),
} );
changeDownloadButtonText( _x( 'Download', 'button label' ) );
}
}, [] );

Expand Down Expand Up @@ -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( {
Expand Down Expand Up @@ -214,9 +219,7 @@ function FileEdit( { attributes, setAttributes, noticeUI, noticeOperations } ) {
withoutInteractiveFormatting
placeholder={ __( 'Add text…' ) }
onChange={ ( text ) =>
setAttributes( {
downloadButtonText: text,
} )
changeDownloadButtonText( text )
}
/>
</div>
Expand Down

0 comments on commit a5b0824

Please sign in to comment.