Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2422a134
Commit
2422a134
authored
Mar 13, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored Html.
parent
a5ababe4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
194 additions
and
194 deletions
+194
-194
Html.php
framework/util/Html.php
+162
-160
HtmlTest.php
tests/unit/framework/util/HtmlTest.php
+32
-34
No files found.
framework/util/Html.php
View file @
2422a134
...
...
@@ -153,15 +153,15 @@ class Html
* @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.
* If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.
* @param array $
a
ttributes the element attributes. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated HTML tag
* @see beginTag
* @see endTag
*/
public
static
function
tag
(
$name
,
$content
=
''
,
$
a
ttributes
=
array
())
public
static
function
tag
(
$name
,
$content
=
''
,
$
tagA
ttributes
=
array
())
{
$html
=
'<'
.
$name
.
static
::
render
Attributes
(
$a
ttributes
);
$html
=
'<'
.
$name
.
static
::
render
TagAttributes
(
$tagA
ttributes
);
if
(
isset
(
static
::
$voidElements
[
strtolower
(
$name
)]))
{
return
$html
.
(
static
::
$closeVoidElements
?
' />'
:
'>'
);
}
else
{
...
...
@@ -172,15 +172,15 @@ class Html
/**
* Generates a start tag.
* @param string $name the tag name
* @param array $
a
ttributes the element attributes. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated start tag
* @see endTag
* @see tag
*/
public
static
function
beginTag
(
$name
,
$
a
ttributes
=
array
())
public
static
function
beginTag
(
$name
,
$
tagA
ttributes
=
array
())
{
return
'<'
.
$name
.
static
::
render
Attributes
(
$a
ttributes
)
.
'>'
;
return
'<'
.
$name
.
static
::
render
TagAttributes
(
$tagA
ttributes
)
.
'>'
;
}
/**
...
...
@@ -208,76 +208,76 @@ class Html
/**
* Generates a style tag.
* @param string $content the style content
* @param array $
a
ttributes the attributes of the style tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* If the attributes does not contain "type", a default one with value "text/css" will be used.
* @return string the generated style tag
*/
public
static
function
style
(
$content
,
$
a
ttributes
=
array
())
public
static
function
style
(
$content
,
$
tagA
ttributes
=
array
())
{
if
(
!
isset
(
$
a
ttributes
[
'type'
]))
{
$
a
ttributes
[
'type'
]
=
'text/css'
;
if
(
!
isset
(
$
tagA
ttributes
[
'type'
]))
{
$
tagA
ttributes
[
'type'
]
=
'text/css'
;
}
return
static
::
tag
(
'style'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
a
ttributes
);
return
static
::
tag
(
'style'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
tagA
ttributes
);
}
/**
* Generates a script tag.
* @param string $content the script content
* @param array $
a
ttributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* If the attributes does not contain "type", a default one with value "text/javascript" will be used.
* @return string the generated script tag
*/
public
static
function
script
(
$content
,
$
a
ttributes
=
array
())
public
static
function
script
(
$content
,
$
tagA
ttributes
=
array
())
{
if
(
!
isset
(
$
a
ttributes
[
'type'
]))
{
$
a
ttributes
[
'type'
]
=
'text/javascript'
;
if
(
!
isset
(
$
tagA
ttributes
[
'type'
]))
{
$
tagA
ttributes
[
'type'
]
=
'text/javascript'
;
}
return
static
::
tag
(
'script'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
a
ttributes
);
return
static
::
tag
(
'script'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
tagA
ttributes
);
}
/**
* 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 $
a
ttributes the attributes of the link tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated link tag
* @see url
*/
public
static
function
cssFile
(
$url
,
$
a
ttributes
=
array
())
public
static
function
cssFile
(
$url
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'rel'
]
=
'stylesheet'
;
$
a
ttributes
[
'type'
]
=
'text/css'
;
$
a
ttributes
[
'href'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'link'
,
''
,
$
a
ttributes
);
$
tagA
ttributes
[
'rel'
]
=
'stylesheet'
;
$
tagA
ttributes
[
'type'
]
=
'text/css'
;
$
tagA
ttributes
[
'href'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'link'
,
''
,
$
tagA
ttributes
);
}
/**
* 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 array $
a
ttributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated script tag
* @see url
*/
public
static
function
jsFile
(
$url
,
$
a
ttributes
=
array
())
public
static
function
jsFile
(
$url
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'type'
]
=
'text/javascript'
;
$
a
ttributes
[
'src'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'script'
,
''
,
$
a
ttributes
);
$
tagA
ttributes
[
'type'
]
=
'text/javascript'
;
$
tagA
ttributes
[
'src'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'script'
,
''
,
$
tagA
ttributes
);
}
/**
* Generates a form start tag.
* @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 array $
a
ttributes the attributes of the form tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated form start tag.
* @see endForm
*/
public
static
function
beginForm
(
$action
=
''
,
$method
=
'post'
,
$
a
ttributes
=
array
())
public
static
function
beginForm
(
$action
=
''
,
$method
=
'post'
,
$
tagA
ttributes
=
array
())
{
$action
=
static
::
url
(
$action
);
...
...
@@ -295,9 +295,9 @@ class Html
$action
=
substr
(
$action
,
0
,
$pos
);
}
$
a
ttributes
[
'action'
]
=
$action
;
$
a
ttributes
[
'method'
]
=
$method
;
$form
=
static
::
beginTag
(
'form'
,
$
a
ttributes
);
$
tagA
ttributes
[
'action'
]
=
$action
;
$
tagA
ttributes
[
'method'
]
=
$method
;
$form
=
static
::
beginTag
(
'form'
,
$
tagA
ttributes
);
if
(
$hiddens
!==
array
())
{
$form
.=
"
\n
"
.
implode
(
"
\n
"
,
$hiddens
);
}
...
...
@@ -323,17 +323,17 @@ class Html
* @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
* will not be generated.
* @param array $
a
ttributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated hyperlink
* @see url
*/
public
static
function
a
(
$text
,
$url
=
null
,
$
a
ttributes
=
array
())
public
static
function
a
(
$text
,
$url
=
null
,
$
tagA
ttributes
=
array
())
{
if
(
$url
!==
null
)
{
$
a
ttributes
[
'href'
]
=
static
::
url
(
$url
);
$
tagA
ttributes
[
'href'
]
=
static
::
url
(
$url
);
}
return
static
::
tag
(
'a'
,
$text
,
$
a
ttributes
);
return
static
::
tag
(
'a'
,
$text
,
$
tagA
ttributes
);
}
/**
...
...
@@ -343,29 +343,29 @@ class Html
* it to prevent XSS attacks.
* @param string $email email address. If this is null, the first parameter (link body) will be treated
* as the email address and used.
* @param array $
a
ttributes the attributes of the hyperlink tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated mailto link
*/
public
static
function
mailto
(
$text
,
$email
=
null
,
$
a
ttributes
=
array
())
public
static
function
mailto
(
$text
,
$email
=
null
,
$
tagA
ttributes
=
array
())
{
return
static
::
a
(
$text
,
'mailto:'
.
(
$email
===
null
?
$text
:
$email
),
$
a
ttributes
);
return
static
::
a
(
$text
,
'mailto:'
.
(
$email
===
null
?
$text
:
$email
),
$
tagA
ttributes
);
}
/**
* Generates an image tag.
* @param string $src the image URL. This parameter will be processed by [[url()]].
* @param array $
a
ttributes the attributes of the image tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated image tag
*/
public
static
function
img
(
$src
,
$
a
ttributes
=
array
())
public
static
function
img
(
$src
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'src'
]
=
static
::
url
(
$src
);
if
(
!
isset
(
$
a
ttributes
[
'alt'
]))
{
$
a
ttributes
[
'alt'
]
=
''
;
$
tagA
ttributes
[
'src'
]
=
static
::
url
(
$src
);
if
(
!
isset
(
$
tagA
ttributes
[
'alt'
]))
{
$
tagA
ttributes
[
'alt'
]
=
''
;
}
return
static
::
tag
(
'img'
,
null
,
$
a
ttributes
);
return
static
::
tag
(
'img'
,
null
,
$
tagA
ttributes
);
}
/**
...
...
@@ -375,14 +375,14 @@ class Html
* it to prevent XSS attacks.
* @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.
* @param array $
a
ttributes the attributes of the label tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated label tag
*/
public
static
function
label
(
$content
,
$for
=
null
,
$
a
ttributes
=
array
())
public
static
function
label
(
$content
,
$for
=
null
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'for'
]
=
$for
;
return
static
::
tag
(
'label'
,
$content
,
$
a
ttributes
);
$
tagA
ttributes
[
'for'
]
=
$for
;
return
static
::
tag
(
'label'
,
$content
,
$
tagA
ttributes
);
}
/**
...
...
@@ -392,19 +392,19 @@ class Html
* @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,
* you should consider [[encode()]] it to prevent XSS attacks.
* @param array $
a
ttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* If the attributes does not contain "type", a default one with value "button" will be used.
* @return string the generated button tag
*/
public
static
function
button
(
$name
=
null
,
$value
=
null
,
$content
=
'Button'
,
$
a
ttributes
=
array
())
public
static
function
button
(
$name
=
null
,
$value
=
null
,
$content
=
'Button'
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'name'
]
=
$name
;
$
a
ttributes
[
'value'
]
=
$value
;
if
(
!
isset
(
$
a
ttributes
[
'type'
]))
{
$
a
ttributes
[
'type'
]
=
'button'
;
$
tagA
ttributes
[
'name'
]
=
$name
;
$
tagA
ttributes
[
'value'
]
=
$value
;
if
(
!
isset
(
$
tagA
ttributes
[
'type'
]))
{
$
tagA
ttributes
[
'type'
]
=
'button'
;
}
return
static
::
tag
(
'button'
,
$content
,
$
a
ttributes
);
return
static
::
tag
(
'button'
,
$content
,
$
tagA
ttributes
);
}
/**
...
...
@@ -414,14 +414,14 @@ class Html
* @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,
* you should consider [[encode()]] it to prevent XSS attacks.
* @param array $
a
ttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated submit button tag
*/
public
static
function
submitButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Submit'
,
$
a
ttributes
=
array
())
public
static
function
submitButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Submit'
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'type'
]
=
'submit'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
a
ttributes
);
$
tagA
ttributes
[
'type'
]
=
'submit'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
tagA
ttributes
);
}
/**
...
...
@@ -431,14 +431,14 @@ class Html
* @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,
* you should consider [[encode()]] it to prevent XSS attacks.
* @param array $
a
ttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated reset button tag
*/
public
static
function
resetButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Reset'
,
$
a
ttributes
=
array
())
public
static
function
resetButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Reset'
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'type'
]
=
'reset'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
a
ttributes
);
$
tagA
ttributes
[
'type'
]
=
'reset'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
tagA
ttributes
);
}
/**
...
...
@@ -446,94 +446,94 @@ class Html
* @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 $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $
a
ttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated input tag
*/
public
static
function
input
(
$type
,
$name
=
null
,
$value
=
null
,
$
a
ttributes
=
array
())
public
static
function
input
(
$type
,
$name
=
null
,
$value
=
null
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'type'
]
=
$type
;
$
a
ttributes
[
'name'
]
=
$name
;
$
a
ttributes
[
'value'
]
=
$value
;
return
static
::
tag
(
'input'
,
null
,
$
a
ttributes
);
$
tagA
ttributes
[
'type'
]
=
$type
;
$
tagA
ttributes
[
'name'
]
=
$name
;
$
tagA
ttributes
[
'value'
]
=
$value
;
return
static
::
tag
(
'input'
,
null
,
$
tagA
ttributes
);
}
/**
* Generates an input button.
* @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $
a
ttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
buttonInput
(
$name
,
$value
=
'Button'
,
$
a
ttributes
=
array
())
public
static
function
buttonInput
(
$name
,
$value
=
'Button'
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'button'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'button'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a submit input button.
* @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 array $
a
ttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
submitInput
(
$name
=
null
,
$value
=
'Submit'
,
$
a
ttributes
=
array
())
public
static
function
submitInput
(
$name
=
null
,
$value
=
'Submit'
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'submit'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'submit'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a reset input button.
* @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 array $
a
ttributes the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
resetInput
(
$name
=
null
,
$value
=
'Reset'
,
$
a
ttributes
=
array
())
public
static
function
resetInput
(
$name
=
null
,
$value
=
'Reset'
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'reset'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'reset'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a text input field.
* @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $
a
ttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
textInput
(
$name
,
$value
=
null
,
$
a
ttributes
=
array
())
public
static
function
textInput
(
$name
,
$value
=
null
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'text'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'text'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a hidden input field.
* @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $
a
ttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
hiddenInput
(
$name
,
$value
=
null
,
$
a
ttributes
=
array
())
public
static
function
hiddenInput
(
$name
,
$value
=
null
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'hidden'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'hidden'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a password input field.
* @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $
a
ttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
passwordInput
(
$name
,
$value
=
null
,
$
a
ttributes
=
array
())
public
static
function
passwordInput
(
$name
,
$value
=
null
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'password'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'password'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
...
...
@@ -543,36 +543,36 @@ class Html
* can be obtained via $_FILES[$name] (see PHP documentation).
* @param string $name the name attribute.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $
a
ttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated button tag
*/
public
static
function
fileInput
(
$name
,
$value
=
null
,
$
a
ttributes
=
array
())
public
static
function
fileInput
(
$name
,
$value
=
null
,
$
tagA
ttributes
=
array
())
{
return
static
::
input
(
'file'
,
$name
,
$value
,
$
a
ttributes
);
return
static
::
input
(
'file'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a text area input.
* @param string $name the input name
* @param string $value the input value. Note that it will be encoded using [[encode()]].
* @param array $
a
ttributes the attributes of the input tag. The values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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.
* @return string the generated text area tag
*/
public
static
function
textarea
(
$name
,
$value
=
''
,
$
a
ttributes
=
array
())
public
static
function
textarea
(
$name
,
$value
=
''
,
$
tagA
ttributes
=
array
())
{
$
a
ttributes
[
'name'
]
=
$name
;
return
static
::
tag
(
'textarea'
,
static
::
encode
(
$value
),
$
a
ttributes
);
$
tagA
ttributes
[
'name'
]
=
$name
;
return
static
::
tag
(
'textarea'
,
static
::
encode
(
$value
),
$
tagA
ttributes
);
}
/**
* Generates a radio button input.
* @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 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 attribute
* @param string $value the value attribute. If it is null, the value attribute will not be rendered.
* @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:
*
* - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute
...
...
@@ -581,26 +581,27 @@ class Html
*
* @return string the generated radio button tag
*/
public
static
function
radio
(
$name
,
$
value
=
'1'
,
$checked
=
false
,
$a
ttributes
=
array
())
public
static
function
radio
(
$name
,
$
checked
=
false
,
$value
=
'1'
,
$tagA
ttributes
=
array
())
{
$attributes
[
'checked'
]
=
$checked
;
if
(
isset
(
$attributes
[
'uncheck'
]))
{
$tagAttributes
[
'checked'
]
=
$checked
;
$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
$hidden
=
static
::
hiddenInput
(
$name
,
$
a
ttributes
[
'uncheck'
]);
unset
(
$
a
ttributes
[
'uncheck'
]);
$hidden
=
static
::
hiddenInput
(
$name
,
$
tagA
ttributes
[
'uncheck'
]);
unset
(
$
tagA
ttributes
[
'uncheck'
]);
}
else
{
$hidden
=
''
;
}
return
$hidden
.
static
::
input
(
'radio'
,
$name
,
$value
,
$
a
ttributes
);
return
$hidden
.
static
::
input
(
'radio'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a checkbox input.
* @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 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 attribute
* @param string $value the value attribute. If it is null, the value attribute will not be rendered.
* @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:
*
* - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute
...
...
@@ -609,22 +610,24 @@ class Html
*
* @return string the generated checkbox tag
*/
public
static
function
checkbox
(
$name
,
$
value
=
'1'
,
$checked
=
false
,
$a
ttributes
=
array
())
public
static
function
checkbox
(
$name
,
$
checked
=
false
,
$value
=
'1'
,
$tagA
ttributes
=
array
())
{
$attributes
[
'checked'
]
=
$checked
;
if
(
isset
(
$attributes
[
'uncheck'
]))
{
$tagAttributes
[
'checked'
]
=
$checked
;
$tagAttributes
[
'value'
]
=
$value
;
if
(
isset
(
$tagAttributes
[
'uncheck'
]))
{
// add a hidden field so that if the checkbox is not selected, it still submits a value
$hidden
=
static
::
hiddenInput
(
$name
,
$
a
ttributes
[
'uncheck'
]);
unset
(
$
a
ttributes
[
'uncheck'
]);
$hidden
=
static
::
hiddenInput
(
$name
,
$
tagA
ttributes
[
'uncheck'
]);
unset
(
$
tagA
ttributes
[
'uncheck'
]);
}
else
{
$hidden
=
''
;
}
return
$hidden
.
static
::
input
(
'checkbox'
,
$name
,
$value
,
$
a
ttributes
);
return
$hidden
.
static
::
input
(
'checkbox'
,
$name
,
$value
,
$
tagA
ttributes
);
}
/**
* Generates a drop-down list.
* @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
* 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.
...
...
@@ -633,9 +636,8 @@ class Html
*
* 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.
* @param string $selection the selected value
* @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 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:
*
* - prompt: string, a prompt text to be displayed as the first option;
...
...
@@ -653,16 +655,17 @@ class Html
* except that the array keys represent the optgroup labels specified in $items.
* @return string the generated drop-down list tag
*/
public
static
function
dropDownList
(
$name
,
$
items
=
array
(),
$selection
=
null
,
$a
ttributes
=
array
())
public
static
function
dropDownList
(
$name
,
$
selection
=
null
,
$items
=
array
(),
$tagA
ttributes
=
array
())
{
$
a
ttributes
[
'name'
]
=
$name
;
$options
=
static
::
render
Options
(
$items
,
$selection
,
$a
ttributes
);
return
static
::
tag
(
'select'
,
"
\n
"
.
$options
.
"
\n
"
,
$
a
ttributes
);
$
tagA
ttributes
[
'name'
]
=
$name
;
$options
=
static
::
render
SelectOptions
(
$selection
,
$items
,
$tagA
ttributes
);
return
static
::
tag
(
'select'
,
"
\n
"
.
$options
.
"
\n
"
,
$
tagA
ttributes
);
}
/**
* Generates a list box.
* @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
* 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.
...
...
@@ -671,9 +674,8 @@ class Html
*
* 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.
* @param string|array $selection the selected value(s)
* @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 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:
*
* - prompt: string, a prompt text to be displayed as the first option;
...
...
@@ -694,27 +696,27 @@ class Html
* mode, we can still obtain the posted unselect value.
* @return string the generated list box tag
*/
public
static
function
listBox
(
$name
,
$
items
=
array
(),
$selection
=
null
,
$a
ttributes
=
array
())
public
static
function
listBox
(
$name
,
$
selection
=
null
,
$items
=
array
(),
$tagA
ttributes
=
array
())
{
if
(
!
isset
(
$
a
ttributes
[
'size'
]))
{
$
a
ttributes
[
'size'
]
=
4
;
if
(
!
isset
(
$
tagA
ttributes
[
'size'
]))
{
$
tagA
ttributes
[
'size'
]
=
4
;
}
if
(
isset
(
$
attributes
[
'multiple'
])
&&
$a
ttributes
[
'multiple'
]
&&
substr
(
$name
,
-
2
)
!==
'[]'
)
{
if
(
isset
(
$
tagAttributes
[
'multiple'
])
&&
$tagA
ttributes
[
'multiple'
]
&&
substr
(
$name
,
-
2
)
!==
'[]'
)
{
$name
.=
'[]'
;
}
$
a
ttributes
[
'name'
]
=
$name
;
if
(
isset
(
$
a
ttributes
[
'unselect'
]))
{
$
tagA
ttributes
[
'name'
]
=
$name
;
if
(
isset
(
$
tagA
ttributes
[
'unselect'
]))
{
// add a hidden field so that if the list box has no option being selected, it still submits a value
if
(
substr
(
$name
,
-
2
)
===
'[]'
)
{
$name
=
substr
(
$name
,
0
,
-
2
);
}
$hidden
=
static
::
hiddenInput
(
$name
,
$
a
ttributes
[
'unselect'
]);
unset
(
$
a
ttributes
[
'unselect'
]);
$hidden
=
static
::
hiddenInput
(
$name
,
$
tagA
ttributes
[
'unselect'
]);
unset
(
$
tagA
ttributes
[
'unselect'
]);
}
else
{
$hidden
=
''
;
}
$options
=
static
::
render
Options
(
$items
,
$selection
,
$a
ttributes
);
return
$hidden
.
static
::
tag
(
'select'
,
"
\n
"
.
$options
.
"
\n
"
,
$
a
ttributes
);
$options
=
static
::
render
SelectOptions
(
$selection
,
$items
,
$tagA
ttributes
);
return
$hidden
.
static
::
tag
(
'select'
,
"
\n
"
.
$options
.
"
\n
"
,
$
tagA
ttributes
);
}
/**
...
...
@@ -722,10 +724,10 @@ class Html
* A checkbox list allows multiple selection, like [[listBox()]].
* As a result, the corresponding submitted value is an array.
* @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.
* 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.
* @param string|array $selection the selected value(s).
* @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.
...
...
@@ -735,7 +737,7 @@ class Html
* 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
...
...
@@ -743,7 +745,7 @@ class Html
* value and the checked status of the checkbox input.
* @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
)
!==
'[]'
)
{
$name
.=
'[]'
;
...
...
@@ -757,9 +759,9 @@ class Html
(
!
is_array
(
$selection
)
&&
!
strcmp
(
$value
,
$selection
)
||
is_array
(
$selection
)
&&
in_array
(
$value
,
$selection
));
if
(
$formatter
!==
null
)
{
$lines
[]
=
call_user_func
(
$formatter
,
$index
,
$label
,
$name
,
$
value
,
$checked
);
$lines
[]
=
call_user_func
(
$formatter
,
$index
,
$label
,
$name
,
$
checked
,
$value
);
}
else
{
$lines
[]
=
static
::
label
(
static
::
checkbox
(
$name
,
$
value
,
$checked
)
.
' '
.
$label
);
$lines
[]
=
static
::
label
(
static
::
checkbox
(
$name
,
$
checked
,
$value
)
.
' '
.
$label
);
}
$index
++
;
}
...
...
@@ -780,10 +782,10 @@ class Html
* Generates a list of radio buttons.
* 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|array $selection the selected value(s).
* @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.
* 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:
*
* - unselect: string, the value that should be submitted when none of the radio buttons is selected.
...
...
@@ -793,7 +795,7 @@ class Html
* 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
...
...
@@ -801,7 +803,7 @@ class Html
* value and the checked status of the radio button input.
* @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
;
$lines
=
array
();
...
...
@@ -811,9 +813,9 @@ class Html
(
!
is_array
(
$selection
)
&&
!
strcmp
(
$value
,
$selection
)
||
is_array
(
$selection
)
&&
in_array
(
$value
,
$selection
));
if
(
$formatter
!==
null
)
{
$lines
[]
=
call_user_func
(
$formatter
,
$index
,
$label
,
$name
,
$
value
,
$checked
);
$lines
[]
=
call_user_func
(
$formatter
,
$index
,
$label
,
$name
,
$
checked
,
$value
);
}
else
{
$lines
[]
=
static
::
label
(
static
::
radio
(
$name
,
$
value
,
$checked
)
.
' '
.
$label
);
$lines
[]
=
static
::
label
(
static
::
radio
(
$name
,
$
checked
,
$value
)
.
' '
.
$label
);
}
$index
++
;
}
...
...
@@ -831,6 +833,8 @@ class Html
/**
* 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
* 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.
...
...
@@ -839,32 +843,30 @@ class Html
*
* 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.
* @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 $attributes the attributes parameter that is passed to the [[dropDownList()]] or [[listBox()]] call.
* @param array $tagAttributes 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
* in [[dropDownList()]] for the explanation of these elements.
*
* @return string the generated list options
*/
public
static
function
render
Options
(
$items
,
$selection
=
null
,
&
$a
ttributes
=
array
())
public
static
function
render
SelectOptions
(
$selection
,
$items
,
&
$tagA
ttributes
=
array
())
{
$lines
=
array
();
if
(
isset
(
$
a
ttributes
[
'prompt'
]))
{
$prompt
=
str_replace
(
' '
,
' '
,
static
::
encode
(
$
a
ttributes
[
'prompt'
]));
if
(
isset
(
$
tagA
ttributes
[
'prompt'
]))
{
$prompt
=
str_replace
(
' '
,
' '
,
static
::
encode
(
$
tagA
ttributes
[
'prompt'
]));
$lines
[]
=
static
::
tag
(
'option'
,
$prompt
,
array
(
'value'
=>
''
));
}
$options
=
isset
(
$
attributes
[
'options'
])
?
$a
ttributes
[
'options'
]
:
array
();
$groups
=
isset
(
$
attributes
[
'groups'
])
?
$a
ttributes
[
'groups'
]
:
array
();
unset
(
$
attributes
[
'prompt'
],
$attributes
[
'options'
],
$a
ttributes
[
'groups'
]);
$options
=
isset
(
$
tagAttributes
[
'options'
])
?
$tagA
ttributes
[
'options'
]
:
array
();
$groups
=
isset
(
$
tagAttributes
[
'groups'
])
?
$tagA
ttributes
[
'groups'
]
:
array
();
unset
(
$
tagAttributes
[
'prompt'
],
$tagAttributes
[
'options'
],
$tagA
ttributes
[
'groups'
]);
foreach
(
$items
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
$groupAttrs
=
isset
(
$groups
[
$key
])
?
$groups
[
$key
]
:
array
();
$groupAttrs
[
'label'
]
=
$key
;
$attrs
=
array
(
'options'
=>
$options
,
'groups'
=>
$groups
);
$content
=
static
::
render
Options
(
$value
,
$selection
,
$attrs
);
$content
=
static
::
render
SelectOptions
(
$selection
,
$value
,
$attrs
);
$lines
[]
=
static
::
tag
(
'optgroup'
,
"
\n
"
.
$content
.
"
\n
"
,
$groupAttrs
);
}
else
{
$attrs
=
isset
(
$options
[
$key
])
?
$options
[
$key
]
:
array
();
...
...
@@ -883,26 +885,26 @@ class Html
* Renders the HTML tag attributes.
* Boolean attributes such as s 'checked', 'disabled', 'readonly', will be handled specially
* according to [[booleanAttributes]] and [[showBooleanAttributeValues]].
* @param array $
a
ttributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].
* @param array $
tagA
ttributes 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
* in a tag. If there is no attribute, an empty string will be returned.
*/
public
static
function
render
Attributes
(
$a
ttributes
)
public
static
function
render
TagAttributes
(
$tagA
ttributes
)
{
if
(
count
(
$
a
ttributes
)
>
1
)
{
if
(
count
(
$
tagA
ttributes
)
>
1
)
{
$sorted
=
array
();
foreach
(
static
::
$attributeOrder
as
$name
)
{
if
(
isset
(
$
a
ttributes
[
$name
]))
{
$sorted
[
$name
]
=
$
a
ttributes
[
$name
];
if
(
isset
(
$
tagA
ttributes
[
$name
]))
{
$sorted
[
$name
]
=
$
tagA
ttributes
[
$name
];
}
}
$
attributes
=
array_merge
(
$sorted
,
$a
ttributes
);
$
tagAttributes
=
array_merge
(
$sorted
,
$tagA
ttributes
);
}
$html
=
''
;
foreach
(
$
a
ttributes
as
$name
=>
$value
)
{
foreach
(
$
tagA
ttributes
as
$name
=>
$value
)
{
if
(
isset
(
static
::
$booleanAttributes
[
strtolower
(
$name
)]))
{
if
(
$value
||
strcasecmp
(
$name
,
$value
)
===
0
)
{
$html
.=
static
::
$showBooleanAttributeValues
?
"
$name
=
\"
$name
\"
"
:
"
$name
"
;
...
...
tests/unit/framework/util/HtmlTest.php
View file @
2422a134
...
...
@@ -220,22 +220,22 @@ class HtmlTest extends \yii\test\TestCase
public
function
testRadio
()
{
$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="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="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"
value="2" checked="checked" />'
,
Html
::
radio
(
'test'
,
true
,
2
,
array
(
'class'
=>
'a'
,
'uncheck'
=>
'0'
)));
}
public
function
testCheckbox
()
{
$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="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="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"
value="2" checked="checked" />'
,
Html
::
checkbox
(
'test'
,
true
,
2
,
array
(
'class'
=>
'a'
,
'uncheck'
=>
'0'
)));
}
public
function
testDropDownList
()
{
$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
\"
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
\"
>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'
,
'value2'
,
$this
->
getDataItems
()
));
}
public
function
testListBox
()
...
...
@@ -252,41 +252,41 @@ EOD;
<option value="value2">text2</option>
</select>
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
<select name="test" size="4">
<option value="value1<>">text1<></option>
<option value="value 2">text 2</option>
</select>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
listBox
(
'test'
,
$this
->
getDataItems2
(),
null
));
$this
->
assertEquals
(
$expected
,
Html
::
listBox
(
'test'
,
null
,
$this
->
getDataItems2
()
));
$expected
=
<<<EOD
<select name="test" size="4">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
listBox
(
'test'
,
$this
->
getDataItems
(),
'value2'
));
$this
->
assertEquals
(
$expected
,
Html
::
listBox
(
'test'
,
'value2'
,
$this
->
getDataItems
()
));
$expected
=
<<<EOD
<select name="test" size="4">
<option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
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
<select name="test[]" multiple="multiple" size="4">
</select>
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
<input type="hidden" name="test" value="0" /><select name="test" size="4">
</select>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
listBox
(
'test'
,
array
(),
''
,
array
(
'unselect'
=>
'0'
)));
$this
->
assertEquals
(
$expected
,
Html
::
listBox
(
'test'
,
''
,
array
()
,
array
(
'unselect'
=>
'0'
)));
}
public
function
testCheckboxList
()
...
...
@@ -297,30 +297,30 @@ EOD;
<label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
checkboxList
(
'test'
,
$this
->
getDataItems
(),
array
(
'value2'
)));
$this
->
assertEquals
(
$expected
,
Html
::
checkboxList
(
'test'
,
array
(
'value2'
),
$this
->
getDataItems
(
)));
$expected
=
<<<EOD
<label><input type="checkbox" name="test[]" value="value1<>" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
checkboxList
(
'test'
,
$this
->
getDataItems2
(),
array
(
'value2'
)));
$this
->
assertEquals
(
$expected
,
Html
::
checkboxList
(
'test'
,
array
(
'value2'
),
$this
->
getDataItems2
(
)));
$expected
=
<<<EOD
<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>
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
"
,
'unselect'
=>
'0'
,
)));
$expected
=
<<<EOD
<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
0
<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
1
<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
checkboxList
(
'test'
,
$this
->
getDataItems
(),
array
(
'value2'
),
array
(
'item'
=>
function
(
$index
,
$label
,
$name
,
$
value
,
$checked
)
{
return
Html
::
label
(
$label
.
' '
.
Html
::
checkbox
(
$name
,
$value
,
$checked
));
$this
->
assertEquals
(
$expected
,
Html
::
checkboxList
(
'test'
,
array
(
'value2'
),
$this
->
getDataItems
(
),
array
(
'item'
=>
function
(
$index
,
$label
,
$name
,
$
checked
,
$value
)
{
return
$index
.
Html
::
label
(
$label
.
' '
.
Html
::
checkbox
(
$name
,
$checked
,
$value
));
}
)));
}
...
...
@@ -333,38 +333,36 @@ EOD;
<label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
radioList
(
'test'
,
$this
->
getDataItems
(),
array
(
'value2'
)));
$this
->
assertEquals
(
$expected
,
Html
::
radioList
(
'test'
,
array
(
'value2'
),
$this
->
getDataItems
(
)));
$expected
=
<<<EOD
<label><input type="radio" name="test" value="value1<>" /> text1<></label>
<label><input type="radio" name="test" value="value 2" /> text 2</label>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
radioList
(
'test'
,
$this
->
getDataItems2
(),
array
(
'value2'
)));
$this
->
assertEquals
(
$expected
,
Html
::
radioList
(
'test'
,
array
(
'value2'
),
$this
->
getDataItems2
(
)));
$expected
=
<<<EOD
<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>
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
"
,
'unselect'
=>
'0'
,
)));
$expected
=
<<<EOD
<label>text1 <input type="radio" name="test" value="value1" /></label>
<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
0
<label>text1 <input type="radio" name="test" value="value1" /></label>
1
<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
radioList
(
'test'
,
$this
->
getDataItems
(),
array
(
'value2'
),
array
(
'item'
=>
function
(
$index
,
$label
,
$name
,
$
value
,
$checked
)
{
return
Html
::
label
(
$label
.
' '
.
Html
::
radio
(
$name
,
$value
,
$checked
));
$this
->
assertEquals
(
$expected
,
Html
::
radioList
(
'test'
,
array
(
'value2'
),
$this
->
getDataItems
(
),
array
(
'item'
=>
function
(
$index
,
$label
,
$name
,
$
checked
,
$value
)
{
return
$index
.
Html
::
label
(
$label
.
' '
.
Html
::
radio
(
$name
,
$checked
,
$value
));
}
)));
}
public
function
testRenderOptions
()
{
$this
->
assertEquals
(
''
,
Html
::
renderOptions
(
array
()));
$data
=
array
(
'value1'
=>
'label1'
,
'group1'
=>
array
(
...
...
@@ -403,15 +401,15 @@ EOD;
'group12'
=>
array
(
'class'
=>
'group'
),
),
);
$this
->
assertEquals
(
$expected
,
Html
::
render
Options
(
$data
,
array
(
'value111'
,
'value1'
)
,
$attributes
));
$this
->
assertEquals
(
$expected
,
Html
::
render
SelectOptions
(
array
(
'value111'
,
'value1'
),
$data
,
$attributes
));
}
public
function
testRenderAttributes
()
{
$this
->
assertEquals
(
''
,
Html
::
renderAttributes
(
array
()));
$this
->
assertEquals
(
' name="test" value="1<>"'
,
Html
::
renderAttributes
(
array
(
'name'
=>
'test'
,
'empty'
=>
null
,
'value'
=>
'1<>'
)));
$this
->
assertEquals
(
''
,
Html
::
render
Tag
Attributes
(
array
()));
$this
->
assertEquals
(
' name="test" value="1<>"'
,
Html
::
render
Tag
Attributes
(
array
(
'name'
=>
'test'
,
'empty'
=>
null
,
'value'
=>
'1<>'
)));
Html
::
$showBooleanAttributeValues
=
false
;
$this
->
assertEquals
(
' checked disabled'
,
Html
::
renderAttributes
(
array
(
'checked'
=>
'checked'
,
'disabled'
=>
true
,
'hidden'
=>
false
)));
$this
->
assertEquals
(
' checked disabled'
,
Html
::
render
Tag
Attributes
(
array
(
'checked'
=>
'checked'
,
'disabled'
=>
true
,
'hidden'
=>
false
)));
Html
::
$showBooleanAttributeValues
=
true
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment