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

Show warning on invalid value for option #22841

Conversation

mdanyalkhan
Copy link

@mdanyalkhan mdanyalkhan commented Nov 27, 2021

Summary

Reference: #11734
if value= {Symbol | Function} for an option, we ensure that it is ignored (with a warning displayed in dev) rather than have an error thrown onto the browser. Note that this is specific to SSR.

The reason why this occurred is because there was an attempt in converting value to a string at line 733, which would then throw if the type of value was a symbol or function. So to prevent this from happening, I re-used the function shouldRemoveAttributeWithWarning to firstly determine if the value was a symbol or function (or any other invalid type) before casting this into a string.

How did you test this change?

I tested this by setting up a simple react component as shown below, and utilised ReactDOMServer.renderToString to render the component and sent this to the client (effectively replicating SSR behaviour).

  export default () => {
    return (
      <>
        <label htmlFor="letters">Choose a letter:</label>

        <select>
          <option value={Symbol('test')}>A</option>
          <option value="B">B</option>
          <option value="C">C</option>
          <option value="D">D</option>
        </select>
      </>
    );
  };

Behaviour if using Main branch (further debug information that is provided as part of the error message has been omitted):
image

Behaviour with my changes:
image

Note that I did attempt to write a test (via the file ReactDOMServerIntegrationSelect-test) to confirm that the html option object's value property is empty if the developer specifies a symbol or function type. However, although this test would pass for client-side rendered html elements, this does not pass if rendered server side (even though no value attribute is explicitly attached to the option element). If rendered server-side and if the option's value attribute is queried, it looks like the option element will implicitly return its child text element if its value attribute is empty. So going with the markup above as an example, querying the value for the first option element will return "A" if rendered server side. This does bring to light some very subtle differences in behaviour both server and client-side that I'm not too sure is worth addressing in this PR.

Here's the test that I wrote below just as an FYI:

 itRenders(
    'a select option with no value if the initial value was a symbol type',
    async render => {
      const e = await render(
        <select readOnly={true}>
          <option value={Symbol('test')}>A</option>
        </select>,
        1
      );
      const option = e.options[0];
      expect(option.value).toBe('');
    }

@sizebot
Copy link

sizebot commented Nov 27, 2021

Comparing: 0cc724c...7f88d1c

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 130.01 kB 130.01 kB = 41.57 kB 41.57 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 134.77 kB 134.77 kB = 42.97 kB 42.97 kB
facebook-www/ReactDOM-prod.classic.js = 424.63 kB 424.63 kB = 78.24 kB 78.24 kB
facebook-www/ReactDOM-prod.modern.js = 413.19 kB 413.19 kB = 76.51 kB 76.51 kB
facebook-www/ReactDOMForked-prod.classic.js = 424.63 kB 424.63 kB = 78.25 kB 78.25 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +1.03% 31.95 kB 32.28 kB +0.89% 10.72 kB 10.81 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +1.03% 31.95 kB 32.28 kB +0.89% 10.72 kB 10.81 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +1.03% 32.08 kB 32.41 kB +0.90% 10.77 kB 10.87 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.production.min.js +1.03% 32.13 kB 32.46 kB +0.94% 10.84 kB 10.94 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.production.min.js +1.03% 32.13 kB 32.46 kB +0.94% 10.84 kB 10.94 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.production.min.js +1.02% 32.27 kB 32.60 kB +0.93% 10.89 kB 11.00 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.min.js +1.01% 32.60 kB 32.93 kB +0.83% 11.15 kB 11.24 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.min.js +1.01% 32.60 kB 32.93 kB +0.83% 11.15 kB 11.24 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.min.js +1.01% 32.74 kB 33.07 kB +0.82% 11.21 kB 11.30 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.production.min.js +1.01% 32.76 kB 33.09 kB +0.85% 11.27 kB 11.36 kB
oss-stable/react-dom/umd/react-dom-server.browser.production.min.js +1.01% 32.76 kB 33.09 kB +0.85% 11.27 kB 11.36 kB
oss-experimental/react-dom/umd/react-dom-server.browser.production.min.js +1.00% 32.90 kB 33.23 kB +0.83% 11.31 kB 11.41 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.min.js +0.93% 35.55 kB 35.88 kB +0.84% 11.95 kB 12.05 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.min.js +0.93% 35.55 kB 35.88 kB +0.84% 11.95 kB 12.05 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.min.js +0.92% 35.71 kB 36.04 kB +0.73% 12.12 kB 12.20 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.min.js +0.92% 35.71 kB 36.04 kB +0.73% 12.12 kB 12.20 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.min.js +0.92% 35.74 kB 36.07 kB +0.83% 12.02 kB 12.12 kB
facebook-www/ReactDOMServer-prod.modern.js +0.92% 74.85 kB 75.54 kB +0.96% 16.09 kB 16.25 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.min.js +0.92% 35.90 kB 36.23 kB +0.73% 12.18 kB 12.27 kB

Generated by 🚫 dangerJS against 7f88d1c

@martinbrose
Copy link

Closes #11734

Copy link

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Resolution: Stale Automatically closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants