Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ Returns the slug for the post being edited, preferring a manually edited value i
_Parameters_

- _state_ `Object`: Editor state.
- _locale_ `string`: Optional site locale (e.g. 'de_DE'). When provided, locale-specific digraph rules are applied so the preview matches what the server will generate.

_Returns_

Expand Down
8 changes: 6 additions & 2 deletions packages/editor/src/components/post-url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,22 @@ export default function PostURL( { onClose } ) {
permalinkPrefix,
permalinkSuffix,
permalink,
siteLocale,
} = useSelect( ( select ) => {
const post = select( editorStore ).getCurrentPost();
const postTypeSlug = select( editorStore ).getCurrentPostType();
const postType = select( coreStore ).getPostType( postTypeSlug );
const permalinkParts = select( editorStore ).getPermalinkParts();
const hasPublishAction = post?._links?.[ 'wp:action-publish' ] ?? false;
const locale = select( coreStore ).getSite()?.language ?? '';

return {
isEditable:
select( editorStore ).isPermalinkEditable() && hasPublishAction,
postSlug: safeDecodeURIComponent(
select( editorStore ).getEditedPostSlug()
select( editorStore ).getEditedPostSlug( locale )
),
siteLocale: locale,
viewPostLabel: postType?.labels.view_item,
postLink: post.link,
permalinkPrefix: permalinkParts?.prefix,
Expand Down Expand Up @@ -151,7 +154,8 @@ export default function PostURL( { onClose } ) {
onBlur={ ( event ) => {
editPost( {
slug: cleanForSlug(
event.target.value
event.target.value,
siteLocale
),
} );
if ( forceEmptyField ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/components/post-url/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export default function PostURLPanel() {

function PostURLToggle( { isOpen, onClick } ) {
const { slug } = useSelect( ( select ) => {
const locale = select( coreStore ).getSite()?.language ?? '';
return {
slug: select( editorStore ).getEditedPostSlug(),
slug: select( editorStore ).getEditedPostSlug( locale ),
};
}, [] );
const decodedSlug = safeDecodeURIComponent( slug );
Expand Down
9 changes: 6 additions & 3 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,14 +994,17 @@ export function getPermalink( state ) {
* value if one exists, then a sanitized version of the current post title, and
* finally the post ID.
*
* @param {Object} state Editor state.
* @param {Object} state Editor state.
* @param {string} locale Optional site locale (e.g. 'de_DE'). When provided,
* locale-specific digraph rules are applied so the
* preview matches what the server will generate.
*
* @return {string} The current slug to be displayed in the editor
*/
export function getEditedPostSlug( state ) {
export function getEditedPostSlug( state, locale = '' ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when no locale is provided, would we want to set the default to the one provided by the browser?

things are odd here between site locale and browser locale, but probably there’s a good chance they are the same.

return (
getEditedPostAttribute( state, 'slug' ) ||
cleanForSlug( getEditedPostAttribute( state, 'title' ) ) ||
cleanForSlug( getEditedPostAttribute( state, 'title' ), locale ) ||
getCurrentPostId( state )
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Converts Latin-1 Supplement and Latin Extended-A letters to basic Latin letters.
_Parameters_

- _string_ `string`: Title or slug to be processed.
- _locale_ Optional BCP 47 / WordPress locale string (e.g. 'de_DE'). When provided, locale-specific digraph replacements are applied before generic accent removal, mirroring PHP's remove_accents().

_Returns_

Expand Down
51 changes: 50 additions & 1 deletion packages/url/src/clean-for-slug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@
*/
import removeAccents from 'remove-accents';

/**
* Locale-specific digraph replacements that must run before removeAccents(),
* mirroring the locale block in PHP's remove_accents() (wp-includes/formatting.php).
*/
const LOCALE_CHAR_MAP: Record< string, Record< string, string > > = {
de: {
Ä: 'Ae',
ä: 'ae',
Ö: 'Oe',
ö: 'oe',
Ü: 'Ue',
ü: 'ue',
ẞ: 'SS',
ß: 'ss',
},
da_DK: {
Æ: 'Ae',
æ: 'ae',
Ø: 'Oe',
ø: 'oe',
Å: 'Aa',
å: 'aa',
},
ca: { 'l·l': 'll' },
sr_RS: { Đ: 'DJ', đ: 'dj' },
bs_BA: { Đ: 'DJ', đ: 'dj' },
};

function getLocaleChars(
locale: string
): Record< string, string > | undefined {
// str_starts_with( $locale, 'de' ) covers de_DE, de_CH, de_AT, etc.
if ( locale.startsWith( 'de' ) ) {
return LOCALE_CHAR_MAP.de;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noticed this late in the review, but BCP47 specifies how to match these, see also the W3C spec.

so I think we would want to do something like this, just to avoid problem sin the future with added locales

const mostSpecificLocaleTag = ( objectWithTagKeys, givenTag ) => {
	if ( objectWithTagKeys.hasOwnProperty( givenTag ) ) {
		return givenTag;
	}

	// @todo verify the difference between _ and - here, with Core
	const lastSeparatorAt = givenTag.lastIndexOf( '_' );
	if ( -1 === lastSeparatorAt ) {
		return null;
	}

	return mostSpecificLocaleTag( objectWithTagKeys, givenTag.slice( 0, lastSeparatorAt );
}

and then…

const mostSpecificLocale = mostSpecificLocalTag( LOCALE_CHAR_MAP, locale );

return mostSpecificLocale
	? LOCALE_CHAR_MAP[ mostSpecificLocale ]
	: null;

that was nobody comes in and adds de_CH and wonders why it’s ignored; it will choose de_CH if it’s available, then fall back to de

return LOCALE_CHAR_MAP[ locale ];
}

/**
* Performs some basic cleanup of a string for use as a post slug.
*
Expand All @@ -16,13 +54,24 @@ import removeAccents from 'remove-accents';
* for octets, HTML entities, or other encoded characters.
*
* @param string Title or slug to be processed.
* @param locale Optional BCP 47 / WordPress locale string (e.g. 'de_DE').
* When provided, locale-specific digraph replacements are applied
* before generic accent removal, mirroring PHP's remove_accents().
*
* @return Processed string.
*/
export function cleanForSlug( string: string ): string {
export function cleanForSlug( string: string, locale = '' ): string {
if ( ! string ) {
return '';
}

const localeChars = getLocaleChars( locale );
if ( localeChars ) {
for ( const [ char, replacement ] of Object.entries( localeChars ) ) {
string = string.replaceAll( char, replacement );
}
}

return (
removeAccents( string )
// Convert &nbsp, &ndash, and &mdash to hyphens.
Expand Down
52 changes: 52 additions & 0 deletions packages/url/src/test/compare-slug-php-parity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Comparison script: cleanForSlug( string, locale ) vs PHP sanitize_title()
*
* Run with: npm run test:unit packages/url/src/test/compare-slug-php-parity.test.js
*
* PHP results were generated with:
* npm run wp-env run cli -- wp eval \
* 'foreach($cases as [$l,$i]){echo sanitize_title_with_dashes(remove_accents($i,$l))."\n";}'
*/
import { cleanForSlug } from '../clean-for-slug';

const cases = [
// German — de_DE
{
locale: 'de_DE',
input: 'Künstler überraschen Hörer',
php: 'kuenstler-ueberraschen-hoerer',
},
{
locale: 'de_DE',
input: 'Straße und Öffentlichkeit',
php: 'strasse-und-oeffentlichkeit',
},
{
locale: 'de_DE',
input: 'Ärger mit Übergängen',
php: 'aerger-mit-uebergaengen',
},
// German — capital Eszett (ẞ, U+1E9E), standardized in 2017 (DIN 5008)
{ locale: 'de_DE', input: 'STRAẞE', php: 'strasse' },
// German variants — same digraph rules apply
{ locale: 'de_CH', input: 'Zürich Straße', php: 'zuerich-strasse' },
{ locale: 'de_AT', input: 'Österreich', php: 'oesterreich' },
// Danish
{ locale: 'da_DK', input: 'Æble Ødemark Åre', php: 'aeble-oedemark-aare' },
// Catalan
{ locale: 'ca', input: 'col·legi', php: 'collegi' },
// Serbian / Bosnian
{ locale: 'sr_RS', input: 'Đorđe', php: 'djordje' },
{ locale: 'bs_BA', input: 'Đakovo', php: 'djakovo' },
// No locale — generic diacritic stripping only (ä → a, not ae)
{ locale: '', input: 'Künstler', php: 'kunstler' },
];

describe( 'cleanForSlug() matches PHP sanitize_title() for locale-specific digraphs', () => {
test.each( cases )(
'[$locale] "$input" → "$php"',
( { locale, input, php } ) => {
expect( cleanForSlug( input, locale ) ).toBe( php );
}
);
} );
52 changes: 52 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,58 @@ describe( 'cleanForSlug', () => {
cleanForSlug( 'No &nbsp; Entities> &ndash; Here &mdash;&lt;' )
).toBe( 'no-entities-here' );
} );

describe( 'locale-aware digraph replacements', () => {
it( 'should apply German digraphs for de_DE locale', () => {
expect(
cleanForSlug( 'Künstler überraschen Hörer', 'de_DE' )
).toBe( 'kuenstler-ueberraschen-hoerer' );
} );

it( 'should apply German digraphs for de_CH locale', () => {
expect( cleanForSlug( 'Zürich Straße', 'de_CH' ) ).toBe(
'zuerich-strasse'
);
} );

it( 'should map capital Eszett (ẞ) to SS for de_DE locale', () => {
expect( cleanForSlug( 'STRAẞE', 'de_DE' ) ).toBe( 'strasse' );
} );

it( 'should apply German digraphs for de_AT locale', () => {
expect( cleanForSlug( 'Österreich', 'de_AT' ) ).toBe(
'oesterreich'
);
} );

it( 'should apply Danish digraphs for da_DK locale', () => {
expect( cleanForSlug( 'Æble Ødemark Åre', 'da_DK' ) ).toBe(
'aeble-oedemark-aare'
);
} );

it( 'should apply Catalan middle dot for ca locale', () => {
expect( cleanForSlug( 'col·legi', 'ca' ) ).toBe( 'collegi' );
} );

it( 'should apply Serbian/Bosnian Đ for sr_RS locale', () => {
expect( cleanForSlug( 'Đorđe', 'sr_RS' ) ).toBe( 'djordje' );
} );

it( 'should apply Serbian/Bosnian Đ for bs_BA locale', () => {
expect( cleanForSlug( 'Đakovo', 'bs_BA' ) ).toBe( 'djakovo' );
} );

it( 'should behave identically to no-locale when locale is empty', () => {
expect( cleanForSlug( 'Künstler', '' ) ).toBe(
cleanForSlug( 'Künstler' )
);
} );

it( 'should not apply German digraphs for non-German locale', () => {
expect( cleanForSlug( 'Künstler', 'fr_FR' ) ).toBe( 'kunstler' );
} );
} );
} );

describe( 'normalizePath', () => {
Expand Down
Loading