Commit 2422a134 by Qiang Xue

Refactored Html.

parent a5ababe4
...@@ -153,15 +153,15 @@ class Html ...@@ -153,15 +153,15 @@ class Html
* @param string $name the tag name * @param string $name the tag name
* @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded. * @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded.
* If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks. * If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the element attributes. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the element attributes. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated HTML tag * @return string the generated HTML tag
* @see beginTag * @see beginTag
* @see endTag * @see endTag
*/ */
public static function tag($name, $content = '', $attributes = array()) public static function tag($name, $content = '', $tagAttributes = array())
{ {
$html = '<' . $name . static::renderAttributes($attributes); $html = '<' . $name . static::renderTagAttributes($tagAttributes);
if (isset(static::$voidElements[strtolower($name)])) { if (isset(static::$voidElements[strtolower($name)])) {
return $html . (static::$closeVoidElements ? ' />' : '>'); return $html . (static::$closeVoidElements ? ' />' : '>');
} else { } else {
...@@ -172,15 +172,15 @@ class Html ...@@ -172,15 +172,15 @@ class Html
/** /**
* Generates a start tag. * Generates a start tag.
* @param string $name the tag name * @param string $name the tag name
* @param array $attributes the element attributes. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the element attributes. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated start tag * @return string the generated start tag
* @see endTag * @see endTag
* @see tag * @see tag
*/ */
public static function beginTag($name, $attributes = array()) public static function beginTag($name, $tagAttributes = array())
{ {
return '<' . $name . static::renderAttributes($attributes) . '>'; return '<' . $name . static::renderTagAttributes($tagAttributes) . '>';
} }
/** /**
...@@ -208,76 +208,76 @@ class Html ...@@ -208,76 +208,76 @@ class Html
/** /**
* Generates a style tag. * Generates a style tag.
* @param string $content the style content * @param string $content the style content
* @param array $attributes the attributes of the style tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the style tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* If the attributes does not contain "type", a default one with value "text/css" will be used. * If the attributes does not contain "type", a default one with value "text/css" will be used.
* @return string the generated style tag * @return string the generated style tag
*/ */
public static function style($content, $attributes = array()) public static function style($content, $tagAttributes = array())
{ {
if (!isset($attributes['type'])) { if (!isset($tagAttributes['type'])) {
$attributes['type'] = 'text/css'; $tagAttributes['type'] = 'text/css';
} }
return static::tag('style', "/*<![CDATA[*/\n{$content}\n/*]]>*/", $attributes); return static::tag('style', "/*<![CDATA[*/\n{$content}\n/*]]>*/", $tagAttributes);
} }
/** /**
* Generates a script tag. * Generates a script tag.
* @param string $content the script content * @param string $content the script content
* @param array $attributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* If the attributes does not contain "type", a default one with value "text/javascript" will be used. * If the attributes does not contain "type", a default one with value "text/javascript" will be used.
* @return string the generated script tag * @return string the generated script tag
*/ */
public static function script($content, $attributes = array()) public static function script($content, $tagAttributes = array())
{ {
if (!isset($attributes['type'])) { if (!isset($tagAttributes['type'])) {
$attributes['type'] = 'text/javascript'; $tagAttributes['type'] = 'text/javascript';
} }
return static::tag('script', "/*<![CDATA[*/\n{$content}\n/*]]>*/", $attributes); return static::tag('script', "/*<![CDATA[*/\n{$content}\n/*]]>*/", $tagAttributes);
} }
/** /**
* Generates a link tag that refers to an external CSS file. * Generates a link tag that refers to an external CSS file.
* @param array|string $url the URL of the external CSS file. This parameter will be processed by [[url()]]. * @param array|string $url the URL of the external CSS file. This parameter will be processed by [[url()]].
* @param array $attributes the attributes of the link tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the link tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated link tag * @return string the generated link tag
* @see url * @see url
*/ */
public static function cssFile($url, $attributes = array()) public static function cssFile($url, $tagAttributes = array())
{ {
$attributes['rel'] = 'stylesheet'; $tagAttributes['rel'] = 'stylesheet';
$attributes['type'] = 'text/css'; $tagAttributes['type'] = 'text/css';
$attributes['href'] = static::url($url); $tagAttributes['href'] = static::url($url);
return static::tag('link', '', $attributes); return static::tag('link', '', $tagAttributes);
} }
/** /**
* Generates a script tag that refers to an external JavaScript file. * Generates a script tag that refers to an external JavaScript file.
* @param string $url the URL of the external JavaScript file. This parameter will be processed by [[url()]]. * @param string $url the URL of the external JavaScript file. This parameter will be processed by [[url()]].
* @param array $attributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated script tag * @return string the generated script tag
* @see url * @see url
*/ */
public static function jsFile($url, $attributes = array()) public static function jsFile($url, $tagAttributes = array())
{ {
$attributes['type'] = 'text/javascript'; $tagAttributes['type'] = 'text/javascript';
$attributes['src'] = static::url($url); $tagAttributes['src'] = static::url($url);
return static::tag('script', '', $attributes); return static::tag('script', '', $tagAttributes);
} }
/** /**
* Generates a form start tag. * Generates a form start tag.
* @param array|string $action the form action URL. This parameter will be processed by [[url()]]. * @param array|string $action the form action URL. This parameter will be processed by [[url()]].
* @param string $method form method, either "post" or "get" (case-insensitive) * @param string $method form method, either "post" or "get" (case-insensitive)
* @param array $attributes the attributes of the form tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the form tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated form start tag. * @return string the generated form start tag.
* @see endForm * @see endForm
*/ */
public static function beginForm($action = '', $method = 'post', $attributes = array()) public static function beginForm($action = '', $method = 'post', $tagAttributes = array())
{ {
$action = static::url($action); $action = static::url($action);
...@@ -295,9 +295,9 @@ class Html ...@@ -295,9 +295,9 @@ class Html
$action = substr($action, 0, $pos); $action = substr($action, 0, $pos);
} }
$attributes['action'] = $action; $tagAttributes['action'] = $action;
$attributes['method'] = $method; $tagAttributes['method'] = $method;
$form = static::beginTag('form', $attributes); $form = static::beginTag('form', $tagAttributes);
if ($hiddens !== array()) { if ($hiddens !== array()) {
$form .= "\n" . implode("\n", $hiddens); $form .= "\n" . implode("\n", $hiddens);
} }
...@@ -323,17 +323,17 @@ class Html ...@@ -323,17 +323,17 @@ class Html
* @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[url()]] * @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[url()]]
* and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute * and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute
* will not be generated. * will not be generated.
* @param array $attributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated hyperlink * @return string the generated hyperlink
* @see url * @see url
*/ */
public static function a($text, $url = null, $attributes = array()) public static function a($text, $url = null, $tagAttributes = array())
{ {
if ($url !== null) { if ($url !== null) {
$attributes['href'] = static::url($url); $tagAttributes['href'] = static::url($url);
} }
return static::tag('a', $text, $attributes); return static::tag('a', $text, $tagAttributes);
} }
/** /**
...@@ -343,29 +343,29 @@ class Html ...@@ -343,29 +343,29 @@ class Html
* it to prevent XSS attacks. * it to prevent XSS attacks.
* @param string $email email address. If this is null, the first parameter (link body) will be treated * @param string $email email address. If this is null, the first parameter (link body) will be treated
* as the email address and used. * as the email address and used.
* @param array $attributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated mailto link * @return string the generated mailto link
*/ */
public static function mailto($text, $email = null, $attributes = array()) public static function mailto($text, $email = null, $tagAttributes = array())
{ {
return static::a($text, 'mailto:' . ($email === null ? $text : $email), $attributes); return static::a($text, 'mailto:' . ($email === null ? $text : $email), $tagAttributes);
} }
/** /**
* Generates an image tag. * Generates an image tag.
* @param string $src the image URL. This parameter will be processed by [[url()]]. * @param string $src the image URL. This parameter will be processed by [[url()]].
* @param array $attributes the attributes of the image tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the image tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated image tag * @return string the generated image tag
*/ */
public static function img($src, $attributes = array()) public static function img($src, $tagAttributes = array())
{ {
$attributes['src'] = static::url($src); $tagAttributes['src'] = static::url($src);
if (!isset($attributes['alt'])) { if (!isset($tagAttributes['alt'])) {
$attributes['alt'] = ''; $tagAttributes['alt'] = '';
} }
return static::tag('img', null, $attributes); return static::tag('img', null, $tagAttributes);
} }
/** /**
...@@ -375,14 +375,14 @@ class Html ...@@ -375,14 +375,14 @@ class Html
* it to prevent XSS attacks. * it to prevent XSS attacks.
* @param string $for the ID of the HTML element that this label is associated with. * @param string $for the ID of the HTML element that this label is associated with.
* If this is null, the "for" attribute will not be generated. * If this is null, the "for" attribute will not be generated.
* @param array $attributes the attributes of the label tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the label tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated label tag * @return string the generated label tag
*/ */
public static function label($content, $for = null, $attributes = array()) public static function label($content, $for = null, $tagAttributes = array())
{ {
$attributes['for'] = $for; $tagAttributes['for'] = $for;
return static::tag('label', $content, $attributes); return static::tag('label', $content, $tagAttributes);
} }
/** /**
...@@ -392,19 +392,19 @@ class Html ...@@ -392,19 +392,19 @@ class Html
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks. * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* If the attributes does not contain "type", a default one with value "button" will be used. * If the attributes does not contain "type", a default one with value "button" will be used.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function button($name = null, $value = null, $content = 'Button', $attributes = array()) public static function button($name = null, $value = null, $content = 'Button', $tagAttributes = array())
{ {
$attributes['name'] = $name; $tagAttributes['name'] = $name;
$attributes['value'] = $value; $tagAttributes['value'] = $value;
if (!isset($attributes['type'])) { if (!isset($tagAttributes['type'])) {
$attributes['type'] = 'button'; $tagAttributes['type'] = 'button';
} }
return static::tag('button', $content, $attributes); return static::tag('button', $content, $tagAttributes);
} }
/** /**
...@@ -414,14 +414,14 @@ class Html ...@@ -414,14 +414,14 @@ class Html
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks. * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated submit button tag * @return string the generated submit button tag
*/ */
public static function submitButton($name = null, $value = null, $content = 'Submit', $attributes = array()) public static function submitButton($name = null, $value = null, $content = 'Submit', $tagAttributes = array())
{ {
$attributes['type'] = 'submit'; $tagAttributes['type'] = 'submit';
return static::button($name, $value, $content, $attributes); return static::button($name, $value, $content, $tagAttributes);
} }
/** /**
...@@ -431,14 +431,14 @@ class Html ...@@ -431,14 +431,14 @@ class Html
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks. * you should consider [[encode()]] it to prevent XSS attacks.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated reset button tag * @return string the generated reset button tag
*/ */
public static function resetButton($name = null, $value = null, $content = 'Reset', $attributes = array()) public static function resetButton($name = null, $value = null, $content = 'Reset', $tagAttributes = array())
{ {
$attributes['type'] = 'reset'; $tagAttributes['type'] = 'reset';
return static::button($name, $value, $content, $attributes); return static::button($name, $value, $content, $tagAttributes);
} }
/** /**
...@@ -446,94 +446,94 @@ class Html ...@@ -446,94 +446,94 @@ class Html
* @param string $type the type attribute. * @param string $type the type attribute.
* @param string $name the name attribute. If it is null, the name attribute will not be generated. * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated input tag * @return string the generated input tag
*/ */
public static function input($type, $name = null, $value = null, $attributes = array()) public static function input($type, $name = null, $value = null, $tagAttributes = array())
{ {
$attributes['type'] = $type; $tagAttributes['type'] = $type;
$attributes['name'] = $name; $tagAttributes['name'] = $name;
$attributes['value'] = $value; $tagAttributes['value'] = $value;
return static::tag('input', null, $attributes); return static::tag('input', null, $tagAttributes);
} }
/** /**
* Generates an input button. * Generates an input button.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function buttonInput($name, $value = 'Button', $attributes = array()) public static function buttonInput($name, $value = 'Button', $tagAttributes = array())
{ {
return static::input('button', $name, $value, $attributes); return static::input('button', $name, $value, $tagAttributes);
} }
/** /**
* Generates a submit input button. * Generates a submit input button.
* @param string $name the name attribute. If it is null, the name attribute will not be generated. * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function submitInput($name = null, $value = 'Submit', $attributes = array()) public static function submitInput($name = null, $value = 'Submit', $tagAttributes = array())
{ {
return static::input('submit', $name, $value, $attributes); return static::input('submit', $name, $value, $tagAttributes);
} }
/** /**
* Generates a reset input button. * Generates a reset input button.
* @param string $name the name attribute. If it is null, the name attribute will not be generated. * @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function resetInput($name = null, $value = 'Reset', $attributes = array()) public static function resetInput($name = null, $value = 'Reset', $tagAttributes = array())
{ {
return static::input('reset', $name, $value, $attributes); return static::input('reset', $name, $value, $tagAttributes);
} }
/** /**
* Generates a text input field. * Generates a text input field.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function textInput($name, $value = null, $attributes = array()) public static function textInput($name, $value = null, $tagAttributes = array())
{ {
return static::input('text', $name, $value, $attributes); return static::input('text', $name, $value, $tagAttributes);
} }
/** /**
* Generates a hidden input field. * Generates a hidden input field.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function hiddenInput($name, $value = null, $attributes = array()) public static function hiddenInput($name, $value = null, $tagAttributes = array())
{ {
return static::input('hidden', $name, $value, $attributes); return static::input('hidden', $name, $value, $tagAttributes);
} }
/** /**
* Generates a password input field. * Generates a password input field.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function passwordInput($name, $value = null, $attributes = array()) public static function passwordInput($name, $value = null, $tagAttributes = array())
{ {
return static::input('password', $name, $value, $attributes); return static::input('password', $name, $value, $tagAttributes);
} }
/** /**
...@@ -543,36 +543,36 @@ class Html ...@@ -543,36 +543,36 @@ class Html
* can be obtained via $_FILES[$name] (see PHP documentation). * can be obtained via $_FILES[$name] (see PHP documentation).
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated. * @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function fileInput($name, $value = null, $attributes = array()) public static function fileInput($name, $value = null, $tagAttributes = array())
{ {
return static::input('file', $name, $value, $attributes); return static::input('file', $name, $value, $tagAttributes);
} }
/** /**
* Generates a text area input. * Generates a text area input.
* @param string $name the input name * @param string $name the input name
* @param string $value the input value. Note that it will be encoded using [[encode()]]. * @param string $value the input value. Note that it will be encoded using [[encode()]].
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. * Attributes whose value is null will be ignored and not put in the tag returned.
* @return string the generated text area tag * @return string the generated text area tag
*/ */
public static function textarea($name, $value = '', $attributes = array()) public static function textarea($name, $value = '', $tagAttributes = array())
{ {
$attributes['name'] = $name; $tagAttributes['name'] = $name;
return static::tag('textarea', static::encode($value), $attributes); return static::tag('textarea', static::encode($value), $tagAttributes);
} }
/** /**
* Generates a radio button input. * Generates a radio button input.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param boolean $checked whether the radio button should be checked. * @param boolean $checked whether the radio button should be checked.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param string $value the value attribute. If it is null, the value attribute will not be rendered.
* Attributes whose value is null will be ignored and not put in the tag returned. The following attribute * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. The following attributes
* will be specially handled and not put in the resulting tag: * will be specially handled and not put in the resulting tag:
* *
* - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute * - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute
...@@ -581,26 +581,27 @@ class Html ...@@ -581,26 +581,27 @@ class Html
* *
* @return string the generated radio button tag * @return string the generated radio button tag
*/ */
public static function radio($name, $value = '1', $checked = false, $attributes = array()) public static function radio($name, $checked = false, $value = '1', $tagAttributes = array())
{ {
$attributes['checked'] = $checked; $tagAttributes['checked'] = $checked;
if (isset($attributes['uncheck'])) { $tagAttributes['value'] = $value;
if (isset($tagAttributes['uncheck'])) {
// add a hidden field so that if the radio button is not selected, it still submits a value // add a hidden field so that if the radio button is not selected, it still submits a value
$hidden = static::hiddenInput($name, $attributes['uncheck']); $hidden = static::hiddenInput($name, $tagAttributes['uncheck']);
unset($attributes['uncheck']); unset($tagAttributes['uncheck']);
} else { } else {
$hidden = ''; $hidden = '';
} }
return $hidden . static::input('radio', $name, $value, $attributes); return $hidden . static::input('radio', $name, $value, $tagAttributes);
} }
/** /**
* Generates a checkbox input. * Generates a checkbox input.
* @param string $name the name attribute. * @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param boolean $checked whether the checkbox should be checked. * @param boolean $checked whether the checkbox should be checked.
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * @param string $value the value attribute. If it is null, the value attribute will not be rendered.
* Attributes whose value is null will be ignored and not put in the tag returned. The following attribute * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* Attributes whose value is null will be ignored and not put in the tag returned. The following attributes
* will be specially handled and not put in the resulting tag: * will be specially handled and not put in the resulting tag:
* *
* - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute * - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute
...@@ -609,22 +610,24 @@ class Html ...@@ -609,22 +610,24 @@ class Html
* *
* @return string the generated checkbox tag * @return string the generated checkbox tag
*/ */
public static function checkbox($name, $value = '1', $checked = false, $attributes = array()) public static function checkbox($name, $checked = false, $value = '1', $tagAttributes = array())
{ {
$attributes['checked'] = $checked; $tagAttributes['checked'] = $checked;
if (isset($attributes['uncheck'])) { $tagAttributes['value'] = $value;
if (isset($tagAttributes['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value // add a hidden field so that if the checkbox is not selected, it still submits a value
$hidden = static::hiddenInput($name, $attributes['uncheck']); $hidden = static::hiddenInput($name, $tagAttributes['uncheck']);
unset($attributes['uncheck']); unset($tagAttributes['uncheck']);
} else { } else {
$hidden = ''; $hidden = '';
} }
return $hidden . static::input('checkbox', $name, $value, $attributes); return $hidden . static::input('checkbox', $name, $value, $tagAttributes);
} }
/** /**
* Generates a drop-down list. * Generates a drop-down list.
* @param string $name the input name * @param string $name the input name
* @param string $selection the selected value
* @param array $items the option data items. The array keys are option values, and the array values * @param array $items the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
...@@ -633,9 +636,8 @@ class Html ...@@ -633,9 +636,8 @@ class Html
* *
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param string $selection the selected value * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * Attributes whose value is null will be ignored and not put in the tag returned. The following attributes
* Attributes whose value is null will be ignored and not put in the tag returned. The following attribute
* will be specially handled and not put in the resulting tag: * will be specially handled and not put in the resulting tag:
* *
* - prompt: string, a prompt text to be displayed as the first option; * - prompt: string, a prompt text to be displayed as the first option;
...@@ -653,16 +655,17 @@ class Html ...@@ -653,16 +655,17 @@ class Html
* except that the array keys represent the optgroup labels specified in $items. * except that the array keys represent the optgroup labels specified in $items.
* @return string the generated drop-down list tag * @return string the generated drop-down list tag
*/ */
public static function dropDownList($name, $items = array(), $selection = null, $attributes = array()) public static function dropDownList($name, $selection = null, $items = array(), $tagAttributes = array())
{ {
$attributes['name'] = $name; $tagAttributes['name'] = $name;
$options = static::renderOptions($items, $selection, $attributes); $options = static::renderSelectOptions($selection, $items, $tagAttributes);
return static::tag('select', "\n" . $options . "\n", $attributes); return static::tag('select', "\n" . $options . "\n", $tagAttributes);
} }
/** /**
* Generates a list box. * Generates a list box.
* @param string $name the input name * @param string $name the input name
* @param string|array $selection the selected value(s)
* @param array $items the option data items. The array keys are option values, and the array values * @param array $items the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
...@@ -671,9 +674,8 @@ class Html ...@@ -671,9 +674,8 @@ class Html
* *
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param string|array $selection the selected value(s) * @param array $tagAttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $attributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]]. * Attributes whose value is null will be ignored and not put in the tag returned. The following attributes
* Attributes whose value is null will be ignored and not put in the tag returned. The following attribute
* will be specially handled and not put in the resulting tag: * will be specially handled and not put in the resulting tag:
* *
* - prompt: string, a prompt text to be displayed as the first option; * - prompt: string, a prompt text to be displayed as the first option;
...@@ -694,27 +696,27 @@ class Html ...@@ -694,27 +696,27 @@ class Html
* mode, we can still obtain the posted unselect value. * mode, we can still obtain the posted unselect value.
* @return string the generated list box tag * @return string the generated list box tag
*/ */
public static function listBox($name, $items = array(), $selection = null, $attributes = array()) public static function listBox($name, $selection = null, $items = array(), $tagAttributes = array())
{ {
if (!isset($attributes['size'])) { if (!isset($tagAttributes['size'])) {
$attributes['size'] = 4; $tagAttributes['size'] = 4;
} }
if (isset($attributes['multiple']) && $attributes['multiple'] && substr($name, -2) !== '[]') { if (isset($tagAttributes['multiple']) && $tagAttributes['multiple'] && substr($name, -2) !== '[]') {
$name .= '[]'; $name .= '[]';
} }
$attributes['name'] = $name; $tagAttributes['name'] = $name;
if (isset($attributes['unselect'])) { if (isset($tagAttributes['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value // add a hidden field so that if the list box has no option being selected, it still submits a value
if (substr($name, -2) === '[]') { if (substr($name, -2) === '[]') {
$name = substr($name, 0, -2); $name = substr($name, 0, -2);
} }
$hidden = static::hiddenInput($name, $attributes['unselect']); $hidden = static::hiddenInput($name, $tagAttributes['unselect']);
unset($attributes['unselect']); unset($tagAttributes['unselect']);
} else { } else {
$hidden = ''; $hidden = '';
} }
$options = static::renderOptions($items, $selection, $attributes); $options = static::renderSelectOptions($selection, $items, $tagAttributes);
return $hidden . static::tag('select', "\n" . $options . "\n", $attributes); return $hidden . static::tag('select', "\n" . $options . "\n", $tagAttributes);
} }
/** /**
...@@ -722,10 +724,10 @@ class Html ...@@ -722,10 +724,10 @@ class Html
* A checkbox list allows multiple selection, like [[listBox()]]. * A checkbox list allows multiple selection, like [[listBox()]].
* As a result, the corresponding submitted value is an array. * As a result, the corresponding submitted value is an array.
* @param string $name the name attribute of each checkbox. * @param string $name the name attribute of each checkbox.
* @param string|array $selection the selected value(s).
* @param array $items the data item used to generate the checkboxes. * @param array $items the data item used to generate the checkboxes.
* The array keys are the labels, while the array values are the corresponding checkbox values. * The array keys are the labels, while the array values are the corresponding checkbox values.
* Note that the labels will NOT be HTML-encoded, while the values will. * Note that the labels will NOT be HTML-encoded, while the values will.
* @param string|array $selection the selected value(s).
* @param array $options options (name => config) for the checkbox list. The following options are supported: * @param array $options options (name => config) for the checkbox list. The following options are supported:
* *
* - unselect: string, the value that should be submitted when none of the checkboxes is selected. * - unselect: string, the value that should be submitted when none of the checkboxes is selected.
...@@ -735,7 +737,7 @@ class Html ...@@ -735,7 +737,7 @@ class Html
* corresponding to a single item in $items. The signature of this callback must be: * corresponding to a single item in $items. The signature of this callback must be:
* *
* ~~~ * ~~~
* function ($index, $label, $name, $value, $checked) * function ($index, $label, $name, $checked, $value)
* ~~~ * ~~~
* *
* where $index is the zero-based index of the checkbox in the whole list; $label * where $index is the zero-based index of the checkbox in the whole list; $label
...@@ -743,7 +745,7 @@ class Html ...@@ -743,7 +745,7 @@ class Html
* value and the checked status of the checkbox input. * value and the checked status of the checkbox input.
* @return string the generated checkbox list * @return string the generated checkbox list
*/ */
public static function checkboxList($name, $items = array(), $selection = null, $options = array()) public static function checkboxList($name, $selection = null, $items = array(), $options = array())
{ {
if (substr($name, -2) !== '[]') { if (substr($name, -2) !== '[]') {
$name .= '[]'; $name .= '[]';
...@@ -757,9 +759,9 @@ class Html ...@@ -757,9 +759,9 @@ class Html
(!is_array($selection) && !strcmp($value, $selection) (!is_array($selection) && !strcmp($value, $selection)
|| is_array($selection) && in_array($value, $selection)); || is_array($selection) && in_array($value, $selection));
if ($formatter !== null) { if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $value, $checked); $lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else { } else {
$lines[] = static::label(static::checkbox($name, $value, $checked) . ' ' . $label); $lines[] = static::label(static::checkbox($name, $checked, $value) . ' ' . $label);
} }
$index++; $index++;
} }
...@@ -780,10 +782,10 @@ class Html ...@@ -780,10 +782,10 @@ class Html
* Generates a list of radio buttons. * Generates a list of radio buttons.
* A radio button list is like a checkbox list, except that it only allows single selection. * A radio button list is like a checkbox list, except that it only allows single selection.
* @param string $name the name attribute of each radio button. * @param string $name the name attribute of each radio button.
* @param string|array $selection the selected value(s).
* @param array $items the data item used to generate the radio buttons. * @param array $items the data item used to generate the radio buttons.
* The array keys are the labels, while the array values are the corresponding radio button values. * The array keys are the labels, while the array values are the corresponding radio button values.
* Note that the labels will NOT be HTML-encoded, while the values will. * Note that the labels will NOT be HTML-encoded, while the values will.
* @param string|array $selection the selected value(s).
* @param array $options options (name => config) for the radio button list. The following options are supported: * @param array $options options (name => config) for the radio button list. The following options are supported:
* *
* - unselect: string, the value that should be submitted when none of the radio buttons is selected. * - unselect: string, the value that should be submitted when none of the radio buttons is selected.
...@@ -793,7 +795,7 @@ class Html ...@@ -793,7 +795,7 @@ class Html
* corresponding to a single item in $items. The signature of this callback must be: * corresponding to a single item in $items. The signature of this callback must be:
* *
* ~~~ * ~~~
* function ($index, $label, $name, $value, $checked) * function ($index, $label, $name, $checked, $value)
* ~~~ * ~~~
* *
* where $index is the zero-based index of the radio button in the whole list; $label * where $index is the zero-based index of the radio button in the whole list; $label
...@@ -801,7 +803,7 @@ class Html ...@@ -801,7 +803,7 @@ class Html
* value and the checked status of the radio button input. * value and the checked status of the radio button input.
* @return string the generated radio button list * @return string the generated radio button list
*/ */
public static function radioList($name, $items = array(), $selection = null, $options = array()) public static function radioList($name, $selection = null, $items = array(), $options = array())
{ {
$formatter = isset($options['item']) ? $options['item'] : null; $formatter = isset($options['item']) ? $options['item'] : null;
$lines = array(); $lines = array();
...@@ -811,9 +813,9 @@ class Html ...@@ -811,9 +813,9 @@ class Html
(!is_array($selection) && !strcmp($value, $selection) (!is_array($selection) && !strcmp($value, $selection)
|| is_array($selection) && in_array($value, $selection)); || is_array($selection) && in_array($value, $selection));
if ($formatter !== null) { if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $value, $checked); $lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else { } else {
$lines[] = static::label(static::radio($name, $value, $checked) . ' ' . $label); $lines[] = static::label(static::radio($name, $checked, $value) . ' ' . $label);
} }
$index++; $index++;
} }
...@@ -831,6 +833,8 @@ class Html ...@@ -831,6 +833,8 @@ class Html
/** /**
* Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]]. * Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]].
* @param string|array $selection the selected value(s). This can be either a string for single selection
* or an array for multiple selections.
* @param array $items the option data items. The array keys are option values, and the array values * @param array $items the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
...@@ -839,32 +843,30 @@ class Html ...@@ -839,32 +843,30 @@ class Html
* *
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
* the labels will also be HTML-encoded. * the labels will also be HTML-encoded.
* @param string|array $selection the selected value(s). This can be either a string for single selection * @param array $tagAttributes the attributes parameter that is passed to the [[dropDownList()]] or [[listBox()]] call.
* or an array for multiple selections.
* @param array $attributes the attributes parameter that is passed to the [[dropDownList()]] or [[listBox()]] call.
* This method will take out these elements, if any: "prompt", "options" and "groups". See more details * This method will take out these elements, if any: "prompt", "options" and "groups". See more details
* in [[dropDownList()]] for the explanation of these elements. * in [[dropDownList()]] for the explanation of these elements.
* *
* @return string the generated list options * @return string the generated list options
*/ */
public static function renderOptions($items, $selection = null, &$attributes = array()) public static function renderSelectOptions($selection, $items, &$tagAttributes = array())
{ {
$lines = array(); $lines = array();
if (isset($attributes['prompt'])) { if (isset($tagAttributes['prompt'])) {
$prompt = str_replace(' ', '&nbsp;', static::encode($attributes['prompt'])); $prompt = str_replace(' ', '&nbsp;', static::encode($tagAttributes['prompt']));
$lines[] = static::tag('option', $prompt, array('value' => '')); $lines[] = static::tag('option', $prompt, array('value' => ''));
} }
$options = isset($attributes['options']) ? $attributes['options'] : array(); $options = isset($tagAttributes['options']) ? $tagAttributes['options'] : array();
$groups = isset($attributes['groups']) ? $attributes['groups'] : array(); $groups = isset($tagAttributes['groups']) ? $tagAttributes['groups'] : array();
unset($attributes['prompt'], $attributes['options'], $attributes['groups']); unset($tagAttributes['prompt'], $tagAttributes['options'], $tagAttributes['groups']);
foreach ($items as $key => $value) { foreach ($items as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
$groupAttrs = isset($groups[$key]) ? $groups[$key] : array(); $groupAttrs = isset($groups[$key]) ? $groups[$key] : array();
$groupAttrs['label'] = $key; $groupAttrs['label'] = $key;
$attrs = array('options' => $options, 'groups' => $groups); $attrs = array('options' => $options, 'groups' => $groups);
$content = static::renderOptions($value, $selection, $attrs); $content = static::renderSelectOptions($selection, $value, $attrs);
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs); $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
} else { } else {
$attrs = isset($options[$key]) ? $options[$key] : array(); $attrs = isset($options[$key]) ? $options[$key] : array();
...@@ -883,26 +885,26 @@ class Html ...@@ -883,26 +885,26 @@ class Html
* Renders the HTML tag attributes. * Renders the HTML tag attributes.
* Boolean attributes such as s 'checked', 'disabled', 'readonly', will be handled specially * Boolean attributes such as s 'checked', 'disabled', 'readonly', will be handled specially
* according to [[booleanAttributes]] and [[showBooleanAttributeValues]]. * according to [[booleanAttributes]] and [[showBooleanAttributeValues]].
* @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]]. * @param array $tagAttributes 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. * 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 * @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 (such that it can be directly appended to the tag name
* in a tag. If there is no attribute, an empty string will be returned. * in a tag. If there is no attribute, an empty string will be returned.
*/ */
public static function renderAttributes($attributes) public static function renderTagAttributes($tagAttributes)
{ {
if (count($attributes) > 1) { if (count($tagAttributes) > 1) {
$sorted = array(); $sorted = array();
foreach (static::$attributeOrder as $name) { foreach (static::$attributeOrder as $name) {
if (isset($attributes[$name])) { if (isset($tagAttributes[$name])) {
$sorted[$name] = $attributes[$name]; $sorted[$name] = $tagAttributes[$name];
} }
} }
$attributes = array_merge($sorted, $attributes); $tagAttributes = array_merge($sorted, $tagAttributes);
} }
$html = ''; $html = '';
foreach ($attributes as $name => $value) { foreach ($tagAttributes as $name => $value) {
if (isset(static::$booleanAttributes[strtolower($name)])) { if (isset(static::$booleanAttributes[strtolower($name)])) {
if ($value || strcasecmp($name, $value) === 0) { if ($value || strcasecmp($name, $value) === 0) {
$html .= static::$showBooleanAttributeValues ? " $name=\"$name\"" : " $name"; $html .= static::$showBooleanAttributeValues ? " $name=\"$name\"" : " $name";
......
...@@ -220,22 +220,22 @@ class HtmlTest extends \yii\test\TestCase ...@@ -220,22 +220,22 @@ class HtmlTest extends \yii\test\TestCase
public function testRadio() public function testRadio()
{ {
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test')); $this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test'));
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', null, true, array('class' => 'a'))); $this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', null, true, array('class' => 'a' ,'uncheck' => '0'))); $this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0')));
} }
public function testCheckbox() public function testCheckbox()
{ {
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test')); $this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test'));
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', null, true, array('class' => 'a'))); $this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', null, true, array('class' => 'a' ,'uncheck' => '0'))); $this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0')));
} }
public function testDropDownList() public function testDropDownList()
{ {
$this->assertEquals("<select name=\"test\">\n\n</select>", Html::dropDownList('test')); $this->assertEquals("<select name=\"test\">\n\n</select>", Html::dropDownList('test'));
$this->assertEquals("<select name=\"test\">\n<option value=\"value1\">text1</option>\n<option value=\"value2\">text2</option>\n</select>", Html::dropDownList('test', $this->getDataItems())); $this->assertEquals("<select name=\"test\">\n<option value=\"value1\">text1</option>\n<option value=\"value2\">text2</option>\n</select>", Html::dropDownList('test', null, $this->getDataItems()));
$this->assertEquals("<select name=\"test\">\n<option value=\"value1\">text1</option>\n<option value=\"value2\" selected=\"selected\">text2</option>\n</select>", Html::dropDownList('test', $this->getDataItems(), 'value2')); $this->assertEquals("<select name=\"test\">\n<option value=\"value1\">text1</option>\n<option value=\"value2\" selected=\"selected\">text2</option>\n</select>", Html::dropDownList('test', 'value2', $this->getDataItems()));
} }
public function testListBox() public function testListBox()
...@@ -252,41 +252,41 @@ EOD; ...@@ -252,41 +252,41 @@ EOD;
<option value="value2">text2</option> <option value="value2">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', $this->getDataItems(), null, array('size' => 5))); $this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5)));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1&lt;&gt;">text1&lt;&gt;</option> <option value="value1&lt;&gt;">text1&lt;&gt;</option>
<option value="value 2">text&nbsp;&nbsp;2</option> <option value="value 2">text&nbsp;&nbsp;2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', $this->getDataItems2(), null)); $this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', $this->getDataItems(), 'value2')); $this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1" selected="selected">text1</option> <option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', $this->getDataItems(), array('value1', 'value2'))); $this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test[]" multiple="multiple" size="4"> <select name="test[]" multiple="multiple" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', array(), null, array('multiple' => true))); $this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true)));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><select name="test" size="4"> <input type="hidden" name="test" value="0" /><select name="test" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', array(), '', array('unselect' => '0'))); $this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0')));
} }
public function testCheckboxList() public function testCheckboxList()
...@@ -297,30 +297,30 @@ EOD; ...@@ -297,30 +297,30 @@ EOD;
<label><input type="checkbox" name="test[]" value="value1" /> text1</label> <label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> <label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', $this->getDataItems(), array('value2'))); $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label> <label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label> <label><input type="checkbox" name="test[]" value="value 2" /> text 2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', $this->getDataItems2(), array('value2'))); $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br /> <input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br />
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> <label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', $this->getDataItems(), array('value2'), array( $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n", 'separator' => "<br />\n",
'unselect' => '0', 'unselect' => '0',
))); )));
$expected = <<<EOD $expected = <<<EOD
<label>text1 <input type="checkbox" name="test[]" value="value1" /></label> 0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label> 1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', $this->getDataItems(), array('value2'), array( $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $value, $checked) { 'item' => function ($index, $label, $name, $checked, $value) {
return Html::label($label . ' ' . Html::checkbox($name, $value, $checked)); return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value));
} }
))); )));
} }
...@@ -333,38 +333,36 @@ EOD; ...@@ -333,38 +333,36 @@ EOD;
<label><input type="radio" name="test" value="value1" /> text1</label> <label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> <label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', $this->getDataItems(), array('value2'))); $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label> <label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label>
<label><input type="radio" name="test" value="value 2" /> text 2</label> <label><input type="radio" name="test" value="value 2" /> text 2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', $this->getDataItems2(), array('value2'))); $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br /> <input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br />
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> <label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', $this->getDataItems(), array('value2'), array( $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n", 'separator' => "<br />\n",
'unselect' => '0', 'unselect' => '0',
))); )));
$expected = <<<EOD $expected = <<<EOD
<label>text1 <input type="radio" name="test" value="value1" /></label> 0<label>text1 <input type="radio" name="test" value="value1" /></label>
<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label> 1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', $this->getDataItems(), array('value2'), array( $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $value, $checked) { 'item' => function ($index, $label, $name, $checked, $value) {
return Html::label($label . ' ' . Html::radio($name, $value, $checked)); return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value));
} }
))); )));
} }
public function testRenderOptions() public function testRenderOptions()
{ {
$this->assertEquals('', Html::renderOptions(array()));
$data = array( $data = array(
'value1' => 'label1', 'value1' => 'label1',
'group1' => array( 'group1' => array(
...@@ -403,15 +401,15 @@ EOD; ...@@ -403,15 +401,15 @@ EOD;
'group12' => array('class' => 'group'), 'group12' => array('class' => 'group'),
), ),
); );
$this->assertEquals($expected, Html::renderOptions($data, array('value111', 'value1'), $attributes)); $this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes));
} }
public function testRenderAttributes() public function testRenderAttributes()
{ {
$this->assertEquals('', Html::renderAttributes(array())); $this->assertEquals('', Html::renderTagAttributes(array()));
$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>'))); $this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
Html::$showBooleanAttributeValues = false; Html::$showBooleanAttributeValues = false;
$this->assertEquals(' checked disabled', Html::renderAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false))); $this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
Html::$showBooleanAttributeValues = true; Html::$showBooleanAttributeValues = true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment