Skip to content

Commit 4ced30a

Browse files
committed
fix(build): handle relative paths with missing authority
closes #387
1 parent 7168049 commit 4ced30a

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/URI.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,19 +669,21 @@
669669

670670
URI.build = function(parts) {
671671
var t = '';
672+
var requireAbsolutePath = false
672673

673674
if (parts.protocol) {
674675
t += parts.protocol + ':';
675676
}
676677

677678
if (!parts.urn && (t || parts.hostname)) {
678679
t += '//';
680+
requireAbsolutePath = true
679681
}
680682

681683
t += (URI.buildAuthority(parts) || '');
682684

683685
if (typeof parts.path === 'string') {
684-
if (parts.path.charAt(0) !== '/' && typeof parts.hostname === 'string') {
686+
if (parts.path.charAt(0) !== '/' && requireAbsolutePath) {
685687
t += '/';
686688
}
687689

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@
234234
})(t);
235235
}
236236

237+
module('serializing');
238+
test('scheme and relative path', function() {
239+
var u = new URI('')
240+
.protocol('food')
241+
.path('test/file.csv')
242+
.toString()
243+
244+
equal(u.toString(), 'food:///test/file.csv', 'relative-path with scheme but no authority');
245+
});
246+
237247
module('mutating basics');
238248
test('protocol', function() {
239249
var u = new URI('http://example.org/foo.html');

test/urls.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,54 @@ var urls = [{
629629
idn: false,
630630
punycode: false
631631
}
632+
}, {
633+
name: 'missing authority',
634+
url: 'food:///test/file.csv',
635+
parts: {
636+
protocol: 'food',
637+
username: null,
638+
password: null,
639+
hostname: null,
640+
port: null,
641+
path: '/test/file.csv',
642+
query: null,
643+
fragment: null
644+
},
645+
accessors: {
646+
protocol: 'food',
647+
username: '',
648+
password: '',
649+
port: '',
650+
path: '/test/file.csv',
651+
query: '',
652+
fragment: '',
653+
resource: '/test/file.csv',
654+
authority: '',
655+
origin: '',
656+
userinfo: '',
657+
subdomain: '',
658+
domain: '',
659+
tld: '',
660+
directory: '/test',
661+
filename: 'file.csv',
662+
suffix: 'csv',
663+
hash: '',
664+
search: '',
665+
host: '',
666+
hostname: ''
667+
},
668+
is: {
669+
urn: false,
670+
url: true,
671+
relative: true,
672+
name: false,
673+
sld: false,
674+
ip: false,
675+
ip4: false,
676+
ip6: false,
677+
idn: false,
678+
punycode: false
679+
}
632680
}, {
633681
name: 'IPv4',
634682
url: 'http://user:pass@123.123.123.123:123/some/directory/file.html?query=string#fragment',

0 commit comments

Comments
 (0)