MOBILE-3086 data: Fix syntax errors in templates

main
Albert Gasset 2019-07-22 14:08:33 +02:00
parent d20e2e057e
commit 3788afe8b3
2 changed files with 30 additions and 0 deletions

View File

@ -598,6 +598,9 @@ export class AddonModDataHelperProvider {
getTemplate(data: any, type: string, fields: any[]): string {
let template = data[type] || this.getDefaultTemplate(type, fields);
// Try to fix syntax errors so the template can be parsed by Angular.
template = this.domUtils.fixHtml(template);
// Add core-link directive to links.
template = template.replace(/<a ([^>]*href="[^>]*)>/i, (match, attributes) => {
return '<a core-link capture="true" ' + attributes + '>';

View File

@ -323,6 +323,33 @@ export class CoreDomUtilsProvider {
return urls;
}
/**
* Fix syntax errors in HTML.
*
* @param {string} html HTML text.
* @return {string} Fixed HTML text.
*/
fixHtml(html: string): string {
this.template.innerHTML = html;
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(this.template.content.children).forEach(fixElement);
return this.template.innerHTML;
}
/**
* Focus an element and open keyboard.
*