function Html::cleanCssIdentifier

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::cleanCssIdentifier()
  2. 10 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::cleanCssIdentifier()
  3. 11.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::cleanCssIdentifier()

Prepares a string for use as a CSS identifier (element, class, or ID name).

Link below shows the syntax for valid CSS identifiers (including element names, classes, and IDs in selectors).

Parameters

string $identifier: The identifier to clean.

array $filter: An array of string replacements to use on the identifier.

Return value

string The cleaned identifier.

See also

http://www.w3.org/TR/CSS21/syndata.html#characters

22 calls to Html::cleanCssIdentifier()
ExposedFormRenderTest::testExposedFormRender in core/modules/views/tests/src/Kernel/Plugin/ExposedFormRenderTest.php
Tests the exposed form markup.
ExposedFormTest::getExpectedExposedFormId in core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php
Returns a views exposed form ID.
FieldPluginBase::elementClasses in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Returns the class of the field.
FieldPluginBase::elementLabelClasses in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Returns the class of the field's label.
FieldPluginBase::elementWrapperClasses in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Returns the class of the field's wrapper.

... See full list

File

core/lib/Drupal/Component/Utility/Html.php, line 95

Class

Html
Provides DOMDocument helpers for parsing and serializing HTML strings.

Namespace

Drupal\Component\Utility

Code

public static function cleanCssIdentifier($identifier, array $filter = [
    ' ' => '-',
    '_' => '-',
    '/' => '-',
    '[' => '-',
    ']' => '',
]) {
    // We could also use strtr() here but its much slower than str_replace(). In
    // order to keep '__' to stay '__' we first replace it with a different
    // placeholder after checking that it is not defined as a filter.
    $double_underscore_replacements = 0;
    if (!isset($filter['__'])) {
        $identifier = str_replace('__', '##', $identifier, $double_underscore_replacements);
    }
    $identifier = str_replace(array_keys($filter), array_values($filter), $identifier);
    // Replace temporary placeholder '##' with '__' only if the original
    // $identifier contained '__'.
    if ($double_underscore_replacements > 0) {
        $identifier = str_replace('##', '__', $identifier);
    }
    // Valid characters in a CSS identifier are:
    // - the hyphen (U+002D)
    // - a-z (U+0030 - U+0039)
    // - A-Z (U+0041 - U+005A)
    // - the underscore (U+005F)
    // - 0-9 (U+0061 - U+007A)
    // - ISO 10646 characters U+00A1 and higher
    // We strip out any character not in the above list.
    $identifier = preg_replace('/[^\\x{002D}\\x{0030}-\\x{0039}\\x{0041}-\\x{005A}\\x{005F}\\x{0061}-\\x{007A}\\x{00A1}-\\x{FFFF}]/u', '', $identifier);
    // Identifiers cannot start with a digit, two hyphens, or a hyphen followed by a digit.
    $identifier = preg_replace([
        '/^[0-9]/',
        '/^(-[0-9])|^(--)/',
    ], [
        '_',
        '__',
    ], $identifier);
    return $identifier;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.