From 7f550f6f37aece6fbf5a8c31beb13d38996f56d7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 26 Dec 2025 16:23:40 +0100 Subject: [PATCH 1/2] Fix "copy code" button --- src/librustdoc/html/static/js/main.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 476ecd42d6f9..72bf816e64bd 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -2163,7 +2163,15 @@ function preLoadCss(cssUrl) { // Should never happen, but the world is a dark and dangerous place. return; } - copyContentToClipboard(codeElem.textContent); + let content = ""; + for (const node of codeElem.childNodes) { + // We exclude line numbers. + if (node instanceof HTMLElement && node.hasAttribute("data-nosnippet")) { + continue; + } + content += node.textContent; + } + copyContentToClipboard(content); } /** From c054e52bc9e092dab9a627fea0ad46d08931e881 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 26 Dec 2025 16:57:35 +0100 Subject: [PATCH 2/2] Add GUI test for copying code when line numbers are displayed in code examples --- tests/rustdoc-gui/copy-code.goml | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/rustdoc-gui/copy-code.goml b/tests/rustdoc-gui/copy-code.goml index a6fb816c4bd6..14421ab746f5 100644 --- a/tests/rustdoc-gui/copy-code.goml +++ b/tests/rustdoc-gui/copy-code.goml @@ -42,7 +42,31 @@ store-size: (".example-wrap:nth-of-type(1) .copy-button", { }) assert: |copy_height| > 0 && |copy_width| > 0 -// Checking same things for the copy button when there is no run button. + +// We now check that copying code when line numbers are displayed don't include these line numbers. + +// First we ensure that the clipboard is empty. +assert-clipboard: "" +// We make the line numbers appear. +click: "rustdoc-toolbar .settings-menu" +wait-for-css: ("#settings", {"display": "block"}) +// We make the line numbers appear. +click: "#line-numbers" +wait-for-local-storage: {"rustdoc-line-numbers": "true" } +// We close the settings menu. +click: "rustdoc-toolbar .settings-menu" +wait-for-css-false: ("#settings", {"display": "block"}) +// We ensure that there are actually line numbers generated in the DOM. +assert-text: (".example-wrap pre.rust code span[data-nosnippet]", "1") +// We make the copy button appear. +move-cursor-to: ".example-wrap pre.rust code" +wait-for-css: (".example-wrap .button-holder", {"visibility": "visible"}) +// We click on the copy button. +click: ".example-wrap .button-holder .copy-button" +assert-clipboard: 'println!("nothing fancy"); +println!("but with two lines!");' + +// Back to UI checks, checking same things for the copy button when there is no run button. go-to: "file://" + |DOC_PATH| + "/lib2/sub_mod/struct.Foo.html" call-function: ("check-copy-button", {}) // Ensure there is no run button.