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

Stringify <option> children #13465

Merged
merged 1 commit into from Aug 22, 2018
Merged

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Aug 22, 2018

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 for option 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.

@@ -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' +
Copy link
Collaborator Author

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');
Copy link
Collaborator Author

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', () => {
Copy link
Collaborator Author

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');
Copy link
Collaborator Author

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') {
Copy link
Collaborator Author

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.

@pull-bot
Copy link

ReactDOM: size: -0.0%, gzip: -0.1%

Details of bundled changes.

Comparing: 3661616...b97ac07

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% +0.1% 649.31 KB 649.5 KB 152.4 KB 152.6 KB UMD_DEV
react-dom.production.min.js -0.0% -0.1% 95.42 KB 95.38 KB 31.21 KB 31.18 KB UMD_PROD
react-dom.development.js 0.0% +0.1% 645.45 KB 645.64 KB 151.28 KB 151.49 KB NODE_DEV
react-dom.production.min.js -0.0% -0.0% 95.41 KB 95.37 KB 30.92 KB 30.91 KB NODE_PROD
react-dom-server.browser.development.js -0.0% -0.0% 104.07 KB 104.02 KB 27.83 KB 27.83 KB UMD_DEV
react-dom-server.browser.production.min.js -0.3% -0.2% 15.37 KB 15.33 KB 5.88 KB 5.86 KB UMD_PROD
react-dom-server.browser.development.js -0.1% 0.0% 100.2 KB 100.15 KB 26.85 KB 26.85 KB NODE_DEV
react-dom-server.browser.production.min.js -0.3% -0.3% 15.27 KB 15.23 KB 5.83 KB 5.81 KB NODE_PROD
react-dom-server.node.development.js -0.1% 0.0% 102.12 KB 102.07 KB 27.38 KB 27.38 KB NODE_DEV
react-dom-server.node.production.min.js -0.3% -0.3% 16.08 KB 16.04 KB 6.13 KB 6.11 KB NODE_PROD
ReactDOM-dev.js 0.0% +0.1% 652.45 KB 652.67 KB 149.35 KB 149.5 KB FB_WWW_DEV
ReactDOM-prod.js -0.0% -0.0% 288.24 KB 288.17 KB 53.34 KB 53.32 KB FB_WWW_PROD
ReactDOMServer-dev.js -0.0% 0.0% 101.27 KB 101.25 KB 26.51 KB 26.51 KB FB_WWW_DEV
ReactDOMServer-prod.js -0.2% -0.3% 33.13 KB 33.06 KB 8.03 KB 8 KB FB_WWW_PROD
react-dom.profiling.min.js -0.0% -0.0% 96.6 KB 96.56 KB 31.32 KB 31.31 KB NODE_PROFILING
ReactDOM-profiling.js -0.0% -0.0% 290.59 KB 290.52 KB 53.95 KB 53.93 KB FB_WWW_PROFILING

Generated by 🚫 dangerJS

@gaearon gaearon merged commit 5cefd9b into facebook:master Aug 22, 2018
@gaearon gaearon deleted the stringify-option-children branch August 22, 2018 23:27
@gaearon gaearon mentioned this pull request Sep 5, 2018
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.

None yet

4 participants