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

Ignore text nodes in childrenOfInstInternal #604

Merged
merged 2 commits into from
Jan 10, 2017

Conversation

aweary
Copy link
Collaborator

@aweary aweary commented Sep 22, 2016

Resolves #603

Currently React will return null for _renderedChildren if the only child is a text node. But if there is an actual DOM node as well, it will return that along with any sibling text nodes.

This change ignores text nodes when calling wrapper.childen() with mount. With shallow, no instances are actually needed when parsing children, so this works already.

 const wrapper = shallow(<div>B<span />C</div>);
 const children = wrapper.children();
// children = ['B', <span/>, 'C']

So there's some inconsistency there. I think it makes more sense to let shallow return text nodes, but we could ignore them if we want consistency.

cc @ljharb

it('should not attempt to get an instance for text nodes', () => {
const wrapper = mount(<div>B<span />C</div>);
const children = wrapper.children();
expect(children.length).to.equal(1);
Copy link
Member

@ljharb ljharb Sep 22, 2016

Choose a reason for hiding this comment

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

i agree it shouldn't attempt to get an instance - but the div has 3 children, not 1.

Copy link
Collaborator Author

@aweary aweary Sep 22, 2016

Choose a reason for hiding this comment

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

That's inconsistent though. Even before this change, mounting an element with a single text node child returns 0 children.

      const wrapper = mount(<div>A</div>);
      wrapper.children().length
      // 0

Copy link
Member

Choose a reason for hiding this comment

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

hmm - that's confusing to me. However, if the current behavior is that text nodes are ignored, then your change is consistent with that - so, LGTM

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah it didn't make a lot of sense to me either. React is being a little inconsistent here, I'm going to look into it more on that side of things.

Copy link
Contributor

Choose a reason for hiding this comment

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

@aweary did you learn anything about the React side of things?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@lencioni _renderedChildren is null if the only child is a string node. Otherwise they're represented as ReactDOMTextComponent instances. I'm still not sure if there's a good reason for this.

@lencioni
Copy link
Contributor

@aweary mind giving this a rebase when you have a moment?

@lencioni
Copy link
Contributor

BTW, the lint error you see in CI here looks like it was fixed by c07e61f, so a rebase onto master should resolve that for you.

@aweary aweary force-pushed the fix-text-children branch from a8a1399 to f53a63e Compare October 11, 2016 16:27
@aweary
Copy link
Collaborator Author

aweary commented Oct 11, 2016

I'm actually hesitant to merge this just yet, I'd like to see if it's reasonable for _renderedChildren to return a single instance of ReactDOMTextComponent instead of null, so we don't have to run the wrong direction with this inconsistency.

@juhoha
Copy link

juhoha commented Oct 21, 2016

Any idea on ETA?

@aweary
Copy link
Collaborator Author

aweary commented Nov 12, 2016

@spicyj would you mind weighing in on this? _renderedChildren is null when rendering single text nodes, but that text node is represented as an instance of ReactDOMTextComponent if the text node has siblings.

Is this something we could address internally with React? I know we're using internal APIs that are not supported, but I'm curious what you think.

@colinthesealion
Copy link

Hoping to see this merged soon. As it is I'm having to merge this branch manually to make enzyme useable.

@colinthesealion
Copy link

The merge of master into fix-text-children has broken MountedTraversal.js because of the new means of traversing renderedchildren introduced here: 08f2f77

@@ -121,6 +121,9 @@ export function childrenOfInstInternal(inst) {
if (REACT013 && !node.getPublicInstance) {
return false;
}
if (typeof renderedChildren[key]._stringText !== 'undefined') {

Choose a reason for hiding this comment

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

The master branch is no longer iterating over the keys, and thus key is undefined here. Should be if (typeof node._stringText !== 'undefined') {

@aweary
Copy link
Collaborator Author

aweary commented Jan 9, 2017

Alright, I'm going to fix the conflicts and then I think we should just merge. Even if we were to get React to fix this behavior moving forward, we still have to support versions with the current behavior.

This solution isn't ideal, but I think it's better to consistent with how we treat text nodes, even if React isn't.

Brandon Dail and others added 2 commits January 9, 2017 17:39
@juhoha
Copy link

juhoha commented Jan 10, 2017

@aweary: Incredible timing. Been stalking this PR for months. Gave up yesterday and published a fork to npm. Any idea when 2.7.1 gets released?

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