[css-selectors] Substring Matching of Individual Class Names

Open
#2,984 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
css
Domain
frontend, web-dev

Research direction

Start with the linked Selectors Level 4 attribute-substrings and attribute-case sections, then review the proposed class and ID selector syntax and examples. Done would require an agreed syntax and specification treatment; the issue does not name implementation files or tests.

Written by the indexing model from the issue text.

Description

selectors-5

It's currently possible to match substrings of attribute values, including the class attribute.

https://www.w3.org/TR/selectors-4/#attribute-substrings

It would also be useful to have the ability to match substrings of individual class names. Using the existing attribute selector syntax as a reference, this might look something like:

.classname /* A standard class selector */

.[^class] /* Matches any class name that begins with "class" */
.[$name]  /* Matches any class name that ends with "name" */
.[*ass]   /* Matches any class name that contains the substring "ass" */

Which would be compatible with the Selectors Level 4 case-sensitivity syntax:

.[^classname i] 
.[$classname i] 
.[*classname i]

Also, it could be modified to support substring matching in ID selectors:

#[^example]  #[^example i] 
#[$example]  #[$example i] 
#[*example]  #[*example i]

Such a selector would make it practical to use a naming convention whereby a .Module parent shares common styles with a .SubModule child because although this can be achieved by grouping the related classes, it becomes unwieldy as the number of children increases, for example:

.Module, .SubModule, .AnotherModule, .YetAnotherModule {…}

:matches(.Module, .SubModule, .AnotherModule, .YetAnotherModule).variant {…}

Whereas, this could be rewritten in a simpler form using a substring matching class selector, so that there is no need to manage lists of dependants and style sharing is automatic:

.[$Module] {…}         /* Equivalent to .Module */
.[$Module].variant {…} /* Equivalent to .Module.variant */

.SubModule {…}         /* Inherits styles from .Module */
.SubModule.variant {…} /* Inherits styles from .Module.variant */

.ModuleUnrelated {…} /* Not matched as name begins not ends with "Module" */

The effect is static inheritance without the use of a preprocessor e.g. @extend in Sass and Stylus.

Dominant language
Bikeshed
Stars
4.9k
Forks
816
Avg merge
2d 18h
Merged PRs (30d)
24

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from w3c/csswg-drafts

All issues in w3c/csswg-drafts

Similar issues

More Web Dev issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.