Skip to content

Commit

Permalink
rustdoc: HTML escape const values
Browse files Browse the repository at this point in the history
  • Loading branch information
ollie27 committed Jan 5, 2020
1 parent cd8377d commit e2305d0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc::util::nodemap::FxHashSet;
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};

Expand Down Expand Up @@ -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))
}
})
}
}

Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,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, ";");
}
Expand All @@ -2292,7 +2292,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));
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/rustdoc/const-generics/const-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
3 changes: 3 additions & 0 deletions src/test/rustdoc/show-const-contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>"#;

0 comments on commit e2305d0

Please sign in to comment.