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

Commit

Permalink
fix trailing segment support regression
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Sep 18, 2017
1 parent 31a72a2 commit 4c9a9ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
7 changes: 2 additions & 5 deletions core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
// new segment - check if it is relative
if (segmented[i] === '.') {
// ../ segment
if (segmented[i + 1] === '.' && segmented[i + 2] === '/') {
if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
output.pop();
i += 2;
}
// ./ segment
else if (segmented[i + 1] === '/') {
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
i += 1;
}
else {
Expand All @@ -95,9 +95,6 @@ export function resolveIfNotPlain (relUrl, parentUrl) {
if (parentIsPlain && output.length === 0)
throwResolveError(relUrl, parentUrl);

// trailing . or .. segment
if (i === segmented.length)
output.push('');
continue;
}

Expand Down
37 changes: 20 additions & 17 deletions test/1-url-resolution.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { resolveIfNotPlain } from '../core/resolve.js';
import assert from 'assert';

describe('Simple normalization tests', function() {
describe('Simple normalization tests', function () {
it('Should trim whitespace from URLs', function () {
assert.equal(resolveIfNotPlain(' c:\\some\\path ', 'file:///c:/adsf/asdf'), 'file:///c:/some/path');
});
it('Should resolve relative with protocol', function () {
assert.equal(resolveIfNotPlain('./x:y', 'https://x.com/y'), 'https://x.com/x:y');
});
it('Should resolve windows paths as file:/// URLs', function() {
it('Should resolve windows paths as file:/// URLs', function () {
assert.equal(resolveIfNotPlain('c:\\some\\path', 'file:///c:/adsf/asdf'), 'file:///c:/some/path');
});
it('Should resolve relative windows paths', function() {
it('Should resolve relative windows paths', function () {
assert.equal(resolveIfNotPlain('./test.js', 'file:///C:/some/path/'), 'file:///C:/some/path/test.js');
});
it('Should resolve unix file paths as file:/// URLs', function() {
it('Should resolve unix file paths as file:/// URLs', function () {
assert.equal(resolveIfNotPlain('/some/file/path.js', 'file:///home/path/to/project'), 'file:///some/file/path.js');
});
it('Should be able to resolve to plain names', function() {
it('Should be able to resolve to plain names', function () {
assert.equal(resolveIfNotPlain('../../asdf/./asdf/.asdf/asdf', 'a/b/c/d'), 'a/asdf/asdf/.asdf/asdf');
});
it('Should support resolving plain URI forms', function() {
it('Should support resolving plain URI forms', function () {
assert.equal(resolveIfNotPlain('./asdf', 'npm:lodash/'), 'npm:lodash/asdf');
});
it('Should not support backtracking below base in plain URI forms', function() {
it('Should not support backtracking below base in plain URI forms', function () {
var thrown = false;
try {
resolveIfNotPlain('../asdf', 'npm:lodash/path');
Expand All @@ -34,7 +34,7 @@ describe('Simple normalization tests', function() {
if (!thrown)
throw new Error('Test should have thrown a RangeError exception');
});
it('Should not support backtracking exactly to the base in plain URI forms', function() {
it('Should not support backtracking exactly to the base in plain URI forms', function () {
var thrown = false;
try {
resolveIfNotPlain('../', 'npm:lodash/asdf/y');
Expand All @@ -45,32 +45,35 @@ describe('Simple normalization tests', function() {
if (thrown)
throw new Error('Test should not have thrown a RangeError exception');
});
it('Should support "." for resolution', function() {
it('Should support "." for resolution', function () {
assert.equal(resolveIfNotPlain('.', 'https://www.google.com/asdf/asdf'), 'https://www.google.com/asdf/');
});
it('Should support ".." resolution', function() {
it('Should support ".." resolution', function () {
assert.equal(resolveIfNotPlain('..', 'https://www.google.com/asdf/asdf/asdf'), 'https://www.google.com/asdf/');
});
it('Should support "./" for resolution', function() {
it('Should support "./" for resolution', function () {
assert.equal(resolveIfNotPlain('./', 'https://www.google.com/asdf/asdf'), 'https://www.google.com/asdf/');
});
it('Should support "../" resolution', function() {
it('Should support "../" resolution', function () {
assert.equal(resolveIfNotPlain('../', 'https://www.google.com/asdf/asdf/asdf'), 'https://www.google.com/asdf/');
});
it('Should leave a trailing "/"', function() {
it('Should leave a trailing "/"', function () {
assert.equal(resolveIfNotPlain('./asdf/', 'file:///x/y'), 'file:///x/asdf/');
});
it('Should leave a trailing "//"', function() {
it('Should leave a trailing "//"', function () {
assert.equal(resolveIfNotPlain('./asdf//', 'file:///x/y'), 'file:///x/asdf//');
});
it('Should support a trailing ".."', function () {
assert.equal(resolveIfNotPlain('../..', 'path/to/test/module.js'), 'path/');
});
});

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

describe('URL resolution selected WhatWG URL spec tests', function() {
describe('URL resolution selected WhatWG URL spec tests', function () {
var run = 0;
testCases.forEach(function(test) {
testCases.forEach(function (test) {
if (typeof test == 'string')
return;

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

it('Should resolve "' + test.input + '" to "' + test.base + '"', function() {
it('Should resolve "' + test.input + '" to "' + test.base + '"', function () {
var failed = false;
try {
var resolved = resolveIfNotPlain(test.input, test.base) || resolveIfNotPlain('./' + test.input, test.base);
Expand Down

0 comments on commit 4c9a9ff

Please sign in to comment.