FIX: whitespaces when copying code blocks (#12730)

FIX: whitespaces when copying code blocks (#12730)

This ensures the copied text always has proper white space characters.

diff --git a/app/assets/javascripts/discourse/app/initializers/copy-codeblocks.js b/app/assets/javascripts/discourse/app/initializers/copy-codeblocks.js
index 18b9448..b119258 100644
--- a/app/assets/javascripts/discourse/app/initializers/copy-codeblocks.js
+++ b/app/assets/javascripts/discourse/app/initializers/copy-codeblocks.js
@@ -7,8 +7,8 @@ import { withPluginApi } from "discourse/lib/plugin-api";
 
 // http://github.com/feross/clipboard-copy
 function clipboardCopy(text) {
-  // Use the Async Clipboard API when available. Requires a secure browsing
-  // context (i.e. HTTPS)
+  // Use the Async Clipboard API when available.
+  // Requires a secure browsing context (i.e. HTTPS)
   if (navigator.clipboard) {
     return navigator.clipboard.writeText(text).catch(function (err) {
       throw err !== undefined
@@ -88,7 +88,15 @@ export default {
         const code = button.nextSibling;
 
         if (code) {
-          clipboardCopy(code.innerText.trim()).then(() => {
+          // replace any weird whitespace characters with a proper '\u20' whitespace
+          const text = code.innerText
+            .replace(
+              /[\f\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/g,
+              " "
+            )
+            .trim();
+
+          clipboardCopy(text).then(() => {
             button.classList.add("copied");
             const state = button.innerHTML;
             button.innerHTML = I18n.t("copy_codeblock.copied");

GitHub sha: 2aed82e6

This commit appears in #12730 which was approved by jjaffeux. It was merged by ZogStriP.