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.