-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Stringify <option> children #13465
Stringify <option> children #13465
Conversation
@@ -41,9 +42,10 @@ describe('ReactDOMOption', () => { | |||
expect(() => { | |||
node = ReactTestUtils.renderIntoDocument(el); | |||
}).toWarnDev( | |||
'<div> cannot appear as a child of <option>.\n' + ' in option (at **)', | |||
'Only strings and numbers are supported as <option> children.\n' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reused the same warning message we have in SSR. Seems to make more sense than the DOM nesting warning I tried to use before.
); | ||
expect(node.innerHTML).toBe('1 2'); | ||
expect(node.innerHTML).toBe('1 [object Object] 2'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the real change here. All cases where behavior changed were warnings.
@@ -61,6 +63,76 @@ describe('ReactDOMOption', () => { | |||
expect(node.innerHTML).toBe('1 2'); | |||
}); | |||
|
|||
it('should throw on object children', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just documenting existing behavior.
@@ -439,7 +439,7 @@ describe('ReactDOMServerIntegration', () => { | |||
); | |||
expect(e.getAttribute('value')).toBe(null); | |||
expect(e.getAttribute('defaultValue')).toBe(null); | |||
expect(e.firstChild.innerHTML).toBe('BarFooBaz'); | |||
expect(e.firstChild.innerHTML).toBe('BarFoo[object Object]Baz'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already a warning.
// is needlessly annoying. | ||
const ancestorInfo = updatedAncestorInfo(null, 'option'); | ||
validateDOMNesting(child.type, null, ancestorInfo); | ||
if (typeof child.type !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will avoid warnings for fbt.
ReactDOM: size: -0.0%, gzip: -0.1% Details of bundled changes.Comparing: 3661616...b97ac07 react-dom
Generated by 🚫 dangerJS |
Follow-up to #13261. We couldn't land this fix internally because we rely on element-like objects (our translation tooling) being stringified inside an
<option>
. That works in other cases (e.g.textarea
values), and worked in the past foroption
because it used to take the non-shouldSetTextContent
codepath. But that created other issues which #13261 solved.This changes
<option>
to actually stringify its children. Should be a tiny bit faster to avoid those checks too, and makes it consistent with<textarea>
. Note that in practice it already errors on objects. So the only thing that would get stringified are other React elements (e.g. a<span />
inside of an<option>
). Or things pretending to be elements.This is not a breaking change because nesting other elements into
<option>
is already unsupported, and we always showed a warning about that.