1
Fork 0

Rollup merge of #67908 - ollie27:rustdoc_const_html_escape, r=GuillaumeGomez

rustdoc: HTML escape const values

r? @GuillaumeGomez
This commit is contained in:
Yuki Okushi 2020-01-07 13:46:07 +09:00 committed by GitHub
commit c07204b1d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 5 deletions

View file

@ -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)?;
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) => {

View file

@ -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));
}
}
}

View file

@ -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() {}
}

View file

@ -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>"#;