Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
protocol parsing (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 26, 2017
1 parent ad8ca7d commit d3a2e33
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { isNode } from './common.js';
function throwResolveError (relUrl, parentUrl) {
throw new RangeError('Unable to resolve "' + relUrl + '" to ' + parentUrl);
}
var protocolRegEx = /^[^/]+:/;
export function resolveIfNotPlain (relUrl, parentUrl) {
relUrl = relUrl.trim();
var parentProtocol = parentUrl && parentUrl.substr(0, parentUrl.indexOf(':') + 1);
if (parentUrl)
var parentProtocol = parentUrl.match(protocolRegEx);

var firstChar = relUrl[0];
var secondChar = relUrl[1];
Expand All @@ -17,12 +19,12 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
if (firstChar === '/' && secondChar === '/') {
if (!parentProtocol)
throwResolveError(relUrl, parentUrl);
return parentProtocol + relUrl;
return parentProtocol[0] + relUrl;
}
// relative-url
else if (firstChar === '.' && (secondChar === '/' || secondChar === '.' && (relUrl[2] === '/' || relUrl.length === 2) || relUrl.length === 1)
|| firstChar === '/') {
var parentIsPlain = !parentProtocol || parentUrl[parentProtocol.length] !== '/';
var parentIsPlain = !parentProtocol || parentUrl[parentProtocol[0].length] !== '/';

// read pathname from parent if a URL
// pathname taken to be part after leading "/"
Expand All @@ -33,10 +35,10 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
throwResolveError(relUrl, parentUrl);
pathname = parentUrl;
}
else if (parentUrl[parentProtocol.length + 1] === '/') {
else if (parentUrl[parentProtocol[0].length + 1] === '/') {
// resolving to a :// so we need to read out the auth and host
if (parentProtocol !== 'file:') {
pathname = parentUrl.substr(parentProtocol.length + 2);
if (parentProtocol[0] !== 'file:') {
pathname = parentUrl.substr(parentProtocol[0].length + 2);
pathname = pathname.substr(pathname.indexOf('/') + 1);
}
else {
Expand All @@ -45,7 +47,7 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
}
else {
// resolving to :/ so pathname is the /... part
pathname = parentUrl.substr(parentProtocol.length + 1);
pathname = parentUrl.substr(parentProtocol[0].length + 1);
}

if (firstChar === '/') {
Expand Down

0 comments on commit d3a2e33

Please sign in to comment.