Skip to content

feat: Support dangerouslySetInnerHTML on Fragments #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 8 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@ import {
HTML_LOWER_CASE,
HTML_ENUMERATED,
SVG_CAMEL_CASE,
createComponent
createComponent,
flattenTopLevelFragments
} from './lib/util.js';
import { options, h, Fragment } from 'preact';
import {
@@ -342,6 +343,8 @@ function _renderToString(
// Fragments are the least used components of core that's why
// branching here for comments has the least effect on perf.
return '<!--' + encodeEntities(props.UNSTABLE_comment) + '-->';
} else if ('dangerouslySetInnerHTML' in props) {
return props.dangerouslySetInnerHTML.__html;
}

rendered = props.children;
@@ -395,14 +398,7 @@ function _renderToString(
options.errorBoundaries &&
(type.getDerivedStateFromError || component.componentDidCatch)
) {
// When a component returns a Fragment node we flatten it in core, so we
// need to mirror that logic here too
let isTopLevelFragment =
rendered != null &&
rendered.type === Fragment &&
rendered.key == null &&
rendered.props.tpl == null;
rendered = isTopLevelFragment ? rendered.props.children : rendered;
rendered = flattenTopLevelFragments(rendered);

try {
return _renderToString(
@@ -432,12 +428,7 @@ function _renderToString(
context = assign({}, context, component.getChildContext());
}

let isTopLevelFragment =
rendered != null &&
rendered.type === Fragment &&
rendered.key == null &&
rendered.props.tpl == null;
rendered = isTopLevelFragment ? rendered.props.children : rendered;
rendered = flattenTopLevelFragments(rendered);

return _renderToString(
rendered,
@@ -459,14 +450,7 @@ function _renderToString(
}
}

// When a component returns a Fragment node we flatten it in core, so we
// need to mirror that logic here too
let isTopLevelFragment =
rendered != null &&
rendered.type === Fragment &&
rendered.key == null &&
rendered.props.tpl == null;
rendered = isTopLevelFragment ? rendered.props.children : rendered;
rendered = flattenTopLevelFragments(rendered);

try {
// Recurse into children before invoking the after-diff hook
@@ -542,7 +526,7 @@ function _renderToString(
: result;
} catch (e) {
if (!e || typeof e.then != 'function') throw e;

return e.then(renderNestedChildren);
}
};
19 changes: 19 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Fragment } from 'preact';

export const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
export const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
export const NAMESPACE_REPLACE_REGEX = /^(xlink|xmlns|xml)([A-Z])/;
@@ -179,3 +181,20 @@ export class Deferred {
});
}
}

/**
* When a component returns a Fragment node we flatten it in core, so we
* need to mirror that logic here too
*
* @param {any} rendered
* @returns {any}
*/
export function flattenTopLevelFragments(rendered) {
const isTopLevelFragment =
rendered != null &&
rendered.type === Fragment &&
rendered.key == null &&
rendered.props.tpl == null &&
rendered.props.dangerouslySetInnerHTML == null;
return isTopLevelFragment ? rendered.props.children : rendered;
}
24 changes: 24 additions & 0 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
@@ -861,6 +861,30 @@ describe('render', () => {
);
expect(rendered).to.equal('<div>foo</div>');
});

it('should accept dangerouslySetInnerHTML on Fragments', () => {
// some invalid HTML to make sure we're being flakey:
let html = '<a href="foo">asdf</a> some text <ul><li>foo<li>bar</ul>';
let rendered = render(
<div>
<Fragment dangerouslySetInnerHTML={{ __html: html }} />
</div>
);
expect(rendered).to.equal(`<div>${html}</div>`);
});

it('should accept dangerouslySetInnerHTML on Fragments in Components', () => {
// some invalid HTML to make sure we're being flakey:
let html = '<a href="foo">asdf</a> some text <ul><li>foo<li>bar</ul>';
const Foo = () => <Fragment dangerouslySetInnerHTML={{ __html: html }} />;

let rendered = render(
<div>
<Foo />
</div>
);
expect(rendered).to.equal(`<div>${html}</div>`);
});
});

describe('className / class massaging', () => {