What I am trying to achieve is a dynamically generated html with some static links in it.
So, in a component, I have an array of objects:
let list = [{
type: 'container',
title: 'SIMPLE LIST'
description: '
'}, {
type: 'container',
title: 'SIMPLE ICON'
description: 'launch'
}]
Then I pass it to a service which sanitize the description key via bypassSecurityTrustHtml() (from DomSanitzer):
export class myDynamicBuilder {
content: Array;
constructor(content, sanitizer) {
content.forEach(each => {
if (item.hasOwnProperty('description') && typeof item['description'] === 'string') {
item['description'] = sanitizer.bypassSecurityTrustHtml(item['description'])
}
this.content = content
})
}
}
Then in template:
{{item?.title}} | |
Links in description field do not work as expected.:
if href is used as attribute a complete reload of the app is triggered (which is very bad)
if the routerLink directive is used the link doesn't work
if [href] is used I get the usual XSS security warning. So, I went back to the docs and I've found the convenient bypassSecurityTrustUrl function. I modified the above mentioned service to replace strings after [href] with the output of the bypassSecurityTrustUrl and then throw the result in the bypassSecurityTrustHtml function. Got a very nicely rendered HTML but with a non functional link.
How should I handle this scenario? I am thinking of building some pipes as shown in this question but not sure if this is the right way to do it. Another idea could be to let my service handle a new key (maybe links) of the input object, sanitize via bypassSecurityTrustUrl and then inject safe links in the sanitized HTML. Is there any defined way to do this? Thanks.