From e83e11b5f92f0b60afa47de5dae9d9f131d3d5d0 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 4 May 2023 16:32:05 +0200 Subject: [PATCH] Strip legacy spacer paragraphs when editing messages --- ts/WoltLabSuite/Core/Component/Ckeditor.ts | 24 +++++++++++++++++++ .../WoltLabSuite/Core/Component/Ckeditor.js | 19 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor.ts b/ts/WoltLabSuite/Core/Component/Ckeditor.ts index 8bd445a887..303115d95c 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor.ts @@ -174,6 +174,28 @@ function initializeConfiguration(element: HTMLElement, features: Features, bbcod return configuration; } +function stripLegacySpacerParagraphs(element: HTMLElement): void { + if (!(element instanceof HTMLTextAreaElement)) { + return; + } + + const div = document.createElement("div"); + div.innerHTML = element.value; + + div.querySelectorAll("p").forEach((paragraph) => { + if (paragraph.childElementCount === 1) { + const child = paragraph.children[0] as HTMLElement; + if (child.tagName === "BR" && child.dataset.ckeFiller !== "true") { + if (paragraph.textContent!.trim() === "") { + paragraph.remove(); + } + } + } + }); + + element.value = div.innerHTML; +} + export async function setupCkeditor( element: HTMLElement, features: Features, @@ -198,6 +220,8 @@ export async function setupCkeditor( const configuration = initializeConfiguration(element, features, bbcodes); + stripLegacySpacerParagraphs(element); + const cke = await window.CKEditor5.create(element, configuration); const ckeditor = new Ckeditor(cke, features); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js index 9bbfdb336f..271151fcd6 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js @@ -124,6 +124,24 @@ define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Medi } return configuration; } + function stripLegacySpacerParagraphs(element) { + if (!(element instanceof HTMLTextAreaElement)) { + return; + } + const div = document.createElement("div"); + div.innerHTML = element.value; + div.querySelectorAll("p").forEach((paragraph) => { + if (paragraph.childElementCount === 1) { + const child = paragraph.children[0]; + if (child.tagName === "BR" && child.dataset.ckeFiller !== "true") { + if (paragraph.textContent.trim() === "") { + paragraph.remove(); + } + } + } + }); + element.value = div.innerHTML; + } async function setupCkeditor(element, features, bbcodes) { if (instances.has(element)) { throw new TypeError(`Cannot initialize the editor for '${element.id}' twice.`); @@ -140,6 +158,7 @@ define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Medi (0, Quote_1.setup)(element); } const configuration = initializeConfiguration(element, features, bbcodes); + stripLegacySpacerParagraphs(element); const cke = await window.CKEditor5.create(element, configuration); const ckeditor = new Ckeditor(cke, features); if (features.autosave) { -- 2.20.1