Skip to content

Commit 0a74890

Browse files
committed
expand forbidden chars
1 parent 1d8668a commit 0a74890

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/lib/slugify.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
// precompile for speed
44
var HTML_TAGS_REGEX = /<[^>]*>/g; // anything contained in < > tags
5-
var FORBIDDEN_CHARS_REGEX = /[\\/:*?"<>|$`'(){}[\],]/g; // Characters in the set: \/:*?"<>|$`'(){}[],
5+
var FORBIDDEN_CHARS_REGEX = /[\\/:*?"<>|$%&!@#~.^`'(){}[\],]/g; // Characters in the set: \/:*?"<>|$%&!@#~.^`'(){}[],
6+
var CONTROL_CHARS_REGEX = /\p{Cc}/gu; // Unicode control characters
7+
68
var UNICODE_REPLACEMENT_CHAR_REGEX = //g; // U+FFFD, the Unicode replacement character
79
var WHITESPACE_REGEX = /\s+/g;
810

@@ -37,14 +39,15 @@ function toWellFormed(str) {
3739
* @return {string}
3840
*/
3941
module.exports = function slugify(str, maxLen = DEFAULT_MAX_LEN) {
40-
var slug = toWellFormed(str ?? '') // Guarantee well-formed Unicode text
41-
.replace(UNICODE_REPLACEMENT_CHAR_REGEX, '') // Drop Unicode replacement characters left by previous step
42-
.replace(HTML_TAGS_REGEX, ' ') // Remove anything contained in < > tags, such as <br> (replace with a space)
43-
.replace(FORBIDDEN_CHARS_REGEX, '') // Remove forbidden filename characters
44-
.toLowerCase() // Lowercase everything
45-
.trim() // Strip leading/trailing whitespace
46-
.replace(WHITESPACE_REGEX, WORD_SEP_CHAR) // Replace any remaining whitespace with the word separator char
47-
.replace(WORD_SEP_CHARS_REGEX, WORD_SEP_CHAR); // Replace multiple consecutive word separator chars with a single one
42+
var slug = toWellFormed(str ?? '') // Guarantee well-formed Unicode text
43+
.replace(UNICODE_REPLACEMENT_CHAR_REGEX, '') // Drop Unicode replacement chars left by previous step
44+
.replace(HTML_TAGS_REGEX, ' ') // Remove < > tags, such as <br> (replace with space)
45+
.replace(FORBIDDEN_CHARS_REGEX, '') // Remove forbidden filename characters
46+
.toLowerCase() // Lowercase everything
47+
.trim() // Strip leading/trailing whitespace
48+
.replace(WHITESPACE_REGEX, WORD_SEP_CHAR) // Replace any remaining whitespace with the word sep char
49+
.replace(CONTROL_CHARS_REGEX, '') // Remove control characters (after whitespace)
50+
.replace(WORD_SEP_CHARS_REGEX, WORD_SEP_CHAR); // Replace multiple word sep chars with a single one
4851

4952
if (slug.length <= maxLen) return slug;
5053
// Apply maxLen to the resulting string. Use Array.from().slice() instead of String.prototype.split()

test/jasmine/tests/download_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('Plotly.downloadImage', function() {
115115
it('strips forbidden characters from the title', function(done) {
116116
downloadDefault({title: {text: 'Revenue, Costs & "Profit" (2024)'}})
117117
.then(function(filename) {
118-
expect(filename).toBe('revenue-costs-&-profit-2024.png');
118+
expect(filename).toBe('revenue-costs-profit-2024.png');
119119
})
120120
.then(done, done.fail);
121121
}, LONG_TIMEOUT_INTERVAL);

test/jasmine/tests/lib_test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,13 @@ describe('Test lib.js:', function () {
19521952
});
19531953

19541954
it('removes illegal filename characters', function () {
1955-
expect(Lib.slugify('a/b\\c:d*e?f"g|h$i')).toBe('abcdefghi');
1955+
expect(Lib.slugify('a/b\\c:d*e?f"g|h$i%j&k!l@m#n~o.p^q`r\'s,t'))
1956+
.toBe('abcdefghijklmnopqrst');
1957+
expect(Lib.slugify('a>b<c)d(e]f[g}h{i')).toBe('abcdefghi');
1958+
});
1959+
1960+
it('removes control characters', function () {
1961+
expect(Lib.slugify('a\x00b\x07c\x1Fd')).toBe('abcd');
19561962
});
19571963

19581964
it('preserves unicode letters (accents, CJK, emoji)', function () {

0 commit comments

Comments
 (0)