From fa1d97a2241a18ec88faf3a825370adbdbc9cddb Mon Sep 17 00:00:00 2001 From: Luke Ward Date: Wed, 3 Apr 2019 15:45:59 +0000 Subject: [PATCH 1/6] Remove non allowed special characters and trim slugifyWithHyphens text util --- app/assets/javascripts/lib/utils/text_utility.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js index cc1d85fd97d4ce..724cd08f353181 100644 --- a/app/assets/javascripts/lib/utils/text_utility.js +++ b/app/assets/javascripts/lib/utils/text_utility.js @@ -44,11 +44,11 @@ export const pluralize = (str, count) => str + (count > 1 || count === 0 ? 's' : export const dasherize = str => str.replace(/[_\s]+/g, '-'); /** - * Replaces whitespaces with hyphens and converts to lower case + * Replaces whitespaces with hyphens, convert to lower case and remove non-allowed special characters * @param {String} str * @returns {String} */ -export const slugifyWithHyphens = str => str.toLowerCase().replace(/\s+/g, '-'); +export const slugifyWithHyphens = str => str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, ''); /** * Replaces whitespaces with underscore and converts to lower case -- GitLab From ce9048828efc9da3171d3153c6f6b348987c38cd Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 17 Jun 2019 10:41:16 +0100 Subject: [PATCH 2/6] Replace text utilities slugify with slugifyWithHyphens --- app/assets/javascripts/group.js | 4 ++-- app/assets/javascripts/lib/utils/text_utility.js | 2 +- app/assets/javascripts/projects/project_new.js | 4 ++-- spec/frontend/lib/utils/text_utility_spec.js | 6 ------ 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/group.js b/app/assets/javascripts/group.js index 903c838e266e2a..460174caf4dab7 100644 --- a/app/assets/javascripts/group.js +++ b/app/assets/javascripts/group.js @@ -1,5 +1,5 @@ import $ from 'jquery'; -import { slugifyWithHyphens } from './lib/utils/text_utility'; +import { slugify } from './lib/utils/text_utility'; export default class Group { constructor() { @@ -14,7 +14,7 @@ export default class Group { } update() { - const slug = slugifyWithHyphens(this.groupName.val()); + const slug = slugify(this.groupName.val()); this.groupPath.val(slug); } diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js index 724cd08f353181..3f05b1218f8a31 100644 --- a/app/assets/javascripts/lib/utils/text_utility.js +++ b/app/assets/javascripts/lib/utils/text_utility.js @@ -48,7 +48,7 @@ export const dasherize = str => str.replace(/[_\s]+/g, '-'); * @param {String} str * @returns {String} */ -export const slugifyWithHyphens = str => str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, ''); +export const slugify = str => str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, ''); /** * Replaces whitespaces with underscore and converts to lower case diff --git a/app/assets/javascripts/projects/project_new.js b/app/assets/javascripts/projects/project_new.js index ea82ff4e340e86..9066844f687545 100644 --- a/app/assets/javascripts/projects/project_new.js +++ b/app/assets/javascripts/projects/project_new.js @@ -1,6 +1,6 @@ import $ from 'jquery'; import { addSelectOnFocusBehaviour } from '../lib/utils/common_utils'; -import { slugifyWithHyphens } from '../lib/utils/text_utility'; +import { slugify } from '../lib/utils/text_utility'; import { s__ } from '~/locale'; let hasUserDefinedProjectPath = false; @@ -34,7 +34,7 @@ const deriveProjectPathFromUrl = $projectImportUrl => { }; const onProjectNameChange = ($projectNameInput, $projectPathInput) => { - const slug = slugifyWithHyphens($projectNameInput.val()); + const slug = slugify($projectNameInput.val()); $projectPathInput.val(slug); }; diff --git a/spec/frontend/lib/utils/text_utility_spec.js b/spec/frontend/lib/utils/text_utility_spec.js index 9e920d5909386e..c5312a6a40bc53 100644 --- a/spec/frontend/lib/utils/text_utility_spec.js +++ b/spec/frontend/lib/utils/text_utility_spec.js @@ -55,12 +55,6 @@ describe('text_utility', () => { }); }); - describe('slugifyWithHyphens', () => { - it('should replaces whitespaces with hyphens and convert to lower case', () => { - expect(textUtils.slugifyWithHyphens('My Input String')).toEqual('my-input-string'); - }); - }); - describe('stripHtml', () => { it('replaces html tag with the default replacement', () => { expect(textUtils.stripHtml('This is a text with

html

.')).toEqual( -- GitLab From a4f9446dff41928a746906e228b253b2858917dd Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 17 Jun 2019 10:41:56 +0100 Subject: [PATCH 3/6] Expanded tests for slugify --- spec/frontend/lib/utils/text_utility_spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/frontend/lib/utils/text_utility_spec.js b/spec/frontend/lib/utils/text_utility_spec.js index c5312a6a40bc53..896d73adeefe1b 100644 --- a/spec/frontend/lib/utils/text_utility_spec.js +++ b/spec/frontend/lib/utils/text_utility_spec.js @@ -49,6 +49,21 @@ describe('text_utility', () => { }); }); + describe('slugify', () => { + it('should remove accents and convert to lower case', () => { + expect(textUtils.slugify('João')).toEqual('joo'); + }); + it('should replaces whitespaces with hyphens and convert to lower case', () => { + expect(textUtils.slugify('My Input String')).toEqual('my-input-string'); + }); + it('should remove trailing whitespace and replace whitespaces within string with a hyphen', () => { + expect(textUtils.slugify(' a new project ')).toEqual('a-new-project'); + }); + it('should only remove non-allowed special characters', () => { + expect(textUtils.slugify('test!_pro-ject~')).toEqual('test_pro-ject'); + }); + }); + describe('capitalizeFirstCharacter', () => { it('returns string with first letter capitalized', () => { expect(textUtils.capitalizeFirstCharacter('gitlab')).toEqual('Gitlab'); -- GitLab From 61531f99352a714c440a411fb658c5e20b316196 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 17 Jun 2019 10:44:59 +0100 Subject: [PATCH 4/6] Replace non-allowed characters in slugify with hyphens to be consistent with backend --- app/assets/javascripts/lib/utils/text_utility.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js index 3f05b1218f8a31..849b098fb1d1a8 100644 --- a/app/assets/javascripts/lib/utils/text_utility.js +++ b/app/assets/javascripts/lib/utils/text_utility.js @@ -48,7 +48,7 @@ export const dasherize = str => str.replace(/[_\s]+/g, '-'); * @param {String} str * @returns {String} */ -export const slugify = str => str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, ''); +export const slugify = str => str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, '-'); /** * Replaces whitespaces with underscore and converts to lower case -- GitLab From 96ff9f8c22b4f0d272bd5b3ec40f5c74b030ff1a Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 17 Jun 2019 10:51:38 +0100 Subject: [PATCH 5/6] Slugify tests updated for non-allowed characters being replaced with hypens --- spec/frontend/lib/utils/text_utility_spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/frontend/lib/utils/text_utility_spec.js b/spec/frontend/lib/utils/text_utility_spec.js index 896d73adeefe1b..57734216f4a480 100644 --- a/spec/frontend/lib/utils/text_utility_spec.js +++ b/spec/frontend/lib/utils/text_utility_spec.js @@ -51,7 +51,7 @@ describe('text_utility', () => { describe('slugify', () => { it('should remove accents and convert to lower case', () => { - expect(textUtils.slugify('João')).toEqual('joo'); + expect(textUtils.slugify('João')).toEqual('jo-o'); }); it('should replaces whitespaces with hyphens and convert to lower case', () => { expect(textUtils.slugify('My Input String')).toEqual('my-input-string'); @@ -60,7 +60,7 @@ describe('text_utility', () => { expect(textUtils.slugify(' a new project ')).toEqual('a-new-project'); }); it('should only remove non-allowed special characters', () => { - expect(textUtils.slugify('test!_pro-ject~')).toEqual('test_pro-ject'); + expect(textUtils.slugify('test!_pro-ject~')).toEqual('test-_pro-ject-'); }); }); -- GitLab From 4e45c4df839c38196e237ca2307037d239746a80 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 18 Jun 2019 09:32:43 +0100 Subject: [PATCH 6/6] Squash hyphens in slugify --- app/assets/javascripts/lib/utils/text_utility.js | 6 +++++- spec/frontend/lib/utils/text_utility_spec.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js index 849b098fb1d1a8..c19685cf256381 100644 --- a/app/assets/javascripts/lib/utils/text_utility.js +++ b/app/assets/javascripts/lib/utils/text_utility.js @@ -48,7 +48,11 @@ export const dasherize = str => str.replace(/[_\s]+/g, '-'); * @param {String} str * @returns {String} */ -export const slugify = str => str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, '-'); +export const slugify = str => { + let slug = str.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-zA-Z0-9_\.\-\s]/g, '-').replace(/-{2,}/g, '-'); + + return (slug == '-') ? '' : slug; +}; /** * Replaces whitespaces with underscore and converts to lower case diff --git a/spec/frontend/lib/utils/text_utility_spec.js b/spec/frontend/lib/utils/text_utility_spec.js index 57734216f4a480..04f1c434581e27 100644 --- a/spec/frontend/lib/utils/text_utility_spec.js +++ b/spec/frontend/lib/utils/text_utility_spec.js @@ -62,6 +62,12 @@ describe('text_utility', () => { it('should only remove non-allowed special characters', () => { expect(textUtils.slugify('test!_pro-ject~')).toEqual('test-_pro-ject-'); }); + it('should squash multiple hypens', () => { + expect(textUtils.slugify('test!!!!_pro-ject~')).toEqual('test-_pro-ject-'); + }); + it('should return empty string if only non-allowed characters', () => { + expect(textUtils.slugify('здрасти')).toEqual(''); + }); }); describe('capitalizeFirstCharacter', () => { -- GitLab