diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index 3191956f8d99b0..6fd55acbbb60a1 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -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_ diff --git a/packages/editor/src/components/post-url/index.js b/packages/editor/src/components/post-url/index.js index f55ac973be50e6..b522b5622af5d2 100644 --- a/packages/editor/src/components/post-url/index.js +++ b/packages/editor/src/components/post-url/index.js @@ -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, @@ -151,7 +154,8 @@ export default function PostURL( { onClose } ) { onBlur={ ( event ) => { editPost( { slug: cleanForSlug( - event.target.value + event.target.value, + siteLocale ), } ); if ( forceEmptyField ) { diff --git a/packages/editor/src/components/post-url/panel.js b/packages/editor/src/components/post-url/panel.js index 97eaa8ccb77f8d..8070f81bc07241 100644 --- a/packages/editor/src/components/post-url/panel.js +++ b/packages/editor/src/components/post-url/panel.js @@ -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 ); diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index 125df2865c2f82..6b0138b47e7447 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -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 = '' ) { return ( getEditedPostAttribute( state, 'slug' ) || - cleanForSlug( getEditedPostAttribute( state, 'title' ) ) || + cleanForSlug( getEditedPostAttribute( state, 'title' ), locale ) || getCurrentPostId( state ) ); } diff --git a/packages/url/README.md b/packages/url/README.md index 86159b22c20343..202d435a410cbf 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -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_ diff --git a/packages/url/src/clean-for-slug.ts b/packages/url/src/clean-for-slug.ts index a6dd495ff61f4f..021786a9787fb7 100644 --- a/packages/url/src/clean-for-slug.ts +++ b/packages/url/src/clean-for-slug.ts @@ -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; + } + return LOCALE_CHAR_MAP[ locale ]; +} + /** * Performs some basic cleanup of a string for use as a post slug. * @@ -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  , &ndash, and &mdash to hyphens. diff --git a/packages/url/src/test/compare-slug-php-parity.test.js b/packages/url/src/test/compare-slug-php-parity.test.js new file mode 100644 index 00000000000000..79299670929709 --- /dev/null +++ b/packages/url/src/test/compare-slug-php-parity.test.js @@ -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 ); + } + ); +} ); diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index 4496e54f242100..e74c1b84a57342 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -1220,6 +1220,58 @@ describe( 'cleanForSlug', () => { cleanForSlug( 'No   Entities> – Here —<' ) ).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', () => {