Add drupal_css_class() to clean class names and rename form_clean_id

Created on 17 May 2009, about 15 years ago
Updated 31 January 2024, 5 months ago

Right now, core uses form_clean_id() in a few places to clean perspective class names before inserting them into the html. But form_clean_id() is the wrong function to clean class names because it enforces a uniqueness to them by appending _0, _1, etc if form_clean_id() has been previously called with the same input.

http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid class names and IDs.

In CSS, identifiers (including element names, classes, and IDs in selectors):

  1. can contain only the characters [a-zA-Z0-9] (U+0030 - U+0039, U+0041 - U+005A, U+0061 - U+007A) and ISO 10646 characters U+00A1 and higher, plus the hyphen (U+002D) and the underscore (U+005F)
  2. cannot start with a digit, or a hyphen followed by a digit.
  3. can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

I'm all for ignoring the 3rd part of that spec (too messy), but we should definitely implement the first 2 parts.

In addition to the validity of classes, we should also look to Drupalโ€™s code style. We currently don't use underscores in class names, so this new function should replace them with dashes by default, but allow callers to relax that restriction if they have a use case (like integrating with 3rd party code.)

โœจ Feature request
Status

Fixed

Version

7.0 โšฐ๏ธ

Component
Themeย  โ†’

Last updated 6 minutes ago

Created by

๐Ÿ‡น๐Ÿ‡ผTaiwan JohnAlbin Taipei, Taiwan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024