Skip to content
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

No whitespace warning #7081

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
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 ?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will skip valid parent check for all children deemed whitespace. Not just elements in <table>

null : isTagValidWithParent(childTag, parentTag) ? null : parentInfo;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should avoid nested ternary operator

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__) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@syranide - We now check for dev mode before adding this property

// validateDOMNesting uses this:
this._isWhitespace = typeof text === 'string' && text.trim().length === 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, since this might be somewhat of a hot path. Have you benchmarked this against REGEX.test(text)? With REGEX being a constant and not inline.

}
};

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 @@ -735,7 +735,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 @@ -1226,21 +1226,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