enhance typings

This commit is contained in:
falsandtru
2017-08-18 19:10:01 +09:00
parent be1e9cb778
commit 79fe3bc6ab
3 changed files with 26 additions and 21 deletions

View File

@@ -3,56 +3,60 @@
import dompurify = require('dompurify');
dompurify.sanitize('<script>alert("hi")</script>');
dompurify.addHook('beforeSanitizeElements', (el, data, config) => {
return el;
});
dompurify.addHook('beforeSanitizeElements', (el, data, config) => undefined);
//examples from the DOMPurify README
let dirty = '<script>alert("hi")</script><p>Totally safe<p><p onerror="blowUp()">Totally not safe</p>';
let str: string;
let elem: HTMLElement;
let frag: DocumentFragment;
// allow only <b>
dompurify.sanitize(dirty, { ALLOWED_TAGS: ['b'] });
str = dompurify.sanitize(dirty, { ALLOWED_TAGS: ['b'] });
// allow only <b> and <q> with style attributes (for whatever reason)
dompurify.sanitize(dirty, { ALLOWED_TAGS: ['b', 'q'], ALLOWED_ATTR: ['style'] });
str = dompurify.sanitize(dirty, { ALLOWED_TAGS: ['b', 'q'], ALLOWED_ATTR: ['style'] });
// leave all as it is but forbid <style>
dompurify.sanitize(dirty, { FORBID_TAGS: ['style'] });
str = dompurify.sanitize(dirty, { FORBID_TAGS: ['style'] });
// leave all as it is but forbid style attributes
dompurify.sanitize(dirty, { FORBID_ATTR: ['style'] });
str = dompurify.sanitize(dirty, { FORBID_ATTR: ['style'] });
// extend the existing array of allowed tags
dompurify.sanitize(dirty, { ADD_TAGS: ['my-tag'] });
str = dompurify.sanitize(dirty, { ADD_TAGS: ['my-tag'] });
// extend the existing array of attributes
dompurify.sanitize(dirty, { ADD_ATTR: ['my-attr'] });
str = dompurify.sanitize(dirty, { ADD_ATTR: ['my-attr'] });
// prohibit HTML5 data attributes (default is true)
dompurify.sanitize(dirty, { ALLOW_DATA_ATTR: false });
str = dompurify.sanitize(dirty, { ALLOW_DATA_ATTR: false });
// return a DOM HTMLBodyElement instead of an HTML string (default is false)
dompurify.sanitize(dirty, { RETURN_DOM: true }) as HTMLElement;
str = dompurify.sanitize(dirty, { RETURN_DOM: false });
elem = dompurify.sanitize(dirty, { RETURN_DOM: true });
elem = dompurify.sanitize(dirty, { RETURN_DOM: true, RETURN_DOM_FRAGMENT: false });
// return a DOM DocumentFragment instead of an HTML string (default is false)
dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true }) as DocumentFragment;
dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true, RETURN_DOM: true }) as DocumentFragment;
str = dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: false });
frag = dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true });
frag = dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true, RETURN_DOM: false });
frag = dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true, RETURN_DOM: true });
// return a DOM DocumentFragment instead of an HTML string (default is false)
// also import it into the current document (default is false).
// RETURN_DOM_IMPORT must be set if you would like to append
// the returned node to the current document
let clean = dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true, RETURN_DOM_IMPORT: true });
document.body.appendChild(clean);
frag = dompurify.sanitize(dirty, { RETURN_DOM_FRAGMENT: true, RETURN_DOM_IMPORT: true });
// return entire document including <html> tags (default is false)
dompurify.sanitize(dirty, { WHOLE_DOCUMENT: true });
str = dompurify.sanitize(dirty, { WHOLE_DOCUMENT: true });
// make output safe for usage in jQuery's $()/html() method (default is false)
dompurify.sanitize(dirty, { SAFE_FOR_JQUERY: true });
str = dompurify.sanitize(dirty, { SAFE_FOR_JQUERY: true });
// disable DOM Clobbering protection on output (default is true, handle with care!)
dompurify.sanitize(dirty, { SANITIZE_DOM: false });
str = dompurify.sanitize(dirty, { SANITIZE_DOM: false });
// discard an element's content when the element is removed (default is true)
dompurify.sanitize(dirty, { KEEP_CONTENT: false });
str = dompurify.sanitize(dirty, { KEEP_CONTENT: false });

View File

@@ -10,9 +10,10 @@ declare var DOMPurify: DOMPurify;
interface DOMPurify {
sanitize(source: string | Node): string;
sanitize(source: string | Node, config: DOMPurifyConfig & { RETURN_DOM_FRAGMENT?: false; RETURN_DOM?: false; }): string;
sanitize(source: string | Node, config: DOMPurifyConfig & { RETURN_DOM_FRAGMENT: true; }): DocumentFragment;
sanitize(source: string | Node, config: DOMPurifyConfig & { RETURN_DOM: true; }): HTMLElement;
sanitize<T extends string | HTMLElement | DocumentFragment>(source: string | Node, config: DOMPurifyConfig): T;
sanitize(source: string | Node, config: DOMPurifyConfig): string | HTMLElement | DocumentFragment;
addHook(hook: 'uponSanitizeElement', cb: (currentNode: Element, data: DOMPurifySanitizeElementHookEvent, config: DOMPurifyConfig) => void): void;
addHook(hook: 'uponSanitizeAttribute', cb: (currentNode: Element, data: DOMPurifySanitizeAttributeHookEvent, config: DOMPurifyConfig) => void): void;
addHook(hook: DOMPurifyHookName, cb: (currentNode: Element, data: DOMPurifyHookEvent, config: DOMPurifyConfig) => void): void;

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"