Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

compose: Completely remove style elements when pasting HTML as markdown.

N-Shar-mac3c72cda6dc6
diff --git a/web/src/copy_and_paste.js b/web/src/copy_and_paste.js
index 40a06a8fc67..70ff9e60715 100644
--- a/web/src/copy_and_paste.js
+++ b/web/src/copy_and_paste.js
@@ -338,6 +338,12 @@ export function paste_handler_converter(paste_html) {
codeBlockStyle: "fenced",
headingStyle: "atx",
});
+ turndownService.addRule("style", {
+ filter: "style",
+ replacement() {
+ return "";
+ },
+ });
turndownService.addRule("strikethrough", {
filter: ["del", "s", "strike"],
replacement(content) {
diff --git a/web/tests/copy_and_paste.test.js b/web/tests/copy_and_paste.test.js
index 975c7631cd8..1726522d5ad 100644
--- a/web/tests/copy_and_paste.test.js
+++ b/web/tests/copy_and_paste.test.js
@@ -115,4 +115,9 @@ run_test("paste_handler_converter", () => {
copy_and_paste.paste_handler_converter(input),
'const compose_ui = mock_esm("../src/compose_ui");\n\nset_global("document", document);',
);
+
+ // Pasting from Google Sheets (remove <style> elements completely)
+ 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="{&quot;1&quot;:3,&quot;3&quot;:123}" data-sheets-userformat="{&quot;2&quot;:769,&quot;3&quot;:{&quot;1&quot;:0},&quot;11&quot;:3,&quot;12&quot;:0}">123</span>';
+ assert.equal(copy_and_paste.paste_handler_converter(input), "123");
});