feat: add dangerouslySetInnerHTML attr
This adds are new attribute to Elements, "dangerouslySetInnerHTML", which like the same attribute from React allows one to take a stirng of unescaped HTML and render it unconditionally. This is of course a potentially dangerous operation that can open your app up to XSS attacks, but for interoperating with existing content management systems and libraries that output HTML (e.g. markdown renderers). Using "dangerouslySetInnerHTML" on an element with children will generate an error within createElement, since it doesn't make sense to have both children and inner HTML.
This commit is contained in:
parent
7f9743a21f
commit
eef25d360d
4 changed files with 61 additions and 3 deletions
|
|
@ -117,4 +117,37 @@ testSuite("renderPage", ({ test, expect }) => {
|
|||
"<!DOCTYPE html><html><div>test of</div><div>fragments</div></html>",
|
||||
);
|
||||
});
|
||||
|
||||
test("renders unescaped HTML via dangerouslySetInnerHTML", () => {
|
||||
const html = renderPage(
|
||||
<html>
|
||||
<div
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: "<div>red alert!</div>",
|
||||
}}
|
||||
/>
|
||||
</html>,
|
||||
);
|
||||
expect(html).toEqual(
|
||||
"<!DOCTYPE html><html><div><div>red alert!</div></div></html>",
|
||||
);
|
||||
});
|
||||
|
||||
test("throws error when both dangerouslySetInnerHTML and children prop present", () => {
|
||||
expect(() =>
|
||||
renderPage(
|
||||
<html>
|
||||
<div
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: "<div>set phasers to kill</div>",
|
||||
}}
|
||||
>
|
||||
<div>set phasers to stun</div>
|
||||
</div>
|
||||
</html>,
|
||||
),
|
||||
).toThrowErrorMatching(
|
||||
'An element with children may not have a "dangerouslySetInnerHTML" prop since children would be overriden',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue