-
Notifications
You must be signed in to change notification settings - Fork 476
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
Conversation
Thanks! As I'm currently preparing the upcomming version 1.6.0, I've "manually merged" your fix locally. parsing will treat |
I don"t see a case where "removing the protocol" make sense. Because if you remove the protocol of You should take a look at Ruby's URI class: >> require 'uri'
>> uri = URI.parse('http://example.com/foo')
>> uri.scheme = ''
URI::InvalidComponentError: bad component(expected scheme component):
from /Users/byroot/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/uri/generic.rb:329:in `check_scheme'
from /Users/byroot/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/uri/generic.rb:370:in `scheme='
from (irb):3
from /Users/byroot/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
>> uri.scheme = nil
>> uri.to_s
=> "//example.com/foo" I think both My 2 cents |
A protocol (scheme) without host makes sense for URLs It is fair to assume that removing the scheme itself is never the intention. If the scheme is to be removed, the host (authority, actually) would be removed as well to have path+query+fragment as the remaining URL. Because there are host-less protocols ( Do you agree? |
Yeah, but I think the solution is in the Currently if we have a scheme we add The algorithm will be: URI.build = function(parts) {
var t = '';
if (typeof parts.protocol === "string" && parts.protocol.length) {
t += parts.protocol + ":";
}
if (parts.hostname) {
t += '//';
}
t += (URI.buildAuthority(parts) || '');
if (typeof parts.path === "string") {
if (parts.path[0] !== '/' && typeof parts.hostname === "string") {
t += '/';
}
t += parts.path;
}
if (typeof parts.query == "string") {
t += '?' + parts.query;
}
if (typeof parts.fragment === "string") {
t += '#' + parts.fragment;
}
return t;
}; And it will perform like this: URI.build({protocol: 'mailto', hostname: 'example.com', username: 'dave.null'}) // -> mailto:dave.null@example.com
URI.build({protocol: 'file', path: '/etc/hosts'}) // -> file:/etc/hosts // not exactly file:///etc/hosts but perfectly valid and equivalent
URI.build({hostname: 'example.com', path: '/foo'}) // -> //example.com/foo |
I give up. var t = '';
if (parts.protocol) {
t += parts.protocol + ":";
}
if (!parts.urn && (t || parts.hostname)) {
t += '//';
} that (again, will be released some time next week) |
Great ! Thanks ! |
… and it's released |
Add support for scheme relative URLs like
//example.com/file.html
Also note that the URL tested here: https://github.com/medialize/URI.js/blob/gh-pages/test/urls.js#L349 is invalid and should throw an error on parsing, or at least consider the whole string as a path.
Example:
//a248.e.akamai.net/assets.github.com/images/modules/header/logov7@4x.png
://a248.e.akamai.net/assets.github.com/images/modules/header/logov7@4x.png