about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-11-20 18:21:49 +0100
committerGitHub <noreply@github.com>2022-11-20 18:21:49 +0100
commitc5f92ce2a075c0f656362513ebbaf19006eab047 (patch)
treecab658b97c22a3d21cec0af024765c28b0a10d52
parentdb5f005f35ee8abdf772d518fea09fcadd97fb43 (diff)
parenta3ae6fec8970a09f2983bbc8db90719f11d438ca (diff)
downloadrust-c5f92ce2a075c0f656362513ebbaf19006eab047.tar.gz
rust-c5f92ce2a075c0f656362513ebbaf19006eab047.zip
Rollup merge of #104611 - notriddle:notriddle/scrape-examples-button, r=GuillaumeGomez
rustdoc: use real buttons for scrape examples controls

This makes the expand and switch controls keyboard-accessible.

Preview: https://notriddle.com/notriddle-rustdoc-demos/scrape-examples-button/test_dingus/fn.test.html
-rw-r--r--src/bootstrap/test.rs2
-rw-r--r--src/librustdoc/html/render/mod.rs2
-rw-r--r--src/librustdoc/html/sources.rs7
-rw-r--r--src/librustdoc/html/static/css/rustdoc.css6
-rw-r--r--src/librustdoc/html/templates/page.html2
-rw-r--r--src/test/rustdoc-gui/scrape-examples-button-focus.goml14
-rw-r--r--src/test/rustdoc-gui/sidebar-source-code.goml2
-rw-r--r--src/test/rustdoc-gui/src/scrape_examples/Cargo.lock7
-rw-r--r--src/test/rustdoc-gui/src/scrape_examples/Cargo.toml8
-rw-r--r--src/test/rustdoc-gui/src/scrape_examples/examples/check.rs25
-rw-r--r--src/test/rustdoc-gui/src/scrape_examples/src/lib.rs7
11 files changed, 77 insertions, 5 deletions
diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs
index b22b7ad4ae0..38873cee93b 100644
--- a/src/bootstrap/test.rs
+++ b/src/bootstrap/test.rs
@@ -1011,6 +1011,8 @@ impl Step for RustdocGUI {
                 //        instead of hard-coding this test
                 if entry.file_name() == "link_to_definition" {
                     cargo.env("RUSTDOCFLAGS", "-Zunstable-options --generate-link-to-definition");
+                } else if entry.file_name() == "scrape_examples" {
+                    cargo.arg("-Zrustdoc-scrape-examples=examples");
                 }
                 builder.run(&mut cargo);
             }
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index 8731efb5e87..647eb69d9a6 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -2915,7 +2915,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
         );
 
         if line_ranges.len() > 1 {
-            write!(w, r#"<span class="prev">&pr;</span> <span class="next">&sc;</span>"#);
+            write!(w, r#"<button class="prev">&pr;</button> <button class="next">&sc;</button>"#);
         }
 
         // Look for the example file in the source map if it exists, otherwise return a dummy span
diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs
index 50135d60190..54e296959b0 100644
--- a/src/librustdoc/html/sources.rs
+++ b/src/librustdoc/html/sources.rs
@@ -287,8 +287,11 @@ pub(crate) fn print_src(
             }
         }
         SourceContext::Embedded { offset, needs_expansion } => {
-            extra =
-                if needs_expansion { Some(r#"<span class="expand">&varr;</span>"#) } else { None };
+            extra = if needs_expansion {
+                Some(r#"<button class="expand">&varr;</button>"#)
+            } else {
+                None
+            };
             for line_number in 1..=lines {
                 let line = line_number + offset;
                 writeln!(line_numbers, "<span>{line}</span>")
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css
index e1234b37f4e..10f3c0e95d7 100644
--- a/src/librustdoc/html/static/css/rustdoc.css
+++ b/src/librustdoc/html/static/css/rustdoc.css
@@ -1965,6 +1965,12 @@ in storage.js
 	top: 0.25em;
 	z-index: 1;
 	cursor: pointer;
+	padding: 0;
+	background: none;
+	border: none;
+	/* iOS button gradient: https://stackoverflow.com/q/5438567 */
+	-webkit-appearance: none;
+	opacity: 1;
 }
 .scraped-example .code-wrapper .prev {
 	right: 2.25em;
diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html
index 4efcfc510a2..df13e597f1f 100644
--- a/src/librustdoc/html/templates/page.html
+++ b/src/librustdoc/html/templates/page.html
@@ -40,7 +40,7 @@
     {%- endif -%}
     <script defer src="{{static_root_path|safe}}{{files.main_js}}"></script> {#- -#}
     {%- if layout.scrape_examples_extension -%}
-    <script defer src="{{page.root_path|safe}}{{files.scrape_examples_js}}"></script> {#- -#}
+    <script defer src="{{static_root_path|safe}}{{files.scrape_examples_js}}"></script> {#- -#}
     {%- endif -%}
     <noscript> {#- -#}
         <link rel="stylesheet" {# -#}
diff --git a/src/test/rustdoc-gui/scrape-examples-button-focus.goml b/src/test/rustdoc-gui/scrape-examples-button-focus.goml
new file mode 100644
index 00000000000..2a263a87a47
--- /dev/null
+++ b/src/test/rustdoc-gui/scrape-examples-button-focus.goml
@@ -0,0 +1,14 @@
+goto: "file://" + |DOC_PATH| + "/scrape_examples/fn.test.html"
+store-property: (smallOffsetHeight, ".scraped-example-list > .scraped-example pre", "offsetHeight")
+assert-property-false: (".scraped-example-list > .scraped-example pre", {
+	"scrollHeight": |smallOffsetHeight|
+})
+focus: ".scraped-example-list > .scraped-example .expand"
+press-key: "Enter"
+assert-property-false: (".scraped-example-list > .scraped-example pre", {
+	"offsetHeight": |smallOffsetHeight|
+})
+store-property: (fullOffsetHeight, ".scraped-example-list > .scraped-example pre", "offsetHeight")
+assert-property: (".scraped-example-list > .scraped-example pre", {
+	"scrollHeight": |fullOffsetHeight|
+})
diff --git a/src/test/rustdoc-gui/sidebar-source-code.goml b/src/test/rustdoc-gui/sidebar-source-code.goml
index e9a987d52bb..36e4d555b8e 100644
--- a/src/test/rustdoc-gui/sidebar-source-code.goml
+++ b/src/test/rustdoc-gui/sidebar-source-code.goml
@@ -28,7 +28,7 @@ assert: "//*[@class='dir-entry' and @open]/*[text()='sub_mod']"
 // Only "another_folder" should be "open" in "lib2".
 assert: "//*[@class='dir-entry' and not(@open)]/*[text()='another_mod']"
 // All other trees should be collapsed.
-assert-count: ("//*[@id='source-sidebar']/details[not(text()='lib2') and not(@open)]", 6)
+assert-count: ("//*[@id='source-sidebar']/details[not(text()='lib2') and not(@open)]", 7)
 
 // We now switch to mobile mode.
 size: (600, 600)
diff --git a/src/test/rustdoc-gui/src/scrape_examples/Cargo.lock b/src/test/rustdoc-gui/src/scrape_examples/Cargo.lock
new file mode 100644
index 00000000000..7cd6d0844e4
--- /dev/null
+++ b/src/test/rustdoc-gui/src/scrape_examples/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "scrape_examples"
+version = "0.1.0"
diff --git a/src/test/rustdoc-gui/src/scrape_examples/Cargo.toml b/src/test/rustdoc-gui/src/scrape_examples/Cargo.toml
new file mode 100644
index 00000000000..aea9b657d30
--- /dev/null
+++ b/src/test/rustdoc-gui/src/scrape_examples/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "scrape_examples"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check.rs
new file mode 100644
index 00000000000..3e69c6086ae
--- /dev/null
+++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check.rs
@@ -0,0 +1,25 @@
+fn main() {
+    for i in 0..9 {
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+    }
+    scrape_examples::test();
+    for i in 0..9 {
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+        println!("hello world!");
+    }
+}
diff --git a/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs b/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs
new file mode 100644
index 00000000000..6412de2c03a
--- /dev/null
+++ b/src/test/rustdoc-gui/src/scrape_examples/src/lib.rs
@@ -0,0 +1,7 @@
+/// # Examples
+///
+/// ```
+/// test();
+/// test();
+/// ```
+pub fn test() {}