Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose_paste: Remove title formatting more carefully.
apoorvapendseef863258c217
diff --git a/web/src/compose_paste.ts b/web/src/compose_paste.tsindex 1afaeb4844b..38ea02c5d7a 100644--- a/web/src/compose_paste.ts+++ b/web/src/compose_paste.ts@@ -40,6 +40,34 @@ function image_to_zulip_markdown(return src ? "[" + title + "](" + src + ")" : (node.getAttribute("alt") ?? "");}+// Returns 2 or more if there are multiple valid text nodes in the tree.+function check_multiple_text_nodes(child_nodes: ChildNode[]): number {+ let textful_nodes = 0;++ for (const child of child_nodes) {+ // We do not consider empty childNodes, comments and+ // childNodes containing newlines.+ if (child.nodeValue?.trim() === "" || child.nodeName === "#comment") {+ continue;+ }++ if (child.nodeValue && child.nodeType === Node.TEXT_NODE) {+ textful_nodes += 1;+ if (textful_nodes >= 2) {+ return textful_nodes;+ }+ }++ textful_nodes += check_multiple_text_nodes([...child.childNodes]);++ if (textful_nodes >= 2) {+ return textful_nodes;+ }+ }++ return textful_nodes;+}+function within_single_element(html_fragment: HTMLElement): boolean {return (html_fragment.childNodes.length === 1 &&@@ -48,6 +76,39 @@ function within_single_element(html_fragment: HTMLElement): boolean {);}+// We count textful child nodes to decide if styles should+// be removed (e.g., for headings that contain <br> and other tags in the fragment).+// Empty nodes like comments or newline-only text should not be counted.+function has_single_textful_child_node(html_fragment: HTMLElement): boolean {+ let textful_nodes = 0;+ textful_nodes = check_multiple_text_nodes([...html_fragment.childNodes]);+ if (textful_nodes >= 2) {+ return false;+ }+ return (+ html_fragment.firstElementChild !== null && html_fragment.firstElementChild.innerHTML !== ""+ );+}++function get_the_only_textful_child_content(child_nodes: ChildNode[]): string | null {+ for (const child of child_nodes) {+ if (child.nodeValue?.trim() === "" || child.nodeName === "#comment") {+ continue;+ }++ if (child.nodeValue && child.nodeType === Node.TEXT_NODE) {+ // We return directly, as it is already verified that exactly one+ // such text node exists.+ return child.nodeValue;+ }+ const child_text_node = get_the_only_textful_child_content([...child.childNodes]);+ if (child_text_node) {+ return child_text_node;+ }+ }+ return null;+}+export function is_white_space_pre(paste_html: string): boolean {const html_fragment = new DOMParser().parseFromString(paste_html, "text/html")@@ -119,16 +180,25 @@ export function paste_handler_converter(.querySelector("body");assert(copied_html_fragment !== null);- const copied_within_single_element = within_single_element(copied_html_fragment);+ const has_single_child_with_valid_text = has_single_textful_child_node(copied_html_fragment);+const outer_elements_to_retain = ["PRE", "UL", "OL", "A", "CODE"];// If the entire selection copied is within a single HTML element (like an// `h1`), we don't want to retain its styling, except when it is needed to// identify the intended structure of the copied content.if (- copied_within_single_element &&+ has_single_child_with_valid_text &&copied_html_fragment.firstElementChild !== null &&!outer_elements_to_retain.includes(copied_html_fragment.firstElementChild.nodeName)) {+ // This will always return some text as it is already ensured+ // that such a text node exists in `has_single_textful_child_node`.+ const text_content = get_the_only_textful_child_content([...copied_html_fragment.children]);+ if (text_content) {+ return text_content;+ }+ // Ideally, this should never happen.+ // Just for fallback in case it does.paste_html = copied_html_fragment.firstElementChild.innerHTML;}diff --git a/web/tests/compose_paste.test.cjs b/web/tests/compose_paste.test.cjsindex b7763c33d7e..c51395f95d8 100644--- a/web/tests/compose_paste.test.cjs+++ b/web/tests/compose_paste.test.cjs@@ -114,7 +114,11 @@ run_test("paste_handler_converter", () => {/*Pasting from another Zulip message*/-+ global.document = window.document;+ global.window = window;+ global.Node = window.Node;+ global.HTMLElement = window.HTMLElement;+ global.HTMLAnchorElement = window.HTMLAnchorElement;// Bold textlet input ='<meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="color: hsl(0, 0%, 13%); font-family: arial, sans-serif; font-size: 12.8px; 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%, 100%); text-decoration-style: initial; text-decoration-color: initial;"><span> </span>love the<span> </span><b>Zulip</b><b> </b></span><b style="color: hsl(0, 0%, 13%); font-family: arial, sans-serif; font-size: 12.8px; 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; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">Organization</b><span style="color: hsl(0, 0%, 13%); font-family: arial, sans-serif; font-size: 12.8px; 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%, 100%); text-decoration-style: initial; text-decoration-color: initial;">.</span>';@@ -129,9 +133,6 @@ run_test("paste_handler_converter", () => {assert.equal(compose_paste.paste_handler_converter(input), "The `JSDOM` constructor");// A python code block- global.document = window.document;- global.window = window;- global.Node = window.Node;input = `<meta http-equiv="content-type" content="text/html; charset=utf-8"><p>zulip code block in python</p><div class="codehilite zulip-code-block" data-code-language="Python"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>\n<span class="nb">print</span><span class="p">(</span><span class="s2">"world"</span><span class="p">)</span></code></pre></div></meta>`;assert.equal(compose_paste.paste_handler_converter(input),@@ -223,6 +224,40 @@ run_test("paste_handler_converter", () => {"* bulleted\n* nested\n * nested level 1\n * nested level 1 continue\n * nested level 2\n * nested level 2 continue",);+ // Heading from https://arxiv.org/abs/1301.3191+ input =+ '<html><body><!--StartFragment--><h1 class="title mathjax" style="font-size: 1.8em !important; font-weight: 700; line-height: 27.9936px; display: block; margin-block: 12px; margin: 0.25em 0px 12px 20px; margin-inline: 20px 0px; color: rgb(0, 0, 0); font-family: "Lucida Grande", Helvetica, Arial, sans-serif; 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; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Enriched categories as a free cocompletion</h1><br class="Apple-interchange-newline"><!--EndFragment--></body></html>';+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "Enriched categories as a free cocompletion",+ );++ // Heading from https://www.sciencedirect.com/science/article/pii/S0001870815004715+ input =+ '<html><body><!--StartFragment--><h2 class="section-title u-h4 u-margin-l-top u-margin-xs-bottom" style="box-sizing: border-box; margin-top: 32px !important; margin-right: 0px; margin-bottom: 8px !important; margin-left: 0px; padding: 0px; font-weight: normal !important; font-size: 20px !important; line-height: var(--sd-ui-line-height) !important; color: rgb(31, 31, 31); font-family: ElsevierGulliver, Georgia, "Times New Roman", Times, STIXGeneral, "Cambria Math", "Lucida Sans Unicode", "Microsoft Sans Serif", "Segoe UI Symbol", "Arial Unicode MS", serif, sans-serif; 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; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Abstract</h2><div id="as0010" style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(31, 31, 31); font-family: ElsevierGulliver, Georgia, "Times New Roman", Times, STIXGeneral, "Cambria Math", "Lucida Sans Unicode", "Microsoft Sans Serif", "Segoe UI Symbol", "Arial Unicode MS", serif, 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; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><br class="Apple-interchange-newline"><!--EndFragment--></body></html>';+ assert.equal(compose_paste.paste_handler_converter(input), "Abstract");++ // Heading from https://en.wikipedia.org/wiki/James_Madison+ input =+ '<html><body><!--StartFragment--><h1 id="firstHeading" class="firstHeading mw-first-heading" style="color: var(--color-emphasized,#101418); font-weight: normal; margin: 0px; padding: 0px; display: flow-root; word-break: break-word; border: 0px; font-size: 1.8em; font-family: "Linux Libertine", Georgia, Times, "Source Serif Pro", serif; line-height: 1.375; overflow-wrap: break-word; flex-grow: 1; 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; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span class="mw-page-title-main">James Madison</span></h1><div id="p-lang-btn" class="vector-dropdown mw-portlet mw-portlet-lang" style="position: relative; float: right; box-sizing: border-box; flex-shrink: 0; margin-right: -12px; color: rgb(32, 33, 34); font-family: 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; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"></div><br class="Apple-interchange-newline"><!--EndFragment--></body></html>';+ assert.equal(compose_paste.paste_handler_converter(input), "James Madison");++ // Heading from https://customer-identity-access-management.hashnode.dev/from-words-to-vectors-understanding-the-magic-of-text-embedding+ input = `<html><body><!--StartFragment--><div class="mt-6 break-words px-4 text-center font-heading text-3xl font-bold text-slate-900 dark:text-white md:mt-10 md:px-5 md:text-4xl lg:px-8 xl:px-20 xl:text-5xl mb-8 md:mb-14" style="box-sizing: border-box; ...">+ <h1 class="leading-tight" data-query="post-title">From Words to Vectors: Understanding the Magic of Text Embedding</h1>+ </div></body></html>`;+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "From Words to Vectors: Understanding the Magic of Text Embedding",+ );++ // Check we don't double-convert HTML to text.+ input = `<del>turtles are cool</del>`;+ assert.equal(compose_paste.paste_handler_converter(input), "turtles are cool");++ input = `<span style="color: rgb(31, 35, 40); font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; font-size: 11.9px; 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; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: break-spaces; background-color: rgba(129, 139, 152, 0.12); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"><del>turtles are cool</del></span>`;+ assert.equal(compose_paste.paste_handler_converter(input), "<del>turtles are cool</del>");+// 2 paragraphs with line break/s in betweeninput ='<meta http-equiv="content-type" content="text/html; charset=utf-8"><p>paragraph 1</p><br><p>paragraph 2</p>';