Created
March 23, 2017 20:04
-
-
Save kubaszumiato/23fb74bc1f19f23b8c2430eb1303d1d3 to your computer and use it in GitHub Desktop.
Angular safe pipe implementation to bypass DomSanitizer stripping out content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Pipe} from '@angular/core'; | |
import {DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl} from '@angular/platform-browser'; | |
//https://forum.ionicframework.com/t/inserting-html-via-angular-2-use-of-domsanitizationservice-bypasssecuritytrusthtml/62562/2 | |
@Pipe({ | |
name: 'safe' | |
}) | |
export class SafePipe { | |
constructor(protected _sanitizer: DomSanitizer) { | |
} | |
public transform(value: string, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl { | |
switch (type) { | |
case 'html': | |
return this._sanitizer.bypassSecurityTrustHtml(value); | |
case 'style': | |
return this._sanitizer.bypassSecurityTrustStyle(value); | |
case 'script': | |
return this._sanitizer.bypassSecurityTrustScript(value); | |
case 'url': | |
return this._sanitizer.bypassSecurityTrustUrl(value); | |
case 'resourceUrl': | |
return this._sanitizer.bypassSecurityTrustResourceUrl(value); | |
default: | |
throw new Error(`Unable to bypass security for invalid type: ${type}`); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gfhfyugujgjhbgj