react-a11y icon indicating copy to clipboard operation
react-a11y copied to clipboard

hasChildTextNode doesn’t handle numeric children

Open ticky opened this issue 10 years ago • 4 comments

The code at assertions.js#L34-L35 doesn’t handle numeric children and throws an uncaught error trying to access properties of the number object in these instances.

This is a seemingly common thing in the wild. Things like react-data-components emit elements whose only children are numbers.

I’m happy to provide a PR, but not sure what the best option is here, but it may be to handle numeric children as strings. That said, this perhaps brings up a question about how “implicit” string conversions should be handled.

ticky avatar Mar 30 '15 00:03 ticky

Upon further reading, this seems to be handled (just not in the way I’d personally expect) by #10.

ticky avatar Mar 30 '15 00:03 ticky

Can you expand on what you expected? Further, not sure what value there would be in making assertions against numeric children.

kloots avatar May 13 '15 18:05 kloots

More or less a matter of laziness. If you render a number in a react component, it’s generally displayed as text.

While I don’t consider myself an expert, and can’t be entirely certain there would be no negative effects effects, I’d suggest a good approach would be to update the tests in that file so we only check;

  1. if the child is null or undefined
  2. if the child is an image
  3. if the child is a React component and has children

Then if it hasn’t matched any of those, treat the child as a string. As far as I’m aware, anything which didn’t fit those checks would be treated as such by React, anyway.

Alternately, react-a11y could mandate that strings must be strings, requiring lots of toString() calls in existing code.

ticky avatar May 14 '15 00:05 ticky

Thanks for following up. Why don't you put together a PR representing the change you'd like to see so we can review it?

kloots avatar May 21 '15 03:05 kloots