diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-01-07 13:46:07 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-07 13:46:07 +0900 |
| commit | c07204b1d1eb9144c8be560a8f5d7f9aed0a5b79 (patch) | |
| tree | 1beee7ab161d7f31572d27e9e78f0905653284df | |
| parent | 9ef3b2ca2a9d37657ea1b5de056dd7324dca0632 (diff) | |
| parent | e2305d0055805effb9d6200f995ad825aa0c56e1 (diff) | |
| download | rust-c07204b1d1eb9144c8be560a8f5d7f9aed0a5b79.tar.gz rust-c07204b1d1eb9144c8be560a8f5d7f9aed0a5b79.zip | |
Rollup merge of #67908 - ollie27:rustdoc_const_html_escape, r=GuillaumeGomez
rustdoc: HTML escape const values r? @GuillaumeGomez
| -rw-r--r-- | src/librustdoc/html/format.rs | 17 | ||||
| -rw-r--r-- | src/librustdoc/html/render.rs | 4 | ||||
| -rw-r--r-- | src/test/rustdoc/const-generics/const-impl.rs | 7 | ||||
| -rw-r--r-- | src/test/rustdoc/show-const-contents.rs | 3 |
4 files changed, 26 insertions, 5 deletions
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 2580bbf982b..6434dccdfc7 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -15,6 +15,7 @@ use rustc_hir::def_id::DefId; use rustc_target::spec::abi::Abi; use crate::clean::{self, PrimitiveType}; +use crate::html::escape::Escape; use crate::html::item_type::ItemType; use crate::html::render::{self, cache, CURRENT_DEPTH}; @@ -314,8 +315,14 @@ impl clean::Lifetime { } impl clean::Constant { - crate fn print(&self) -> &str { - &self.expr + crate fn print(&self) -> impl fmt::Display + '_ { + display_fn(move |f| { + if f.alternate() { + f.write_str(&self.expr) + } else { + write!(f, "{}", Escape(&self.expr)) + } + }) } } @@ -689,7 +696,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) -> clean::Array(ref t, ref n) => { primitive_link(f, PrimitiveType::Array, "[")?; fmt::Display::fmt(&t.print(), f)?; - primitive_link(f, PrimitiveType::Array, &format!("; {}]", n)) + if f.alternate() { + primitive_link(f, PrimitiveType::Array, &format!("; {}]", n)) + } else { + primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n))) + } } clean::Never => primitive_link(f, PrimitiveType::Never, "!"), clean::RawPointer(m, ref t) => { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 025257b801f..a01e2f79394 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2280,7 +2280,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons ); if c.value.is_some() || c.is_literal { - write!(w, " = {expr};", expr = c.expr); + write!(w, " = {expr};", expr = Escape(&c.expr)); } else { write!(w, ";"); } @@ -2293,7 +2293,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons if value_lowercase != expr_lowercase && value_lowercase.trim_end_matches("i32") != expr_lowercase { - write!(w, " // {value}", value = value); + write!(w, " // {value}", value = Escape(value)); } } } diff --git a/src/test/rustdoc/const-generics/const-impl.rs b/src/test/rustdoc/const-generics/const-impl.rs index 819adfeb9c7..7361b22b747 100644 --- a/src/test/rustdoc/const-generics/const-impl.rs +++ b/src/test/rustdoc/const-generics/const-impl.rs @@ -30,3 +30,10 @@ impl <T> VSet<T, {Order::Unsorted}> { Self { inner: Vec::new() } } } + +pub struct Escape<const S: &'static str>; + +// @has foo/struct.Escape.html '//h3[@id="impl"]/code' 'impl Escape<{ r#"<script>alert("Escape");</script>"# }>' +impl Escape<{ r#"<script>alert("Escape");</script>"# }> { + pub fn f() {} +} diff --git a/src/test/rustdoc/show-const-contents.rs b/src/test/rustdoc/show-const-contents.rs index 6d95f7827a1..e84f6e52c75 100644 --- a/src/test/rustdoc/show-const-contents.rs +++ b/src/test/rustdoc/show-const-contents.rs @@ -62,3 +62,6 @@ macro_rules! int_module { // @has show_const_contents/constant.MIN.html '= i16::min_value(); // -32_768i16' int_module!(i16); + +// @has show_const_contents/constant.ESCAPE.html //pre '= r#"<script>alert("ESCAPE");</script>"#;' +pub const ESCAPE: &str = r#"<script>alert("ESCAPE");</script>"#; |
