Skip to content

Commit

Permalink
Ignore dangerouslySetInnerHTML during hydration (#1595)
Browse files Browse the repository at this point in the history
Ignore dangerouslySetInnerHTML during hydration
  • Loading branch information
marvinhagemeister committed May 16, 2019
2 parents 98d13cd + ee570d5 commit f4e7d71
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ function diffElementNodes(dom, newVNode, oldVNode, context, isSvg, excessDomChil
}
let oldHtml = oldProps.dangerouslySetInnerHTML;
let newHtml = newProps.dangerouslySetInnerHTML;
if (newHtml || oldHtml) {
if ((newHtml || oldHtml) && excessDomChildren==null) {
// Avoid re-applying the same '__html' if it did not changed between re-render
if (!newHtml || !oldHtml || newHtml.__html!=oldHtml.__html) {
dom.innerHTML = newHtml && newHtml.__html || '';
Expand Down
6 changes: 3 additions & 3 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,14 @@ describe('render()', () => {
expect(scratch.innerHTML).to.equal('<div><foo><bar>test</bar></foo></div>');
});

it('should hydrate with dangerouslySetInnerHTML', () => {
it('should not hydrate with dangerouslySetInnerHTML', () => {
let html = '<b>foo &amp; bar</b>';
scratch.innerHTML = `<div>${html}</div>`;
// eslint-disable-next-line react/no-danger
render(<div dangerouslySetInnerHTML={{ __html: html }} />, scratch);

expect(scratch.firstChild).to.have.property('innerHTML', html);
expect(scratch.innerHTML).to.equal(`<div>${html}</div>`);
expect(scratch.firstChild).to.have.property('innerHTML', '');
expect(scratch.innerHTML).to.equal(`<div></div>`);
});

it('should avoid reapplying innerHTML when __html property of dangerouslySetInnerHTML attr remains unchanged', () => {
Expand Down

0 comments on commit f4e7d71

Please sign in to comment.