Skip to content

Commit

Permalink
Remove DOM nesting warning for whitespace
Browse files Browse the repository at this point in the history
  * Identify if content of ReactDOMTextComponent is only whitespace
  * Fixed misc. lint errors
  • Loading branch information
hkal committed Jun 23, 2016
1 parent 416b5ef commit 28614a6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/renderers/dom/client/validateDOMNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ if (__DEV__) {
var parentInfo = ancestorInfo.current;
var parentTag = parentInfo && parentInfo.tag;

var invalidParent =
isTagValidWithParent(childTag, parentTag) ? null : parentInfo;
var invalidParent = childInstance._isWhitespace ?
null : isTagValidWithParent(childTag, parentTag) ? null : parentInfo;
var invalidAncestor =
invalidParent ? null : findInvalidAncestorForTag(childTag, ancestorInfo);
var problematic = invalidParent || invalidAncestor;
Expand Down
5 changes: 5 additions & 0 deletions src/renderers/dom/shared/ReactDOMTextComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ var ReactDOMTextComponent = function(text) {
this._mountIndex = 0;
this._closingComment = null;
this._commentNodes = null;

if (__DEV__) {
// validateDOMNesting uses this:
this._isWhitespace = typeof text === 'string' && text.trim().length === 0;
}
};

Object.assign(ReactDOMTextComponent.prototype, {
Expand Down
22 changes: 15 additions & 7 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ describe('ReactDOMComponent', function() {
});

it('should work error event on <source> element', function() {
spyOn(console, 'error');
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(
<video>
Expand Down Expand Up @@ -1215,21 +1215,29 @@ describe('ReactDOMComponent', function() {
});
var Foo = React.createClass({
render: function() {
return <table><Row /> </table>;
return <table><Row /></table>;
},
});
ReactTestUtils.renderIntoDocument(<Foo />);

expect(console.error.calls.count()).toBe(2);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: validateDOMNesting(...): <tr> cannot appear as a child of ' +
'<table>. See Foo > table > Row > tr. Add a <tbody> to your code to ' +
'match the DOM tree generated by the browser.'
);
expect(console.error.calls.argsFor(1)[0]).toBe(
'Warning: validateDOMNesting(...): #text cannot appear as a child ' +
'of <table>. See Foo > table > #text.'
);
});

it('does not warn for arbitrary table whitespace', () => {
spyOn(console, 'error');
var Foo = React.createClass({
render: function() {
return <table><tbody><tr /> </tbody></table>;
},
});
ReactTestUtils.renderIntoDocument(<Foo />);

expect(console.error.calls.count()).toBe(0);
});

it('gives useful context in warnings', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/native/NativeMethodsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,6 @@ function mountSafeCallback(
}
return callback.apply(context, arguments);
};
};
}

module.exports = NativeMethodsMixin;
2 changes: 1 addition & 1 deletion src/renderers/shared/__tests__/ReactPerf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,5 @@ describe('ReactPerf', function() {
expect(console.error.calls.count()).toBe(1);

__DEV__ = true;
})
});
});

0 comments on commit 28614a6

Please sign in to comment.