diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index e9ede3b..408ea1f 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -18,7 +18,7 @@ * $attributes = new Attribute(array('id' => 'socks')); * $attributes['class'] = array('black-cat', 'white-cat'); * $attributes['class'][] = 'black-white-cat'; - * echo ''; + * echo ''; * // Produces * @endcode * @@ -27,7 +27,7 @@ * $attributes = new Attribute(array('id' => 'socks')); * $attributes['class'] = array('black-cat', 'white-cat'); * $attributes['class'][] = 'black-white-cat'; - * echo ''; + * echo ''; * // Produces * @endcode */ @@ -65,6 +65,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); } @@ -74,13 +86,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; } /** @@ -104,9 +110,9 @@ public function __toString() { $return = ''; foreach ($this->storage as $name => $value) { if (!$value->printed()) { - $rendered = is_object($value) ? $value->render() : (String::checkPlain($name) . ' = "' . String::checkPlain($value) . '"'); + $rendered = $value->render(); if ($rendered) { - $return .= " $rendered"; + $return .= ' ' . $rendered; } } } @@ -118,9 +124,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; } } diff --git a/core/lib/Drupal/Core/Template/AttributeArray.php b/core/lib/Drupal/Core/Template/AttributeArray.php index a32201e..4f1875f 100644 --- a/core/lib/Drupal/Core/Template/AttributeArray.php +++ b/core/lib/Drupal/Core/Template/AttributeArray.php @@ -15,13 +15,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 * @@ -67,7 +67,7 @@ public function offsetExists($offset) { */ public function __toString() { $this->printed = TRUE; - return implode(' ', array_map(array('Drupal\Component\Utility\String', 'checkPlain'), $this->value)); + return String::checkPlain(implode(' ', $this->value)); } /** diff --git a/core/lib/Drupal/Core/Template/AttributeBoolean.php b/core/lib/Drupal/Core/Template/AttributeBoolean.php index ecd1dcf..839eaf0 100644 --- a/core/lib/Drupal/Core/Template/AttributeBoolean.php +++ b/core/lib/Drupal/Core/Template/AttributeBoolean.php @@ -8,6 +8,8 @@ namespace Drupal\Core\Template; use Drupal\Component\Utility\String; +use Drupal\Component\Utility\String; + /** * A class that defines a type of boolean HTML attribute. * @@ -17,12 +19,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/lib/Drupal/Core/Template/AttributeValueBase.php b/core/lib/Drupal/Core/Template/AttributeValueBase.php index df187df..b06255e 100644 --- a/core/lib/Drupal/Core/Template/AttributeValueBase.php +++ b/core/lib/Drupal/Core/Template/AttributeValueBase.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Template; +use Drupal\Component\Utility\String; + /** * Defines the base class for an attribute type. * @@ -53,7 +55,7 @@ public function __construct($name, $value) { * The string representation of the attribute. */ public function render() { - return $this->name . '="' . $this . '"'; + return String::checkPlain($this->name) . '="' . $this . '"'; } /** diff --git a/core/tests/Drupal/Tests/Core/Common/AttributesTest.php b/core/tests/Drupal/Tests/Core/Common/AttributesTest.php index 997c050..5bc5ab3 100644 --- a/core/tests/Drupal/Tests/Core/Common/AttributesTest.php +++ b/core/tests/Drupal/Tests/Core/Common/AttributesTest.php @@ -31,9 +31,13 @@ public static function getInfo() { public function providerTestAttributeData() { return array( // Verify that special characters are HTML encoded. + array(array('&"\'<>' => 'value'), ' &"'<>="value"', 'HTML encode attribute names.'), array(array('title' => '&"\'<>'), ' title="&"'<>"', 'HTML encode attribute values.'), // Verify multi-value attributes are concatenated with spaces. array(array('class' => array('first', 'last')), ' class="first last"', 'Concatenate multi-value attributes.'), + // Verify boolean attribute values are rendered correctly. + array(array('disabled' => TRUE), ' disabled', 'Boolean attribute is rendered.'), + array(array('disabled' => FALSE), '', 'Boolean attribute is not rendered.'), // Verify empty attribute values are rendered. array(array('alt' => ''), ' alt=""', 'Empty attribute value #1.'), array(array('alt' => NULL), ' alt=""', 'Empty attribute value #2.'),