From: Alexander Ebert Date: Wed, 10 May 2023 13:36:19 +0000 (+0200) Subject: Rename the Cleanup module for CKEditor to `Normalizer` X-Git-Tag: 6.0.0_Alpha_1~116 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=2d44eb2c92355e8299b05095a6989e3138f4fbe4;p=GitHub%2FWoltLab%2FWCF.git Rename the Cleanup module for CKEditor to `Normalizer` --- diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor.ts b/ts/WoltLabSuite/Core/Component/Ckeditor.ts index 9b7703e3a0..b9cb225ffe 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor.ts @@ -20,7 +20,7 @@ import { deleteDraft, initializeAutosave, setupRestoreDraft } from "./Ckeditor/A import { createConfigurationFor, Features } from "./Ckeditor/Configuration"; import { dispatchToCkeditor } from "./Ckeditor/Event"; import { setup as setupSubmitOnEnter } from "./Ckeditor/SubmitOnEnter"; -import { normalizeLegacyMessage } from "./Ckeditor/Cleanup"; +import { normalizeLegacyMessage } from "./Ckeditor/Normalizer"; import Devtools from "../Devtools"; import { ClassicEditor, EditorConfig, Element as CkeElement } from "./Ckeditor/Types"; diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts b/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts deleted file mode 100644 index 9b6447c646..0000000000 --- a/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Cleans up the markup of legacy messages. - * - * Messages created in the previous editor used empty paragraphs to create empty - * lines. In addition, Firefox kept trailing
in lines with content, which - * causes issues with CKEditor. - * - * @author Alexander Ebert - * @copyright 2001-2023 WoltLab GmbH - * @license GNU Lesser General Public License - * @since 6.0 - */ - -import DomUtil from "../../Dom/Util"; - -function normalizeBr(div: HTMLElement): void { - div.querySelectorAll("br").forEach((br) => { - unwrapBr(br); - removeTrailingBr(br); - }); -} - -function unwrapBr(br: HTMLElement): void { - for (;;) { - if (br.previousSibling || br.nextSibling) { - return; - } - - const parent = br.parentElement!; - switch (parent.tagName) { - case "B": - case "DEL": - case "EM": - case "I": - case "STRONG": - case "SUB": - case "SUP": - case "SPAN": - case "U": - parent.insertAdjacentElement("afterend", br); - parent.remove(); - break; - - default: - return; - } - } -} - -function removeTrailingBr(br: HTMLElement): void { - if (br.dataset.ckeFiller === "true") { - return; - } - - const paragraphOrTableCell = br.closest("p, td"); - if (paragraphOrTableCell === null) { - return; - } - - if (!DomUtil.isAtNodeEnd(br, paragraphOrTableCell)) { - return; - } - - if (paragraphOrTableCell.tagName === "TD" || paragraphOrTableCell.childNodes.length > 1) { - br.remove(); - } -} - -function getPossibleSpacerParagraphs(div: HTMLElement): HTMLParagraphElement[] { - const paragraphs: HTMLParagraphElement[] = []; - - div.querySelectorAll("p").forEach((paragraph) => { - paragraph.normalize(); - - if (paragraph.childNodes.length === 1) { - const child = paragraph.childNodes[0]; - if (child instanceof HTMLBRElement && child.dataset.ckeFiller !== "true") { - paragraphs.push(paragraph); - } - } - }); - - return paragraphs; -} - -function reduceSpacerParagraphs(paragraphs: HTMLParagraphElement[]): void { - if (paragraphs.length === 0) { - return; - } - - for (let i = 0, length = paragraphs.length; i < length; i++) { - const candidate = paragraphs[i]; - let offset = 0; - - // Searches for adjacent paragraphs. - while (i + offset + 1 < length) { - const nextCandidate = paragraphs[i + offset + 1]; - if (candidate.nextElementSibling !== nextCandidate) { - break; - } - - offset++; - } - - if (offset === 0) { - // An offset of 0 means that this is a single paragraph and we - // can safely remove it. - candidate.remove(); - } else { - // We need to reduce the number of paragraphs by half, unless it - // is an uneven number in which case we need to remove one - // additional paragraph. - const totalNumberOfParagraphs = offset + 1; - const numberOfParagraphsToRemove = Math.ceil(totalNumberOfParagraphs / 2); - - const removeParagraphs = paragraphs.slice(i, i + numberOfParagraphsToRemove); - removeParagraphs.forEach((paragraph) => { - paragraph.remove(); - }); - - i += offset; - } - } -} - -export function normalizeLegacyMessage(element: HTMLElement): void { - if (!(element instanceof HTMLTextAreaElement)) { - throw new TypeError("Expected the element to be a