Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: medialize/URI.js
base: gh-pages
...
head fork: byroot/URI.js
compare: handle-scheme-relative-urls
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 17, 2012
@byroot byroot Handle scheme relative URIs 7236fd4
Showing with 58 additions and 4 deletions.
  1. +12 −1 src/URI.js
  2. +2 −2 test/test.js
  3. +44 −1 test/urls.js
View
13 src/URI.js
@@ -205,6 +205,13 @@ URI.parse = function(string) {
// 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);
}
// what's left must be the path
@@ -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") {
View
4 test/test.js
@@ -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");
View
45 test/urls.js
@@ -302,6 +302,49 @@ var urls = [{
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',
parts: {
@@ -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',

No commit comments for this range

Something went wrong with that request. Please try again.