From 01a283bb99be9dfd10ef0ff99eb54b83d39cfa02 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 31 Aug 2021 16:42:16 +0200 Subject: [PATCH] Check for non-existence of block tags instead of exclusive existence of inline tags in editable --- ts/editor/editable.ts | 15 +++---- ts/editor/helpers.ts | 95 ++++++++++++++++--------------------------- 2 files changed, 41 insertions(+), 69 deletions(-) diff --git a/ts/editor/editable.ts b/ts/editor/editable.ts index 3c657b7ef..38ee75646 100644 --- a/ts/editor/editable.ts +++ b/ts/editor/editable.ts @@ -2,18 +2,13 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import { bridgeCommand } from "./lib"; -import { nodeIsInline, caretToEnd, getBlockElement } from "./helpers"; +import { elementIsBlock, caretToEnd, getBlockElement } from "./helpers"; import { inCodable } from "./toolbar"; import { wrap } from "./wrap"; -function containsInlineContent(field: Element): boolean { - if (field.childNodes.length === 0) { - // for now, for all practical purposes, empty fields are in block mode - return false; - } - - for (const child of field.children) { - if (!nodeIsInline(child)) { +function containsInlineContent(element: Element): boolean { + for (const child of element.children) { + if (elementIsBlock(child) || !containsInlineContent(child)) { return false; } } @@ -25,7 +20,7 @@ export class Editable extends HTMLElement { set fieldHTML(content: string) { this.innerHTML = content; - if (containsInlineContent(this)) { + if (content.length > 0 && containsInlineContent(this)) { this.appendChild(document.createElement("br")); } } diff --git a/ts/editor/helpers.ts b/ts/editor/helpers.ts index 8b4342aae..e89cdeb58 100644 --- a/ts/editor/helpers.ts +++ b/ts/editor/helpers.ts @@ -9,67 +9,44 @@ export function nodeIsElement(node: Node): node is Element { return node.nodeType === Node.ELEMENT_NODE; } -const INLINE_TAGS = [ - "A", - "ABBR", - "ACRONYM", - "AUDIO", - "B", - "BDI", - "BDO", - "BIG", - "BR", - "BUTTON", - "CANVAS", - "CITE", - "CODE", - "DATA", - "DATALIST", - "DEL", - "DFN", - "EM", - "EMBED", - "FONT", - "I", - "IFRAME", - "IMG", - "INPUT", - "INS", - "KBD", - "LABEL", - "MAP", - "MARK", - "METER", - "NOSCRIPT", - "OBJECT", - "OUTPUT", - "PICTURE", - "PROGRESS", - "Q", - "RUBY", - "S", - "SAMP", - "SCRIPT", - "SELECT", - "SLOT", - "SMALL", - "SPAN", - "STRONG", - "SUB", - "SUP", - "SVG", - "TEMPLATE", - "TEXTAREA", - "TIME", - "U", - "TT", - "VAR", - "VIDEO", - "WBR", +const BLOCK_TAGS = [ + "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", ]; -export function nodeIsInline(node: Node): boolean { - return !nodeIsElement(node) || INLINE_TAGS.includes(node.tagName); +export function elementIsBlock(element: Element): boolean { + return BLOCK_TAGS.includes(element.tagName); } export function caretToEnd(node: Node): void {