commit 631e9c072dbd16030ea18c44ec5c23ad7fa17db0 Author: Joel Pittet Date: Thu Jul 31 21:53:53 2014 -0700 addClass/removeClass added and a few more tests, no more class.add and class.remove diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index ffef99a..6f5a779 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -117,23 +117,65 @@ public function offsetExists($name) { } /** - * Adds the argument values to the class element. + * Adds the argument values by merging them on to the value array. + * + * @return \Drupal\Core\Template\Attribute + * The attribute with the class arguments added. */ public function addClass() { $args = func_get_args(); + $classes = array(); foreach ($args as $arg) { - $this->storage['class']->add($arg); + // Merge the values passed in from the classes array. + // The argument is cast to an array to support comma separated single + // values or one or more array arguments. + $classes = array_merge($classes, (array) $arg); + } + // var_dump($classes); + + // Merge if there are values, just add them otherwise. + if (isset($this->storage['class']) && $this->storage['class'] instanceOf AttributeArray && !empty($this->storage['class']->value())) { + // Merge the values passed in from the class value array. + $classes = array_merge($this->storage['class']->value(), $classes); + // Filter out any empty values. + $classes = array_filter($classes); + $this->storage['class']->exchangeArray($classes); + } + else { + // Filter out any empty values. + $classes = array_filter($classes); + $this->offsetSet('class', $classes); } + + // var_dump($this->storage['class']); + return $this; } /** - * Removes the argument values from the class element. + * Removes the argument values from the value array. + * + * @return \Drupal\Core\Template\Attribute + * The attribute with the class arguments removed. */ public function removeClass() { - $args = func_get_args(); - foreach ($args as $arg) { - $this->storage['class']->remove($arg); + // With no class attribute, there is no need to remove. + if (isset($this->storage['class']) && $this->storage['class'] instanceOf AttributeArray) { + $args = func_get_args(); + $classes = array(); + foreach ($args as $arg) { + // Merge the values passed in from the classes array. + // The argument is cast to an array to support comma separated single + // values or one or more array arguments. + $classes = array_merge($classes, (array) $arg); + } + + // Remove the values passed in from the value array. + $classes = array_diff($this->storage['class']->value(), $classes); + // Filter out any empty values. + $classes = array_filter($classes); + $this->storage['class']->exchangeArray($classes); } + return $this; } /** diff --git a/core/lib/Drupal/Core/Template/AttributeArray.php b/core/lib/Drupal/Core/Template/AttributeArray.php index 277a9bb..a204f4b 100644 --- a/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -70,44 +70,6 @@ public function __toString() { } /** - * Adds the argument values by merging them on to the value array. - * - * @param mixed $value - * A value, or array of values, to add to the array. - * - * @return \Drupal\Core\Template\AttributeArray - * The attribute array with the arguments added. - */ - public function add($value) { - // Merge the values passed in from the value array. - // The argument is cast to an array to support comma separated single - // values or one or more array arguments. - $this->value = array_merge($this->value, (array) $value); - // Filter out any empty values. - $this->value = array_filter($this->value); - return $this; - } - - /** - * Removes the argument values from the value array. - * - * @param mixed $value - * A value, or array of values, to remove from the array. - * - * @return \Drupal\Core\Template\AttributeArray - * The attribute array with the arguments removed. - */ - public function remove($value) { - // Remove the values passed in from the value array. - // The argument is cast to an array to support comma separated single - // values or one or more array arguments. - $this->value = array_diff($this->value, (array) $value); - // Filter out any empty values. - $this->value = array_filter($this->value); - return $this; - } - - /** * Implements IteratorAggregate::getIterator(). */ public function getIterator() { @@ -121,4 +83,21 @@ public function value() { return $this->value; } + /** + * Exchange the array for another one. + * + * @see ArrayObject::exchangeArray + * + * @param array $input + * The array input to replace the internal value. + * + * @return array + * The old array value. + */ + public function exchangeArray($input) { + $old = $this->value; + $this->value = $input; + return $old; + } + } diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index b8fc41d..b73321e 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -57,23 +57,30 @@ public function testRemove() { $this->assertFalse(isset($attribute['class'])); } - /** * Tests adding class attributes with the AttributeArray helper method. + * @covers ::addClass() */ public function testAddClasses() { - $attribute = new Attribute(array('class' => array('example-class'))); + + // Add empty Attribute object with no classes. + $attribute = new Attribute(); + // Add one class. + $attribute->addClass('banana'); + + var_dump($attribute); + $this->assertEquals(new AttributeArray('class', array('banana')), $attribute['class']); // Add one class. $attribute->addClass('aa'); - $this->assertEquals(new AttributeArray('class', array('example-class', 'aa')), $attribute['class']); + $this->assertEquals(new AttributeArray('class', array('banana', 'aa')), $attribute['class']); // Add multiple classes. $attribute->addClass('xx', 'yy'); $this->assertEquals( new AttributeArray( 'class', - array('example-class', 'aa', 'xx', 'yy')), + array('banana', 'aa', 'xx', 'yy')), $attribute['class'] ); @@ -82,22 +89,19 @@ public function testAddClasses() { $this->assertEquals( new AttributeArray( 'class', - array('example-class', 'aa', 'xx', 'yy', 'red', 'green', 'blue') + array('banana', 'aa', 'xx', 'yy', 'red', 'green', 'blue') ), $attribute['class'] ); - } /** * Tests removing class attributes with the AttributeArray helper method. + * @covers ::removeClass() */ public function testRemoveClasses() { - $attribute = new Attribute( - array( - 'class' => array('example-class', 'aa', 'xx', 'yy', 'red', 'green', 'blue'), - ) - ); + $classes = array('example-class', 'aa', 'xx', 'yy', 'red', 'green', 'blue'); + $attribute = new Attribute(array('class' => $classes)); // Remove one class. $attribute->removeClass('example-class'); @@ -114,13 +118,20 @@ public function testRemoveClasses() { /** * Tests removing class attributes with the AttributeArray helper method. + * @covers ::removeClass() + * @covers ::addClass() */ public function testChainAddRemoveClasses() { $attribute = new Attribute( array('class' => array('example-class', 'red', 'green', 'blue')) ); - unset($attribute['class']); - $this->assertFalse(isset($attribute['class'])); + + $attribute + ->removeClass(array('red', 'green', 'pink')) + ->addClass(array('apple', 'lime', 'grapefruit')) + ->addClass(array('banana')); + $expected = array('example-class', 'blue', 'apple', 'lime', 'grapefruit', 'banana'); + $this->assertArrayEquals($expected, $attribute['class']->value(), 'Attributes chained'); } /**