Commit d2a5fe79 authored by lauriii's avatar lauriii

Issue #2837013 by benjifisher, leslieg, alexpott: clean_class Twig filter does...

Issue #2837013 by benjifisher, leslieg, alexpott: clean_class Twig filter does not work with Views rewriting
parent 10db6623
...@@ -61,13 +61,15 @@ class Html { ...@@ -61,13 +61,15 @@ class Html {
* Do not pass one string containing multiple classes as they will be * Do not pass one string containing multiple classes as they will be
* incorrectly concatenated with dashes, i.e. "one two" will become "one-two". * incorrectly concatenated with dashes, i.e. "one two" will become "one-two".
* *
* @param string $class * @param mixed $class
* The class name to clean. * The class name to clean. It can be a string or anything that can be cast
* to string.
* *
* @return string * @return string
* The cleaned class name. * The cleaned class name.
*/ */
public static function getClass($class) { public static function getClass($class) {
$class = (string) $class;
if (!isset(static::$classes[$class])) { if (!isset(static::$classes[$class])) {
static::$classes[$class] = static::cleanCssIdentifier(Unicode::strtolower($class)); static::$classes[$class] = static::cleanCssIdentifier(Unicode::strtolower($class));
} }
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Drupal\Tests\Component\Utility; namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Render\MarkupTrait;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
...@@ -87,7 +89,12 @@ public function providerTestCleanCssIdentifier() { ...@@ -87,7 +89,12 @@ public function providerTestCleanCssIdentifier() {
*/ */
public function testHtmlClass() { public function testHtmlClass() {
// Verify Drupal coding standards are enforced. // 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() { ...@@ -390,3 +397,11 @@ public function providerTestTransformRootRelativeUrlsToAbsoluteAssertion() {
} }
} }
/**
* Marks an object's __toString() method as returning markup.
*/
class HtmlTestMarkup implements MarkupInterface {
use MarkupTrait;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment