Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose_paste: Keep bare auto-link URLs when pasting/quoting.
Apoorva Pendsecfd55070cbd9
diff --git a/web/src/compose_paste.ts b/web/src/compose_paste.tsindex 291d5a9d9a2..4c99ad360c8 100644--- a/web/src/compose_paste.ts+++ b/web/src/compose_paste.ts@@ -244,6 +244,38 @@ function get_code_block_language(export const MENTION_SELECTOR = ".user-mention, .user-group-mention, .topic-mention";+function is_absolute_http_url(url: string): boolean {+ try {+ const parsed = new URL(url);+ return parsed.protocol === "http:" || parsed.protocol === "https:";+ } catch {+ return false;+ }+}++// The visible text is a raw URL when it equals the serialized href.+// It is also considered as the raw URL when it equals+// the href attribute and is an absolute http or https URL.+// Serialization adds a trailing slash to an empty path, so the+// attribute can match the visible text when the serialized href+// does not. A relative reference can equal the attribute without+// being the resolved URL, which is something we want to avoid classifying+// as raw URL link text (see comment below for example).+function is_raw_url_link_text(content: string, node: HTMLAnchorElement): boolean {+ if (content === node.href) {+ return true;+ }+ const href_attr = node.getAttribute("href") ?? "";++ // For something like `<a href="#usage">#usage</a>`,+ // `node.href` will be the page URL plus `#usage`, like+ // `http://zulip.zulipdev.com/#usage`. Emitting `#usage` raw,+ // just because it is equal to the href would drop that URL.+ // so we use this check to make it stay+ // `[#usage](http://zulip.zulipdev.com/#usage)`.+ return content === href_attr && is_absolute_http_url(content);+}+export function paste_handler_converter(paste_html: string,$textarea?: JQuery<HTMLTextAreaElement>,@@ -389,8 +421,8 @@ export function paste_handler_converter(filter: ["a"],replacement(content, node) {assert(node instanceof HTMLAnchorElement);- if (node.href === content) {- // Checks for raw links without custom text.+ // Checks for raw links without custom text.+ if (is_raw_url_link_text(content, node)) {return content;}if (node.childNodes.length === 1 && node.firstChild!.nodeName === "IMG") {diff --git a/web/tests/compose_paste.test.cjs b/web/tests/compose_paste.test.cjsindex 898a7a82e17..286536a8664 100644--- a/web/tests/compose_paste.test.cjs+++ b/web/tests/compose_paste.test.cjs@@ -447,6 +447,58 @@ run_test("paste_handler_converter", () => {"https://zulip.readthedocs.io/en/latest/subsystems/logging.html",);+ // Raw link cases where the href attribute matches the text.+ // Serialized node.href in this case is https://google.com/ which+ // would not match with the text content.+ input = '<a href="https://google.com">https://google.com</a>';+ assert.equal(compose_paste.paste_handler_converter(input), "https://google.com");++ input = '<p>Check this link <a href="https://google.com">https://google.com</a> please</p>';+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "Check this link https://google.com please",+ );++ // Case when text equals serialized node.href.+ input = '<a href="https://google.com">https://google.com/</a>';+ assert.equal(compose_paste.paste_handler_converter(input), "https://google.com/");++ // Text that differs from href by a real path (a trailing slash in this case)+ // stays labeled.+ // node.href only adds a trailing slash for URLs that had an empty path pre-serialization.+ input = '<a href="https://api.github.com/zen">https://api.github.com/zen/</a>';+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "[https://api.github.com/zen/](https://api.github.com/zen)",+ );++ // Case where we have a relative href, and text content contains an absolute URL.+ // In the web app, node.href resolves against the page, which for this+ // case would translate to http://zulip.zulipdev.com/#narrow/channel/999-hello.+ // Node tests parse this HTML with DOMParser into an about:blank+ // document, so without <base> the getter returns about:blank#narrow/...+ // and the link is wrongly treated as labeled.+ input =+ '<base href="http://zulip.zulipdev.com/"><a href="#narrow/channel/999-hello">http://zulip.zulipdev.com/#narrow/channel/999-hello</a>';+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "http://zulip.zulipdev.com/#narrow/channel/999-hello",+ );++ // Relative href text that repeats the reference stays a labeled+ // link to the resolved URL.+ input = '<base href="http://zulip.zulipdev.com/"><a href="#usage">#usage</a>';+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "[#usage](http://zulip.zulipdev.com/#usage)",+ );+ input =+ '<base href="http://zulip.zulipdev.com/"><p>See <a href="#usage">#usage</a> for details.</p>';+ assert.equal(+ compose_paste.paste_handler_converter(input),+ "See [#usage](http://zulip.zulipdev.com/#usage) for details.",+ );+// 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>';