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

Commit c18ec09

Browse files
committedOct 9, 2017
ensure backslash conversion on all paths
1 parent d35348d commit c18ec09

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed
 

‎core/resolve.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { isNode } from './common.js';
66
function throwResolveError (relUrl, parentUrl) {
77
throw new RangeError('Unable to resolve "' + relUrl + '" to ' + parentUrl);
88
}
9+
var backslashRegEx = /\\/g;
910
export function resolveIfNotPlain (relUrl, parentUrl) {
10-
relUrl = relUrl.trim();
11+
if (relUrl[0] === ' ' || relUrl[relUrl.length - 1] === ' ')
12+
relUrl = relUrl.trim();
1113
var parentProtocol = parentUrl && parentUrl.substr(0, parentUrl.indexOf(':') + 1);
1214

1315
var firstChar = relUrl[0];
@@ -17,12 +19,16 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
1719
if (firstChar === '/' && secondChar === '/') {
1820
if (!parentProtocol)
1921
throwResolveError(relUrl, parentUrl);
22+
if (relUrl.indexOf('\\') !== -1)
23+
relUrl = relUrl.replace(backslashRegEx, '/');
2024
return parentProtocol + relUrl;
2125
}
2226
// relative-url
2327
else if (firstChar === '.' && (secondChar === '/' || secondChar === '.' && (relUrl[2] === '/' || relUrl.length === 2 && (relUrl += '/')) ||
2428
relUrl.length === 1 && (relUrl += '/')) ||
2529
firstChar === '/') {
30+
if (relUrl.indexOf('\\') !== -1)
31+
relUrl = relUrl.replace(backslashRegEx, '/');
2632
var parentIsPlain = !parentProtocol || parentUrl[parentProtocol.length] !== '/';
2733

2834
// read pathname from parent if a URL
@@ -115,7 +121,7 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
115121
if (isNode) {
116122
// C:\x becomes file:///c:/x (we don't support C|\x)
117123
if (relUrl[1] === ':' && relUrl[2] === '\\' && relUrl[0].match(/[a-z]/i))
118-
return 'file:///' + relUrl.replace(/\\/g, '/');
124+
return 'file:///' + relUrl.replace(backslashRegEx, '/');
119125
}
120126
return relUrl;
121127
}

‎test/1-url-resolution.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ describe('Simple normalization tests', function () {
88
it('Should resolve relative with protocol', function () {
99
assert.equal(resolveIfNotPlain('./x:y', 'https://x.com/y'), 'https://x.com/x:y');
1010
});
11+
it('Should convert \ into /', function () {
12+
assert.equal(resolveIfNotPlain('./a\\b', 'https://x.com/z'), 'https://x.com/a/b')
13+
});
1114
it('Should resolve windows paths as file:/// URLs', function () {
1215
assert.equal(resolveIfNotPlain('c:\\some\\path', 'file:///c:/adsf/asdf'), 'file:///c:/some/path');
1316
});

0 commit comments

Comments
 (0)
This repository has been archived.