Skip to content

Commit e2305d0

Browse files
committed
rustdoc: HTML escape const values
1 parent cd8377d commit e2305d0

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/librustdoc/html/format.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::util::nodemap::FxHashSet;
1515
use rustc_target::spec::abi::Abi;
1616

1717
use crate::clean::{self, PrimitiveType};
18+
use crate::html::escape::Escape;
1819
use crate::html::item_type::ItemType;
1920
use crate::html::render::{self, cache, CURRENT_DEPTH};
2021

@@ -314,8 +315,14 @@ impl clean::Lifetime {
314315
}
315316

316317
impl clean::Constant {
317-
crate fn print(&self) -> &str {
318-
&self.expr
318+
crate fn print(&self) -> impl fmt::Display + '_ {
319+
display_fn(move |f| {
320+
if f.alternate() {
321+
f.write_str(&self.expr)
322+
} else {
323+
write!(f, "{}", Escape(&self.expr))
324+
}
325+
})
319326
}
320327
}
321328

@@ -689,7 +696,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
689696
clean::Array(ref t, ref n) => {
690697
primitive_link(f, PrimitiveType::Array, "[")?;
691698
fmt::Display::fmt(&t.print(), f)?;
692-
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n))
699+
if f.alternate() {
700+
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n))
701+
} else {
702+
primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)))
703+
}
693704
}
694705
clean::Never => primitive_link(f, PrimitiveType::Never, "!"),
695706
clean::RawPointer(m, ref t) => {

src/librustdoc/html/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
22792279
);
22802280

22812281
if c.value.is_some() || c.is_literal {
2282-
write!(w, " = {expr};", expr = c.expr);
2282+
write!(w, " = {expr};", expr = Escape(&c.expr));
22832283
} else {
22842284
write!(w, ";");
22852285
}
@@ -2292,7 +2292,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
22922292
if value_lowercase != expr_lowercase
22932293
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
22942294
{
2295-
write!(w, " // {value}", value = value);
2295+
write!(w, " // {value}", value = Escape(value));
22962296
}
22972297
}
22982298
}

src/test/rustdoc/const-generics/const-impl.rs

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ impl <T> VSet<T, {Order::Unsorted}> {
3030
Self { inner: Vec::new() }
3131
}
3232
}
33+
34+
pub struct Escape<const S: &'static str>;
35+
36+
// @has foo/struct.Escape.html '//h3[@id="impl"]/code' 'impl Escape<{ r#"<script>alert("Escape");</script>"# }>'
37+
impl Escape<{ r#"<script>alert("Escape");</script>"# }> {
38+
pub fn f() {}
39+
}

src/test/rustdoc/show-const-contents.rs

+3
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ macro_rules! int_module {
6262

6363
// @has show_const_contents/constant.MIN.html '= i16::min_value(); // -32_768i16'
6464
int_module!(i16);
65+
66+
// @has show_const_contents/constant.ESCAPE.html //pre '= r#"<script>alert("ESCAPE");</script>"#;'
67+
pub const ESCAPE: &str = r#"<script>alert("ESCAPE");</script>"#;

0 commit comments

Comments
 (0)