From 54ff529337c1ae553a49b73050d22a24f4ad0d72 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Fri, 24 Nov 2023 12:05:35 +0900 Subject: [PATCH 1/3] File block: Remove anchor tag when copy pasting to file name --- packages/block-library/src/button/edit.js | 12 ++++++++---- packages/block-library/src/file/edit.js | 23 +++++++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index 8400dabddad348..bb7c9f41acf821 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -166,9 +166,9 @@ function ButtonEdit( props ) { const TagName = tagName || 'a'; - function setButtonText( newText ) { - // Remove anchor tags from button text content. - setAttributes( { text: newText.replace( /<\/?a[^>]*>/g, '' ) } ); + // Remove anchor tags from button text content. + function removeAnchorTag( newValue ) { + return newValue.replace( /<\/?a[^>]*>/g, '' ); } function onKeyDown( event ) { @@ -245,7 +245,11 @@ function ButtonEdit( props ) { aria-label={ __( 'Button text' ) } placeholder={ placeholder || __( 'Add text…' ) } value={ text } - onChange={ ( value ) => setButtonText( value ) } + onChange={ ( value ) => + setAttributes( { + text: removeAnchorTag( value ), + } ) + } withoutInteractiveFormatting className={ classnames( className, diff --git a/packages/block-library/src/file/edit.js b/packages/block-library/src/file/edit.js index 733cdf9a9351fc..80f8fe3a09354e 100644 --- a/packages/block-library/src/file/edit.js +++ b/packages/block-library/src/file/edit.js @@ -102,7 +102,11 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { } if ( downloadButtonText === undefined ) { - changeDownloadButtonText( _x( 'Download', 'button label' ) ); + setAttributes( { + downloadButtonText: removeAnchorTag( + _x( 'Download', 'button label' ) + ), + } ); } }, [] ); @@ -148,11 +152,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { setAttributes( { showDownloadButton: newValue } ); } - function changeDownloadButtonText( newValue ) { - // Remove anchor tags from button text content. - setAttributes( { - downloadButtonText: newValue.replace( /<\/?a[^>]*>/g, '' ), - } ); + // Remove anchor tags from file name and button text content. + function removeAnchorTag( value ) { + return value.replace( /<\/?a[^>]*>/g, '' ); } function changeDisplayPreview( newValue ) { @@ -277,7 +279,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { placeholder={ __( 'Write file name…' ) } withoutInteractiveFormatting onChange={ ( text ) => - setAttributes( { fileName: text } ) + setAttributes( { + fileName: removeAnchorTag( text ), + } ) } href={ textLinkHref } /> @@ -301,7 +305,10 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { withoutInteractiveFormatting placeholder={ __( 'Add text…' ) } onChange={ ( text ) => - changeDownloadButtonText( text ) + setAttributes( { + downloadButtonText: + removeAnchorTag( text ), + } ) } /> From 0bf95cd4d2d98510604fe82a1bebe9a3c19ae10d Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Sat, 25 Nov 2023 01:23:17 +0900 Subject: [PATCH 2/3] Don't strip default button text --- packages/block-library/src/file/edit.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/block-library/src/file/edit.js b/packages/block-library/src/file/edit.js index 80f8fe3a09354e..a2bb675d3b75a8 100644 --- a/packages/block-library/src/file/edit.js +++ b/packages/block-library/src/file/edit.js @@ -103,9 +103,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { if ( downloadButtonText === undefined ) { setAttributes( { - downloadButtonText: removeAnchorTag( - _x( 'Download', 'button label' ) - ), + downloadButtonText: _x( 'Download', 'button label' ), } ); } }, [] ); From f0618f48373b07e330d52dae69b0707123d8c806 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Mon, 27 Nov 2023 20:54:41 +0900 Subject: [PATCH 3/3] eEtract removeAnchorTag() function --- packages/block-library/src/button/edit.js | 6 +----- packages/block-library/src/file/edit.js | 6 +----- packages/block-library/src/utils/remove-anchor-tag.js | 10 ++++++++++ 3 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 packages/block-library/src/utils/remove-anchor-tag.js diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index bb7c9f41acf821..d41d917d5b8fe1 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -8,6 +8,7 @@ import classnames from 'classnames'; */ import { NEW_TAB_TARGET, NOFOLLOW_REL } from './constants'; import { getUpdatedLinkAttributes } from './get-updated-link-attributes'; +import removeAnchorTag from '../utils/remove-anchor-tag'; /** * WordPress dependencies @@ -166,11 +167,6 @@ function ButtonEdit( props ) { const TagName = tagName || 'a'; - // Remove anchor tags from button text content. - function removeAnchorTag( newValue ) { - return newValue.replace( /<\/?a[^>]*>/g, '' ); - } - function onKeyDown( event ) { if ( isKeyboardEvent.primary( event, 'k' ) ) { startEditing( event ); diff --git a/packages/block-library/src/file/edit.js b/packages/block-library/src/file/edit.js index a2bb675d3b75a8..e3328fd9851c38 100644 --- a/packages/block-library/src/file/edit.js +++ b/packages/block-library/src/file/edit.js @@ -35,6 +35,7 @@ import { store as noticesStore } from '@wordpress/notices'; */ import FileBlockInspector from './inspector'; import { browserSupportsPdfs } from './utils'; +import removeAnchorTag from '../utils/remove-anchor-tag'; export const MIN_PREVIEW_HEIGHT = 200; export const MAX_PREVIEW_HEIGHT = 2000; @@ -150,11 +151,6 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { setAttributes( { showDownloadButton: newValue } ); } - // Remove anchor tags from file name and button text content. - function removeAnchorTag( value ) { - return value.replace( /<\/?a[^>]*>/g, '' ); - } - function changeDisplayPreview( newValue ) { setAttributes( { displayPreview: newValue } ); } diff --git a/packages/block-library/src/utils/remove-anchor-tag.js b/packages/block-library/src/utils/remove-anchor-tag.js new file mode 100644 index 00000000000000..31d1877082f50d --- /dev/null +++ b/packages/block-library/src/utils/remove-anchor-tag.js @@ -0,0 +1,10 @@ +/** + * Removes anchor tags from a string. + * + * @param {string} value The value to remove anchor tags from. + * + * @return {string} The value with anchor tags removed. + */ +export default function removeAnchorTag( value ) { + return value.replace( /<\/?a[^>]*>/g, '' ); +}