Issue #2347511 by davidhernandez, lauriii, rpayanm, kasn: Add method to test if class attribute has class on Attribute object.

8.0.x
Alex Pott 2014-11-07 12:21:55 -08:00
parent 4493d029c8
commit 1d8f7c546e
2 changed files with 33 additions and 0 deletions

View File

@ -188,6 +188,24 @@ class Attribute implements \ArrayAccess, \IteratorAggregate {
return $this;
}
/**
* Checks if the class array has the given CSS class.
*
* @param string $class
* The CSS class to check for.
*
* @return bool
* Returns TRUE if the class exists, or FALSE otherwise.
*/
public function hasClass($class) {
if (isset($this->storage['class']) && $this->storage['class'] instanceOf AttributeArray) {
return in_array($class, $this->storage['class']->value());
}
else {
return FALSE;
}
}
/**
* Implements the magic __toString() method.
*/

View File

@ -140,6 +140,21 @@ class AttributeTest extends UnitTestCase {
$this->assertEmpty((string) $attribute);
}
/**
* Tests checking for class names with the Attribute method.
* @covers ::hasClass()
*/
public function testHasClass() {
// Test an attribute without any classes.
$attribute = new Attribute();
$this->assertFalse($attribute->hasClass('a-class-nowhere-to-be-found'));
// Add a class to check for.
$attribute->addClass('we-totally-have-this-class');
// Check that this class exists.
$this->assertTrue($attribute->hasClass('we-totally-have-this-class'));
}
/**
* Tests removing class attributes with the Attribute helper methods.
* @covers ::removeClass()