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

Clarify updateNavigationLinkBlockAttributes #41657

Merged
Merged
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
32 changes: 20 additions & 12 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,28 @@ export const updateNavigationLinkBlockAttributes = (
} = blockAttributes;

const {
title = '', // the title of any provided Post.
url = '',
title: newLabel = '', // the title of any provided Post.
url: newUrl = '',

opensInNewTab,
id,
kind: newKind = originalKind,
type: newType = originalType,
} = updatedValue;
const normalizedTitle = title.replace( /http(s?):\/\//gi, '' );
const normalizedURL = url.replace( /http(s?):\/\//gi, '' );
const escapeTitle =
title !== '' &&
normalizedTitle !== normalizedURL &&
originalLabel !== title;

const newLabelWithoutHttp = newLabel.replace( /http(s?):\/\//gi, '' );
const newUrlWithoutHttp = newUrl.replace( /http(s?):\/\//gi, '' );

const useNewLabel =
newLabel &&
newLabel !== originalLabel &&
// LinkControl without the title field relies
// on the check below. Specifically, it assumes that
// the URL is the same as a title.
// This logic a) looks suspicious and b) should really
// live in the LinkControl and not here. It's a great
// candidate for future refactoring.
newLabelWithoutHttp !== newUrlWithoutHttp;

// Unfortunately this causes the escaping model to be inverted.
// The escaped content is stored in the block attributes (and ultimately in the database),
Expand All @@ -249,9 +257,9 @@ export const updateNavigationLinkBlockAttributes = (
// See also:
// - https://github.com/WordPress/gutenberg/pull/41063
// - https://github.com/WordPress/gutenberg/pull/18617.
const label = escapeTitle
? escape( title )
: originalLabel || escape( normalizedURL );
const label = useNewLabel
? escape( newLabel )
: originalLabel || escape( newUrlWithoutHttp );

// In https://github.com/WordPress/gutenberg/pull/24670 we decided to use "tag" in favor of "post_tag"
const type = newType === 'post_tag' ? 'tag' : newType.replace( '-', '_' );
Expand All @@ -265,7 +273,7 @@ export const updateNavigationLinkBlockAttributes = (

setAttributes( {
// Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string.
...( url && { url: encodeURI( safeDecodeURI( url ) ) } ),
...( newUrl && { url: encodeURI( safeDecodeURI( newUrl ) ) } ),
...( label && { label } ),
...( undefined !== opensInNewTab && { opensInNewTab } ),
...( id && Number.isInteger( id ) && { id } ),
Expand Down