diff --git a/client/src/entrypoints/admin/page-editor.js b/client/src/entrypoints/admin/page-editor.js index ee4bcd1379..c4263cef77 100644 --- a/client/src/entrypoints/admin/page-editor.js +++ b/client/src/entrypoints/admin/page-editor.js @@ -1,4 +1,5 @@ import $ from 'jquery'; +import { cleanForSlug } from '../../utils/cleanForSlug'; window.halloPlugins = {}; @@ -184,26 +185,6 @@ function InlinePanel(opts) { // lgtm[js/unused-local-variable] } window.InlinePanel = InlinePanel; -function cleanForSlug(val, useURLify) { - if (useURLify) { - // URLify performs extra processing on the string (e.g. removing stopwords) and is more suitable - // for creating a slug from the title, rather than sanitising a slug entered manually - // eslint-disable-next-line no-undef, new-cap - const cleaned = URLify(val, 255, window.unicodeSlugsEnabled); - - // if the result is blank (e.g. because the title consisted entirely of stopwords), - // fall through to the non-URLify method - if (cleaned) { - return cleaned; - } - } - - // just do the "replace" - if (window.unicodeSlugsEnabled) { - return val.replace(/\s/g, '-').replace(/[&/\\#,+()$~%.'":`@^!*?<>{}]/g, '').toLowerCase(); - } - return val.replace(/\s/g, '-').replace(/[^A-Za-z0-9\-_]/g, '').toLowerCase(); -} window.cleanForSlug = cleanForSlug; function initSlugAutoPopulate() { @@ -425,7 +406,3 @@ window.updateFooterSaveWarning = (formDirty, commentsDirty) => { updateWarnings(); } }; - -if (typeof module !== 'undefined' && module.exports) { - module.exports.cleanForSlug = cleanForSlug; -} diff --git a/client/src/utils/cleanForSlug.js b/client/src/utils/cleanForSlug.js new file mode 100644 index 0000000000..938e0a3d94 --- /dev/null +++ b/client/src/utils/cleanForSlug.js @@ -0,0 +1,20 @@ +export function cleanForSlug(val, useURLify) { + if (useURLify) { + // URLify performs extra processing on the string (e.g. removing stopwords) and is more suitable + // for creating a slug from the title, rather than sanitising a slug entered manually + // eslint-disable-next-line no-undef, new-cap + const cleaned = URLify(val, 255, window.unicodeSlugsEnabled); + + // if the result is blank (e.g. because the title consisted entirely of stopwords), + // fall through to the non-URLify method + if (cleaned) { + return cleaned; + } + } + + // just do the "replace" + if (window.unicodeSlugsEnabled) { + return val.replace(/\s/g, '-').replace(/[&/\\#,+()$~%.'":`@^!*?<>{}]/g, '').toLowerCase(); + } + return val.replace(/\s/g, '-').replace(/[^A-Za-z0-9\-_]/g, '').toLowerCase(); +} diff --git a/client/src/entrypoints/admin/page-editor.test.js b/client/src/utils/cleanForSlug.test.js similarity index 81% rename from client/src/entrypoints/admin/page-editor.test.js rename to client/src/utils/cleanForSlug.test.js index 3431af1ba9..d582e44559 100644 --- a/client/src/entrypoints/admin/page-editor.test.js +++ b/client/src/utils/cleanForSlug.test.js @@ -1,12 +1,6 @@ -window.$ = require('../../../../wagtail/admin/static_src/wagtailadmin/js/vendor/jquery-3.5.1.min'); // eslint-disable-next-line no-unused-expressions -require('../../../../wagtail/admin/static_src/wagtailadmin/js/vendor/urlify').default; - -window.comments = { - getContentPath: jest.fn(), -}; - -const cleanForSlug = require('./page-editor').cleanForSlug; +require('../../../wagtail/admin/static_src/wagtailadmin/js/vendor/urlify').default; +import { cleanForSlug } from './cleanForSlug'; describe('page-editor tests', () => { describe('cleanForSlug without unicode slugs enabled', () => {