diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index 076cfd3..b84cc79 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Template; - /** * A class that can be used for collecting then rendering HTML attributtes. * @@ -17,7 +16,7 @@ * $attributes = new Attribute(array('id' => 'socks')); * $attributes['class'] = array('black-cat', 'white-cat'); * $attributes['class'][] = 'black-white-cat'; - * echo ''; + * echo ''; * // Produces * @endcode * @@ -26,7 +25,7 @@ * $attributes = new Attribute(array('id' => 'socks')); * $attributes['class'] = array('black-cat', 'white-cat'); * $attributes['class'][] = 'black-white-cat'; - * echo ''; + * echo ''; * // Produces * @endcode */ @@ -64,6 +63,18 @@ public function offsetGet($name) { * Implements ArrayAccess::offsetSet(). */ public function offsetSet($name, $value) { + $this->storage[$name] = $this->createAttributeValue($name, $value); + } + +/** + * Creates the different types of attribute values. + * @param string $name + * The attribute name. + * @param mixed $value + * The attribute value. + * @return object + */ + protected function createAttributeValue($name, $value) { if (is_array($value)) { $value = new AttributeArray($name, $value); } @@ -73,13 +84,7 @@ public function offsetSet($name, $value) { elseif (!is_object($value)) { $value = new AttributeString($name, $value); } - // The $name could be NULL. - if (isset($name)) { - $this->storage[$name] = $value; - } - else { - $this->storage[] = $value; - } + return $value; } /** @@ -103,9 +108,9 @@ public function __toString() { $return = ''; foreach ($this->storage as $name => $value) { if (!$value->printed()) { - $rendered = is_object($value) ? $value->render() : (check_plain($name) . ' = "' . check_plain($value) . '"'); + $rendered = $value->render(); if ($rendered) { - $return .= " $rendered"; + $return .= ' ' . $rendered; } } } @@ -117,9 +122,7 @@ public function __toString() { */ public function __clone() { foreach ($this->storage as $name => $value) { - if (is_object($value)) { - $this->storage[$name] = clone $value; - } + $this->storage[$name] = clone $value; } } @@ -129,12 +132,4 @@ public function __clone() { public function getIterator() { return new \ArrayIterator($this->storage); } - - /** - * Returns the whole array. - */ - public function value() { - return $this->value; - } - } diff --git a/core/lib/Drupal/Core/Template/AttributeArray.php b/core/lib/Drupal/Core/Template/AttributeArray.php index d3521e0..a4d1d12 100644 --- a/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -14,13 +14,13 @@ * To use with Attribute, the array must be specified. * Correct: * @code - * $attributes = new Attribute(array()); + * $attributes = new Attribute(); * $attributes['class'] = array(); * $attributes['class'][] = 'cat'; * @endcode * Incorrect: * @code - * $attributes = new Attribute(array()); + * $attributes = new Attribute(); * $attributes['class'][] = 'cat'; * @endcode * @@ -66,7 +66,7 @@ public function offsetExists($offset) { */ public function __toString() { $this->printed = TRUE; - return implode(' ', array_map('check_plain', $this->value)); + return check_plain(implode(' ', $this->value)); } /** diff --git a/core/lib/Drupal/Core/Template/AttributeBoolean.php b/core/lib/Drupal/Core/Template/AttributeBoolean.php index 48ca388..7bfc527 100644 --- a/core/lib/Drupal/Core/Template/AttributeBoolean.php +++ b/core/lib/Drupal/Core/Template/AttributeBoolean.php @@ -16,12 +16,12 @@ * * To set a boolean attribute on the Attribute class, set it to TRUE. * @code - * $attributes = new Attribute(array()); + * $attributes = new Attribute(); * $attributes['disabled'] = TRUE; - * echo '; * $attributes['disabled'] = FALSE; - * echo '; * @endcode * diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/AttributesUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/AttributesUnitTest.php index 7b5305d..c28f675 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/AttributesUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/AttributesUnitTest.php @@ -27,6 +27,7 @@ public static function getInfo() { */ function testDrupalAttributes() { // Verify that special characters are HTML encoded. + $this->assertIdentical((string) new Attribute(array('&"\'<>' => 'value')), ' &"'<>="value"', 'HTML encode attribute names.'); $this->assertIdentical((string) new Attribute(array('title' => '&"\'<>')), ' title="&"'<>"', 'HTML encode attribute values.'); // Verify multi-value attributes are concatenated with spaces. @@ -37,6 +38,10 @@ function testDrupalAttributes() { $this->assertIdentical((string) new Attribute(array('alt' => '')), ' alt=""', 'Empty attribute value #1.'); $this->assertIdentical((string) new Attribute(array('alt' => NULL)), ' alt=""', 'Empty attribute value #2.'); + // Verify boolean attribute values are rendered correctly. + $this->assertIdentical((string) new Attribute(array('disabled' => TRUE)), ' disabled', 'Boolean attribute is rendered.'); + $this->assertIdentical((string) new Attribute(array('disabled' => FALSE)), '', 'Boolean attribute is not rendered.'); + // Verify multiple attributes are rendered. $attributes = array( 'id' => 'id-test',