Skip to content

Commit

Permalink
minor #3170 [3.0] SlugField: don't translate characters & remove spec…
Browse files Browse the repository at this point in the history
…ial characters (jmsche)

This PR was squashed before being merged into the 3.0.x-dev branch.

Discussion
----------

[3.0] SlugField: don't translate characters & remove special characters

Follow-up of Javier's comment: #3164 (comment)

This PR:

* extends (only once) the slugify() function to remove translated symbols (eg. "$" => "dollar")
* uses the "lower" option of slugify instead of .toLowerCase()
* allows only alphanumeric characters (and dashes) in the slug

Could supersede ec3c8da ?

Commits
-------

8f8e10c [3.0] SlugField: don't translate characters & remove special characters
  • Loading branch information
javiereguiluz committed May 2, 2020
2 parents e2331de + 8f8e10c commit b4e8289
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions assets/js/form-type-slug.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
const slugify = require('slugify');
slugify.extend({
"$": "",
"%": "",
"&": "",
"<": "",
">": "",
"|": "",
"¢": "",
"£": "",
"¤": "",
"¥": "",
"₠": "",
"₢": "",
"₣": "",
"₤": "",
"₥": "",
"₦": "",
"₧": "",
"₨": "",
"₩": "",
"₪": "",
"₫": "",
"€": "",
"₭": "",
"₮": "",
"₯": "",
"₰": "",
"₱": "",
"₲": "",
"₳": "",
"₴": "",
"₵": "",
"₸": "",
"₹": "",
"₽": "",
"₿": "",
"∂": "",
"∆": "",
"∑": "",
"∞": "",
"♥": "",
"元": "",
"円": "",
"﷼": "",
});

class Slugger {
constructor(field) {
Expand Down Expand Up @@ -75,23 +120,11 @@ class Slugger {
}

updateValue() {
// this is needed because the special char conversion is done first,
// so we cannot remove these symbols later with the 'remove' option of slugify()
slugify.extend({
'$': '',
'%': '',
'&': '',
'<': '',
'>': '',
'|': '',
'¢': '',
'£': '',
'¥': '',
'©': '',
'®': '',
this.field.value = slugify(this.target.value, {
remove: /[^A-Za-z0-9\s-]/g,
lower: true,
strict: true,
});

this.field.value = slugify(this.target.value, { lower: true, strict: true });
}

/**
Expand Down

0 comments on commit b4e8289

Please sign in to comment.