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
26 changes: 22 additions & 4 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ pub(crate) struct MarkdownWithToc<'a> {
}
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
/// and includes no paragraph tags.
pub(crate) struct MarkdownItemInfo<'a>(pub(crate) &'a str, pub(crate) &'a mut IdMap);
pub(crate) struct MarkdownItemInfo<'a>(
pub(crate) &'a str,
pub(crate) &'a mut IdMap,
pub(crate) Edition,
);
/// A tuple struct like `Markdown` that renders only the first paragraph.
pub(crate) struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [RenderedLink]);

Expand Down Expand Up @@ -1461,7 +1465,8 @@ impl MarkdownWithToc<'_> {

impl MarkdownItemInfo<'_> {
pub(crate) fn write_into(self, mut f: impl fmt::Write) -> fmt::Result {
let MarkdownItemInfo(md, ids) = self;
let MarkdownItemInfo(md, ids, edition) = self;
let legacy_wrap = edition != Edition::EditionFuture;

// This is actually common enough to special-case
if md.is_empty() {
Expand All @@ -1479,10 +1484,23 @@ impl MarkdownItemInfo<'_> {
let p = HeadingLinks::new(p, None, ids, HeadingOffset::H1);
let p = footnotes::Footnotes::new(p, existing_footnotes);
let p = TableWrapper::new(p.map(|(ev, _)| ev));
// in legacy wrap mode, strip <p> elements to avoid them inserting newlines
let p = p.filter(|event| {
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph))
!legacy_wrap
|| !matches!(
event,
Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph)
)
});
html::write_html_fmt(&mut f, p)
if legacy_wrap {
f.write_str("<span class=\"legacy-wrap\">")?;
}
html::write_html_fmt(&mut f, p)?;
if legacy_wrap {
f.write_str("</span>")?;
}

Ok(())
})
}
}
Expand Down
25 changes: 22 additions & 3 deletions src/librustdoc/html/markdown/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,31 @@ fn test_plain_text_summary() {
}

#[test]
fn test_markdown_html_escape() {
fn test_markdown_html_escape_legacy() {
fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new();
let mut output = String::new();
MarkdownItemInfo(input, &mut idmap).write_into(&mut output).unwrap();
assert_eq!(output, expect, "original: {}", input);
MarkdownItemInfo(input, &mut idmap, Edition::Edition2015).write_into(&mut output).unwrap();
assert_eq!(
output,
format!("<span class=\"legacy-wrap\">{}</span>", expect),
"original: {}",
input
);
}

t("`Struct<'a, T>`", "<code>Struct&lt;'a, T&gt;</code>");
t("Struct<'a, T>", "Struct&lt;’a, T&gt;");
t("Struct<br>", "Struct&lt;br&gt;");
}

#[test]
fn test_markdown_html_escape_new() {
fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new();
let mut output = String::new();
MarkdownItemInfo(input, &mut idmap, Edition::Edition2015).write_into(&mut output).unwrap();
assert_eq!(output, format!("<p>{}</p>", expect), "original: {}", input);
}

t("`Struct<'a, T>`", "<code>Struct&lt;'a, T&gt;</code>");
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ fn short_item_info(
parent: Option<&clean::Item>,
) -> Vec<ShortItemInfo> {
let mut extra_info = vec![];
let edition = item.span(cx.tcx()).expect("unable to get edition of span").inner().edition();

if let Some(depr @ Deprecation { note, since, suggestion: _ }) = item.deprecation(cx.tcx()) {
// We display deprecation messages for #[deprecated], but only display
Expand All @@ -877,7 +878,7 @@ fn short_item_info(
if let Some(note) = note {
let note = note.as_str();
let mut id_map = cx.id_map.borrow_mut();
let html = MarkdownItemInfo(note, &mut id_map);
let html = MarkdownItemInfo(note, &mut id_map, edition);
message.push_str(": ");
html.write_into(&mut message).unwrap();
}
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,6 @@ so that we can apply CSS-filters to change the arrow color in themes */
color: var(--main-color);
background-color: var(--stab-background-color);
width: fit-content;
white-space: pre-wrap;
border-radius: 3px;
display: inline;
vertical-align: baseline;
Expand All @@ -1588,6 +1587,10 @@ so that we can apply CSS-filters to change the arrow color in themes */
color: var(--stab-code-color);
}

.stab span.legacy-wrap {
white-space: pre-wrap;
}

.stab .emoji, .item-info .stab::before {
font-size: 1.25rem;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/rustdoc/deprecated.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//@ compile-flags: -Zunstable-options
//@ edition: future

//@ has deprecated/index.html '//dt/span[@class="stab deprecated"]' 'Deprecated'
//@ has - '//dd' 'Deprecated docs'

Expand Down Expand Up @@ -30,3 +33,8 @@ pub struct W;
// 'Deprecated: shorthand reason: code$'
#[deprecated = "shorthand reason: `code`"]
pub struct X;

//@ matches deprecated/struct.Y.html '//*[@class="stab deprecated"]//p[1]' 'multiple'
//@ matches deprecated/struct.Y.html '//*[@class="stab deprecated"]//p[2]' 'paragraphs'
#[deprecated = "multiple\n\nparagraphs"]
pub struct Y;
Loading