Skip to content

Commit

Permalink
Merge pull request #114 from curbengh/whatwg-url
Browse files Browse the repository at this point in the history
refactor: whatwg url api
  • Loading branch information
curbengh committed Oct 26, 2019
2 parents c505094 + f60127b commit 635161b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 37 deletions.
4 changes: 4 additions & 0 deletions lib/decode_url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

// To be refactored to WHATWG URL API
// after support for Node 8 is dropped.
// url.format(WHATWG URL object) in Node 8 doesn't support port number

const { parse, format } = require('url');
const { toUnicode } = require('./punycode');

Expand Down
49 changes: 21 additions & 28 deletions lib/encode_url.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
'use strict';

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

function encodeURL(str) {
const parsed = parse(str);
if (parsed.slashes) {
const obj = Object.assign({}, {
auth: parsed.auth,
protocol: parsed.protocol,
host: parsed.host,
pathname: encodeURI(safeDecodeURI(parsed.pathname))
});

if (parsed.hash) {
Object.assign(obj, { hash: encodeURI(safeDecodeURI(parsed.hash)) });
}

if (parsed.search) {
Object.assign(obj, { search: encodeURI(safeDecodeURI(parsed.search)) });
}

return format(obj);
const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};

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

return encodeURI(safeDecodeURI(str));
}

function safeDecodeURI(str) {
const safeDecodeURI = (str) => {
try {
return decodeURI(str);
} catch (err) {
return str;
}
}
};

const encodeURL = (str) => {
const parsed = urlObj(str);
if (typeof parsed === 'object') {
if (parsed.origin === 'null') return str;

parsed.search = encodeURI(safeDecodeURI(parsed.search));
return parsed.toString();
}

return encodeURI(safeDecodeURI(str));
};

module.exports = encodeURL;
16 changes: 13 additions & 3 deletions lib/full_url_for.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
'use strict';

const { parse } = require('url');
const { URL } = require('url');
const encodeURL = require('./encode_url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};

function fullUrlForHelper(path = '/') {
if (path.startsWith('//')) return path;
const { config } = this;
const data = parse(path);
const data = urlObj(path);

// Exit if this is an external path
if (data.protocol) return path;
if (typeof data === 'object') {
if (data.origin !== 'null') return path;
}

path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/'));
return path;
Expand Down
16 changes: 12 additions & 4 deletions lib/url_for.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
'use strict';

const { parse } = require('url');
const { URL } = require('url');
const encodeURL = require('./encode_url');
const relative_url = require('./relative_url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};

function urlForHelper(path = '/', options) {
if (path[0] === '#' || path.startsWith('//')) {
return path;
}

const { config } = this;
const { root } = config;
const data = parse(path);
const data = urlObj(path);

options = Object.assign({
relative: config.relative_link
}, options);

// Exit if this is an external path
if (data.protocol) {
return path;
if (typeof data === 'object') {
if (data.origin !== 'null') return path;
}

// Resolve relative url
Expand Down
2 changes: 1 addition & 1 deletion test/decode_url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('decodeURL', () => {
});

it('port', () => {
const content = 'http://foo.com:80/';
const content = 'http://foo.com:8080/';
decodeURL(content).should.eql(content);
});

Expand Down
2 changes: 1 addition & 1 deletion test/encode_url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('encodeURL', () => {
});

it('port', () => {
const content = 'http://foo.com:80/';
const content = 'http://foo.com:8080/';
encodeURL(content).should.eql(content);
});

Expand Down
5 changes: 5 additions & 0 deletions test/full_url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ describe('full_url_for', () => {
ctx.config.url = 'https://example.com/blog';
fullUrlFor('#test').should.eql(ctx.config.url + '/#test');
});

it('path with semicolon', () => {
ctx.config.url = 'https://example.com/blog';
fullUrlFor('foo:bar').should.eql(ctx.config.url + '/foo:bar');
});
});
8 changes: 8 additions & 0 deletions test/url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ describe('url_for', () => {
it('only hash', () => {
urlFor('#test').should.eql('#test');
});

it('path with semicolon', () => {
ctx.config.root = '/';
urlFor('foo:bar').should.eql('/foo:bar');

ctx.config.root = '/foo/';
urlFor('bar:baz').should.eql('/foo/bar:baz');
});
});

0 comments on commit 635161b

Please sign in to comment.