Skip to content

Commit

Permalink
Relaxes regex for removing duplicate slashes;
Browse files Browse the repository at this point in the history
Prior to this, duplicate slashes would be removed unless preceeded by "http" or "https" protocols. This was too strict.
The regex was updated to allow for any 2+ character alphanumeric protocol. Went with requiring at least 2 characters since every scheme listed here (https://en.wikipedia.org/wiki/List_of_URI_schemes) is at least 2 characters.
  • Loading branch information
gcox committed Jul 29, 2020
1 parent 17177aa commit 996b991
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -117,7 +117,7 @@ const normalizeUrl = (urlString, options) => {

// Remove duplicate slashes if not preceded by a protocol
if (urlObj.pathname) {
urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');
urlObj.pathname = urlObj.pathname.replace(/(?<!(?:[a-z0-9]{2,}):)\/{2,}/g, '/');
}

// Decode URI octets
Expand Down
7 changes: 7 additions & 0 deletions test.js
Expand Up @@ -205,6 +205,13 @@ test('remove duplicate pathname slashes', t => {
t.is(normalizeUrl('http://sindresorhus.com///foo'), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com:5000//foo'), 'http://sindresorhus.com:5000/foo');
t.is(normalizeUrl('http://sindresorhus.com//foo'), 'http://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/s3://sindresorhus.com'), 'http://sindresorhus.com/s3://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/s3://sindresorhus.com//foo'), 'http://sindresorhus.com/s3://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com//foo/s3://sindresorhus.com'), 'http://sindresorhus.com/foo/s3://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/git://sindresorhus.com'), 'http://sindresorhus.com/git://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/git://sindresorhus.com//foo'), 'http://sindresorhus.com/git://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com//foo/git://sindresorhus.com//foo'), 'http://sindresorhus.com/foo/git://sindresorhus.com/foo');
t.is(normalizeUrl('http://sindresorhus.com/a://sindresorhus.com//foo'), 'http://sindresorhus.com/a:/sindresorhus.com/foo');
});

test('data URL', t => {
Expand Down

0 comments on commit 996b991

Please sign in to comment.