Skip to content

Commit

Permalink
URL: Fix code drift in the Editor package by removing duplicate clean…
Browse files Browse the repository at this point in the history
…ForSlug function (#39033)

* URL: Fix code drift in the Editor package by removing duplicate clearForSlug function.

* Editor: deprecate cleanForSlug

* Editor: remove the unexported getWPAdminURL and update internal references to use addQueryArgs

* URL: simplify test

* add additional tests for other languages
  • Loading branch information
gwwar committed Feb 25, 2022
1 parent 4bfe584 commit ebb4a8a
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 66 deletions.
4 changes: 2 additions & 2 deletions packages/edit-post/src/components/sidebar/post-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { __ } from '@wordpress/i18n';
import { PanelBody, TextControl, ExternalLink } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, ifCondition } from '@wordpress/compose';
import { cleanForSlug, store as editorStore } from '@wordpress/editor';
import { safeDecodeURIComponent } from '@wordpress/url';
import { store as editorStore } from '@wordpress/editor';
import { safeDecodeURIComponent, cleanForSlug } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';
import { useState } from '@wordpress/element';

Expand Down
4 changes: 4 additions & 0 deletions packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Deprecations

- Deprecated `cleanForSlug` that is now part of `@wordpress/url`.

## 12.2.0 (2022-02-10)

### Enhancement
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-last-revision/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { sprintf, _n } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { backup } from '@wordpress/icons';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import PostLastRevisionCheck from './check';
import { getWPAdminURL } from '../../utils/url';
import { store as editorStore } from '../../store';

function LastRevision( { lastRevisionId, revisionsCount } ) {
return (
<PostLastRevisionCheck>
<Button
href={ getWPAdminURL( 'revision.php', {
href={ addQueryArgs( 'revision.php', {
revision: lastRevisionId,
gutenberg: true,
} ) }
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/src/components/post-locked-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { getWPAdminURL } from '../../utils/url';
import { store as editorStore } from '../../store';

export default function PostLockedModal() {
Expand Down Expand Up @@ -164,7 +163,7 @@ export default function PostLockedModal() {
action: 'edit',
_wpnonce: postLockUtils.nonce,
} );
const allPostsUrl = getWPAdminURL( 'edit.php', {
const allPostsUrl = addQueryArgs( 'edit.php', {
post_type: get( postType, [ 'slug' ] ),
} );
const allPostsLabel = __( 'Exit editor' );
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/src/components/post-slug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { withDispatch, withSelect } from '@wordpress/data';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { withInstanceId, compose } from '@wordpress/compose';
import { safeDecodeURIComponent } from '@wordpress/url';
import { safeDecodeURIComponent, cleanForSlug } from '@wordpress/url';

/**
* Internal dependencies
*/
import PostSlugCheck from './check';
import { cleanForSlug } from '../../utils/url';
import { store as editorStore } from '../../store';

export class PostSlug extends Component {
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
__unstableSerializeAndClean,
} from '@wordpress/blocks';
import { isInTheFuture, getDate } from '@wordpress/date';
import { addQueryArgs } from '@wordpress/url';
import { addQueryArgs, cleanForSlug } from '@wordpress/url';
import { createRegistrySelector } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { Platform } from '@wordpress/element';
Expand All @@ -32,7 +32,6 @@ import {
AUTOSAVE_PROPERTIES,
} from './constants';
import { getPostRawValue } from './reducer';
import { cleanForSlug } from '../utils/url';
import { getTemplatePartIcon } from '../utils/get-template-part-icon';

/**
Expand Down
22 changes: 0 additions & 22 deletions packages/editor/src/utils/test/url.js

This file was deleted.

38 changes: 8 additions & 30 deletions packages/editor/src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
/**
* External dependencies
*/
import { deburr, trim } from 'lodash';

/**
* WordPress dependencies
*/
import { addQueryArgs } from '@wordpress/url';

/**
* Returns the URL of a WPAdmin Page.
*
* TODO: This should be moved to a module less specific to the editor.
*
* @param {string} page Page to navigate to.
* @param {Object} query Query Args.
*
* @return {string} WPAdmin URL.
*/
export function getWPAdminURL( page, query ) {
return addQueryArgs( page, query );
}
import { cleanForSlug as urlCleanForSlug } from '@wordpress/url';
import deprecated from '@wordpress/deprecated';

/**
* Performs some basic cleanup of a string for use as a post slug
Expand All @@ -39,14 +21,10 @@ export function getWPAdminURL( page, query ) {
* @return {string} Processed string
*/
export function cleanForSlug( string ) {
if ( ! string ) {
return '';
}
return trim(
deburr( string )
.replace( /[\s\./]+/g, '-' )
.replace( /[^\p{L}\p{N}_-]+/gu, '' )
.toLowerCase(),
'-'
);
deprecated( 'wp.editor.cleanForSlug', {
since: '12.7',
plugin: 'Gutenberg',
alternative: 'wp.url.cleanForSlug',
} );
return urlCleanForSlug( string );
}
2 changes: 1 addition & 1 deletion packages/url/src/clean-for-slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function cleanForSlug( string ) {
return trim(
deburr( string )
.replace( /[\s\./]+/g, '-' )
.replace( /[^\w-]+/g, '' )
.replace( /[^\p{L}\p{N}_-]+/gu, '' )
.toLowerCase(),
'-'
);
Expand Down
22 changes: 19 additions & 3 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,17 +974,33 @@ describe( 'filterURLForDisplay', () => {
} );

describe( 'cleanForSlug', () => {
it( 'should return string prepared for use as url slug', () => {
it( 'Should return string prepared for use as url slug', () => {
expect( cleanForSlug( '/Is th@t Déjà_vu? ' ) ).toBe( 'is-tht-deja_vu' );
} );

it( 'should return an empty string for missing argument', () => {
it( 'Should return an empty string for missing argument', () => {
expect( cleanForSlug() ).toBe( '' );
} );

it( 'should return an empty string for falsy argument', () => {
it( 'Should return an empty string for falsy argument', () => {
expect( cleanForSlug( null ) ).toBe( '' );
} );

it( 'Should not allow characters used internally in rich-text', () => {
//The last space is an object replacement character
expect( cleanForSlug( 'the long cat' ) ).toBe( 'the-long-cat' );
} );

it( 'Creates a slug for languages that use multibyte encodings', () => {
expect( cleanForSlug( '新荣记 ' ) ).toBe( '新荣记' );
expect( cleanForSlug( '私のテンプレートパーツのテスト ' ) ).toBe(
'私のテンプレートパーツのテスト'
);
expect( cleanForSlug( 'ქართული ნაწილი' ) ).toBe( 'ქართული-ნაწილი' );
expect( cleanForSlug( 'Καλημέρα Κόσμε' ) ).toBe( 'καλημέρα-κόσμε' );
expect( cleanForSlug( '안녕하세요 ' ) ).toBe( '안녕하세요' );
expect( cleanForSlug( '繁体字 ' ) ).toBe( '繁体字' );
} );
} );

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

0 comments on commit ebb4a8a

Please sign in to comment.