2021-08-04 00:32:30 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
import { getSelection } from "./cross-browser";
|
|
|
|
|
2021-08-04 00:32:30 +02:00
|
|
|
export function nodeIsElement(node: Node): node is Element {
|
|
|
|
return node.nodeType === Node.ELEMENT_NODE;
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
/**
|
|
|
|
* In the web this is probably equivalent to `nodeIsElement`, but this is
|
|
|
|
* convenient to convince Typescript.
|
|
|
|
*/
|
|
|
|
export function nodeIsCommonElement(node: Node): node is HTMLElement | SVGElement {
|
|
|
|
return node instanceof HTMLElement || node instanceof SVGElement;
|
|
|
|
}
|
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
export function nodeIsText(node: Node): node is Text {
|
|
|
|
return node.nodeType === Node.TEXT_NODE;
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
export function nodeIsComment(node: Node): node is Comment {
|
|
|
|
return node.nodeType === Node.COMMENT_NODE;
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:32:30 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
|
2022-01-08 02:46:01 +01:00
|
|
|
export const BLOCK_ELEMENTS = [
|
2021-08-04 00:32:30 +02:00
|
|
|
"ADDRESS",
|
|
|
|
"ARTICLE",
|
|
|
|
"ASIDE",
|
|
|
|
"BLOCKQUOTE",
|
|
|
|
"DETAILS",
|
|
|
|
"DIALOG",
|
|
|
|
"DD",
|
|
|
|
"DIV",
|
|
|
|
"DL",
|
|
|
|
"DT",
|
|
|
|
"FIELDSET",
|
|
|
|
"FIGCAPTION",
|
|
|
|
"FIGURE",
|
|
|
|
"FOOTER",
|
|
|
|
"FORM",
|
|
|
|
"H1",
|
|
|
|
"H2",
|
|
|
|
"H3",
|
|
|
|
"H4",
|
|
|
|
"H5",
|
|
|
|
"H6",
|
|
|
|
"HEADER",
|
|
|
|
"HGROUP",
|
|
|
|
"HR",
|
|
|
|
"LI",
|
|
|
|
"MAIN",
|
|
|
|
"NAV",
|
|
|
|
"OL",
|
|
|
|
"P",
|
|
|
|
"PRE",
|
|
|
|
"SECTION",
|
|
|
|
"TABLE",
|
|
|
|
"UL",
|
|
|
|
];
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export function hasBlockAttribute(element: Element): boolean {
|
|
|
|
return element.hasAttribute("block") && element.getAttribute("block") !== "false";
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:32:30 +02:00
|
|
|
export function elementIsBlock(element: Element): boolean {
|
2022-01-08 02:46:01 +01:00
|
|
|
return BLOCK_ELEMENTS.includes(element.tagName) || hasBlockAttribute(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NO_SPLIT_TAGS = ["RUBY"];
|
|
|
|
|
|
|
|
export function elementShouldNotBeSplit(element: Element): boolean {
|
|
|
|
return elementIsBlock(element) || NO_SPLIT_TAGS.includes(element.tagName);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Glossary/Empty_element
|
2022-01-08 02:46:01 +01:00
|
|
|
export const EMPTY_ELEMENTS = [
|
2021-11-18 10:18:39 +01:00
|
|
|
"AREA",
|
|
|
|
"BASE",
|
|
|
|
"BR",
|
|
|
|
"COL",
|
|
|
|
"EMBED",
|
|
|
|
"HR",
|
|
|
|
"IMG",
|
|
|
|
"INPUT",
|
|
|
|
"LINK",
|
|
|
|
"META",
|
|
|
|
"PARAM",
|
|
|
|
"SOURCE",
|
|
|
|
"TRACK",
|
|
|
|
"WBR",
|
|
|
|
];
|
|
|
|
|
|
|
|
export function elementIsEmpty(element: Element): boolean {
|
|
|
|
return EMPTY_ELEMENTS.includes(element.tagName);
|
2021-08-04 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
export function nodeContainsInlineContent(node: Node): boolean {
|
|
|
|
for (const child of node.childNodes) {
|
|
|
|
if (
|
2022-11-28 00:33:04 +01:00
|
|
|
(nodeIsElement(child) && elementIsBlock(child))
|
|
|
|
|| !nodeContainsInlineContent(child)
|
2021-10-18 14:01:15 +02:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty node is trivially inline
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-25 05:42:54 +02:00
|
|
|
/**
|
|
|
|
* Consumes the input fragment.
|
|
|
|
*/
|
2021-10-18 14:01:15 +02:00
|
|
|
export function fragmentToString(fragment: DocumentFragment): string {
|
|
|
|
const fragmentDiv = document.createElement("div");
|
|
|
|
fragmentDiv.appendChild(fragment);
|
|
|
|
const html = fragmentDiv.innerHTML;
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:32:30 +02:00
|
|
|
const getAnchorParent =
|
2022-11-28 00:33:04 +01:00
|
|
|
<T extends Element>(predicate: (element: Element) => element is T) => (root: Node): T | null => {
|
2022-01-08 02:46:01 +01:00
|
|
|
const anchor = getSelection(root)?.anchorNode;
|
2021-08-04 00:32:30 +02:00
|
|
|
|
|
|
|
if (!anchor) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let anchorParent: T | null = null;
|
|
|
|
let element = nodeIsElement(anchor) ? anchor : anchor.parentElement;
|
|
|
|
|
|
|
|
while (element) {
|
|
|
|
anchorParent = anchorParent || (predicate(element) ? element : null);
|
|
|
|
element = element.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return anchorParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isListItem = (element: Element): element is HTMLLIElement =>
|
|
|
|
window.getComputedStyle(element).display === "list-item";
|
2022-11-28 00:33:04 +01:00
|
|
|
const isParagraph = (element: Element): element is HTMLParamElement => element.tagName === "P";
|
2021-08-04 00:32:30 +02:00
|
|
|
const isBlockElement = (
|
2021-10-19 01:06:00 +02:00
|
|
|
element: Element,
|
2022-11-28 00:33:04 +01:00
|
|
|
): element is HTMLLIElement & HTMLParamElement => isListItem(element) || isParagraph(element);
|
2021-08-04 00:32:30 +02:00
|
|
|
|
|
|
|
export const getListItem = getAnchorParent(isListItem);
|
|
|
|
export const getParagraph = getAnchorParent(isParagraph);
|
|
|
|
export const getBlockElement = getAnchorParent(isBlockElement);
|