Skip to content

Commit

Permalink
Merge 98a12b8 into af84da0
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed May 14, 2021
2 parents af84da0 + 98a12b8 commit 91d913b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Expand Up @@ -33,7 +33,7 @@ var rules = [
['#', 'hash'], // Extract from the back.
['?', 'query'], // Extract from the back.
function sanitize(address) { // Sanitize what is left of the address
return address.replace('\\', '/');
return address.replace(/\\/g, '/');
},
['/', 'pathname'], // Extract from the back.
['@', 'auth', 1], // Extract from the front.
Expand Down Expand Up @@ -224,7 +224,9 @@ function Url(address, location, parser) {
// When the authority component is absent the URL starts with a path
// component.
//
if (!extracted.slashes) instructions[3] = [/(.*)/, 'pathname'];
if (!extracted.slashes || url.protocol === 'file:') {
instructions[3] = [/(.*)/, 'pathname'];
}

for (; i < instructions.length; i++) {
instruction = instructions[i];
Expand Down Expand Up @@ -288,7 +290,10 @@ function Url(address, location, parser) {
// Default to a / for pathname if none exists. This normalizes the URL
// to always have a /
//
if (url.pathname.charAt(0) !== '/' && url.hostname) {
if (
url.pathname.charAt(0) !== '/'
&& (url.hostname || url.protocol === 'file:')
) {
url.pathname = '/' + url.pathname;
}

Expand Down Expand Up @@ -430,7 +435,7 @@ function toString(stringify) {

if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';

var result = protocol + (url.slashes ? '//' : '');
var result = protocol + (url.slashes || url.protocol === 'file:' ? '//' : '');

if (url.username) {
result += url.username;
Expand Down
42 changes: 42 additions & 0 deletions test/test.js
Expand Up @@ -438,6 +438,48 @@ describe('url-parse', function () {
data.set('protocol', 'https:');
assume(data.href).equals('https://google.com/foo');
});

it('handles the file: protocol', function () {
var slashes = ['', '/', '//', '///', '////', '/////'];
var data;
var url;

for (var i = 0; i < slashes.length; i++) {
data = parse('file:' + slashes[i]);
assume(data.protocol).equals('file:');
assume(data.pathname).equals('/');
assume(data.href).equals('file:///');
}

url = 'file:///Users/foo/BAR/baz.pdf';
data = parse(url);
assume(data.protocol).equals('file:');
assume(data.pathname).equals('/Users/foo/BAR/baz.pdf');
assume(data.href).equals(url);

url = 'file:///foo/bar?baz=qux#hash';
data = parse(url);
assume(data.protocol).equals('file:');
assume(data.hash).equals('#hash');
assume(data.query).equals('?baz=qux');
assume(data.pathname).equals('/foo/bar');
assume(data.href).equals(url);

data = parse('file://c:\\foo\\bar\\');
assume(data.protocol).equals('file:');
assume(data.pathname).equals('/c:/foo/bar/');
assume(data.href).equals('file:///c:/foo/bar/');

data = parse('foo/bar', 'file:///baz');
assume(data.protocol).equals('file:');
assume(data.pathname).equals('/foo/bar');
assume(data.href).equals('file:///foo/bar');

data = parse('foo/bar', 'file:///baz/');
assume(data.protocol).equals('file:');
assume(data.pathname).equals('/baz/foo/bar');
assume(data.href).equals('file:///baz/foo/bar');
});
});

describe('ip', function () {
Expand Down

0 comments on commit 91d913b

Please sign in to comment.