Skip to content

Commit 7a10cc5

Browse files
committed
support percent-encoding, fix #135
1 parent 70787aa commit 7a10cc5

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ quick-xml = "0.22.0"
1717
jwalk = "0.6.0"
1818
patricia_tree = "0.3.1"
1919
bumpalo = { version = "3.8.0", features = ["collections"] }
20+
percent-encoding = "2.1.0"
2021

2122
[dev-dependencies]
2223
assert_cmd = "2.0.2"

src/html.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ mod test_push_and_canonicalize {
128128
}
129129
}
130130

131+
#[inline]
132+
fn try_percent_decode(input: &str) -> Cow<'_, str> {
133+
percent_encoding::percent_decode_str(input)
134+
.decode_utf8()
135+
.unwrap_or(Cow::Borrowed(input))
136+
}
137+
131138
#[inline]
132139
fn try_unescape_attribute_value<'a>(attr: &'a Attribute<'_>) -> Cow<'a, [u8]> {
133140
attr.unescaped_value().unwrap_or(Cow::Borrowed(&attr.value))
@@ -220,6 +227,7 @@ impl Document {
220227
.to_str()
221228
.expect("Invalid unicode in path")
222229
.to_owned();
230+
223231
if cfg!(windows) {
224232
unsafe {
225233
// safety: we replace ascii bytes only
@@ -260,7 +268,7 @@ impl Document {
260268
href.push('/');
261269
}
262270

263-
push_and_canonicalize(&mut href, &rel_href[..qs_start]);
271+
push_and_canonicalize(&mut href, &try_percent_decode(&rel_href[..qs_start]));
264272

265273
if preserve_anchor {
266274
let anchor = &rel_href[anchor_start..];
@@ -458,6 +466,10 @@ fn test_document_links() {
458466
<a href='../../go/?foo=bar&bar=baz' href='../../go/'>
459467
<a href="&#109;&#97;" />
460468
469+
<!-- test url encoding within HTML + percent-encoding within html encoding -->
470+
<a href="%5Bslug%5D.js" />
471+
<a href="&#37;5Bschlug%5D.js" />
472+
461473
<!-- obfuscated mailto: link -->
462474
<a href='&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#102;&#111;&#111;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;' />
463475
"""#
@@ -484,6 +496,8 @@ fn test_document_links() {
484496
used_link("platforms/go"),
485497
used_link("platforms/go"),
486498
used_link("platforms/python/troubleshooting/ma"),
499+
used_link("platforms/python/troubleshooting/[slug].js"),
500+
used_link("platforms/python/troubleshooting/[schlug].js"),
487501
]
488502
);
489503
}

0 commit comments

Comments
 (0)