Skip to content

Instantly share code, notes, and snippets.

@chaitanya1248
Created January 29, 2018 22:45
Show Gist options
  • Save chaitanya1248/520b6c21c1445f24fcbc0e65fd53661f to your computer and use it in GitHub Desktop.
Save chaitanya1248/520b6c21c1445f24fcbc0e65fd53661f to your computer and use it in GitHub Desktop.
doc-preview component to sanitize src
import {Component, Input} from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import {DOMPurify} from 'dompurify';
@Component({
selector: 'tb-doc-preview',
template: ''
})
export class TbDocPreviewComponent {
@Input() public document: String;
constructor(private sanitizer: DomSanitizer) {
}
get processedDocument(): SafeHtml {
console.log('document received into getProcessedDocument', document);
if (this.document) {
const sanitized = DOMPurify.sanitize(this.document, {FORBID_TAGS: ['script'], RETURN_DOM: true});
const script = document.createElement('script');
script.src = 'assets/js/iframeResizer.contentWindow.js';
sanitized.appendChild(script);
/* Return result */
return this.sanitizer.bypassSecurityTrustHtml(sanitized.outerHTML);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment