Skip to content
Merged
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
35 changes: 21 additions & 14 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2123,20 +2123,27 @@ function preLoadCss(cssUrl) {
return;
}
but.onclick = () => {
// Most page titles are '<Item> in <path::to::module> - Rust', except
// modules (which don't have the first part) and keywords/primitives
// (which don't have a module path)
const titleElement = document.querySelector("title");
const title = titleElement && titleElement.textContent ?
titleElement.textContent.replace(" - Rust", "") : "";
const [item, module] = title.split(" in ");
const path = [item];
if (module !== undefined) {
path.unshift(module);
}

copyContentToClipboard(path.join("::"));
copyButtonAnimation(but);
// We get the path from the "breadcrumbs" and the actual item name.
let path = "";
// @ts-expect-error
const heading = document.getElementById(MAIN_ID).querySelector(".main-heading");

if (heading) {
const breadcrumbs = heading.querySelector(".rustdoc-breadcrumbs");
if (breadcrumbs) {
// @ts-expect-error
path = breadcrumbs.innerText;
if (path.length > 0) {
path += "::";
}
}

// @ts-expect-error
path += heading.querySelector("h1 > span").innerText;

copyContentToClipboard(path);
copyButtonAnimation(but);
}
};

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/rustdoc-gui/copy-path.goml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ assert-size: ("#copy-path.clicked", {"width": |width|, "height": |height|})
wait-for: "#copy-path:not(.clicked)"
// We check that the size is still the same.
assert-size: ("#copy-path:not(.clicked)", {"width": |width|, "height": |height|})

// Check the path for a module.
go-to: "file://" + |DOC_PATH| + "/test_docs/foreign_impl_order/index.html"
click: "#copy-path"
// We wait for the new text to appear.
wait-for: "#copy-path.clicked"
// We check that the clipboard value is the expected one.
assert-clipboard: "test_docs::foreign_impl_order"

// Check the path for the crate.
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
click: "#copy-path"
// We wait for the new text to appear.
wait-for: "#copy-path.clicked"
// We check that the clipboard value is the expected one.
assert-clipboard: "test_docs"
Loading