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

Fix false positives for "never" in selector-attribute-quotes #4300

Closed
thybzi opened this issue Sep 24, 2019 · 7 comments · Fixed by #6571
Closed

Fix false positives for "never" in selector-attribute-quotes #4300

thybzi opened this issue Sep 24, 2019 · 7 comments · Fixed by #6571
Labels
good first issue is good for newcomers status: ready to implement is ready to be worked on by someone type: bug a problem with a feature or rule

Comments

@thybzi
Copy link

thybzi commented Sep 24, 2019

What is the problem you're trying to solve?

We try avoid any unnecessary quotes in selectors, so we use "selector-attribute-quotes": "never".

But some selectors really require quotes to be valid.

div[style='*'] { ... }
li[data-id='42'] { ... }

And here is why:

According to the spec http://www.w3.org/TR/CSS2/selector.html#attribute-selectors:

Attribute values must be identifiers or strings

Furthermore, according to the spec http://www.w3.org/TR/CSS2/syndata.html#value-def-identifier:

identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.

And, according to the spec http://www.w3.org/TR/CSS2/syndata.html#strings:

Strings can either be written with double quotes or with single quotes.

What solution would you like to see?

A new option for selector-attribute-quotes (e.g. "only-required" or "only-necessary"), which would work like "never", but allow/require quotes when the correspondent part of selector:

  • contains chars other than latin letters, digits, hyphen and underscore
  • starts with a digit
  • starts with double hyphen
  • starts with a hyphen followed by a digit
@alexander-akait alexander-akait added type: enhancement a new feature that isn't related to rules type: new option a new option for an existing rule labels Sep 24, 2019
@alexander-akait
Copy link
Member

Mark as enhancement

@jeddy3
Copy link
Member

jeddy3 commented Jan 11, 2020

@thybzi Thanks for the suggestion.

SGTM as there's precedent for this in font-family-name-quotes "always-where-required" option.

Labelling as help wanted. You're welcome to submit a PR if you like. Most of the answers should already been in the font-family-name-quotes source.

@jeddy3 jeddy3 changed the title Add new selector-attribute-quotes option (e.g. "only-required") Add "always-where-required" to selector-attribute-quotes Jan 11, 2020
@jeddy3 jeddy3 added status: ready to implement is ready to be worked on by someone and removed type: enhancement a new feature that isn't related to rules labels Jan 11, 2020
@jeddy3 jeddy3 added good first issue is good for newcomers type: bug a problem with a feature or rule and removed type: new option a new option for an existing rule labels Jan 19, 2022
@jeddy3 jeddy3 changed the title Add "always-where-required" to selector-attribute-quotes Fix false positives for "never" in selector-attribute-quotes Jan 19, 2022
@jeddy3
Copy link
Member

jeddy3 commented Jan 19, 2022

Revisiting this. It sounds like the never option produces invalid CSS. Rather than add a new option, let's fix the never option so that it doesn't removes quotes when the part of the selector:

  • contains chars other than latin letters, digits, hyphen and underscore
  • starts with a digit
  • starts with double hyphen
  • starts with a hyphen followed by a digit

I've labelled the issue as ready to implement. Please consider contributing if you have time.

There are steps on how to fix a bug in a rule in the Developer guide.

@mattxwang
Copy link
Member

Hey all, I gave a quick spin to the suggested proposal in #6571. I just wanted to clarify the expected behaviour - after implementing the logic enumerated by @jeddy3, some existing tests for the "never" option fail - namely, those involving attributes with quotes within them:

[href="te'st"] { }
[href='te"st'] { }
[href='te\'s\'t'] { }
[href="te\"s\"t"] { }

Currently, the new implementation skips these; the current implementation of the rule flags a warning. To me, it seems like the rule shouldn't remove these quotes, since the resulting attribute value wouldn't be a valid identifier. Wanted to make sure that this wasn't necessarily a regression (since it does change the rule behaviour).

If we agree, I can then write a few more tests to capture other behaviour and mark the PR as ready to review!

@jeddy3
Copy link
Member

jeddy3 commented Jan 11, 2023

To me, it seems like the rule shouldn't remove these quotes,

SGTM, it seems safer not to remove the outer quotes if there are inner quotes (escaped or otherwise).

@mattxwang
Copy link
Member

Thanks for your patience everyone, have updated #6571 with what I believe is now the desired behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue is good for newcomers status: ready to implement is ready to be worked on by someone type: bug a problem with a feature or rule
Development

Successfully merging a pull request may close this issue.

5 participants