-
Notifications
You must be signed in to change notification settings - Fork 693
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
CSS Regex Selector #1010
Comments
This feature sounds like overkill; if one needs regular expressions to match element names, then something would seem to be wrong with the design of the language being styled. In the case of In the case of |
@patrickdark When you look at an html document from the perspective of a screenreader, heading tags are important to providing a logical structure. Even a site without a lot of content could under best practices go down to an h4. It is still useful even if not all decide to make websites accessible. I agree that regex is unnecessary (and think it might be possible hazardous). I think that simple pattern matching the same as attribute selectors have would be more than sufficient to solve this problem and when using a framework that prefixes their custom elements. |
@notoriousb1t I don't disagree that heading elements are useful, but the paradigm of...
...makes having numbered heading elements redundant for anything other than subheadings since the heading level can be inferred from the number of sections in which a heading element is nested. In those cases, In modern HTML, the only in case where numbered headings are useful are in And even the latter case I think shouldn't exist. It'd be better to infer subheadings via element nesting by doing something like repurposing the
|
@patrickdark I respectfully disagree, but I think we should discontinue this conversation in order to keep the issue on topic. |
The heading case could be solved with something like https://drafts.csswg.org/css-extensions/#custom-selectors The section case could be solved with attribute selectors: section[id^="card-"] {
padding: 10px;
border-radius: 10px;
background: white;
} I think RegExp would only add a lot of complexity for everyone. |
For some complex real cases, it could be useful, I had a case this afternoon, let's try to explain it simply: I want to select all elements that have I had to use: Problems with this targeting:
I agree: it is complex, not everyone will need it => but complex cases sometimes needs powerfull solutions. |
I'd recommend you to change your class system to have a |
@nico3333fr's specifity problem would be solved if attribute selectors could have multiple alternate values, e.g. |
Have you ever written automated tests? Not always see the problems from your point of view. |
Have you ever written automated tests? Not always see the problems from your point of view. |
@AndreasKarz If you have some specific use cases where you think regular expressions would greatly improve what we have so far, you should share them. I, for myself, also already thought about them being useful, though I can totally understand that implementors are reluctant to the idea of adding regular expressions to selectors because this would definitely come with some performance cost. The use cases I had so far mainly only required advanced attribute selectors similar to the ones @nico3333fr mentioned. Sebastian |
Sounds like a cool feature for a preprocessor: |
This would be nice to select all custom elements (containing hyphens). |
CSS is used in more contexts than simply writing stylesheets. It is commonly used to select specific elements to perform a function on - for example, in client side JS or in a CI testing flow. Regex attribute selection would, indeed, be useful in those contexts. (Especially since the person writing the JS/tests isn't necessarily the same person writing the CSS) |
Sorry to dig this up, I ran into a need for RegEx on and landed here. Could we imagine perhaps a CSS attribute selector that accepts a RegEx, instead of a way to match selectors in general? For example: [data-filters-sections#="(0|9)+"] {
opacity: 0.5;
}
/* I hate this example, too */
.col-wide-sm[class#="col-([0-9]|1[0-2])"] {
width: 100%;
} Two things:
Once again, sorry about the old issue revival, just wanted to share the idea, and I'm happy to create a new issue if that makes more sense! |
Regarding attribute value selectors, also see #354. |
following on from @chriskirknielsen's idea of limiting CSS-RegEx to attr selectors im currently trying to make an atomic CSS framework (mainly for personal use) that would have something like [border*="y-"][border*="blue"] {...}
[border*="x-"][border*="red"] {...} (I have others for n/s/e/w/a => north/south/east/west/all => block-start/block-end/inline-start/inline-end/all) Now extrapolate that to 7 edge selections, 5 border thicknesses, every border style, and however many colors I pass into my config, and dozens of other CSS properties... RegEx would solve my issue gracefully. below im using [border?=/\s?y-[^\s]+blue/] {...}
[border?=/\s?x-[^\s]+red/] {...} The selectors at the top of this post are a wonderful thing, they say "elements that have this and this", but we have no way to say "elements that have something containing this and this", no way to differentiate I understand CSS is a stylesheet, but looking at the spec and at the way we develop websites these days, we're heading more and more towards smarter CSS, if FORTRAN can have RegEx, why not CSS? |
Well, because there could be huge performance implications. I am also curious about this and so have landed here. As mentioned above, the main use case is flexible, non-semantic naming schemes. For example, preprocessors have ways to generate many CSS rules programmatically. Regular expressions with capturing group support could handle this natively (although this is not equivalent, as explained below). Whether it is in-scope is debatable at best and depends on implementation feasibility. Example use case: [class*="line-clamp"] {
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
}
/*
* New attribute selector with regex support on the RHS only,
* since the main use-case is based on classes - not attributes
*/
[class=/line-clamp-(\d+)/] {
-webkit-line-clamp: $1;
} Pros vs. Preprocessor Programmatic Generation
Cons
|
This would resolve #1008.
It would be very nice to have the ability to do a regex selector on a tag, like this:
and so on. Better yet, allow regex to be used in classes, etc.
This would be an extremely helpful feature of CSS that would help developers write lean, beautiful code and I would love to see it added to the spec!
The text was updated successfully, but these errors were encountered: