copy_and_paste: Extract function `within_single_element` for reuse.

This is a prep commit for the next which will introduce a new function
that also calls `within_single_element`.
This commit is contained in:
N-Shar-ma 2024-02-29 21:04:30 +05:30 committed by Tim Abbott
parent ad9f7a4549
commit c440b4575e
1 changed files with 9 additions and 4 deletions

View File

@ -314,14 +314,19 @@ function image_to_zulip_markdown(_content, node) {
return src ? "[" + title + "](" + src + ")" : node.getAttribute("alt") || "";
}
function within_single_element(html_fragment) {
return (
html_fragment.childNodes.length === 1 &&
html_fragment.firstElementChild &&
html_fragment.firstElementChild.innerHTML
);
}
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;
const copied_within_single_element = within_single_element(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