]> git.lizzy.rs Git - rust.git/commitdiff
Escape item search summaries
authorNoah Lev <camelidcamel@gmail.com>
Mon, 5 Jul 2021 02:22:24 +0000 (19:22 -0700)
committerNoah Lev <camelidcamel@gmail.com>
Sun, 25 Jul 2021 00:15:01 +0000 (17:15 -0700)
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!

src/librustdoc/html/markdown.rs
src/librustdoc/html/markdown/tests.rs

index 908e292d968efbb045a2bb474a540a1e1e0cc06d..783b8b2db46a10be5619ca8b660331d85e2b9ab0 100644 (file)
@@ -34,6 +34,7 @@
 use crate::clean::RenderedLink;
 use crate::doctest;
 use crate::html::escape::Escape;
+use crate::html::format::Buffer;
 use crate::html::highlight;
 use crate::html::toc::TocBuilder;
 
@@ -41,8 +42,6 @@
     html, BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag,
 };
 
-use super::format::Buffer;
-
 #[cfg(test)]
 mod tests;
 
@@ -1086,7 +1085,7 @@ fn markdown_summary_with_limit(
     let mut stopped_early = false;
 
     fn push(s: &mut String, text_length: &mut usize, text: &str) {
-        s.push_str(text);
+        write!(s, "{}", Escape(text)).unwrap();
         *text_length += text.len();
     }
 
index d10da64ccfaa5447a50e4610d046e21e6e3dd520..1e4bdc2d15199525b173ed084984cfcc50ff04e2 100644 (file)
@@ -234,7 +234,17 @@ fn t(input: &str, expect: &str) {
     t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
     t("dud [link]", "dud [link]");
     t("code `let x = i32;` ...", "code <code>let x = i32;</code> …");
-    t("type `Type<'static>` ...", "type <code>Type<'static></code> …");
+    t("type `Type<'static>` ...", "type <code>Type&lt;&#39;static&gt;</code> …");
+    // Test to ensure escaping and length-limiting work well together.
+    // The output should be limited based on the input length,
+    // rather than the output, because escaped versions of characters
+    // are usually longer than how the character is actually displayed.
+    t(
+        "& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &",
+        "&amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; \
+         &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; &amp; \
+         &amp; &amp; &amp; &amp; &amp; …",
+    );
     t("# top header", "top header");
     t("# top header\n\nfollowed by a paragraph", "top header");
     t("## header", "header");