Skip to content

[Fiber] Fix to render falsy value as dangerouslySetInnerHTML - #8652

Merged
gaearon merged 2 commits into
react:masterfrom
koba04:fix-render-dangerouslySetInnerHTML
Jan 11, 2017
Merged

gaearon merged 2 commits into
react:masterfrom
koba04:fix-render-dangerouslySetInnerHTML

Conversation

@koba04

@koba04 koba04 commented Dec 29, 2016 •

Copy link
Copy Markdown
Contributor

This is to fix a bug which is unable to render falsy values(0 or '' or false) as dangerouslySetInnerHTML.

var nextHtml = nextProp ? nextProp[HTML] : undefined;
var lastHtml = lastProp ? lastProp[HTML] : undefined;
if (nextHtml) {
if (nextHtml || nextHtml === 0 || nextHtml === '') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do a typeof instead? that is what we have done in other places where we need to detect numbers / strings. I am not sure this works with negative values

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That makes sense. I'll update it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it.

@koba04
koba04 force-pushed the fix-render-dangerouslySetInnerHTML branch from 5493a36 to f344590 Compare December 29, 2016 13:44
var lastHtml = lastProp ? lastProp[HTML] : undefined;
if (nextHtml) {
if (
nextHtml ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need this check now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay 👍

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about false? Does Stack render that?

@gaearon
gaearon requested a review from acdlite December 30, 2016 01:24
acdlite
acdlite previously requested changes Jan 6, 2017
var lastHtml = lastProp ? lastProp[HTML] : undefined;
if (nextHtml) {
if (
nextHtml ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about false? Does Stack render that?

expect(container.textContent).toEqual('10');
});

it('should render dangerouslySetInnerHTML', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this test in ReactDOMComponent-test.js next to the other dangrouslySetInnerHTML tests. And give it a more specific name, like "can dangerouslySetInnerHTML with falsy value."

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for moving the test. Can you remove the ones in this file now? We don't need duplicates because ReactDOMComponent-test is run in both Stack and Fiber mode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acdlite OK, fixed it. I've added the tests using ReactDOM.render instead of _createContentMarkup into ReactDOMComponent-test.

@koba04
koba04 force-pushed the fix-render-dangerouslySetInnerHTML branch from f344590 to afc208f Compare January 6, 2017 04:35
@koba04

koba04 commented Jan 6, 2017

Copy link
Copy Markdown
Contributor Author

@acdlite Thank you for your review! I've put the tests for falsy values in ReactDOMComponent-test.js and fixed the behavior for false. In stack, dangrouslySetInnerHTML={{_html: false}} is rendered as false.

var nextHtml = nextProp ? nextProp[HTML] : undefined;
var lastHtml = lastProp ? lastProp[HTML] : undefined;
if (nextHtml) {
if (lastHtml) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this condition still need? It seems to be unnecessary because it is covered by if (lastHtml !== nextHtml).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm idk why that branch was there. Seems ok to remove it.

@koba04
koba04 force-pushed the fix-render-dangerouslySetInnerHTML branch from afc208f to 46d3e81 Compare January 10, 2017 01:22
@gaearon
gaearon dismissed acdlite’s stale review January 11, 2017 21:16

concerns were addressed

@gaearon

gaearon commented Jan 11, 2017

Copy link
Copy Markdown
Collaborator

This looks good, thank you for fixing!

@koba04

koba04 commented Jan 12, 2017

Copy link
Copy Markdown
Contributor Author

Thank you!

@koba04
koba04 deleted the fix-render-dangerouslySetInnerHTML branch January 12, 2017 00:51
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
)

* [Fiber] Fix to render falsy value as dangerouslySetInnerHTML

* Add more cases and rename test for clarity
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
)

* [Fiber] Fix to render falsy value as dangerouslySetInnerHTML

* Add more cases and rename test for clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants