Skip to content

[css-selectors] add numeric comparisons on attribute values #5890

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

Open
liamquin opened this issue Jan 25, 2021 · 6 comments
Open

[css-selectors] add numeric comparisons on attribute values #5890

liamquin opened this issue Jan 25, 2021 · 6 comments
Labels

Comments

@liamquin
Copy link

Can we have numeric comparisons?

e.g.
[data-balance<0] { color: red; } (common in accounting/finance, along with putting ( ) around the value)
[data-eyes>7.9] { color: black; background-colour: yellow } /* spider! */

with < <= > >=

Noting that testing for equality is string equality so testing 06=6 fails, an alternative design might be to calc() to fetch an attribute value, and to be allowed in selectors, but that's more complex.

Maybe a new section inhttps://drafts.csswg.org/selectors/#attribute-selectors for Numeric Attribute Values?

@Loirooriol
Copy link
Contributor

There is already #354, which has various proposals like this. But not sure if it's better to split them among multiple subissues, like this one.

@liamquin
Copy link
Author

Thanks @Loirooriol - i don't know why i didn't find 354 when i was searching. On the other hand maybe considering numeric values by themselves might make them easier to get adopted.

@bramus
Copy link
Contributor

bramus commented Sep 12, 2022

(Cross-post from #354)

To numerically compare attribute values today, authors can work around this by:

  1. Injecting all the values that the item is not equal to, using data-* attributes.
  2. Using the CSS Attribute “Value Contains” Selector (e.g. [attribute~=value]) on those data-* attributes.

I’ve detailed this nasty and cumbersome hack back in the day over at https://www.bram.us/2016/10/13/css-attribute-value-less-than-greater-than-equals-selectors/.

@romainmenke
Copy link
Member

Can a flag like i help?

  • [foo="bar" i] matches cases insensitively
  • [foo="1e1" n] switch to numeric comparison. (matches <div foo="10">, <div foo="010"> <div foo="10.00">)

@Loirooriol
Copy link
Contributor

It's not much clear how n would work with ^=, $=, etc.
And it could cover some usecases, but it doesn't address inequalities < <= > >=

@tabatkins
Copy link
Member

It's not much clear how n would work with ^=, $=, etc.

I assume it wouldn't. Using it would be a syntax error.

And it could cover some usecases, but it doesn't address inequalities < <= > >=

Right, inequalities are vital for this use-case, so this wouldn't really help.

A flag doesn't really add anything anyway - attribute selectors currently require the value to be an ident or a string, so numbers are invalid. Thus [foo=5] would already unambiguous as a numeric comparison rather than a string comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants