Skip to content

Commit 37fe8a7

Browse files
author
bors-servo
authored
Auto merge of #248 - Manishearth:percent-fragment, r=SimonSapin
Percent-encode fragments while parsing Based on whatwg/url#169 Fixes #246 cc @valenting r? @SimonSapin <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/248) <!-- Reviewable:end -->
2 parents 117d6f2 + 6512d12 commit 37fe8a7

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1083,12 +1083,13 @@ impl<'a> Parser<'a> {
10831083
}
10841084

10851085
pub fn parse_fragment(&mut self, mut input: Input) {
1086-
while let Some(c) = input.next() {
1086+
while let Some((c, utf8_c)) = input.next_utf8() {
10871087
if c == '\0' {
10881088
self.syntax_violation("NULL characters are ignored in URL fragment identifiers")
10891089
} else {
10901090
self.check_url_code_point(c, &input);
1091-
self.serialization.push(c); // No percent-encoding here.
1091+
self.serialization.extend(utf8_percent_encode(utf8_c,
1092+
SIMPLE_ENCODE_SET));
10921093
}
10931094
}
10941095
}

tests/setters_tests.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1127,21 +1127,21 @@
11271127
}
11281128
},
11291129
{
1130-
"comment": "No percent-encoding at all (!); nuls, tabs, and newlines are removed. Leading or training C0 controls and space are removed.",
1130+
"comment": "Simple percent-encoding; nuls, tabs, and newlines are removed",
11311131
"href": "a:/",
11321132
"new_value": "\u0000\u0001\t\n\r\u001f !\u0000\u0001\t\n\r\u001f !\"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~\u007f\u0080\u0081Éé",
11331133
"expected": {
1134-
"href": "a:/#!\u0001\u001f !\"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~\u007f\u0080\u0081Éé",
1135-
"hash": "#!\u0001\u001f !\"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~\u007f\u0080\u0081Éé"
1134+
"href": "a:/#!%01%1F !\"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~%7F%C2%80%C2%81%C3%89%C3%A9",
1135+
"hash": "#!%01%1F !\"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~%7F%C2%80%C2%81%C3%89%C3%A9"
11361136
}
11371137
},
11381138
{
11391139
"comment": "Bytes already percent-encoded are left as-is",
11401140
"href": "http://example.net",
11411141
"new_value": "%c3%89té",
11421142
"expected": {
1143-
"href": "http://example.net/#%c3%89té",
1144-
"hash": "#%c3%89té"
1143+
"href": "http://example.net/#%c3%89t%C3%A9",
1144+
"hash": "#%c3%89t%C3%A9"
11451145
}
11461146
}
11471147
]

tests/urltestdata.json

+23-6
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@
12921292
{
12931293
"input": "",
12941294
"base": "http://example.org/foo/bar",
1295-
"href": "http://example.org/foo/bar#β",
1295+
"href": "http://example.org/foo/bar#%CE%B2",
12961296
"origin": "http://example.org",
12971297
"protocol": "http:",
12981298
"username": "",
@@ -1302,7 +1302,7 @@
13021302
"port": "",
13031303
"pathname": "/foo/bar",
13041304
"search": "",
1305-
"hash": "#β"
1305+
"hash": "#%CE%B2"
13061306
},
13071307
{
13081308
"input": "data:text/html,test#test",
@@ -2161,7 +2161,7 @@
21612161
{
21622162
"input": "http://www.google.com/foo?bar=baz# »",
21632163
"base": "about:blank",
2164-
"href": "http://www.google.com/foo?bar=baz# »",
2164+
"href": "http://www.google.com/foo?bar=baz# %C2%BB",
21652165
"origin": "http://www.google.com",
21662166
"protocol": "http:",
21672167
"username": "",
@@ -2171,12 +2171,12 @@
21712171
"port": "",
21722172
"pathname": "/foo",
21732173
"search": "?bar=baz",
2174-
"hash": "# »"
2174+
"hash": "# %C2%BB"
21752175
},
21762176
{
21772177
"input": "data:test# »",
21782178
"base": "about:blank",
2179-
"href": "data:test# »",
2179+
"href": "data:test# %C2%BB",
21802180
"origin": "null",
21812181
"protocol": "data:",
21822182
"username": "",
@@ -2186,7 +2186,7 @@
21862186
"port": "",
21872187
"pathname": "test",
21882188
"search": "",
2189-
"hash": "# »"
2189+
"hash": "# %C2%BB"
21902190
},
21912191
{
21922192
"input": "http://[www.google.com]/",
@@ -4356,5 +4356,22 @@
43564356
"search": "",
43574357
"searchParams": "",
43584358
"hash": ""
4359+
},
4360+
"# Percent encoding of fragments",
4361+
{
4362+
"input": "http://foo.bar/baz?qux#foo\bbar",
4363+
"base": "about:blank",
4364+
"href": "http://foo.bar/baz?qux#foo%08bar",
4365+
"origin": "http://foo.bar",
4366+
"protocol": "http:",
4367+
"username": "",
4368+
"password": "",
4369+
"host": "foo.bar",
4370+
"hostname": "foo.bar",
4371+
"port": "",
4372+
"pathname": "/baz",
4373+
"search": "?qux",
4374+
"searchParams": "",
4375+
"hash": "#foo%08bar"
43594376
}
43604377
]

0 commit comments

Comments
 (0)