Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle scheme relative URIs #19

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ URI.parse = function(string) {
parts.protocol = string.substring(0, pos);
string = string.substring(pos + 3);

// extract "user:pass@host:port"
string = URI.parseAuthority(string, parts);
} else if (string.slice(0, 2) === '//') {
// Scheme relative URI like //example.com/file.html
parts.protocol = null;
string = string.substring(2);

// extract "user:pass@host:port"
string = URI.parseAuthority(string, parts);
}
Expand Down Expand Up @@ -305,11 +312,15 @@ URI.parseQuery = function(string) {
URI.build = function(parts) {
var t = '';

var authority = URI.buildAuthority(parts) || '';

if (typeof parts.protocol === "string" && parts.protocol.length) {
t += parts.protocol + "://";
} else if (authority) {
t += '//';
}

t += (URI.buildAuthority(parts) || '');
t += authority

if (typeof parts.path === "string") {
if (parts.path[0] !== '/' && typeof parts.hostname === "string") {
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ test("protocol", function() {

u.protocol('');
equal(u.protocol(), "", "missing protocol");
equal(u+"", "example.org/foo.html", "no-scheme url");
equal(u+"", "//example.org/foo.html", "no-scheme url");

u.protocol(null);
equal(u.protocol(), "", "missing protocol");
equal(u+"", "example.org/foo.html", "no-scheme url");
equal(u+"", "//example.org/foo.html", "no-scheme url");
});
test("username", function() {
var u = new URI("http://example.org/foo.html");
Expand Down
45 changes: 44 additions & 1 deletion test/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,49 @@ var urls = [{
idn: false,
punycode: false
}
}, {
name: 'scheme-relative: URL',
url: '//example.com/some/directory/file.html?query=string#fragment',
parts: {
protocol: null,
username: null,
password: null,
hostname: 'example.com',
port: null,
path: '/some/directory/file.html',
query: 'query=string',
fragment: 'fragment'
},
accessors: {
protocol: '',
username: '',
password: '',
port: '',
path: '/some/directory/file.html',
query: 'query=string',
fragment: 'fragment',
authority: 'example.com',
subdomain: '',
domain: 'example.com',
tld: 'com',
directory: '/some/directory',
filename: 'file.html',
suffix: 'html',
hash: '#fragment',
search: '?query=string',
host: 'example.com',
hostname: 'example.com'
},
is: {
relative: false,
name: true,
sld: false,
ip: false,
ip4: false,
ip6: false,
idn: false,
punycode: false
}
}, {
name: 'missing scheme',
url: 'user:pass@www.example.org:123/some/directory/file.html?query=string#fragment',
Expand Down Expand Up @@ -347,7 +390,7 @@ var urls = [{
}, {
name: 'ignoring scheme',
url: '://user:pass@example.org:123/some/directory/file.html?query=string#fragment',
_url: 'user:pass@example.org:123/some/directory/file.html?query=string#fragment',
_url: '//user:pass@example.org:123/some/directory/file.html?query=string#fragment',
parts: {
protocol: "", // not null
username: 'user',
Expand Down