Created
May 31, 2017 10:27
-
-
Save tnqsoft/ef540cacd655fbe55cde00c306b0c23c to your computer and use it in GitHub Desktop.
SafePipe
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, PipeTransform } from '@angular/core'; | |
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser'; | |
/* | |
* Trust pipe | |
* Usage: | |
* value | safe | |
*/ | |
@Pipe({ | |
name: 'safe' | |
}) | |
export class SafePipe implements PipeTransform { | |
constructor(private sanitizer: DomSanitizer) { } | |
public transform(value: any, safeType: string = 'html'): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl { | |
switch (safeType) { | |
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(`Invalid safe type specified: ${safeType}`); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment