Skip to content

Commit a9d7027

Browse files
rustdoc: percent-encode URL fragments
1 parent cd061c7 commit a9d7027

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/librustdoc/html/markdown.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for HeadingLinks<'a, '_,
582582
}
583583
}
584584
let id = self.id_map.derive(id);
585+
let percent_encoded_id = small_url_encode(id.clone());
585586

586587
if let Some(ref mut builder) = self.toc {
587588
let mut text_header = String::new();
@@ -596,8 +597,9 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for HeadingLinks<'a, '_,
596597
std::cmp::min(level as u32 + (self.heading_offset as u32), MAX_HEADER_LEVEL);
597598
self.buf.push_back((Event::Html(format!("</h{level}>").into()), 0..0));
598599

599-
let start_tags =
600-
format!("<h{level} id=\"{id}\"><a class=\"doc-anchor\" href=\"#{id}\">§</a>");
600+
let start_tags = format!(
601+
"<h{level} id=\"{id}\"><a class=\"doc-anchor\" href=\"#{percent_encoded_id}\">§</a>"
602+
);
601603
return Some((Event::Html(start_tags.into()), 0..0));
602604
}
603605
event

tests/rustdoc-html/unicode.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![crate_name = "unicode"]
2+
3+
pub struct Foo;
4+
5+
impl Foo {
6+
//@ has unicode/struct.Foo.html //a/@href "#%C3%BA"
7+
//@ !has unicode/struct.Foo.html //a/@href "#ú"
8+
/// # ú
9+
pub fn foo() {}
10+
}

0 commit comments

Comments
 (0)