Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
copy_and_paste: Paste Excel copied content as image and text.
apoorvapendse6586d9078ea4
diff --git a/web/src/copy_and_paste.ts b/web/src/copy_and_paste.tsindex 40ae732af52..51e3c4818f9 100644--- a/web/src/copy_and_paste.ts+++ b/web/src/copy_and_paste.ts@@ -385,15 +385,53 @@ export function is_white_space_pre(paste_html: string): boolean {);}-function is_single_image(paste_html: string): boolean {+function is_from_excel(html_fragment: HTMLBodyElement): boolean {+ const html_tag = html_fragment.parentElement;+ if (!html_tag || html_tag.nodeName !== "HTML") {+ return false;+ }++ const excel_namespaces = [+ "urn:schemas-microsoft-com:office:excel",+ "urn:schemas-microsoft-com:office:office",+ ];++ const has_excel_metadata = [...html_tag.querySelectorAll("meta")].some(+ (meta) =>+ (meta.name === "ProgId" && meta.content === "Excel.Sheet") ||+ (meta.name === "Generator" && meta.content?.includes("Microsoft Excel")),+ );+ if (!has_excel_metadata) {+ return false;+ }++ if (!html_tag.querySelector("[class^='xl']")) {+ return false;+ }++ const html_outer = html_tag.outerHTML;++ if (!html_outer.includes("<!--StartFragment-->")) {+ return false;+ }++ if (!excel_namespaces.some((ns) => html_outer.includes(ns))) {+ return false;+ }++ return true;+}++export function is_single_image(paste_html: string): boolean {const html_fragment = new DOMParser().parseFromString(paste_html, "text/html").querySelector("body");assert(html_fragment !== null);return (- html_fragment.childNodes.length === 1 &&- html_fragment.firstElementChild !== null &&- html_fragment.firstElementChild.nodeName === "IMG"+ is_from_excel(html_fragment) ||+ (html_fragment.childNodes.length === 1 &&+ html_fragment.firstElementChild !== null &&+ html_fragment.firstElementChild.nodeName === "IMG"));}diff --git a/web/tests/copy_and_paste.test.cjs b/web/tests/copy_and_paste.test.cjsindex 1feb8a674eb..84f790137aa 100644--- a/web/tests/copy_and_paste.test.cjs+++ b/web/tests/copy_and_paste.test.cjs@@ -209,4 +209,13 @@ run_test("paste_handler_converter", () => {input ='<meta http-equiv="content-type" content="text/html; charset=utf-8"><style type="text/css"><!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style><span style="font-size:10pt;font-family:Arial;font-style:normal;text-align:right;" data-sheets-value="{"1":3,"3":123}" data-sheets-userformat="{"2":769,"3":{"1":0},"11":3,"12":0}">123</span>';assert.equal(copy_and_paste.paste_handler_converter(input), "123");++ // Pasting from Excel+ input = `<html xmlns:v="urn:schemas-microsoft-com:vml"\nxmlns:o="urn:schemas-microsoft-com:office:office"\nxmlns:x="urn:schemas-microsoft-com:office:excel"\nxmlns="http://www.w3.org/TR/REC-html40">\n<head>\n<meta http-equiv=Content-Type content="text/html; charset=utf-8">\n<meta name=ProgId content=Excel.Sheet>\n<meta name=Generator content="Microsoft Excel 15">\n<link id=Main-File rel=Main-File\nhref="file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip.htm">\n<link rel=File-List\nhref="file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml">\n<style>\n<!--table\n {mso-displayed-decimal-separator:"\\.";\n mso-displayed-thousand-separator:"\\,";}\n@page\n {margin:.75in .7in .75in .7in;\n mso-header-margin:.3in;\n mso-footer-margin:.3in;}\ntr\n {mso-height-source:auto;}\ncol\n {mso-width-source:auto;}\nbr\n {mso-data-placement:same-cell;}\ntd\n {padding-top:1px;\n padding-right:1px;\n padding-left:1px;\n mso-ignore:padding;\n color:black;\n font-size:11.0pt;\n font-weight:400;\n font-style:normal;\n text-decoration:none;\n font-family:Calibri, sans-serif;\n mso-font-charset:0;\n mso-number-format:General;\n text-align:general;\n vertical-align:bottom;\n border:none;\n mso-background-source:auto;\n mso-pattern:auto;\n mso-protection:locked visible;\n white-space:nowrap;\n mso-rotate:0;}\n.xl65\n {mso-number-format:"_\\(\\0022$\\0022* \\#\\,\\#\\#0\\.00_\\)\\;_\\(\\0022$\\0022* \\\\\\(\\#\\,\\#\\#0\\.00\\\\\\)\\;_\\(\\0022$\\0022* \\0022-\\0022??_\\)\\;_\\(\\@_\\)";}\n-->\n</style>\n</head>\n<body link="#0563C1" vlink="#954F72">\n<table border=0 cellpadding=0 cellspacing=0 width=88 style='border-collapse:\n collapse;width:66pt'>\n<!--StartFragment-->\n <col width=88 style='mso-width-source:userset;mso-width-alt:3218;width:66pt'>\n <tr height=20 style='height:15.0pt'>\n <td height=20 class=xl65 width=88 style='height:15.0pt;width:66pt;font-size:\n 11.0pt;color:black;font-weight:400;text-decoration:none;text-underline-style:\n none;text-line-through:none;font-family:Calibri, sans-serif;border-top:.5pt solid #5B9BD5;\n border-right:none;border-bottom:none;border-left:none'><span\n style='mso-spacerun:yes'> </span>$<span style='mso-spacerun:yes'>\n </span>20.00 </td>\n </tr>\n <tr height=20 style='height:15.0pt'>\n <td height=20 class=xl65 style='height:15.0pt;font-size:11.0pt;color:black;\n font-weight:400;text-decoration:none;text-underline-style:none;text-line-through:\n none;font-family:Calibri, sans-serif;border-top:.5pt solid #5B9BD5;\n border-right:none;border-bottom:none;border-left:none'><span\n style='mso-spacerun:yes'> </span>$<span\n style='mso-spacerun:yes'> </span>7.00 </td>\n </tr>\n<!--EndFragment-->\n</table>\n</body>\n</html>`;++ // Pasting from Excel using ^V should paste an image.+ assert.ok(copy_and_paste.is_single_image(input));++ // Pasting from Excel using ^⇧V should paste formatted text.+ assert.equal(copy_and_paste.paste_handler_converter(input), " \n\n$ 20.00\n\n$ 7.00");});