From 4dd949b10ef3cfbdffdc28edf0e51a077bf38c68 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Thu, 17 Oct 2024 12:29:27 +0200 Subject: [PATCH] MOBILE-4616 database: Fix processing of templates Some elements of templates were being removed, like or
  • . --- src/core/services/utils/dom.ts | 30 +++++++++++++++------------ src/core/singletons/text.ts | 1 + src/core/utils/create-html-element.ts | 1 + 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index 924ef2b8c..165c6595b 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -262,23 +262,27 @@ export class CoreDomUtilsProvider { * @returns Fixed HTML text. */ fixHtml(html: string): string { - return CoreText.processHTML(html, (element) => { - // eslint-disable-next-line no-control-regex - const attrNameRegExp = /[^\x00-\x20\x7F-\x9F"'>/=]+/; - const fixElement = (element: Element): void => { - // Remove attributes with an invalid name. - Array.from(element.attributes).forEach((attr) => { - if (!attrNameRegExp.test(attr.name)) { - element.removeAttributeNode(attr); - } - }); + // We can't use CoreText.processHTML because it removes elements that + // are not allowed as a child of
    , like
  • or . + const template = document.createElement('template'); + template.innerHTML = html; - Array.from(element.children).forEach(fixElement); - }; + // eslint-disable-next-line no-control-regex + const attrNameRegExp = /[^\x00-\x20\x7F-\x9F"'>/=]+/; + const fixElement = (element: Element): void => { + // Remove attributes with an invalid name. + Array.from(element.attributes).forEach((attr) => { + if (!attrNameRegExp.test(attr.name)) { + element.removeAttributeNode(attr); + } + }); Array.from(element.children).forEach(fixElement); - }); + }; + Array.from(template.content.children).forEach(fixElement); + + return template.innerHTML; } /** diff --git a/src/core/singletons/text.ts b/src/core/singletons/text.ts index 482ce044a..19f0c49fc 100644 --- a/src/core/singletons/text.ts +++ b/src/core/singletons/text.ts @@ -418,6 +418,7 @@ export class CoreText { /** * Process HTML string. + * Warning: Top-level elements that are not allowed as a child of
    (like or
  • ) will be removed. * * @param text HTML string. * @param process Method to process the HTML. diff --git a/src/core/utils/create-html-element.ts b/src/core/utils/create-html-element.ts index 076d6bbc4..ebdb9a2ff 100644 --- a/src/core/utils/create-html-element.ts +++ b/src/core/utils/create-html-element.ts @@ -14,6 +14,7 @@ /** * Convert some HTML as text into an HTMLElement. This HTML is put inside a div. + * Warning: Top-level elements that are not allowed as a child of
    (like or
  • ) will be removed. * * @param html Text to convert. * @returns Element.