Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Remove formatting enclosing entire selection when pasting.
N-Shar-ma3a8aac8c67ba
diff --git a/web/src/copy_and_paste.js b/web/src/copy_and_paste.jsindex 12a51f76ea5..4a37f6de7e3 100644--- a/web/src/copy_and_paste.js+++ b/web/src/copy_and_paste.js@@ -312,6 +312,19 @@ function image_to_zulip_markdown(_content, node) {}export function paste_handler_converter(paste_html) {+ const copied_html_fragment = new DOMParser()+ .parseFromString(paste_html, "text/html")+ .querySelector("body");+ const copied_within_single_element =+ copied_html_fragment.childNodes.length === 1 &&+ copied_html_fragment.firstElementChild &&+ copied_html_fragment.firstElementChild.innerHTML;+ // If the entire selection copied is within a single HTML element (like an+ // `h1`), we don't want to retain its styling. We retain `pre` for code blocks.+ if (copied_within_single_element && copied_html_fragment.firstElementChild.nodeName !== "PRE") {+ paste_html = copied_html_fragment.firstChild.innerHTML;+ }+// turning off escaping (for now) to remove extra `/`TurndownService.prototype.escape = (string) => string;diff --git a/web/tests/copy_and_paste.test.js b/web/tests/copy_and_paste.test.jsindex 18fe85ac0ef..5d229454607 100644--- a/web/tests/copy_and_paste.test.js+++ b/web/tests/copy_and_paste.test.js@@ -45,10 +45,10 @@ run_test("paste_handler_converter", () => {// Links with custom textinput =- '<meta http-equiv="content-type" content="text/html; charset=utf-8"><a class="reference external" href="https://zulip.readthedocs.io/en/latest/contributing/contributing.html" style="box-sizing: border-box; color: hsl(283, 39%, 53%); text-decoration: none; cursor: pointer; outline: 0px; font-family: Lato, proxima-nova, "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 99%);">Contributing guide</a>';+ '<meta http-equiv="content-type" content="text/html; charset=utf-8">normal text <a class="reference external" href="https://zulip.readthedocs.io/en/latest/contributing/contributing.html" style="box-sizing: border-box; color: hsl(283, 39%, 53%); text-decoration: none; cursor: pointer; outline: 0px; font-family: Lato, proxima-nova, "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 99%);">Contributing guide</a>';assert.equal(copy_and_paste.paste_handler_converter(input),- "[Contributing guide](https://zulip.readthedocs.io/en/latest/contributing/contributing.html)",+ "normal text [Contributing guide](https://zulip.readthedocs.io/en/latest/contributing/contributing.html)",);// Numbered list item@@ -58,18 +58,24 @@ run_test("paste_handler_converter", () => {// Headinginput =- '<meta http-equiv="content-type" content="text/html; charset=utf-8"><h1 style="box-sizing: border-box; font-size: 2em; margin-top: 0px !important; margin-right: 0px; margin-bottom: 16px; margin-left: 0px; font-weight: 600; line-height: 1.25; padding-bottom: 0.3em; border-bottom: 1px solid hsl(216, 14%, 93%); color: hsl(210, 12%, 16%); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Zulip overview</h1>';- assert.equal(copy_and_paste.paste_handler_converter(input), "# Zulip overview");+ '<meta http-equiv="content-type" content="text/html; charset=utf-8"><h1 style="box-sizing: border-box; font-size: 2em; margin-top: 0px !important; margin-right: 0px; margin-bottom: 16px; margin-left: 0px; font-weight: 600; line-height: 1.25; padding-bottom: 0.3em; border-bottom: 1px solid hsl(216, 14%, 93%); color: hsl(210, 12%, 16%); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Zulip overview</h1><p>normal text</p>';+ assert.equal(copy_and_paste.paste_handler_converter(input), "# Zulip overview\n\nnormal text");// Italic textinput =- '<meta http-equiv="content-type" content="text/html; charset=utf-8"><i style="box-sizing: inherit; color: hsl(0, 0%, 0%); font-family: Verdana, sans-serif; font-size: 15px; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">This text is italic</i>';- assert.equal(copy_and_paste.paste_handler_converter(input), "*This text is italic*");+ '<meta http-equiv="content-type" content="text/html; charset=utf-8">normal text <i style="box-sizing: inherit; color: hsl(0, 0%, 0%); font-family: Verdana, sans-serif; font-size: 15px; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">This text is italic</i>';+ assert.equal(+ copy_and_paste.paste_handler_converter(input),+ "normal text *This text is italic*",+ );// Strikethrough textinput =- '<meta http-equiv="content-type" content="text/html; charset=utf-8"><del style="box-sizing: inherit; color: hsl(0, 0%, 0%); font-family: Verdana, sans-serif; font-size: 15px; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">This text is struck through</del>';- assert.equal(copy_and_paste.paste_handler_converter(input), "~~This text is struck through~~");+ '<meta http-equiv="content-type" content="text/html; charset=utf-8">normal text <del style="box-sizing: inherit; color: hsl(0, 0%, 0%); font-family: Verdana, sans-serif; font-size: 15px; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">This text is struck through</del>';+ assert.equal(+ copy_and_paste.paste_handler_converter(input),+ "normal text ~~This text is struck through~~",+ );// Emojisinput =