diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php index 75bb62f806c22d7783b9d30f472f25db2f874374..97a14ecaf1d32edfb1fa97d0656f03434eb89060 100644 --- a/core/lib/Drupal/Component/Utility/Html.php +++ b/core/lib/Drupal/Component/Utility/Html.php @@ -61,13 +61,15 @@ class Html { * Do not pass one string containing multiple classes as they will be * incorrectly concatenated with dashes, i.e. "one two" will become "one-two". * - * @param string $class - * The class name to clean. + * @param mixed $class + * The class name to clean. It can be a string or anything that can be cast + * to string. * * @return string * The cleaned class name. */ public static function getClass($class) { + $class = (string) $class; if (!isset(static::$classes[$class])) { static::$classes[$class] = static::cleanCssIdentifier(Unicode::strtolower($class)); } diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php index 593ebc5c4835db29861ae308c7952310fe2e9954..46283421373f8e6d97ec5cf8a5217e862b6ff10e 100644 --- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\Component\Utility; +use Drupal\Component\Render\MarkupInterface; +use Drupal\Component\Render\MarkupTrait; use Drupal\Component\Utility\Html; use Drupal\Tests\UnitTestCase; @@ -87,7 +89,12 @@ public function providerTestCleanCssIdentifier() { */ public function testHtmlClass() { // Verify Drupal coding standards are enforced. - $this->assertSame(Html::getClass('CLASS NAME_[Ü]'), 'class-name--ü', 'Enforce Drupal coding standards.'); + $this->assertSame('class-name--ü', Html::getClass('CLASS NAME_[Ü]'), 'Enforce Drupal coding standards.'); + + // Test Html::getClass() handles Drupal\Component\Render\MarkupInterface + // input. + $markup = HtmlTestMarkup::create('CLASS_FROM_OBJECT'); + $this->assertSame('class-from-object', Html::getClass($markup), 'Markup object is converted to CSS class.'); } /** @@ -390,3 +397,11 @@ public function providerTestTransformRootRelativeUrlsToAbsoluteAssertion() { } } + +/** + * Marks an object's __toString() method as returning markup. + */ +class HtmlTestMarkup implements MarkupInterface { + use MarkupTrait; + +}