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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
03e212bc
Commit
03e212bc
authored
Mar 11, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added HtmlTest.
parent
30d70be0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
297 additions
and
28 deletions
+297
-28
Html.php
framework/util/Html.php
+56
-22
Application.php
framework/web/Application.php
+5
-5
ArrayHelperTest.php
tests/unit/framework/util/ArrayHelperTest.php
+1
-1
HtmlTest.php
tests/unit/framework/util/HtmlTest.php
+234
-0
.gitignore
tests/unit/runtime/.gitignore
+1
-0
No files found.
framework/util/Html.php
View file @
03e212bc
...
...
@@ -217,7 +217,7 @@ class Html
if
(
!
isset
(
$attributes
[
'type'
]))
{
$attributes
[
'type'
]
=
'text/css'
;
}
return
static
::
tag
(
'style'
,
"
\n
/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/
\n
"
,
$attributes
);
return
static
::
tag
(
'style'
,
"
/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/
"
,
$attributes
);
}
/**
...
...
@@ -233,7 +233,7 @@ class Html
if
(
!
isset
(
$attributes
[
'type'
]))
{
$attributes
[
'type'
]
=
'text/javascript'
;
}
return
static
::
tag
(
'script'
,
"
\n
/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/
\n
"
,
$attributes
);
return
static
::
tag
(
'script'
,
"
/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/
"
,
$attributes
);
}
/**
...
...
@@ -278,23 +278,25 @@ class Html
*/
public
static
function
beginForm
(
$action
=
''
,
$method
=
'post'
,
$attributes
=
array
())
{
$attributes
[
'action'
]
=
$url
=
static
::
url
(
$action
);
$attributes
[
'method'
]
=
$method
;
$form
=
static
::
beginTag
(
'form'
,
$attributes
);
$action
=
static
::
url
(
$action
);
// query parameters in the action are ignored for GET method
// we use hidden fields to add them back
$hiddens
=
array
();
if
(
!
strcasecmp
(
$method
,
'get'
)
&&
(
$pos
=
strpos
(
$
url
,
'?'
))
!==
false
)
{
foreach
(
explode
(
'&'
,
substr
(
$
url
,
$pos
+
1
))
as
$pair
)
{
if
((
$pos
=
strpos
(
$pair
,
'='
))
!==
false
)
{
$hiddens
[]
=
static
::
hiddenInput
(
urldecode
(
substr
(
$pair
,
0
,
$pos
)),
urldecode
(
substr
(
$pair
,
$pos
+
1
)));
if
(
!
strcasecmp
(
$method
,
'get'
)
&&
(
$pos
=
strpos
(
$
action
,
'?'
))
!==
false
)
{
foreach
(
explode
(
'&'
,
substr
(
$
action
,
$pos
+
1
))
as
$pair
)
{
if
((
$pos
1
=
strpos
(
$pair
,
'='
))
!==
false
)
{
$hiddens
[]
=
static
::
hiddenInput
(
urldecode
(
substr
(
$pair
,
0
,
$pos
1
)),
urldecode
(
substr
(
$pair
,
$pos1
+
1
)));
}
else
{
$hiddens
[]
=
static
::
hiddenInput
(
urldecode
(
$pair
),
''
);
}
}
$action
=
substr
(
$action
,
0
,
$pos
);
}
$attributes
[
'action'
]
=
$action
;
$attributes
[
'method'
]
=
$method
;
$form
=
static
::
beginTag
(
'form'
,
$attributes
);
if
(
$hiddens
!==
array
())
{
$form
.=
"
\n
"
.
implode
(
"
\n
"
,
$hiddens
);
}
...
...
@@ -696,17 +698,20 @@ class Html
if
(
!
isset
(
$attributes
[
'size'
]))
{
$attributes
[
'size'
]
=
4
;
}
if
(
isset
(
$attributes
[
'multiple'
])
&&
$attributes
[
'multiple'
]
&&
substr
(
$name
,
-
2
)
!==
'[]'
)
{
$name
.=
'[]'
;
}
$attributes
[
'name'
]
=
$name
;
if
(
isset
(
$attributes
[
'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
,
$attributes
[
'unselect'
]);
unset
(
$attributes
[
'unselect'
]);
}
else
{
$hidden
=
''
;
}
if
(
isset
(
$attributes
[
'multiple'
])
&&
$attributes
[
'multiple'
]
&&
substr
(
$name
,
-
2
)
!==
'[]'
)
{
$name
.=
'[]'
;
}
$attributes
[
'name'
]
=
$name
;
$options
=
static
::
renderOptions
(
$items
,
$selection
,
$attributes
);
return
$hidden
.
static
::
tag
(
'select'
,
"
\n
"
.
$options
.
"
\n
"
,
$attributes
);
}
...
...
@@ -720,8 +725,13 @@ class Html
* 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 callable $formatter a callback that can be used to customize the generation of the HTML code
* corresponding to a single checkbox. The signature of this callback must be:
* @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.
* By setting this option, a hidden input will be generated.
* - separator: string, the HTML code that separates items.
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $value, $checked)
...
...
@@ -732,12 +742,13 @@ class Html
* value and the checked status of the checkbox input.
* @return string the generated checkbox list
*/
public
static
function
checkboxList
(
$name
,
$items
,
$selection
=
null
,
$
formatter
=
null
)
public
static
function
checkboxList
(
$name
,
$items
,
$selection
=
null
,
$
options
=
array
()
)
{
if
(
substr
(
$name
,
-
2
)
!==
'[]'
)
{
$name
.=
'[]'
;
}
$formatter
=
isset
(
$options
[
'item'
])
?
$options
[
'item'
]
:
null
;
$lines
=
array
();
$index
=
0
;
foreach
(
$items
as
$value
=>
$label
)
{
...
...
@@ -752,7 +763,16 @@ class Html
$index
++
;
}
return
implode
(
"
\n
"
,
$lines
);
if
(
isset
(
$options
[
'unselect'
]))
{
// add a hidden field so that if the list box has no option being selected, it still submits a value
$name2
=
substr
(
$name
,
-
2
)
===
'[]'
?
substr
(
$name
,
0
,
-
2
)
:
$name
;
$hidden
=
static
::
hiddenInput
(
$name2
,
$options
[
'unselect'
]);
}
else
{
$hidden
=
''
;
}
$separator
=
isset
(
$options
[
'separator'
])
?
$options
[
'separator'
]
:
"
\n
"
;
return
$hidden
.
implode
(
$separator
,
$lines
);
}
/**
...
...
@@ -763,8 +783,13 @@ class Html
* 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 callable $formatter a callback that can be used to customize the generation of the HTML code
* corresponding to a single radio button. The signature of this callback must be:
* @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.
* By setting this option, a hidden input will be generated.
* - separator: string, the HTML code that separates items.
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $value, $checked)
...
...
@@ -775,8 +800,9 @@ 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
,
$selection
=
null
,
$
formatter
=
null
)
public
static
function
radioList
(
$name
,
$items
,
$selection
=
null
,
$
options
=
array
()
)
{
$formatter
=
isset
(
$options
[
'item'
])
?
$options
[
'item'
]
:
null
;
$lines
=
array
();
$index
=
0
;
foreach
(
$items
as
$value
=>
$label
)
{
...
...
@@ -791,7 +817,15 @@ class Html
$index
++
;
}
return
implode
(
"
\n
"
,
$lines
);
$separator
=
isset
(
$options
[
'separator'
])
?
$options
[
'separator'
]
:
"
\n
"
;
if
(
isset
(
$options
[
'unselect'
]))
{
// add a hidden field so that if the list box has no option being selected, it still submits a value
$hidden
=
static
::
hiddenInput
(
$name
,
$options
[
'unselect'
]);
}
else
{
$hidden
=
''
;
}
return
$hidden
.
implode
(
$separator
,
$lines
);
}
/**
...
...
framework/web/Application.php
View file @
03e212bc
...
...
@@ -66,9 +66,9 @@ class Application extends \yii\base\Application
* Creates a URL using the given route and parameters.
*
* This method first normalizes the given route by converting a relative route into an absolute one.
* A relative route is a route without
slash. If the route is an empty string, it stands for
*
the route of the currently active [[controller]]. If the route is not empty, it stands for
*
an action ID of the [[controller]]
.
* A relative route is a route without
a leading slash. It is considered to be relative to the currently
*
requested route. If the route is an empty string, it stands for the route of the currently active
*
[[controller]]. Otherwise, the [[Controller::uniqueId]] will be prepended to the route
.
*
* After normalizing the route, this method calls [[\yii\web\UrlManager::createUrl()]]
* to create a relative URL.
...
...
@@ -81,12 +81,12 @@ class Application extends \yii\base\Application
*/
public
function
createUrl
(
$route
,
$params
=
array
())
{
if
(
str
pos
(
$route
,
'/'
)
===
false
)
{
if
(
str
ncmp
(
$route
,
'/'
,
1
)
!==
0
)
{
// a relative route
if
(
$this
->
controller
!==
null
)
{
$route
=
$route
===
''
?
$this
->
controller
->
route
:
$this
->
controller
->
uniqueId
.
'/'
.
$route
;
}
else
{
throw
new
InvalidParamException
(
'
No active controller exists for resolving a relative route
.'
);
throw
new
InvalidParamException
(
'
Relative route cannot be handled because there is no active controller
.'
);
}
}
return
$this
->
getUrlManager
()
->
createUrl
(
$route
,
$params
);
...
...
tests/unit/framework/util/ArrayHelperTest.php
View file @
03e212bc
<?php
namespace
yiiunit\framework\
db
;
namespace
yiiunit\framework\
util
;
use
yii\util\ArrayHelper
;
...
...
tests/unit/framework/util/HtmlTest.php
0 → 100644
View file @
03e212bc
<?php
namespace
yiiunit\framework\util
;
use
Yii
;
use
yii\util\Html
;
use
yii\web\Application
;
class
HtmlTest
extends
\yii\test\TestCase
{
public
function
setUp
()
{
new
Application
(
'test'
,
'@yiiunit/runtime'
,
array
(
'components'
=>
array
(
'request'
=>
array
(
'class'
=>
'yii\web\Request'
,
'url'
=>
'/test'
,
),
),
));
}
public
function
tearDown
()
{
Yii
::
$app
=
null
;
}
public
function
testEncode
()
{
$this
->
assertEquals
(
"a<>&"'"
,
Html
::
encode
(
"a<>&
\"
'"
));
}
public
function
testDecode
()
{
$this
->
assertEquals
(
"a<>&
\"
'"
,
Html
::
decode
(
"a<>&"'"
));
}
public
function
testTag
()
{
$this
->
assertEquals
(
'<br />'
,
Html
::
tag
(
'br'
));
$this
->
assertEquals
(
'<span></span>'
,
Html
::
tag
(
'span'
));
$this
->
assertEquals
(
'<div>content</div>'
,
Html
::
tag
(
'div'
,
'content'
));
$this
->
assertEquals
(
'<input type="text" name="test" value="<>" />'
,
Html
::
tag
(
'input'
,
''
,
array
(
'type'
=>
'text'
,
'name'
=>
'test'
,
'value'
=>
'<>'
)));
Html
::
$closeVoidElements
=
false
;
$this
->
assertEquals
(
'<br>'
,
Html
::
tag
(
'br'
));
$this
->
assertEquals
(
'<span></span>'
,
Html
::
tag
(
'span'
));
$this
->
assertEquals
(
'<div>content</div>'
,
Html
::
tag
(
'div'
,
'content'
));
$this
->
assertEquals
(
'<input type="text" name="test" value="<>">'
,
Html
::
tag
(
'input'
,
''
,
array
(
'type'
=>
'text'
,
'name'
=>
'test'
,
'value'
=>
'<>'
)));
Html
::
$closeVoidElements
=
true
;
}
public
function
testBeginTag
()
{
$this
->
assertEquals
(
'<br>'
,
Html
::
beginTag
(
'br'
));
$this
->
assertEquals
(
'<span id="test" class="title">'
,
Html
::
beginTag
(
'span'
,
array
(
'id'
=>
'test'
,
'class'
=>
'title'
)));
}
public
function
testEndTag
()
{
$this
->
assertEquals
(
'</br>'
,
Html
::
endTag
(
'br'
));
$this
->
assertEquals
(
'</span>'
,
Html
::
endTag
(
'span'
));
}
public
function
testCdata
()
{
$data
=
'test<>'
;
$this
->
assertEquals
(
'<![CDATA['
.
$data
.
']]>'
,
Html
::
cdata
(
$data
));
}
public
function
testStyle
()
{
$content
=
'a <>'
;
$this
->
assertEquals
(
"<style type=
\"
text/css
\"
>/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/</style>"
,
Html
::
style
(
$content
));
$this
->
assertEquals
(
"<style type=
\"
text/less
\"
>/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/</style>"
,
Html
::
style
(
$content
,
array
(
'type'
=>
'text/less'
)));
}
public
function
testScript
()
{
$content
=
'a <>'
;
$this
->
assertEquals
(
"<script type=
\"
text/javascript
\"
>/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/</script>"
,
Html
::
script
(
$content
));
$this
->
assertEquals
(
"<script type=
\"
text/js
\"
>/*<![CDATA[*/
\n
{
$content
}
\n
/*]]>*/</script>"
,
Html
::
script
(
$content
,
array
(
'type'
=>
'text/js'
)));
}
public
function
testCssFile
()
{
$this
->
assertEquals
(
'<link type="text/css" href="http://example.com" rel="stylesheet" />'
,
Html
::
cssFile
(
'http://example.com'
));
$this
->
assertEquals
(
'<link type="text/css" href="/test" rel="stylesheet" />'
,
Html
::
cssFile
(
''
));
}
public
function
testJsFile
()
{
$this
->
assertEquals
(
'<script type="text/javascript" src="http://example.com"></script>'
,
Html
::
jsFile
(
'http://example.com'
));
$this
->
assertEquals
(
'<script type="text/javascript" src="/test"></script>'
,
Html
::
jsFile
(
''
));
}
public
function
testBeginForm
()
{
$this
->
assertEquals
(
'<form action="/test" method="post">'
,
Html
::
beginForm
());
$this
->
assertEquals
(
'<form action="/example" method="get">'
,
Html
::
beginForm
(
'/example'
,
'get'
));
$hiddens
=
array
(
'<input type="hidden" name="id" value="1" />'
,
'<input type="hidden" name="title" value="<" />'
,
);
$this
->
assertEquals
(
'<form action="/example" method="get">'
.
"
\n
"
.
implode
(
"
\n
"
,
$hiddens
),
Html
::
beginForm
(
'/example?id=1&title=%3C'
,
'get'
));
}
public
function
testEndForm
()
{
$this
->
assertEquals
(
'</form>'
,
Html
::
endForm
());
}
public
function
testA
()
{
$this
->
assertEquals
(
'<a>something<></a>'
,
Html
::
a
(
'something<>'
));
$this
->
assertEquals
(
'<a href="/example">something</a>'
,
Html
::
a
(
'something'
,
'/example'
));
$this
->
assertEquals
(
'<a href="/test">something</a>'
,
Html
::
a
(
'something'
,
''
));
}
public
function
testMailto
()
{
$this
->
assertEquals
(
'<a href="mailto:test<>">test<></a>'
,
Html
::
mailto
(
'test<>'
));
$this
->
assertEquals
(
'<a href="mailto:test>">test<></a>'
,
Html
::
mailto
(
'test<>'
,
'test>'
));
}
public
function
testImg
()
{
$this
->
assertEquals
(
'<img src="/example" alt="" />'
,
Html
::
img
(
'/example'
));
$this
->
assertEquals
(
'<img src="/test" alt="" />'
,
Html
::
img
(
''
));
$this
->
assertEquals
(
'<img src="/example" width="10" alt="something" />'
,
Html
::
img
(
'/example'
,
array
(
'alt'
=>
'something'
,
'width'
=>
10
)));
}
public
function
testLabel
()
{
$this
->
assertEquals
(
'<label>something<></label>'
,
Html
::
label
(
'something<>'
));
$this
->
assertEquals
(
'<label for="a">something<></label>'
,
Html
::
label
(
'something<>'
,
'a'
));
$this
->
assertEquals
(
'<label class="test" for="a">something<></label>'
,
Html
::
label
(
'something<>'
,
'a'
,
array
(
'class'
=>
'test'
)));
}
public
function
testButton
()
{
$this
->
assertEquals
(
'<button type="button">Button</button>'
,
Html
::
button
());
$this
->
assertEquals
(
'<button type="button" name="test" value="value">content<></button>'
,
Html
::
button
(
'test'
,
'value'
,
'content<>'
));
$this
->
assertEquals
(
'<button type="submit" class="t" name="test" value="value">content<></button>'
,
Html
::
button
(
'test'
,
'value'
,
'content<>'
,
array
(
'type'
=>
'submit'
,
'class'
=>
"t"
)));
}
public
function
testSubmitButton
()
{
$this
->
assertEquals
(
'<button type="submit">Submit</button>'
,
Html
::
submitButton
());
$this
->
assertEquals
(
'<button type="submit" class="t" name="test" value="value">content<></button>'
,
Html
::
submitButton
(
'test'
,
'value'
,
'content<>'
,
array
(
'class'
=>
't'
)));
}
public
function
testResetButton
()
{
$this
->
assertEquals
(
'<button type="reset">Reset</button>'
,
Html
::
resetButton
());
$this
->
assertEquals
(
'<button type="reset" class="t" name="test" value="value">content<></button>'
,
Html
::
resetButton
(
'test'
,
'value'
,
'content<>'
,
array
(
'class'
=>
't'
)));
}
public
function
testInput
()
{
$this
->
assertEquals
(
'<input type="text" />'
,
Html
::
input
(
'text'
));
$this
->
assertEquals
(
'<input type="text" class="t" name="test" value="value" />'
,
Html
::
input
(
'text'
,
'test'
,
'value'
,
array
(
'class'
=>
't'
)));
}
public
function
testButtonInput
()
{
}
public
function
testSubmitInput
()
{
}
public
function
testResetInput
()
{
}
public
function
testTextInput
()
{
}
public
function
testHiddenInput
()
{
}
public
function
testPasswordInput
()
{
}
public
function
testFileInput
()
{
}
public
function
testTextarea
()
{
}
public
function
testRadio
()
{
}
public
function
testCheckbox
()
{
}
public
function
testDropDownList
()
{
}
public
function
testListBox
()
{
}
public
function
testCheckboxList
()
{
}
public
function
testRadioList
()
{
}
public
function
testRenderOptions
()
{
}
public
function
testRenderAttributes
()
{
}
public
function
testUrl
()
{
}
}
tests/unit/runtime/.gitignore
0 → 100644
View file @
03e212bc
*
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