Skip to content

Instantly share code, notes, and snippets.

@kubaszumiato
Created March 23, 2017 20:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kubaszumiato/23fb74bc1f19f23b8c2430eb1303d1d3 to your computer and use it in GitHub Desktop.
Save kubaszumiato/23fb74bc1f19f23b8c2430eb1303d1d3 to your computer and use it in GitHub Desktop.
Angular safe pipe implementation to bypass DomSanitizer stripping out content
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}`);
}
}
}
@ckuchhadiya
Copy link

gfhfyugujgjhbgj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment