From 7af63051996a25d76806b550750d60dc4252e77d Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 1 Feb 2024 18:48:50 +0100 Subject: [PATCH 1/2] Strip trailing newline in markdown code copy Fixes: https://github.com/go-gitea/gitea/issues/29012 --- web_src/js/markup/codecopy.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web_src/js/markup/codecopy.js b/web_src/js/markup/codecopy.js index a12802ef73479..79f038ae5096b 100644 --- a/web_src/js/markup/codecopy.js +++ b/web_src/js/markup/codecopy.js @@ -13,7 +13,8 @@ export function renderCodeCopy() { for (const el of els) { const btn = makeCodeCopyButton(); - btn.setAttribute('data-clipboard-text', el.textContent); + // remove final trailing newline introduced during HTML rendering + btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, '')); el.after(btn); } } From d01977bb3e8f33fdaf50f5d5a7ebb89fb0e5a46b Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 1 Feb 2024 18:51:06 +0100 Subject: [PATCH 2/2] safeguard against null textContent --- web_src/js/markup/codecopy.js | 1 + 1 file changed, 1 insertion(+) diff --git a/web_src/js/markup/codecopy.js b/web_src/js/markup/codecopy.js index 79f038ae5096b..078d741253860 100644 --- a/web_src/js/markup/codecopy.js +++ b/web_src/js/markup/codecopy.js @@ -12,6 +12,7 @@ export function renderCodeCopy() { if (!els.length) return; for (const el of els) { + if (!el.textContent) continue; const btn = makeCodeCopyButton(); // remove final trailing newline introduced during HTML rendering btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));