Skip to content

Commit 2aa7b8f

Browse files
committed
Parser: fix leading slashes logic
1 parent cc94000 commit 2aa7b8f

File tree

4 files changed

+14
-39
lines changed

4 files changed

+14
-39
lines changed

CHANGELOG.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
unreleased:
2+
fixed bugs:
3+
- Fixed a bug where leading slashes are trimmed incorrectly
24
chores:
35
- Removed punycode fallback for Electron v3
46

parser/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ function parse (urlString) {
158158
},
159159
replacements = new ReplacementTracker(),
160160
pointer = 0,
161-
protocol,
162161
_length,
163162
length,
164163
index,
@@ -202,27 +201,27 @@ function parse (urlString) {
202201
// @todo support `protocol:host/path` and `protocol:/host/path`
203202
if ((index = urlString.indexOf(PROTOCOL_SEPARATOR)) !== -1) {
204203
// extract from the front
205-
url.protocol.value = protocol = urlString.slice(0, index);
204+
url.protocol.value = urlString.slice(0, index);
206205
url.protocol.beginIndex = pointer;
207206
url.protocol.endIndex = pointer + index;
208207

209208
urlString = urlString.slice(index + 3);
210209
length -= index + 3;
211210
pointer += index + 3;
212-
}
213211

214-
// special handling for leading slashes e.g, http:///example.com
215-
_length = length; // length with leading slashes
212+
// special handling for extra slashes in protocol e.g, http:///example.com
213+
_length = length; // length with leading slashes
216214

217-
urlString = urlString.replace(REGEX_LEADING_SLASHES,
218-
(protocol && protocol.toLowerCase() === FILE_PROTOCOL) ?
219-
// file:////path -> file:///path
220-
PATH_SEPARATOR :
221-
// protocol:////host/path -> protocol://host/path
222-
E);
215+
urlString = urlString.replace(REGEX_LEADING_SLASHES,
216+
(url.protocol.value.toLowerCase() === FILE_PROTOCOL) ?
217+
// file:////path -> file:///path
218+
PATH_SEPARATOR :
219+
// protocol:////host/path -> protocol://host/path
220+
E);
223221

224-
length = urlString.length; // length without slashes
225-
pointer += _length - length; // update pointer
222+
length = urlString.length; // length without slashes
223+
pointer += _length - length; // update pointer
224+
}
226225

227226
// 4. url.path
228227
if ((index = urlString.indexOf(PATH_SEPARATOR)) !== -1) {

test/unit/parser/parser.test.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,6 @@ describe('parser', function () {
246246
});
247247
});
248248

249-
it('should handle leading slashes', function () {
250-
expect(parser.parse('//localhost/foo')).to.deep.include({
251-
raw: '//localhost/foo',
252-
protocol: undefined,
253-
host: ['localhost'],
254-
path: ['foo']
255-
});
256-
});
257-
258-
it('should handle leading backslashes', function () {
259-
expect(parser.parse('\\\\localhost\\foo')).to.deep.include({
260-
raw: '\\\\localhost\\foo',
261-
protocol: undefined,
262-
host: ['localhost'],
263-
path: ['foo']
264-
});
265-
});
266-
267249
it('should handle file://host/foo', function () {
268250
expect(parser.parse('file://host/foo')).to.deep.include({
269251
raw: 'file://host/foo',

test/unit/toNodeUrl.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,6 @@ describe('.toNodeUrl', function () {
732732
href: 'https://example.com/foo/bar'
733733
});
734734

735-
expect(toNodeUrl('////example.com/foo/bar')).to.include({
736-
protocol: 'http:',
737-
host: 'example.com',
738-
hostname: 'example.com',
739-
pathname: '/foo/bar',
740-
href: 'http://example.com/foo/bar'
741-
});
742-
743735
// eslint-disable-next-line no-useless-escape
744736
expect(toNodeUrl('https:/\/\/\example.com/foo/bar')).to.include({
745737
protocol: 'https:',

0 commit comments

Comments
 (0)