Advertisement
zagushka

Untitled

Mar 13th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Pipe } from '@angular/core';
  2. import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl} from '@angular/platform-browser';
  3.  
  4. @Pipe({
  5.   name: 'safe'
  6. })
  7. export class SafePipe {
  8.  
  9.   constructor(protected _sanitizer: DomSanitizer) {
  10.  
  11.   }
  12.  
  13.   public transform(value: string, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
  14.     switch (type) {
  15.       case 'html':
  16.         return this._sanitizer.bypassSecurityTrustHtml(value);
  17.       case 'style':
  18.         return this._sanitizer.bypassSecurityTrustStyle(value);
  19.       case 'script':
  20.         return this._sanitizer.bypassSecurityTrustScript(value);
  21.       case 'url':
  22.         return this._sanitizer.bypassSecurityTrustUrl(value);
  23.       case 'resourceUrl':
  24.         return this._sanitizer.bypassSecurityTrustResourceUrl(value);
  25.       default:
  26.         throw new Error(`Unable to bypass security for invalid type: ${type}`);
  27.     }
  28.   }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement