From 9e51f50b9aafb7a5794013618bb1967b69301310 Mon Sep 17 00:00:00 2001 From: Bastian Rihm Date: Tue, 16 Jul 2024 13:03:46 +0200 Subject: [PATCH] Fix tiptap html list cleanup (#3853) --- .../components/editor/editor.component.ts | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/client/src/app/ui/modules/editor/components/editor/editor.component.ts b/client/src/app/ui/modules/editor/components/editor/editor.component.ts index 441c7b3917..331d990736 100644 --- a/client/src/app/ui/modules/editor/components/editor/editor.component.ts +++ b/client/src/app/ui/modules/editor/components/editor/editor.component.ts @@ -40,6 +40,7 @@ import Text from '@tiptap/extension-text'; import TextAlign from '@tiptap/extension-text-align'; import TextStyle from '@tiptap/extension-text-style'; import Underline from '@tiptap/extension-underline'; +import { unwrapNode } from 'src/app/infrastructure/utils/dom-helpers'; import { BaseFormControlComponent } from 'src/app/ui/base/base-form-control'; import tinycolor from 'tinycolor2'; @@ -150,6 +151,8 @@ export class EditorComponent extends BaseFormControlComponent implements private cd: ChangeDetectorRef = inject(ChangeDetectorRef); + private domParser = new DOMParser(); + public constructor( private dialog: MatDialog, private translate: TranslateService @@ -221,7 +224,7 @@ export class EditorComponent extends BaseFormControlComponent implements this.cd.detectChanges(); }, onUpdate: () => { - const content = this.editor.getHTML(); + const content = this.cleanupOutput(this.editor.getHTML()); if (this.value != content) { this.updateForm(content); } @@ -403,7 +406,7 @@ export class EditorComponent extends BaseFormControlComponent implements public editCode(): void { this.dialog .open(EditorHtmlDialogComponent, { - data: this.editor.getHTML() + data: this.cleanupOutput(this.editor.getHTML()) }) .afterClosed() .subscribe((result: EditorHtmlDialogOutput) => { @@ -418,20 +421,17 @@ export class EditorComponent extends BaseFormControlComponent implements } protected updateForm(value: string | null): void { - const html = value?.replace(/(]*>)(.*?)(<\/ul>)||(]*>)(.*?)(<\/li>)/g, res => { - if (res) { - return res.replace(/(]*>)(.*?)(<\/li[^>]*>)/g, response => { - if (response.match(/<\/?p[^>]*>/g)?.length == 2) { - return response.replace(/<\/?p[^>]*>/g, ``); - } else if (response) { - return response; - } - return ``; - }); - } - return ``; - }); + this.contentForm.setValue(value); + } + + private cleanupOutput(html: string): string { + const dom = this.domParser.parseFromString(html, `text/html`); + + const listParagraphs = dom.querySelectorAll(`li > p`); + for (let i = 0; i < listParagraphs.length; i++) { + unwrapNode(listParagraphs.item(i)); + } - this.contentForm.setValue(html); + return dom.body.innerHTML; } }