Skip to content

Commit

Permalink
Merge pull request facebook#8520 from bvaughn/do-not-reset-inner-html…
Browse files Browse the repository at this point in the history
…-for-null-children

Do not reset inner html for null children
  • Loading branch information
bvaughn authored Dec 7, 2016
2 parents 343fb95 + bbdfc28 commit 9a31391
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
* should update arbitrary attributes for tags containing dashes
* should clear all the styles when removing `style`
* should update styles when `style` changes from null to object
* should not reset innerHTML for when children is null
* should reset innerHTML when switching from a direct text child to an empty child
* should empty element when removing innerHTML
* should transition from string content to innerHTML
* should transition from innerHTML to string content
Expand Down
26 changes: 26 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,32 @@ describe('ReactDOMComponent', () => {
expect(stubStyle.color).toEqual('red');
});

it('should not reset innerHTML for when children is null', () => {
var container = document.createElement('div');
ReactDOM.render(<div />, container);
container.firstChild.innerHTML = 'bonjour';
expect(container.firstChild.innerHTML).toEqual('bonjour');

ReactDOM.render(<div />, container);
expect(container.firstChild.innerHTML).toEqual('bonjour');
});

it('should reset innerHTML when switching from a direct text child to an empty child', () => {
const transitionToValues = [
null,
undefined,
false,
];
transitionToValues.forEach((transitionToValue) => {
var container = document.createElement('div');
ReactDOM.render(<div>bonjour</div>, container);
expect(container.firstChild.innerHTML).toEqual('bonjour');

ReactDOM.render(<div>{transitionToValue}</div>, container);
expect(container.firstChild.innerHTML).toEqual('');
});
});

it('should empty element when removing innerHTML', () => {
var container = document.createElement('div');
ReactDOM.render(<div dangerouslySetInnerHTML={{__html: ':)'}} />, container);
Expand Down
11 changes: 5 additions & 6 deletions src/renderers/shared/fiber/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,17 @@ module.exports = function<T, P, I, TI, C>(
const prevProps = current ? current.memoizedProps : null;
let nextChildren = nextProps.children;
const isDirectTextChild = shouldSetTextContent(nextProps);

if (isDirectTextChild) {
// We special case a direct text child of a host node. This is a common
// case. We won't handle it as a reified child. We will instead handle
// this in the host environment that also have access to this prop. That
// avoids allocating another HostText fiber and traversing it.
nextChildren = null;
} else if (prevProps && (
shouldSetTextContent(prevProps) ||
prevProps.children === null ||
typeof prevProps.children === 'undefined' ||
typeof prevProps.children === 'boolean'
)) {
} else if (
prevProps &&
shouldSetTextContent(prevProps)
) {
// If we're switching from a direct text child to a normal child, or to
// empty, we need to schedule the text content to be reset.
workInProgress.effectTag |= ContentReset;
Expand Down

0 comments on commit 9a31391

Please sign in to comment.