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

NumberControl: Infinity isn't a valid value for min and max #69022

Open
2 of 6 tasks
afercia opened this issue Feb 4, 2025 · 8 comments · May be fixed by #69033
Open
2 of 6 tasks

NumberControl: Infinity isn't a valid value for min and max #69022

afercia opened this issue Feb 4, 2025 · 8 comments · May be fixed by #69033
Assignees
Labels
[Package] Components /packages/components [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@afercia
Copy link
Contributor

afercia commented Feb 4, 2025

Description

Discovered while investigating #68599 and similar issues.

By default, the NumberControl component and other components that extend it e.g. UnitControl > ValueInpu > NumberControl use Infinity and -Infinity for the min and max HTMl attributes values.

That is: the rendered HTML attribute values is:
max="Infinity"
min="-Infinity"

Those aren't valid values, it's invalid HTML.

I'm not sure what was the reasoning about using Infinity as default value. Shouldn't the max and min attributes be simply omitted to allow infinite values?

Reference:

https://html.spec.whatwg.org/multipage/input.html#the-min-and-max-attributes
https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number)

The min attribute, if specified, must have a value that is a valid floating-point number. The max attribute, if specified, must have a value that is a valid floating-point number.

Cc @WordPress/gutenberg-components

Step-by-step reproduction instructions

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@afercia afercia added [Package] Components /packages/components [Type] Bug An existing feature does not function as intended labels Feb 4, 2025
@hbhalodia
Copy link
Contributor

Hi @afercia, Looks like this was added while the component was initially being created - 91e624a, and from there only this values are being used.

But yes, we need an eye from @WordPress/gutenberg-components team on why we have added an Infinity and -Infinity options.

@mirka
Copy link
Member

mirka commented Feb 4, 2025

Looks like the Infinity values are used to calculate stuff in JS, which is fine, but yes I think they should be normalized to proper HTML attribute values when they're passed to the input element:

max={ max }
min={ min }

@Infinite-Null
Copy link
Contributor

Infinite-Null commented Feb 4, 2025

You’re right, @mirka! Using "Infinity" and "-Infinity" as HTML attribute values for min and max is invalid.

For an unrestricted numeric input, the best practices might be:

  1. To omit the min and max attributes entirely.
  2. We can use very large numbers to effectively remove practical restrictions, such as:
    • min="-999999999999" max="999999999999"
  3. Alternatively, using ±9007199254740991 aligns with JavaScript’s maximum safe integer values.

I think Both the second and third approaches seem to work well, but I’m a bit unsure about which one would be the best choice.

If these seem to work. I would like to work on this!

@mirka
Copy link
Member

mirka commented Feb 4, 2025

  1. To omit the min and max attributes entirely.

Why not the first approach, as @afercia suggested in the description? I think that would be the standard way to do it.

max={ max === Infinity ? undefined : max } or something like that (untested).

@Infinite-Null
Copy link
Contributor

Infinite-Null commented Feb 4, 2025

Missed that 😅. Thanks for the suggestion! I'll create a PR and test it.

@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Feb 4, 2025
@Infinite-Null
Copy link
Contributor

Hi @mirka, @afercia, and @hbhalodia,

I've created the PR implementing the solution and tested it. Would appreciate it if you could test and review the PR.

Thank you!

@afercia
Copy link
Contributor Author

afercia commented Feb 5, 2025

I'd agree they should be entirely omitted.

Looks like the Infinity values are used to calculate stuff in JS

@mirka not sure where the Infinity is actually used? I may be missing something here.

@mirka
Copy link
Member

mirka commented Feb 5, 2025

@afercia I was thinking about this part:

const constrainValue = (
value: number | string,
stepOverride?: number
) => {
// When step is "any" clamp the value, otherwise round and clamp it.
// Use '' + to convert to string for use in input value attribute.
return isStepAny
? '' + Math.min( max, Math.max( min, ensureNumber( value ) ) )
: '' + roundClamp( value, min, max, stepOverride ?? baseStep );
};

To be fair, it looks like roundClamp() can already handle undefined min/max values, but Math.min/Math.max cannot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants