Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function slugify (string, opts) {
char = localeMap[char]
} else if (opts.charmap[char]) {
char = opts.charmap[char].replace(opts.replacement, ' ')
} else if (char.includes(opts.replacement)) {
} else if (opts.replacement !== '' && char.includes(opts.replacement)) {
// preserve the replacement character in case it is excluded by disallowedChars
char = char.replace(opts.replacement, ' ')
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/slug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ describe('slug', function () {
assert.strictEqual(slug('foo bar baz', ''), 'foobarbaz')
})

it('should sanitize disallowed characters even with an empty replacement', function () {
assert.strictEqual(slug('<script>alert(1)</script>', ''), 'scriptalert1script')
assert.strictEqual(slug('a/../../etc/passwd', ''), 'aetcpasswd')
Comment thread
Trott marked this conversation as resolved.
Outdated
assert.strictEqual(slug('a<b>c', ''), 'abc')
assert.strictEqual(slug('a<b>c', { replacement: '', mode: 'rfc3986' }), 'abc')
// documented behavior must remain intact
Comment thread
Trott marked this conversation as resolved.
Outdated
assert.strictEqual(slug('foo bar baz', ''), 'foobarbaz')
})

it('should replace multiple spaces and dashes with a single instance', function () {
assert.strictEqual(slug('foo bar--baz'), 'foo-bar-baz')
})
Expand Down