Merge pull request #1353 from hgiesel/checkforblockinstead

Check for non-existence of block tags instead of exclusive existence of inline tags in editable
This commit is contained in:
Damien Elmes 2021-09-02 22:39:58 +10:00 committed by GitHub
commit e5409a25b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 74 deletions

View file

@ -6,11 +6,6 @@ anki-editable {
overflow: auto; overflow: auto;
padding: 6px; padding: 6px;
&:empty::after {
content: "\a";
white-space: pre;
}
* { * {
max-width: 100%; max-width: 100%;
} }

View file

@ -2,18 +2,13 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { bridgeCommand } from "./lib"; import { bridgeCommand } from "./lib";
import { nodeIsInline, caretToEnd, getBlockElement } from "./helpers"; import { elementIsBlock, caretToEnd, getBlockElement } from "./helpers";
import { inCodable } from "./toolbar"; import { inCodable } from "./toolbar";
import { wrap } from "./wrap"; import { wrap } from "./wrap";
function containsInlineContent(field: Element): boolean { function containsInlineContent(element: Element): boolean {
if (field.childNodes.length === 0) { for (const child of element.children) {
// for now, for all practical purposes, empty fields are in block mode if (elementIsBlock(child) || !containsInlineContent(child)) {
return false;
}
for (const child of field.children) {
if (!nodeIsInline(child)) {
return false; return false;
} }
} }
@ -25,7 +20,7 @@ export class Editable extends HTMLElement {
set fieldHTML(content: string) { set fieldHTML(content: string) {
this.innerHTML = content; this.innerHTML = content;
if (containsInlineContent(this)) { if (content.length > 0 && containsInlineContent(this)) {
this.appendChild(document.createElement("br")); this.appendChild(document.createElement("br"));
} }
} }

View file

@ -9,67 +9,45 @@ export function nodeIsElement(node: Node): node is Element {
return node.nodeType === Node.ELEMENT_NODE; return node.nodeType === Node.ELEMENT_NODE;
} }
const INLINE_TAGS = [ // https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
"A", const BLOCK_TAGS = [
"ABBR", "ADDRESS",
"ACRONYM", "ARTICLE",
"AUDIO", "ASIDE",
"B", "BLOCKQUOTE",
"BDI", "DETAILS",
"BDO", "DIALOG",
"BIG", "DD",
"BR", "DIV",
"BUTTON", "DL",
"CANVAS", "DT",
"CITE", "FIELDSET",
"CODE", "FIGCAPTION",
"DATA", "FIGURE",
"DATALIST", "FOOTER",
"DEL", "FORM",
"DFN", "H1",
"EM", "H2",
"EMBED", "H3",
"FONT", "H4",
"I", "H5",
"IFRAME", "H6",
"IMG", "HEADER",
"INPUT", "HGROUP",
"INS", "HR",
"KBD", "LI",
"LABEL", "MAIN",
"MAP", "NAV",
"MARK", "OL",
"METER", "P",
"NOSCRIPT", "PRE",
"OBJECT", "SECTION",
"OUTPUT", "TABLE",
"PICTURE", "UL",
"PROGRESS",
"Q",
"RUBY",
"S",
"SAMP",
"SCRIPT",
"SELECT",
"SLOT",
"SMALL",
"SPAN",
"STRONG",
"SUB",
"SUP",
"SVG",
"TEMPLATE",
"TEXTAREA",
"TIME",
"U",
"TT",
"VAR",
"VIDEO",
"WBR",
]; ];
export function nodeIsInline(node: Node): boolean { export function elementIsBlock(element: Element): boolean {
return !nodeIsElement(node) || INLINE_TAGS.includes(node.tagName); return BLOCK_TAGS.includes(element.tagName);
} }
export function caretToEnd(node: Node): void { export function caretToEnd(node: Node): void {