Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special case the file: protocol #204

Merged
merged 1 commit into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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