Skip to content

Commit f63ec77

Browse files
committed
Auto merge of #87331 - camelid:summary-escaping, r=GuillaumeGomez
Escape item search summaries I noticed that `Pin::new()`'s search summary looked off, and I realized that the reason is that it has inline code containing `Pin<P>`, which is not escaped and thus renders as a paragraph tag!
2 parents 2b4196e + 4ad2d68 commit f63ec77

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/librustdoc/html/markdown.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ use std::str;
3434
use crate::clean::RenderedLink;
3535
use crate::doctest;
3636
use crate::html::escape::Escape;
37+
use crate::html::format::Buffer;
3738
use crate::html::highlight;
3839
use crate::html::toc::TocBuilder;
3940

4041
use pulldown_cmark::{
4142
html, BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag,
4243
};
4344

44-
use super::format::Buffer;
45-
4645
#[cfg(test)]
4746
mod tests;
4847

@@ -1086,7 +1085,7 @@ fn markdown_summary_with_limit(
10861085
let mut stopped_early = false;
10871086

10881087
fn push(s: &mut String, text_length: &mut usize, text: &str) {
1089-
s.push_str(text);
1088+
write!(s, "{}", Escape(text)).unwrap();
10901089
*text_length += text.len();
10911090
}
10921091

src/librustdoc/html/markdown/tests.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,17 @@ fn test_short_markdown_summary() {
234234
t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
235235
t("dud [link]", "dud [link]");
236236
t("code `let x = i32;` ...", "code <code>let x = i32;</code> …");
237-
t("type `Type<'static>` ...", "type <code>Type<'static></code> …");
237+
t("type `Type<'static>` ...", "type <code>Type&lt;&#39;static&gt;</code> …");
238+
// Test to ensure escaping and length-limiting work well together.
239+
// The output should be limited based on the input length,
240+
// rather than the output, because escaped versions of characters
241+
// are usually longer than how the character is actually displayed.
242+
t(
243+
"& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &",
244+
"&amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; \
245+
&amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; \
246+
&amp; &amp; &amp; &amp; &amp; …",
247+
);
238248
t("# top header", "top header");
239249
t("# top header\n\nfollowed by a paragraph", "top header");
240250
t("## header", "header");

0 commit comments

Comments
 (0)