Skip to content

Commit

Permalink
Merge d56cd07 into 486f4a0
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Sep 22, 2019
2 parents 486f4a0 + d56cd07 commit d975b42
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/encode_url.js
@@ -1,10 +1,11 @@
'use strict';

const { parse, format } = require('url');
const regexNonUrl = /^(data|javascript|mailto|vbscript)/gi

function encodeURL(str) {
const parsed = parse(str);
if (parsed.protocol) {
if (parsed.slashes) {
const obj = Object.assign({}, {
auth: parsed.auth,
protocol: parsed.protocol,
Expand All @@ -23,6 +24,8 @@ function encodeURL(str) {
return format(obj);
}

if (str.match(regexNonUrl)) return str;

return encodeURI(safeDecodeURI(str));
}

Expand Down
5 changes: 5 additions & 0 deletions test/encode_url.spec.js
Expand Up @@ -94,4 +94,9 @@ describe('encodeURL', () => {
const content = '#fóo-bár';
encodeURL(content).should.eql('#f%C3%B3o-b%C3%A1r');
});

it('data URLs', () => {
const content = 'data:,Hello%2C%20World!';
encodeURL(content).should.eql(content);
});
});

0 comments on commit d975b42

Please sign in to comment.