From 3f7b37ba3c6f273ee3bc1333564643e32cdc88b2 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Wed, 6 Mar 2024 14:11:37 +0100 Subject: [PATCH] Embed metadata about the current page instead of reading from the rendered html --- static/index.js | 19 ++++--------------- templates/rustdoc/topbar.html | 8 ++++++++ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/static/index.js b/static/index.js index d6cbc5fa5..4a0bdc49f 100644 --- a/static/index.js +++ b/static/index.js @@ -1,8 +1,3 @@ -function formatCrateName(crateTitleAndVersion) { - const stringParts = crateTitleAndVersion.split(" ", 2); - return stringParts[0] + ' = "' + stringParts[1] + '"'; -} - (function() { const clipboard = document.getElementById("clipboard"); if (clipboard) { @@ -14,23 +9,17 @@ function formatCrateName(crateTitleAndVersion) { clipboard.innerHTML = resetClipboardIcon; } - function copyTextHandler() { - const crateTitleAndVersion = document.getElementById("crate-title"); - // On rustdoc pages, we use `textTransform: uppercase`, which copies as uppercase. - // To avoid that, reset the styles temporarily. - const oldTransform = crateTitleAndVersion.style.textTransform; - crateTitleAndVersion.style.textTransform = "none"; - const temporaryInput = document.createElement("input"); + async function copyTextHandler() { + const metadata = JSON.parse(document.getElementById("crate-metadata").innerText) + const temporaryInput = document.createElement("input"); temporaryInput.type = "text"; - temporaryInput.value = formatCrateName(crateTitleAndVersion.innerText); + temporaryInput.value = `${metadata.name} = "${metadata.version}"`; document.body.append(temporaryInput); temporaryInput.select(); document.execCommand("copy"); - temporaryInput.remove(); - crateTitleAndVersion.style.textTransform = oldTransform; clipboard.textContent = "✓"; if (resetClipboardTimeout !== null) { diff --git a/templates/rustdoc/topbar.html b/templates/rustdoc/topbar.html index 20435e10c..a5b6e1bfd 100644 --- a/templates/rustdoc/topbar.html +++ b/templates/rustdoc/topbar.html @@ -13,6 +13,14 @@ {%- include "header/topbar_begin.html" -%}{# extra whitespace unremovable, need to use html tags unaffacted by whitespace T_T #}