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
5227d2db
Commit
5227d2db
authored
Mar 13, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring.
parent
2422a134
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
224 additions
and
180 deletions
+224
-180
Html.php
framework/util/Html.php
+204
-177
HtmlTest.php
tests/unit/framework/util/HtmlTest.php
+20
-3
No files found.
framework/util/Html.php
View file @
5227d2db
...
...
@@ -121,7 +121,6 @@ class Html
'media'
,
);
/**
* Encodes special characters into HTML entities.
* The [[yii\base\Application::charset|application charset]] will be used for encoding.
...
...
@@ -153,15 +152,16 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated HTML tag
* @see beginTag
* @see endTag
*/
public
static
function
tag
(
$name
,
$content
=
''
,
$
tagAttribute
s
=
array
())
public
static
function
tag
(
$name
,
$content
=
''
,
$
option
s
=
array
())
{
$html
=
'<'
.
$name
.
static
::
renderTagAttributes
(
$
tagAttribute
s
);
$html
=
'<'
.
$name
.
static
::
renderTagAttributes
(
$
option
s
);
if
(
isset
(
static
::
$voidElements
[
strtolower
(
$name
)]))
{
return
$html
.
(
static
::
$closeVoidElements
?
' />'
:
'>'
);
}
else
{
...
...
@@ -172,15 +172,16 @@ class Html
/**
* Generates a start tag.
* @param string $name the tag name
* @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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated start tag
* @see endTag
* @see tag
*/
public
static
function
beginTag
(
$name
,
$
tagAttribute
s
=
array
())
public
static
function
beginTag
(
$name
,
$
option
s
=
array
())
{
return
'<'
.
$name
.
static
::
renderTagAttributes
(
$
tagAttribute
s
)
.
'>'
;
return
'<'
.
$name
.
static
::
renderTagAttributes
(
$
option
s
)
.
'>'
;
}
/**
...
...
@@ -208,76 +209,81 @@ class Html
/**
* Generates a style tag.
* @param string $content the style content
* @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.
* If the attributes does not contain "type", a default one with value "text/css" will be used.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "text/css" will be used.
* @return string the generated style tag
*/
public
static
function
style
(
$content
,
$
tagAttribute
s
=
array
())
public
static
function
style
(
$content
,
$
option
s
=
array
())
{
if
(
!
isset
(
$
tagAttribute
s
[
'type'
]))
{
$
tagAttribute
s
[
'type'
]
=
'text/css'
;
if
(
!
isset
(
$
option
s
[
'type'
]))
{
$
option
s
[
'type'
]
=
'text/css'
;
}
return
static
::
tag
(
'style'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
tagAttribute
s
);
return
static
::
tag
(
'style'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
option
s
);
}
/**
* Generates a script tag.
* @param string $content the script content
* @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.
* If the attributes does not contain "type", a default one with value "text/javascript" will be used.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "text/javascript" will be rendered.
* @return string the generated script tag
*/
public
static
function
script
(
$content
,
$
tagAttribute
s
=
array
())
public
static
function
script
(
$content
,
$
option
s
=
array
())
{
if
(
!
isset
(
$
tagAttribute
s
[
'type'
]))
{
$
tagAttribute
s
[
'type'
]
=
'text/javascript'
;
if
(
!
isset
(
$
option
s
[
'type'
]))
{
$
option
s
[
'type'
]
=
'text/javascript'
;
}
return
static
::
tag
(
'script'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
tagAttribute
s
);
return
static
::
tag
(
'script'
,
"/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/"
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated link tag
* @see url
*/
public
static
function
cssFile
(
$url
,
$
tagAttribute
s
=
array
())
public
static
function
cssFile
(
$url
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'rel'
]
=
'stylesheet'
;
$
tagAttribute
s
[
'type'
]
=
'text/css'
;
$
tagAttribute
s
[
'href'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'link'
,
''
,
$
tagAttribute
s
);
$
option
s
[
'rel'
]
=
'stylesheet'
;
$
option
s
[
'type'
]
=
'text/css'
;
$
option
s
[
'href'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'link'
,
''
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated script tag
* @see url
*/
public
static
function
jsFile
(
$url
,
$
tagAttribute
s
=
array
())
public
static
function
jsFile
(
$url
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'type'
]
=
'text/javascript'
;
$
tagAttribute
s
[
'src'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'script'
,
''
,
$
tagAttribute
s
);
$
option
s
[
'type'
]
=
'text/javascript'
;
$
option
s
[
'src'
]
=
static
::
url
(
$url
);
return
static
::
tag
(
'script'
,
''
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated form start tag.
* @see endForm
*/
public
static
function
beginForm
(
$action
=
''
,
$method
=
'post'
,
$
tagAttribute
s
=
array
())
public
static
function
beginForm
(
$action
=
''
,
$method
=
'post'
,
$
option
s
=
array
())
{
$action
=
static
::
url
(
$action
);
...
...
@@ -295,9 +301,9 @@ class Html
$action
=
substr
(
$action
,
0
,
$pos
);
}
$
tagAttribute
s
[
'action'
]
=
$action
;
$
tagAttribute
s
[
'method'
]
=
$method
;
$form
=
static
::
beginTag
(
'form'
,
$
tagAttribute
s
);
$
option
s
[
'action'
]
=
$action
;
$
option
s
[
'method'
]
=
$method
;
$form
=
static
::
beginTag
(
'form'
,
$
option
s
);
if
(
$hiddens
!==
array
())
{
$form
.=
"
\n
"
.
implode
(
"
\n
"
,
$hiddens
);
}
...
...
@@ -323,17 +329,18 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated hyperlink
* @see url
*/
public
static
function
a
(
$text
,
$url
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
a
(
$text
,
$url
=
null
,
$
option
s
=
array
())
{
if
(
$url
!==
null
)
{
$
tagAttribute
s
[
'href'
]
=
static
::
url
(
$url
);
$
option
s
[
'href'
]
=
static
::
url
(
$url
);
}
return
static
::
tag
(
'a'
,
$text
,
$
tagAttribute
s
);
return
static
::
tag
(
'a'
,
$text
,
$
option
s
);
}
/**
...
...
@@ -343,29 +350,31 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated mailto link
*/
public
static
function
mailto
(
$text
,
$email
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
mailto
(
$text
,
$email
=
null
,
$
option
s
=
array
())
{
return
static
::
a
(
$text
,
'mailto:'
.
(
$email
===
null
?
$text
:
$email
),
$
tagAttribute
s
);
return
static
::
a
(
$text
,
'mailto:'
.
(
$email
===
null
?
$text
:
$email
),
$
option
s
);
}
/**
* Generates an image tag.
* @param string $src the image URL. This parameter will be processed by [[url()]].
* @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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated image tag
*/
public
static
function
img
(
$src
,
$
tagAttribute
s
=
array
())
public
static
function
img
(
$src
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'src'
]
=
static
::
url
(
$src
);
if
(
!
isset
(
$
tagAttribute
s
[
'alt'
]))
{
$
tagAttribute
s
[
'alt'
]
=
''
;
$
option
s
[
'src'
]
=
static
::
url
(
$src
);
if
(
!
isset
(
$
option
s
[
'alt'
]))
{
$
option
s
[
'alt'
]
=
''
;
}
return
static
::
tag
(
'img'
,
null
,
$
tagAttribute
s
);
return
static
::
tag
(
'img'
,
null
,
$
option
s
);
}
/**
...
...
@@ -375,14 +384,15 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated label tag
*/
public
static
function
label
(
$content
,
$for
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
label
(
$content
,
$for
=
null
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'for'
]
=
$for
;
return
static
::
tag
(
'label'
,
$content
,
$
tagAttribute
s
);
$
option
s
[
'for'
]
=
$for
;
return
static
::
tag
(
'label'
,
$content
,
$
option
s
);
}
/**
...
...
@@ -392,19 +402,20 @@ 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 $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.
* If the attributes does not contain "type", a default one with value "button" will be used.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "button" will be rendered.
* @return string the generated button tag
*/
public
static
function
button
(
$name
=
null
,
$value
=
null
,
$content
=
'Button'
,
$
tagAttribute
s
=
array
())
public
static
function
button
(
$name
=
null
,
$value
=
null
,
$content
=
'Button'
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'name'
]
=
$name
;
$
tagAttribute
s
[
'value'
]
=
$value
;
if
(
!
isset
(
$
tagAttribute
s
[
'type'
]))
{
$
tagAttribute
s
[
'type'
]
=
'button'
;
$
option
s
[
'name'
]
=
$name
;
$
option
s
[
'value'
]
=
$value
;
if
(
!
isset
(
$
option
s
[
'type'
]))
{
$
option
s
[
'type'
]
=
'button'
;
}
return
static
::
tag
(
'button'
,
$content
,
$
tagAttribute
s
);
return
static
::
tag
(
'button'
,
$content
,
$
option
s
);
}
/**
...
...
@@ -414,14 +425,15 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated submit button tag
*/
public
static
function
submitButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Submit'
,
$
tagAttribute
s
=
array
())
public
static
function
submitButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Submit'
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'type'
]
=
'submit'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
tagAttribute
s
);
$
option
s
[
'type'
]
=
'submit'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
option
s
);
}
/**
...
...
@@ -431,14 +443,15 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated reset button tag
*/
public
static
function
resetButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Reset'
,
$
tagAttribute
s
=
array
())
public
static
function
resetButton
(
$name
=
null
,
$value
=
null
,
$content
=
'Reset'
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'type'
]
=
'reset'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
tagAttribute
s
);
$
option
s
[
'type'
]
=
'reset'
;
return
static
::
button
(
$name
,
$value
,
$content
,
$
option
s
);
}
/**
...
...
@@ -446,94 +459,100 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated input tag
*/
public
static
function
input
(
$type
,
$name
=
null
,
$value
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
input
(
$type
,
$name
=
null
,
$value
=
null
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'type'
]
=
$type
;
$
tagAttribute
s
[
'name'
]
=
$name
;
$
tagAttribute
s
[
'value'
]
=
$value
;
return
static
::
tag
(
'input'
,
null
,
$
tagAttribute
s
);
$
option
s
[
'type'
]
=
$type
;
$
option
s
[
'name'
]
=
$name
;
$
option
s
[
'value'
]
=
$value
;
return
static
::
tag
(
'input'
,
null
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag
*/
public
static
function
buttonInput
(
$name
,
$value
=
'Button'
,
$
tagAttribute
s
=
array
())
public
static
function
buttonInput
(
$name
,
$value
=
'Button'
,
$
option
s
=
array
())
{
return
static
::
input
(
'button'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'button'
,
$name
,
$value
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag
*/
public
static
function
submitInput
(
$name
=
null
,
$value
=
'Submit'
,
$
tagAttribute
s
=
array
())
public
static
function
submitInput
(
$name
=
null
,
$value
=
'Submit'
,
$
option
s
=
array
())
{
return
static
::
input
(
'submit'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'submit'
,
$name
,
$value
,
$
option
s
);
}
/**
* 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 $
tagAttribute
s the attributes of the button tag. The values will be HTML-encoded using [[encode()]].
* @param array $
option
s 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'
,
$
tagAttribute
s
=
array
())
public
static
function
resetInput
(
$name
=
null
,
$value
=
'Reset'
,
$
option
s
=
array
())
{
return
static
::
input
(
'reset'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'reset'
,
$name
,
$value
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag
*/
public
static
function
textInput
(
$name
,
$value
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
textInput
(
$name
,
$value
=
null
,
$
option
s
=
array
())
{
return
static
::
input
(
'text'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'text'
,
$name
,
$value
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag
*/
public
static
function
hiddenInput
(
$name
,
$value
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
hiddenInput
(
$name
,
$value
=
null
,
$
option
s
=
array
())
{
return
static
::
input
(
'hidden'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'hidden'
,
$name
,
$value
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag
*/
public
static
function
passwordInput
(
$name
,
$value
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
passwordInput
(
$name
,
$value
=
null
,
$
option
s
=
array
())
{
return
static
::
input
(
'password'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'password'
,
$name
,
$value
,
$
option
s
);
}
/**
...
...
@@ -543,27 +562,29 @@ 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated button tag
*/
public
static
function
fileInput
(
$name
,
$value
=
null
,
$
tagAttribute
s
=
array
())
public
static
function
fileInput
(
$name
,
$value
=
null
,
$
option
s
=
array
())
{
return
static
::
input
(
'file'
,
$name
,
$value
,
$
tagAttribute
s
);
return
static
::
input
(
'file'
,
$name
,
$value
,
$
option
s
);
}
/**
* 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 $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.
* @param array $options the tag options in terms of name-value pairs. These be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered.
* @return string the generated text area tag
*/
public
static
function
textarea
(
$name
,
$value
=
''
,
$
tagAttribute
s
=
array
())
public
static
function
textarea
(
$name
,
$value
=
''
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'name'
]
=
$name
;
return
static
::
tag
(
'textarea'
,
static
::
encode
(
$value
),
$
tagAttribute
s
);
$
option
s
[
'name'
]
=
$name
;
return
static
::
tag
(
'textarea'
,
static
::
encode
(
$value
),
$
option
s
);
}
/**
...
...
@@ -571,28 +592,29 @@ class Html
* @param string $name the name attribute.
* @param boolean $checked whether the radio button should be checked.
* @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:
* @param array $options the tag options in terms of name-value pairs. The following options are supported:
*
* - uncheck: string, the value associated with the uncheck state of the radio button. When this attribute
* is present, a hidden input will be generated so that if the radio button is not checked and is submitted,
* the value of this attribute will still be submitted to the server via the hidden input.
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
*
* @return string the generated radio button tag
*/
public
static
function
radio
(
$name
,
$checked
=
false
,
$value
=
'1'
,
$
tagAttribute
s
=
array
())
public
static
function
radio
(
$name
,
$checked
=
false
,
$value
=
'1'
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'checked'
]
=
$checked
;
$
tagAttribute
s
[
'value'
]
=
$value
;
if
(
isset
(
$
tagAttribute
s
[
'uncheck'
]))
{
$
option
s
[
'checked'
]
=
$checked
;
$
option
s
[
'value'
]
=
$value
;
if
(
isset
(
$
option
s
[
'uncheck'
]))
{
// add a hidden field so that if the radio button is not selected, it still submits a value
$hidden
=
static
::
hiddenInput
(
$name
,
$
tagAttribute
s
[
'uncheck'
]);
unset
(
$
tagAttribute
s
[
'uncheck'
]);
$hidden
=
static
::
hiddenInput
(
$name
,
$
option
s
[
'uncheck'
]);
unset
(
$
option
s
[
'uncheck'
]);
}
else
{
$hidden
=
''
;
}
return
$hidden
.
static
::
input
(
'radio'
,
$name
,
$value
,
$
tagAttribute
s
);
return
$hidden
.
static
::
input
(
'radio'
,
$name
,
$value
,
$
option
s
);
}
/**
...
...
@@ -600,28 +622,29 @@ class Html
* @param string $name the name attribute.
* @param boolean $checked whether the checkbox should be checked.
* @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:
* @param array $options the tag options in terms of name-value pairs. The following options are supported:
*
* - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute
* is present, a hidden input will be generated so that if the checkbox is not checked and is submitted,
* the value of this attribute will still be submitted to the server via the hidden input.
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
*
* @return string the generated checkbox tag
*/
public
static
function
checkbox
(
$name
,
$checked
=
false
,
$value
=
'1'
,
$
tagAttribute
s
=
array
())
public
static
function
checkbox
(
$name
,
$checked
=
false
,
$value
=
'1'
,
$
option
s
=
array
())
{
$
tagAttribute
s
[
'checked'
]
=
$checked
;
$
tagAttribute
s
[
'value'
]
=
$value
;
if
(
isset
(
$
tagAttribute
s
[
'uncheck'
]))
{
$
option
s
[
'checked'
]
=
$checked
;
$
option
s
[
'value'
]
=
$value
;
if
(
isset
(
$
option
s
[
'uncheck'
]))
{
// add a hidden field so that if the checkbox is not selected, it still submits a value
$hidden
=
static
::
hiddenInput
(
$name
,
$
tagAttribute
s
[
'uncheck'
]);
unset
(
$
tagAttribute
s
[
'uncheck'
]);
$hidden
=
static
::
hiddenInput
(
$name
,
$
option
s
[
'uncheck'
]);
unset
(
$
option
s
[
'uncheck'
]);
}
else
{
$hidden
=
''
;
}
return
$hidden
.
static
::
input
(
'checkbox'
,
$name
,
$value
,
$
tagAttribute
s
);
return
$hidden
.
static
::
input
(
'checkbox'
,
$name
,
$value
,
$
option
s
);
}
/**
...
...
@@ -636,12 +659,10 @@ 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 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:
* @param array $options the tag options in terms of name-value pairs. The following options are supported:
*
* - prompt: string, a prompt text to be displayed as the first option;
* - options: array, the attributes for the option tags. The array keys must be valid option values,
* - options: array, the attributes for the
select
option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example,
*
* ~~~
...
...
@@ -653,13 +674,17 @@ class Html
*
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options',
* except that the array keys represent the optgroup labels specified in $items.
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
*
* @return string the generated drop-down list tag
*/
public
static
function
dropDownList
(
$name
,
$selection
=
null
,
$items
=
array
(),
$
tagAttribute
s
=
array
())
public
static
function
dropDownList
(
$name
,
$selection
=
null
,
$items
=
array
(),
$
option
s
=
array
())
{
$
tagAttribute
s
[
'name'
]
=
$name
;
$
options
=
static
::
renderSelectOptions
(
$selection
,
$items
,
$tagAttribute
s
);
return
static
::
tag
(
'select'
,
"
\n
"
.
$
options
.
"
\n
"
,
$tagAttribute
s
);
$
option
s
[
'name'
]
=
$name
;
$
selectOptions
=
static
::
renderSelectOptions
(
$selection
,
$items
,
$option
s
);
return
static
::
tag
(
'select'
,
"
\n
"
.
$
selectOptions
.
"
\n
"
,
$option
s
);
}
/**
...
...
@@ -674,12 +699,10 @@ 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 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:
* @param array $options the tag options in terms of name-value pairs. The following options are supported:
*
* - prompt: string, a prompt text to be displayed as the first option;
* - options: array, the attributes for the option tags. The array keys must be valid option values,
* - options: array, the attributes for the
select
option tags. The array keys must be valid option values,
* and the array values are the extra attributes for the corresponding option tags. For example,
*
* ~~~
...
...
@@ -694,29 +717,33 @@ class Html
* - unselect: string, the value that will be submitted when no option is selected.
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple
* mode, we can still obtain the posted unselect value.
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
*
* @return string the generated list box tag
*/
public
static
function
listBox
(
$name
,
$selection
=
null
,
$items
=
array
(),
$
tagAttribute
s
=
array
())
public
static
function
listBox
(
$name
,
$selection
=
null
,
$items
=
array
(),
$
option
s
=
array
())
{
if
(
!
isset
(
$
tagAttribute
s
[
'size'
]))
{
$
tagAttribute
s
[
'size'
]
=
4
;
if
(
!
isset
(
$
option
s
[
'size'
]))
{
$
option
s
[
'size'
]
=
4
;
}
if
(
isset
(
$
tagAttributes
[
'multiple'
])
&&
$tagAttribute
s
[
'multiple'
]
&&
substr
(
$name
,
-
2
)
!==
'[]'
)
{
if
(
isset
(
$
options
[
'multiple'
])
&&
$option
s
[
'multiple'
]
&&
substr
(
$name
,
-
2
)
!==
'[]'
)
{
$name
.=
'[]'
;
}
$
tagAttribute
s
[
'name'
]
=
$name
;
if
(
isset
(
$
tagAttribute
s
[
'unselect'
]))
{
$
option
s
[
'name'
]
=
$name
;
if
(
isset
(
$
option
s
[
'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
,
$
tagAttribute
s
[
'unselect'
]);
unset
(
$
tagAttribute
s
[
'unselect'
]);
$hidden
=
static
::
hiddenInput
(
$name
,
$
option
s
[
'unselect'
]);
unset
(
$
option
s
[
'unselect'
]);
}
else
{
$hidden
=
''
;
}
$
options
=
static
::
renderSelectOptions
(
$selection
,
$items
,
$tagAttribute
s
);
return
$hidden
.
static
::
tag
(
'select'
,
"
\n
"
.
$
options
.
"
\n
"
,
$tagAttribute
s
);
$
selectOptions
=
static
::
renderSelectOptions
(
$selection
,
$items
,
$option
s
);
return
$hidden
.
static
::
tag
(
'select'
,
"
\n
"
.
$
selectOptions
.
"
\n
"
,
$option
s
);
}
/**
...
...
@@ -843,23 +870,23 @@ 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 array $tag
Attributes the attribute
s parameter that is passed to the [[dropDownList()]] or [[listBox()]] call.
* @param array $tag
Options the $option
s 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
renderSelectOptions
(
$selection
,
$items
,
&
$tag
Attribute
s
=
array
())
public
static
function
renderSelectOptions
(
$selection
,
$items
,
&
$tag
Option
s
=
array
())
{
$lines
=
array
();
if
(
isset
(
$tag
Attribute
s
[
'prompt'
]))
{
$prompt
=
str_replace
(
' '
,
' '
,
static
::
encode
(
$tag
Attribute
s
[
'prompt'
]));
if
(
isset
(
$tag
Option
s
[
'prompt'
]))
{
$prompt
=
str_replace
(
' '
,
' '
,
static
::
encode
(
$tag
Option
s
[
'prompt'
]));
$lines
[]
=
static
::
tag
(
'option'
,
$prompt
,
array
(
'value'
=>
''
));
}
$options
=
isset
(
$tag
Attributes
[
'options'
])
?
$tagAttribute
s
[
'options'
]
:
array
();
$groups
=
isset
(
$tag
Attributes
[
'groups'
])
?
$tagAttribute
s
[
'groups'
]
:
array
();
unset
(
$tag
Attributes
[
'prompt'
],
$tagAttributes
[
'options'
],
$tagAttribute
s
[
'groups'
]);
$options
=
isset
(
$tag
Options
[
'options'
])
?
$tagOption
s
[
'options'
]
:
array
();
$groups
=
isset
(
$tag
Options
[
'groups'
])
?
$tagOption
s
[
'groups'
]
:
array
();
unset
(
$tag
Options
[
'prompt'
],
$tagOptions
[
'options'
],
$tagOption
s
[
'groups'
]);
foreach
(
$items
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
...
...
@@ -885,26 +912,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 $
tagA
ttributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].
* @param array $
a
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
renderTagAttributes
(
$
tagA
ttributes
)
public
static
function
renderTagAttributes
(
$
a
ttributes
)
{
if
(
count
(
$
tagA
ttributes
)
>
1
)
{
if
(
count
(
$
a
ttributes
)
>
1
)
{
$sorted
=
array
();
foreach
(
static
::
$attributeOrder
as
$name
)
{
if
(
isset
(
$
tagA
ttributes
[
$name
]))
{
$sorted
[
$name
]
=
$
tagA
ttributes
[
$name
];
if
(
isset
(
$
a
ttributes
[
$name
]))
{
$sorted
[
$name
]
=
$
a
ttributes
[
$name
];
}
}
$
tagAttributes
=
array_merge
(
$sorted
,
$tagA
ttributes
);
$
attributes
=
array_merge
(
$sorted
,
$a
ttributes
);
}
$html
=
''
;
foreach
(
$
tagA
ttributes
as
$name
=>
$value
)
{
foreach
(
$
a
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 @
5227d2db
...
...
@@ -233,9 +233,26 @@ class HtmlTest extends \yii\test\TestCase
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'
,
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
()));
$expected
=
<<<EOD
<select name="test">
</select>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
dropDownList
(
'test'
));
$expected
=
<<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
dropDownList
(
'test'
,
null
,
$this
->
getDataItems
()));
$expected
=
<<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
$this
->
assertEquals
(
$expected
,
Html
::
dropDownList
(
'test'
,
'value2'
,
$this
->
getDataItems
()));
}
public
function
testListBox
()
...
...
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