MOBILE-4616 database: Fix processing of templates

Some elements of templates were being removed, like <tr> or <li>.
main
Albert Gasset 2024-10-17 12:29:27 +02:00
parent ce87157622
commit 4dd949b10e
3 changed files with 19 additions and 13 deletions

View File

@ -262,7 +262,11 @@ export class CoreDomUtilsProvider {
* @returns Fixed HTML text. * @returns Fixed HTML text.
*/ */
fixHtml(html: string): string { fixHtml(html: string): string {
return CoreText.processHTML(html, (element) => { // We can't use CoreText.processHTML because it removes elements that
// are not allowed as a child of <div>, like <li> or <tr>.
const template = document.createElement('template');
template.innerHTML = html;
// eslint-disable-next-line no-control-regex // eslint-disable-next-line no-control-regex
const attrNameRegExp = /[^\x00-\x20\x7F-\x9F"'>/=]+/; const attrNameRegExp = /[^\x00-\x20\x7F-\x9F"'>/=]+/;
const fixElement = (element: Element): void => { const fixElement = (element: Element): void => {
@ -276,9 +280,9 @@ export class CoreDomUtilsProvider {
Array.from(element.children).forEach(fixElement); Array.from(element.children).forEach(fixElement);
}; };
Array.from(element.children).forEach(fixElement); Array.from(template.content.children).forEach(fixElement);
});
return template.innerHTML;
} }
/** /**

View File

@ -418,6 +418,7 @@ export class CoreText {
/** /**
* Process HTML string. * Process HTML string.
* Warning: Top-level elements that are not allowed as a child of <div> (like <tr> or <li>) will be removed.
* *
* @param text HTML string. * @param text HTML string.
* @param process Method to process the HTML. * @param process Method to process the HTML.

View File

@ -14,6 +14,7 @@
/** /**
* Convert some HTML as text into an HTMLElement. This HTML is put inside a div. * 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 <div> (like <tr> or <li>) will be removed.
* *
* @param html Text to convert. * @param html Text to convert.
* @returns Element. * @returns Element.