File

projects/wvr-elements/src/lib/shared/pipes/safe.pipe.ts

Metadata

Methods

transform
transform(value: string, type: "html" | "style" | "script" | "url" | "resourceUrl")
Parameters :
Name Type Optional
value string No
type "html" | "style" | "script" | "url" | "resourceUrl" No
Returns : SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeResourceUrl, SafeScript, SafeStyle, SafeUrl } from '@angular/platform-browser';

@Pipe({
  name: 'safe',
  pure: true
})
export class SafePipe implements PipeTransform {

  constructor(protected _sanitizer: DomSanitizer) {

  }

  transform(value: string, type: 'html' | 'style' | 'script' | 'url' | 'resourceUrl')
    : 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}`);
    }
  }

}

results matching ""

    No results matching ""