Skip to content

Commit

Permalink
Fixes yiisoft#653: use boolean type to indicate boolean attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Jul 24, 2013
1 parent 1f5d777 commit 915a3cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 45 deletions.
51 changes: 7 additions & 44 deletions framework/yii/helpers/HtmlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,6 @@ class HtmlBase
'track' => 1,
'wbr' => 1,
);
/**
* @var array list of boolean attributes. The presence of a boolean attribute on
* an element represents the true value, and the absence of the attribute represents the false value.
* @see http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
*/
public static $booleanAttributes = array(
'async' => 1,
'autobuffer' => 1,
'autofocus' => 1,
'autoplay' => 1,
'checked' => 1,
'controls' => 1,
'declare' => 1,
'default' => 1,
'defer' => 1,
'disabled' => 1,
'formnovalidate' => 1,
'hidden' => 1,
'ismap' => 1,
'itemscope' => 1,
'loop' => 1,
'multiple' => 1,
'muted' => 1,
'nohref' => 1,
'noresize' => 1,
'novalidate' => 1,
'open' => 1,
'pubdate' => 1,
'readonly' => 1,
'required' => 1,
'reversed' => 1,
'scoped' => 1,
'seamless' => 1,
'selected' => 1,
'typemustmatch' => 1,
);
/**
* @var array the preferred order of attributes in a tag. This mainly affects the order of the attributes
* that are rendered by [[renderAttributes()]].
Expand Down Expand Up @@ -450,7 +414,7 @@ public static function input($type, $name = null, $value = null, $options = arra
{
$options['type'] = $type;
$options['name'] = $name;
$options['value'] = $value;
$options['value'] = $value === null ? null : (string)$value;
return static::tag('input', '', $options);
}

Expand Down Expand Up @@ -1322,7 +1286,7 @@ public static function renderSelectOptions($selection, $items, &$tagOptions = ar
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
} else {
$attrs = isset($options[$key]) ? $options[$key] : array();
$attrs['value'] = $key;
$attrs['value'] = (string)$key;
$attrs['selected'] = $selection !== null &&
(!is_array($selection) && !strcmp($key, $selection)
|| is_array($selection) && in_array($key, $selection));
Expand All @@ -1335,12 +1299,11 @@ public static function renderSelectOptions($selection, $items, &$tagOptions = ar

/**
* Renders the HTML tag attributes.
* Boolean attributes such as s 'checked', 'disabled', 'readonly', will be handled specially
* according to [[booleanAttributes]] and [[showBooleanAttributeValues]].
* Attributes whose values are of boolean type will be treated as [boolean attributes](http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes).
* And attributes whose values are null will not be rendered.
* @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the rendering result.
* @return string the rendering result. If the attributes are not empty, they will be rendered
* into a string with a leading white space (such that it can be directly appended to the tag name
* into a string with a leading white space (so that it can be directly appended to the tag name
* in a tag. If there is no attribute, an empty string will be returned.
*/
public static function renderTagAttributes($attributes)
Expand All @@ -1357,8 +1320,8 @@ public static function renderTagAttributes($attributes)

$html = '';
foreach ($attributes as $name => $value) {
if (isset(static::$booleanAttributes[strtolower($name)])) {
if ($value || strcasecmp($name, $value) === 0) {
if (is_bool($value)) {
if ($value) {
$html .= " $name";
}
} elseif ($value !== null) {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/framework/helpers/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function testRenderAttributes()
{
$this->assertEquals('', Html::renderTagAttributes(array()));
$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => true, 'disabled' => true, 'hidden' => false)));
}

public function testAddCssClass()
Expand Down

0 comments on commit 915a3cd

Please sign in to comment.