6

Here's my problem, I want to include a button inside an HTML template, that links to an external website. But, when HTML is read, Google Chrome says :

WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).

It seems that the Angular 2's innerHTML attribute doesn't allow 2 recursive HTML bindings.

Here's my code :

<ion-col width-80 innerHtml="{{ slide.content + '<p><button round full (click)=`' 
+ slide.button.url +'`>'+slide.button.text+'</button>' }}"></ion-col>

The (click) attribute is deleted, and only (slide.button.text) is displayed as single text.

Any solution ?

slide is like this :

{
    img: 'img/picture.png',
    content: `sometext`,
    button : {
        url: `http://www.foo.com`,
        text: `Site foo`
    }
}
20
  • See stackoverflow.com/questions/34467000/sanitize-input-in-angular2 Commented Aug 9, 2016 at 14:40
  • I'm reading the comments, and it seems that it didn't fix my problem... Commented Aug 9, 2016 at 14:44
  • " seems that it didn't fix my problem" is hardly actionable ;-) What did you try? How did it not work? The message you get is from angular.io/docs/ts/latest/api/platform-browser/index/… You need to tell Angular2 that you're sure the content is safe. Commented Aug 9, 2016 at 14:50
  • I tried to include the pipe that PierreDuc wrote, but the return type of this._sanitizer.bypassSecurityTrustHtml is SafeHtml. And I don't know this object ^^ Check here for the doc of SafeHtml Commented Aug 9, 2016 at 14:58
  • Why do you need to know this object? Just pass it to `[innerHTML]="..."´ Commented Aug 9, 2016 at 15:00

1 Answer 1

2

NOTE: I don't know ion thing so changed it to HTML control. changed button to a (link) control.

You can use DomSanitizationService as shown below,

working demo : http://plnkr.co/edit/y2BXvIO8egNxJPmM3j43?p=preview

//our root app component
import {Component, Pipe} from '@angular/core'
import {DomSanitizationService} from '@angular/platform-browser';

@Component({
  selector: 'my-app',

  template: `
    <span [innerHTML]="myHTML"></span>
  `,
})

export class AppComponent {
  slide={
    img: 'img/picture.png',
    content: `sometext`,
    button : {
        url: `http://www.foo.com`,
        text: `Site foo`
    }
  };

  dangerousUrl='<p><a href='+`${this.slide.button.url}`+'>'+`${this.slide.button.text}`+'</a></p>';

  constructor(sanitizer: DomSanitizationService) {
       this.myHTML= sanitizer.bypassSecurityTrustHtml(this.dangerousUrl);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.