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,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 <div>, like <li> or <tr>.
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;
}
/**

View File

@ -418,6 +418,7 @@ export class CoreText {
/**
* 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 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.
* 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.
* @returns Element.