Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Cfg {
msg
}

fn render_long_inner(&self, format: Format) -> String {
fn render_long_inner(&self, format: Format) -> impl fmt::Display {
let on = if self.omit_preposition() {
" "
} else if self.should_use_with_in_description() {
Expand All @@ -120,26 +120,26 @@ impl Cfg {
" on "
};

let mut msg = if matches!(format, Format::LongHtml) {
format!("Available{on}<strong>{}</strong>", Display(&self.0, format))
} else {
format!("Available{on}{}", Display(&self.0, format))
};
if self.should_append_only_to_description() {
msg.push_str(" only");
}
msg
fmt::from_fn(move |f| {
if matches!(format, Format::LongHtml) {
write!(f, "Available{on}<strong>{}</strong>", Display(&self.0, format))?
} else {
write!(f, "Available{on}{}", Display(&self.0, format))?
};
if self.should_append_only_to_description() {
f.write_str(" only")?;
}
Ok(())
})
}

/// Renders the configuration for long display, as a long HTML description.
pub(crate) fn render_long_html(&self) -> String {
let mut msg = self.render_long_inner(Format::LongHtml);
msg.push('.');
msg
pub(crate) fn render_long_html(&self) -> impl fmt::Display {
fmt::from_fn(|f| write!(f, "{}.", self.render_long_inner(Format::LongHtml)))
}

/// Renders the configuration for long display, as a long plain text description.
pub(crate) fn render_long_plain(&self) -> String {
pub(crate) fn render_long_plain(&self) -> impl fmt::Display {
self.render_long_inner(Format::LongPlain)
}

Expand Down
44 changes: 26 additions & 18 deletions src/librustdoc/clean/cfg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,73 +308,81 @@ fn test_render_short_html() {

#[test]
fn test_render_long_html() {
trait Cfg {
fn to_long_html(&self) -> String;
}

impl Cfg for super::Cfg {
fn to_long_html(&self) -> String {
self.render_long_html().to_string()
}
}

create_default_session_globals_then(|| {
assert_eq!(word_cfg("unix").render_long_html(), "Available on <strong>Unix</strong> only.");
assert_eq!(word_cfg("unix").to_long_html(), "Available on <strong>Unix</strong> only.");
assert_eq!(
name_value_cfg("target_os", "macos").render_long_html(),
name_value_cfg("target_os", "macos").to_long_html(),
"Available on <strong>macOS</strong> only."
);
assert_eq!(
name_value_cfg("target_os", "wasi").render_long_html(),
name_value_cfg("target_os", "wasi").to_long_html(),
"Available on <strong>WASI</strong> only."
);
assert_eq!(
name_value_cfg("target_pointer_width", "16").render_long_html(),
name_value_cfg("target_pointer_width", "16").to_long_html(),
"Available on <strong>16-bit</strong> only."
);
assert_eq!(
name_value_cfg("target_endian", "little").render_long_html(),
name_value_cfg("target_endian", "little").to_long_html(),
"Available on <strong>little-endian</strong> only."
);
assert_eq!(
(!word_cfg("windows")).render_long_html(),
(!word_cfg("windows")).to_long_html(),
"Available on <strong>non-Windows</strong> only."
);
assert_eq!(
(word_cfg("unix") & word_cfg("windows")).render_long_html(),
(word_cfg("unix") & word_cfg("windows")).to_long_html(),
"Available on <strong>Unix and Windows</strong> only."
);
assert_eq!(
(word_cfg("unix") | word_cfg("windows")).render_long_html(),
(word_cfg("unix") | word_cfg("windows")).to_long_html(),
"Available on <strong>Unix or Windows</strong> only."
);
assert_eq!(
(word_cfg("unix") & word_cfg("windows") & word_cfg("debug_assertions"))
.render_long_html(),
(word_cfg("unix") & word_cfg("windows") & word_cfg("debug_assertions")).to_long_html(),
"Available on <strong>Unix and Windows and debug-assertions enabled</strong> only."
);
assert_eq!(
(word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions"))
.render_long_html(),
(word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions")).to_long_html(),
"Available on <strong>Unix or Windows or debug-assertions enabled</strong> only."
);
assert_eq!(
(!(word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions")))
.render_long_html(),
.to_long_html(),
"Available on <strong>neither Unix nor Windows nor debug-assertions enabled</strong>."
);
assert_eq!(
((word_cfg("unix") & name_value_cfg("target_arch", "x86_64"))
| (word_cfg("windows") & name_value_cfg("target_pointer_width", "64")))
.render_long_html(),
.to_long_html(),
"Available on <strong>Unix and x86-64, or Windows and 64-bit</strong> only."
);
assert_eq!(
(!(word_cfg("unix") & word_cfg("windows"))).render_long_html(),
(!(word_cfg("unix") & word_cfg("windows"))).to_long_html(),
"Available on <strong>not (Unix and Windows)</strong>."
);
assert_eq!(
((word_cfg("debug_assertions") | word_cfg("windows")) & word_cfg("unix"))
.render_long_html(),
.to_long_html(),
"Available on <strong>(debug-assertions enabled or Windows) and Unix</strong> only."
);
assert_eq!(
name_value_cfg("target_feature", "sse2").render_long_html(),
name_value_cfg("target_feature", "sse2").to_long_html(),
"Available with <strong>target feature <code>sse2</code></strong> only."
);
assert_eq!(
(name_value_cfg("target_arch", "x86_64") & name_value_cfg("target_feature", "sse2"))
.render_long_html(),
.to_long_html(),
"Available on <strong>x86-64 and target feature <code>sse2</code></strong> only."
);
})
Expand Down
62 changes: 55 additions & 7 deletions src/librustdoc/html/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,60 @@

use std::fmt;

use pulldown_cmark_escape::FmtWriter;
use unicode_segmentation::UnicodeSegmentation;

#[inline]
fn escape(s: &str, mut w: impl fmt::Write, escape_quotes: bool) -> fmt::Result {
// Because the internet is always right, turns out there's not that many
// characters to escape: http://stackoverflow.com/questions/7381974
let pile_o_bits = s;
let mut last = 0;
for (i, ch) in s.char_indices() {
let s = match ch {
'>' => "&gt;",
'<' => "&lt;",
'&' => "&amp;",
'\'' if escape_quotes => "&#39;",
'"' if escape_quotes => "&quot;",
_ => continue,
};
w.write_str(&pile_o_bits[last..i])?;
w.write_str(s)?;
// NOTE: we only expect single byte characters here - which is fine as long as we
// only match single byte characters
last = i + 1;
}

if last < s.len() {
w.write_str(&pile_o_bits[last..])?;
}
Ok(())
}

struct WriteEscaped<W: fmt::Write> {
writer: W,
escape_quotes: bool,
}

impl<W: fmt::Write> fmt::Write for WriteEscaped<W> {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
escape(s, &mut self.writer, self.escape_quotes)
}
}

/// Wrapper struct which will emit the HTML-escaped version of the contained
/// string when passed to a format string.
pub(crate) struct Escape<'a>(pub &'a str);
pub(crate) struct Escape<T>(pub T);

impl fmt::Display for Escape<'_> {
impl<T: fmt::Display> fmt::Display for Escape<T> {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
pulldown_cmark_escape::escape_html(FmtWriter(fmt), self.0)
self.0.fmt(
&mut fmt
.options()
.create_formatter(&mut WriteEscaped { writer: fmt, escape_quotes: true }),
)
}
}

Expand All @@ -24,11 +68,15 @@ impl fmt::Display for Escape<'_> {
/// This is only safe to use for text nodes. If you need your output to be
/// safely contained in an attribute, use [`Escape`]. If you don't know the
/// difference, use [`Escape`].
pub(crate) struct EscapeBodyText<'a>(pub &'a str);
pub(crate) struct EscapeBodyText<T>(pub T);

impl fmt::Display for EscapeBodyText<'_> {
impl<T: fmt::Display> fmt::Display for EscapeBodyText<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
pulldown_cmark_escape::escape_html_body_text(FmtWriter(fmt), self.0)
self.0.fmt(
&mut fmt
.options()
.create_formatter(&mut WriteEscaped { writer: fmt, escape_quotes: false }),
)
}
}

Expand Down
25 changes: 10 additions & 15 deletions src/librustdoc/html/escape/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use std::iter;

#[test]
fn escape() {
use super::Escape as E;
assert_eq!(format!("<Hello> {}", E("<World>")), "<Hello> &lt;World&gt;");
}

// basic examples
#[test]
fn escape_body_text_with_wbr() {
Expand Down Expand Up @@ -47,21 +55,8 @@ fn escape_body_text_with_wbr_makes_sense() {
use itertools::Itertools as _;

use super::EscapeBodyTextWithWbr as E;
const C: [u8; 3] = [b'a', b'A', b'_'];
for chars in [
C.into_iter(),
C.into_iter(),
C.into_iter(),
C.into_iter(),
C.into_iter(),
C.into_iter(),
C.into_iter(),
C.into_iter(),
]
.into_iter()
.multi_cartesian_product()
{
let s = String::from_utf8(chars).unwrap();
for chars in iter::repeat("aA_").take(8).map(str::chars).multi_cartesian_product() {
let s = chars.into_iter().collect::<String>();
assert_eq!(s.len(), 8);
let esc = E(&s).to_string();
assert!(!esc.contains("<wbr><wbr>"));
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl D
r#"<a class="{kind}" href="{url}{anchor}" title="{kind} {path}">{text}</a>"#,
anchor = fragment(did, cx.tcx()),
path = join_path_syms(rust_path),
text = EscapeBodyText(text.as_str()),
text = EscapeBodyText(text),
)
} else {
f.write_str(text.as_str())
Expand Down
13 changes: 8 additions & 5 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,10 @@ fn document_item_info(
ItemInfo { items }
}

fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<String> {
fn portability<'a>(
item: &'a clean::Item,
parent: Option<&'a clean::Item>,
) -> Option<impl fmt::Display + 'a> {
let cfg = match (&item.cfg, parent.and_then(|p| p.cfg.as_ref())) {
(Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg),
(cfg, _) => cfg.as_deref().cloned(),
Expand All @@ -858,7 +861,7 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin
parent_cfg = parent.and_then(|p| p.cfg.as_ref()),
);

Some(cfg?.render_long_html())
cfg.map(|cfg| fmt::from_fn(move |f| cfg.render_long_html().fmt(f)))
}

#[derive(Template)]
Expand Down Expand Up @@ -901,7 +904,7 @@ fn short_item_info(
}
DeprecatedSince::Future => String::from("Deprecating in a future version"),
DeprecatedSince::NonStandard(since) => {
format!("Deprecated since {}", Escape(since.as_str()))
format!("Deprecated since {}", Escape(since))
}
DeprecatedSince::Unspecified | DeprecatedSince::Err => String::from("Deprecated"),
};
Expand Down Expand Up @@ -935,7 +938,7 @@ fn short_item_info(
}

if let Some(message) = portability(item, parent) {
extra_info.push(ShortItemInfo::Portability { message });
extra_info.push(ShortItemInfo::Portability { message: message.to_string() });
}

extra_info
Expand Down Expand Up @@ -1682,7 +1685,7 @@ fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<impl fmt:
write!(
f,
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
ty = Escape(&format!("{:#}", print_type(ty, cx))),
ty = Escape(format_args!("{:#}", print_type(ty, cx))),
)
})
})
Expand Down
8 changes: 6 additions & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,16 @@ fn print_extra_info_tags(
import_def_id: Option<DefId>,
) -> impl Display {
fmt::from_fn(move |f| {
fn tag_html(class: &str, title: &str, contents: &str) -> impl Display {
fn tag_html<'a>(
class: impl fmt::Display + 'a,
title: impl fmt::Display + 'a,
contents: impl fmt::Display + 'a,
) -> impl Display + 'a {
fmt::from_fn(move |f| {
write!(
f,
r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
title = Escape(title),
title = Escape(&title),
)
})
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![feature(ascii_char_variants)]
#![feature(deref_patterns)]
#![feature(file_buffered)]
#![feature(format_args_nl)]
#![feature(formatting_options)]
#![feature(iter_intersperse)]
#![feature(iter_order_by)]
Expand Down
Loading