DefinitelyTyped/types/sanitize-html/sanitize-html-tests.ts
WillGibson f5aa57dcab Update sanitize-html tests to cover with & without parser options (#28682)
See https://github.com/punkave/sanitize-html/issues/248#issuecomment-418859354 for context

Re: "Increase the version number in the header if appropriate." My brain is not able to figure out what that means.

- [x] Use a meaningful title for the pull request. Include the name of the package modified.
- [x] Test the change in your own code. (Compile and run.)
- [x] Add or edit tests to reflect the change. (Run with `npm test`.)
- [x] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#make-a-pull-request).
- [x] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#common-mistakes).
- [x] Run `npm run lint package-name` (or `tsc` if no `tslint.json` is present).

If changing an existing definition:
- [x] Provide a URL to documentation or source code which provides context for the suggested changes: <<url here>>
- [ ] Increase the version number in the header if appropriate.
- [ ] If you are making substantial changes, consider adding a `tslint.json` containing `{ "extends": "dtslint/dt.json" }`.
2018-09-10 14:56:08 -07:00

44 lines
1.2 KiB
TypeScript

import sanitize = require('sanitize-html');
let options: sanitize.IOptions = {
allowedTags: sanitize.defaults.allowedTags.concat('h1', 'h2', 'img'),
allowedAttributes: {
'a': sanitize.defaults.allowedAttributes['a'].concat('rel'),
'img': ['src', 'height', 'width', 'alt', 'style']
},
allowedStyles: {
'*': {
color: [/^red$/],
background: [/^green$/],
'background-color': [/^#0000FF$/]
}
},
allowedIframeHostnames: ['www.youtube.com'],
allowedSchemesAppliedToAttributes: [ 'href', 'src', 'cite' ],
transformTags: {
'a': sanitize.simpleTransform('a', { 'rel': 'nofollow' }),
'img': (tagName: string, attribs: sanitize.Attributes) => {
let img = { tagName, attribs };
img.attribs['alt'] = 'transformed' ;
return img;
}
},
exclusiveFilter: function(frame: sanitize.IFrame) {
return frame.tag === 'a' && !frame.text.trim();
},
allowedSchemesByTag: {
'a': ['http', 'https']
},
allowProtocolRelative: false
};
let unsafe = '<div><script>alert("hello");</script></div>';
let safe = sanitize(unsafe, options);
options.parser = {
decodeEntities: true
};
safe = sanitize(unsafe, options);