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

File block: Remove anchor tag when copy pasting to file name #56508

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,11 +167,6 @@ function ButtonEdit( props ) {

const TagName = tagName || 'a';

function setButtonText( newText ) {
// Remove anchor tags from button text content.
setAttributes( { text: newText.replace( /<\/?a[^>]*>/g, '' ) } );
}

function onKeyDown( event ) {
if ( isKeyboardEvent.primary( event, 'k' ) ) {
startEditing( event );
Expand Down Expand Up @@ -245,7 +241,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,
Expand Down
21 changes: 11 additions & 10 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +103,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
}

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

Expand Down Expand Up @@ -148,13 +151,6 @@ 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, '' ),
} );
}

function changeDisplayPreview( newValue ) {
setAttributes( { displayPreview: newValue } );
}
Expand Down Expand Up @@ -277,7 +273,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
placeholder={ __( 'Write file name…' ) }
withoutInteractiveFormatting
onChange={ ( text ) =>
setAttributes( { fileName: text } )
setAttributes( {
fileName: removeAnchorTag( text ),
} )
}
href={ textLinkHref }
/>
Expand All @@ -301,7 +299,10 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
withoutInteractiveFormatting
placeholder={ __( 'Add text…' ) }
onChange={ ( text ) =>
changeDownloadButtonText( text )
setAttributes( {
downloadButtonText:
removeAnchorTag( text ),
} )
}
/>
</div>
Expand Down
10 changes: 10 additions & 0 deletions packages/block-library/src/utils/remove-anchor-tag.js
Original file line number Diff line number Diff line change
@@ -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, '' );
}
Loading