-
Notifications
You must be signed in to change notification settings - Fork 3
Add new SafeInnerHTML component #89
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import React from 'react'; | ||
| import DOMPurify from 'isomorphic-dompurify'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| export const purifySettings = { | ||
| KEEP_CONTENT: false, | ||
| FORBID_TAGS: ['style', 'img'], // In most cases we don't need style or img tags | ||
| FORBID_ATTR: ['style'] // In most cases we don't want inline css | ||
| }; | ||
|
|
||
| const SafeInnerHTML = ({ as = 'div', html = '', ...props }) => { | ||
| const Comp = as; | ||
| if (!html) return null; | ||
|
|
||
| return <Comp {...props} dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(html, purifySettings) }} />; | ||
| }; | ||
|
|
||
| SafeInnerHTML.propTypes = { | ||
| as: PropTypes.elementType, | ||
| html: PropTypes.string | ||
| }; | ||
|
|
||
| export default SafeInnerHTML; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,9 @@ import DisplayFormikState from '../../components/DisplayFormikState'; | |
| import { BreadcrumbItem, Breadcrumbs } from '../../components/Breadcrumbs'; | ||
| import { Link } from 'react-router-dom'; | ||
| import ReactSelect from '../../components/form/ReactSelect'; | ||
| import WysiwygEditor from '../../components/form/WysiwygEditor'; | ||
|
|
||
| import { FaExternalLinkAlt } from 'react-icons/fa'; | ||
|
|
||
| const FormsPage = () => { | ||
| let fruits = ['apple', 'banana', 'orange', 'avocado']; | ||
|
|
@@ -48,7 +51,11 @@ const FormsPage = () => { | |
| </Breadcrumbs> | ||
| <h2>Forms</h2> | ||
| <p> | ||
| Forms should be built with <a href="https://jaredpalmer.com/formik/">Formik</a>. | ||
| Forms should be built with{' '} | ||
| <a href="https://jaredpalmer.com/formik/" target="_blank" rel="noopener noreferrer"> | ||
| Formik <FaExternalLinkAlt style={{ fontSize: '75%' }} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either add title, aria-label, or aria-hidden. As of now the screen reader has no context for this icon. |
||
| </a> | ||
| . | ||
| </p> | ||
| <p>Form markup is derrived from Bootstrap.</p> | ||
| <hr /> | ||
|
|
@@ -136,6 +143,24 @@ const FormsPage = () => { | |
| <hr /> | ||
| <TextArea id="text_area" label="Text Area" /> | ||
|
|
||
| <h3>WYSIWYG Editor</h3> | ||
| <hr /> | ||
| <p> | ||
| The HTML content passed into and generated from <code>WysiwygEditor</code> is sanitized with{' '} | ||
| <a href="https://github.com/cure53/DOMPurify" target="_blank" rel="noopener noreferrer"> | ||
| DOMPurify <FaExternalLinkAlt style={{ fontSize: '75%' }} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class instead of inline style
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either add title, aria-label, or aria-hidden. As of now the screen reader has no context for this icon. |
||
| </a> | ||
| </p> | ||
| <WysiwygEditor | ||
| label="WYSIWYG Editor" | ||
| html={`<p>Pellentesque habitant <strong style="color: red">morbi tristique senectus</strong> <img src=x onerror=alert('this should never get shown') /> fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>`} | ||
| /> | ||
| <WysiwygEditor | ||
| label="Read Only WYSIWYG Editor" | ||
| html="<p>Pellentesque habitant <strong>morbi tristique senectus</strong> <img src=x onerror=alert('this should never get shown') /> et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>" | ||
| readOnly | ||
| /> | ||
|
Comment on lines
+154
to
+162
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would the intent of this code (i.e. presumably to demonstrate that e.g. pseudo describe('WysiwygEditor', () => {
test('html prop is sanitized', () => {
render(<WysiwygEditor html={<img src=x onerror=alert('this should never get shown') />)
expect(wysiwygEditorDiv).toHaveTextContent('');
})
})
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, 100% |
||
|
|
||
| <h3>Checkbox</h3> | ||
| <hr /> | ||
| <Checkbox label="Checkbox" id="checkbox_1" /> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we merged in the work on TS in this PR, we could write this in TS from the start and replace
propTypeswithPropsinterfaceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, seems like that PR is not quite ready for prime time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that the TS PR has been approved, can we refactor this to typescript