mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
Check for non-existence of block tags instead of exclusive existence of inline tags in editable
This commit is contained in:
parent
cb5cb006a9
commit
01a283bb99
2 changed files with 41 additions and 69 deletions
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue