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

Commit 4c9a9ff

Browse files
committed
fix trailing segment support regression
1 parent 31a72a2 commit 4c9a9ff

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

core/resolve.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
7777
// new segment - check if it is relative
7878
if (segmented[i] === '.') {
7979
// ../ segment
80-
if (segmented[i + 1] === '.' && segmented[i + 2] === '/') {
80+
if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
8181
output.pop();
8282
i += 2;
8383
}
8484
// ./ segment
85-
else if (segmented[i + 1] === '/') {
85+
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
8686
i += 1;
8787
}
8888
else {
@@ -95,9 +95,6 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
9595
if (parentIsPlain && output.length === 0)
9696
throwResolveError(relUrl, parentUrl);
9797

98-
// trailing . or .. segment
99-
if (i === segmented.length)
100-
output.push('');
10198
continue;
10299
}
103100

test/1-url-resolution.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { resolveIfNotPlain } from '../core/resolve.js';
22
import assert from 'assert';
33

4-
describe('Simple normalization tests', function() {
4+
describe('Simple normalization tests', function () {
55
it('Should trim whitespace from URLs', function () {
66
assert.equal(resolveIfNotPlain(' c:\\some\\path ', 'file:///c:/adsf/asdf'), 'file:///c:/some/path');
77
});
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 resolve windows paths as file:/// URLs', function() {
11+
it('Should resolve windows paths as file:/// URLs', function () {
1212
assert.equal(resolveIfNotPlain('c:\\some\\path', 'file:///c:/adsf/asdf'), 'file:///c:/some/path');
1313
});
14-
it('Should resolve relative windows paths', function() {
14+
it('Should resolve relative windows paths', function () {
1515
assert.equal(resolveIfNotPlain('./test.js', 'file:///C:/some/path/'), 'file:///C:/some/path/test.js');
1616
});
17-
it('Should resolve unix file paths as file:/// URLs', function() {
17+
it('Should resolve unix file paths as file:/// URLs', function () {
1818
assert.equal(resolveIfNotPlain('/some/file/path.js', 'file:///home/path/to/project'), 'file:///some/file/path.js');
1919
});
20-
it('Should be able to resolve to plain names', function() {
20+
it('Should be able to resolve to plain names', function () {
2121
assert.equal(resolveIfNotPlain('../../asdf/./asdf/.asdf/asdf', 'a/b/c/d'), 'a/asdf/asdf/.asdf/asdf');
2222
});
23-
it('Should support resolving plain URI forms', function() {
23+
it('Should support resolving plain URI forms', function () {
2424
assert.equal(resolveIfNotPlain('./asdf', 'npm:lodash/'), 'npm:lodash/asdf');
2525
});
26-
it('Should not support backtracking below base in plain URI forms', function() {
26+
it('Should not support backtracking below base in plain URI forms', function () {
2727
var thrown = false;
2828
try {
2929
resolveIfNotPlain('../asdf', 'npm:lodash/path');
@@ -34,7 +34,7 @@ describe('Simple normalization tests', function() {
3434
if (!thrown)
3535
throw new Error('Test should have thrown a RangeError exception');
3636
});
37-
it('Should not support backtracking exactly to the base in plain URI forms', function() {
37+
it('Should not support backtracking exactly to the base in plain URI forms', function () {
3838
var thrown = false;
3939
try {
4040
resolveIfNotPlain('../', 'npm:lodash/asdf/y');
@@ -45,32 +45,35 @@ describe('Simple normalization tests', function() {
4545
if (thrown)
4646
throw new Error('Test should not have thrown a RangeError exception');
4747
});
48-
it('Should support "." for resolution', function() {
48+
it('Should support "." for resolution', function () {
4949
assert.equal(resolveIfNotPlain('.', 'https://www.google.com/asdf/asdf'), 'https://www.google.com/asdf/');
5050
});
51-
it('Should support ".." resolution', function() {
51+
it('Should support ".." resolution', function () {
5252
assert.equal(resolveIfNotPlain('..', 'https://www.google.com/asdf/asdf/asdf'), 'https://www.google.com/asdf/');
5353
});
54-
it('Should support "./" for resolution', function() {
54+
it('Should support "./" for resolution', function () {
5555
assert.equal(resolveIfNotPlain('./', 'https://www.google.com/asdf/asdf'), 'https://www.google.com/asdf/');
5656
});
57-
it('Should support "../" resolution', function() {
57+
it('Should support "../" resolution', function () {
5858
assert.equal(resolveIfNotPlain('../', 'https://www.google.com/asdf/asdf/asdf'), 'https://www.google.com/asdf/');
5959
});
60-
it('Should leave a trailing "/"', function() {
60+
it('Should leave a trailing "/"', function () {
6161
assert.equal(resolveIfNotPlain('./asdf/', 'file:///x/y'), 'file:///x/asdf/');
6262
});
63-
it('Should leave a trailing "//"', function() {
63+
it('Should leave a trailing "//"', function () {
6464
assert.equal(resolveIfNotPlain('./asdf//', 'file:///x/y'), 'file:///x/asdf//');
6565
});
66+
it('Should support a trailing ".."', function () {
67+
assert.equal(resolveIfNotPlain('../..', 'path/to/test/module.js'), 'path/');
68+
});
6669
});
6770

6871
import fs from 'fs';
6972
var testCases = eval('(' + fs.readFileSync('test/fixtures/url-resolution-cases.json') + ')');
7073

71-
describe('URL resolution selected WhatWG URL spec tests', function() {
74+
describe('URL resolution selected WhatWG URL spec tests', function () {
7275
var run = 0;
73-
testCases.forEach(function(test) {
76+
testCases.forEach(function (test) {
7477
if (typeof test == 'string')
7578
return;
7679

@@ -121,7 +124,7 @@ describe('URL resolution selected WhatWG URL spec tests', function() {
121124
if (test.input == '')
122125
return;
123126

124-
it('Should resolve "' + test.input + '" to "' + test.base + '"', function() {
127+
it('Should resolve "' + test.input + '" to "' + test.base + '"', function () {
125128
var failed = false;
126129
try {
127130
var resolved = resolveIfNotPlain(test.input, test.base) || resolveIfNotPlain('./' + test.input, test.base);

0 commit comments

Comments
 (0)