From db8ddbf570fa7d8df5d71b19b44a771c80c689ba Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 5 Dec 2020 17:31:31 +0100 Subject: [PATCH 1/3] Move tooltips messages to CSS instead of inside HTML --- src/librustdoc/html/highlight.rs | 15 +++-- src/librustdoc/html/markdown.rs | 61 +++++---------------- src/librustdoc/html/static/rustdoc.css | 24 +++++--- src/librustdoc/html/static/themes/ayu.css | 4 +- src/librustdoc/html/static/themes/dark.css | 4 +- src/librustdoc/html/static/themes/light.css | 4 +- 6 files changed, 47 insertions(+), 65 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 1cbfbf50dd745..47d2707e40dfc 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -11,6 +11,7 @@ use std::fmt::{Display, Write}; use std::iter::Peekable; use rustc_lexer::{LiteralKind, TokenKind}; +use rustc_span::edition::Edition; use rustc_span::symbol::Ident; use rustc_span::with_default_session_globals; @@ -19,16 +20,20 @@ crate fn render_with_highlighting( src: String, class: Option<&str>, playground_button: Option<&str>, - tooltip: Option<(&str, &str)>, + tooltip: Option<(Option, &str)>, ) -> String { debug!("highlighting: ================\n{}\n==============", src); let mut out = String::with_capacity(src.len()); - if let Some((tooltip, class)) = tooltip { + if let Some((edition_info, class)) = tooltip { write!( out, - "
{}
", - class, tooltip + "
", + class, + if let Some(edition_info) = edition_info { + format!(" edition=\"{}\"", edition_info) + } else { + String::new() + }, ) .unwrap(); } diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 8ce686c65502f..6229a479b4d09 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -286,60 +286,27 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'_, 'a, I> { }); let tooltip = if ignore != Ignore::None { - Some(("This example is not tested".to_owned(), "ignore")) + Some((None, "ignore")) } else if compile_fail { - Some(("This example deliberately fails to compile".to_owned(), "compile_fail")) + Some((None, "compile_fail")) } else if should_panic { - Some(("This example panics".to_owned(), "should_panic")) + Some((None, "should_panic")) } else if explicit_edition { - Some((format!("This code runs with edition {}", edition), "edition")) + Some((Some(edition), "edition")) } else { None }; - if let Some((s1, s2)) = tooltip { - s.push_str(&highlight::render_with_highlighting( - text, - Some(&format!( - "rust-example-rendered{}", - if ignore != Ignore::None { - " ignore" - } else if compile_fail { - " compile_fail" - } else if should_panic { - " should_panic" - } else if explicit_edition { - " edition " - } else { - "" - } - )), - playground_button.as_deref(), - Some((s1.as_str(), s2)), - )); - Some(Event::Html(s.into())) - } else { - s.push_str(&highlight::render_with_highlighting( - text, - Some(&format!( - "rust-example-rendered{}", - if ignore != Ignore::None { - " ignore" - } else if compile_fail { - " compile_fail" - } else if should_panic { - " should_panic" - } else if explicit_edition { - " edition " - } else { - "" - } - )), - playground_button.as_deref(), - None, - )); - Some(Event::Html(s.into())) - } + s.push_str(&highlight::render_with_highlighting( + text, + Some(&format!( + "rust-example-rendered{}", + if let Some((_, class)) = tooltip { format!(" {}", class) } else { String::new() } + )), + playground_button.as_deref(), + tooltip, + )); + Some(Event::Html(s.into())) } } diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 8eef65a231d0b..afc4308b68f93 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -1079,20 +1079,29 @@ h3 > .collapse-toggle, h4 > .collapse-toggle { cursor: pointer; } -.tooltip .tooltiptext { - width: 120px; +.tooltip::after { display: none; text-align: center; padding: 5px 3px 3px 3px; border-radius: 6px; margin-left: 5px; - top: -5px; - left: 105%; - z-index: 10; font-size: 16px; } -.tooltip .tooltiptext::after { +.tooltip.ignore::after { + content: "This example is not tested"; +} +.tooltip.compile_fail::after { + content: "This example deliberately fails to compile"; +} +.tooltip.should_panic::after { + content: "This example panics"; +} +.tooltip.edition::after { + content: "This code runs with edition " attr(edition); +} + +.tooltip::before { content: " "; position: absolute; top: 50%; @@ -1100,9 +1109,10 @@ h3 > .collapse-toggle, h4 > .collapse-toggle { margin-top: -5px; border-width: 5px; border-style: solid; + display: none; } -.tooltip:hover .tooltiptext { +.tooltip:hover::before, .tooltip:hover::after { display: inline; } diff --git a/src/librustdoc/html/static/themes/ayu.css b/src/librustdoc/html/static/themes/ayu.css index c1f796f09e804..9c71fbb7f03a4 100644 --- a/src/librustdoc/html/static/themes/ayu.css +++ b/src/librustdoc/html/static/themes/ayu.css @@ -388,13 +388,13 @@ pre.ignore:hover, .information:hover + pre.ignore { color: #39AFD7; } -.tooltip .tooltiptext { +.tooltip::after { background-color: #314559; color: #c5c5c5; border: 1px solid #5c6773; } -.tooltip .tooltiptext::after { +.tooltip::before { border-color: transparent #314559 transparent transparent; } diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css index 946ca0a40c9dc..eb25272ea3bbc 100644 --- a/src/librustdoc/html/static/themes/dark.css +++ b/src/librustdoc/html/static/themes/dark.css @@ -337,13 +337,13 @@ pre.ignore:hover, .information:hover + pre.ignore { color: #0089ff; } -.tooltip .tooltiptext { +.tooltip::after { background-color: #000; color: #fff; border-color: #000; } -.tooltip .tooltiptext::after { +.tooltip::before { border-color: transparent black transparent transparent; } diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css index e0b9a04921a80..20e3c0a40929a 100644 --- a/src/librustdoc/html/static/themes/light.css +++ b/src/librustdoc/html/static/themes/light.css @@ -329,12 +329,12 @@ pre.ignore:hover, .information:hover + pre.ignore { color: #0089ff; } -.tooltip .tooltiptext { +.tooltip::after { background-color: #000; color: #fff; } -.tooltip .tooltiptext::after { +.tooltip::before { border-color: transparent black transparent transparent; } From 9b6293692801f703e694c4c7f61b165b939cea8d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 5 Dec 2020 17:31:39 +0100 Subject: [PATCH 2/3] Update tests --- src/test/rustdoc/codeblock-title.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/test/rustdoc/codeblock-title.rs b/src/test/rustdoc/codeblock-title.rs index b59b21111b009..1327fd67d316b 100644 --- a/src/test/rustdoc/codeblock-title.rs +++ b/src/test/rustdoc/codeblock-title.rs @@ -1,10 +1,9 @@ #![crate_name = "foo"] -// ignore-tidy-linelength - -// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]/span' "This example deliberately fails to compile" -// @has foo/fn.bar.html '//*[@class="tooltip ignore"]/span' "This example is not tested" -// @has foo/fn.bar.html '//*[@class="tooltip should_panic"]/span' "This example panics" +// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]' "ⓘ" +// @has foo/fn.bar.html '//*[@class="tooltip ignore"]' "ⓘ" +// @has foo/fn.bar.html '//*[@class="tooltip should_panic"]' "ⓘ" +// @has foo/fn.bar.html '//*[@edition="2018"]' "ⓘ" /// foo /// @@ -20,7 +19,7 @@ /// hoo(); /// ``` /// -/// ``` +/// ```edition2018 /// let x = 0; /// ``` pub fn bar() -> usize { 2 } From 152d4e74bec46c08e388b47f3c5693929caf377f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 23 Dec 2020 20:27:12 +0100 Subject: [PATCH 3/3] Update HTML DOM attribute "edition" to "data-edition" --- src/librustdoc/html/highlight.rs | 2 +- src/librustdoc/html/static/rustdoc.css | 2 +- src/test/rustdoc/codeblock-title.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 47d2707e40dfc..077481c89895e 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -30,7 +30,7 @@ crate fn render_with_highlighting( "
", class, if let Some(edition_info) = edition_info { - format!(" edition=\"{}\"", edition_info) + format!(" data-edition=\"{}\"", edition_info) } else { String::new() }, diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index afc4308b68f93..0b40480f738da 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -1098,7 +1098,7 @@ h3 > .collapse-toggle, h4 > .collapse-toggle { content: "This example panics"; } .tooltip.edition::after { - content: "This code runs with edition " attr(edition); + content: "This code runs with edition " attr(data-edition); } .tooltip::before { diff --git a/src/test/rustdoc/codeblock-title.rs b/src/test/rustdoc/codeblock-title.rs index 1327fd67d316b..140c5b3a67203 100644 --- a/src/test/rustdoc/codeblock-title.rs +++ b/src/test/rustdoc/codeblock-title.rs @@ -3,7 +3,7 @@ // @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]' "ⓘ" // @has foo/fn.bar.html '//*[@class="tooltip ignore"]' "ⓘ" // @has foo/fn.bar.html '//*[@class="tooltip should_panic"]' "ⓘ" -// @has foo/fn.bar.html '//*[@edition="2018"]' "ⓘ" +// @has foo/fn.bar.html '//*[@data-edition="2018"]' "ⓘ" /// foo ///