Move cleanForSlug into its own utils module

This allows us to test it without having to jump through hoops to make the rest of page-editor.js module-friendly, and moving the test module out of client/src/entrypoints means we don't duplicate it into wagtail/admin/static (where the test runner tries to run it again with broken imports)
pull/7285/head
Matt Westcott 2021-06-10 23:05:55 +01:00 zatwierdzone przez Matt Westcott
rodzic 39e168c574
commit ac2e07e8c4
3 zmienionych plików z 23 dodań i 32 usunięć

Wyświetl plik

@ -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;
}

Wyświetl plik

@ -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();
}

Wyświetl plik

@ -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', () => {