Skip to content

Commit 5a27f63

Browse files
watildeevanlucas
authored andcommitted
url: trim leading slashes of file URL paths
It should trim the slashes after the colon into three for file URL. PR-URL: #12507 Refs: web-platform-tests/wpt#5195 Fixes: #11188 Reviewed-By: James M Snell <[email protected]>
1 parent b50a84a commit 5a27f63

File tree

3 files changed

+393
-10
lines changed

3 files changed

+393
-10
lines changed

src/node_url.cc

+16-6
Original file line numberDiff line numberDiff line change
@@ -1108,12 +1108,14 @@ namespace url {
11081108
state = kFileHost;
11091109
} else {
11101110
if (has_base &&
1111-
base->scheme == "file:" &&
1112-
base->flags & URL_FLAGS_HAS_PATH &&
1113-
base->path.size() > 0 &&
1114-
NORMALIZED_WINDOWS_DRIVE_LETTER(base->path[0])) {
1115-
url->flags |= URL_FLAGS_HAS_PATH;
1116-
url->path.push_back(base->path[0]);
1111+
base->scheme == "file:") {
1112+
if (NORMALIZED_WINDOWS_DRIVE_LETTER(base->path[0])) {
1113+
url->flags |= URL_FLAGS_HAS_PATH;
1114+
url->path.push_back(base->path[0]);
1115+
} else {
1116+
url->flags |= URL_FLAGS_HAS_HOST;
1117+
url->host = base->host;
1118+
}
11171119
}
11181120
state = kPath;
11191121
continue;
@@ -1196,6 +1198,14 @@ namespace url {
11961198
url->path.push_back(segment);
11971199
}
11981200
buffer.clear();
1201+
if (url->scheme == "file:" &&
1202+
(ch == kEOL ||
1203+
ch == '?' ||
1204+
ch == '#')) {
1205+
while (url->path.size() > 1 && url->path[0].length() == 0) {
1206+
url->path.erase(url->path.begin());
1207+
}
1208+
}
11991209
if (ch == '?') {
12001210
url->flags |= URL_FLAGS_HAS_QUERY;
12011211
state = kQuery;

test/fixtures/url-setter-tests.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
/* WPT Refs:
4-
https://github.com/w3c/web-platform-tests/blob/e48dd15/url/setters_tests.json
4+
https://github.com/w3c/web-platform-tests/blob/3eff1bd/url/setters_tests.json
55
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
66
*/
77
module.exports =
@@ -1620,6 +1620,33 @@ module.exports =
16201620
"href": "sc://example.net/%23",
16211621
"pathname": "/%23"
16221622
}
1623+
},
1624+
{
1625+
"comment": "File URLs and (back)slashes",
1626+
"href": "file://monkey/",
1627+
"new_value": "\\\\",
1628+
"expected": {
1629+
"href": "file://monkey/",
1630+
"pathname": "/"
1631+
}
1632+
},
1633+
{
1634+
"comment": "File URLs and (back)slashes",
1635+
"href": "file:///unicorn",
1636+
"new_value": "//\\/",
1637+
"expected": {
1638+
"href": "file:///",
1639+
"pathname": "/"
1640+
}
1641+
},
1642+
{
1643+
"comment": "File URLs and (back)slashes",
1644+
"href": "file:///unicorn",
1645+
"new_value": "//monkey/..//",
1646+
"expected": {
1647+
"href": "file:///",
1648+
"pathname": "/"
1649+
}
16231650
}
16241651
],
16251652
"search": [

0 commit comments

Comments
 (0)