From 9bb0ce4b1cfe52b3a441692599b6d0d93d9ce64b Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Wed, 19 Jun 2013 20:14:40 +0400 Subject: [PATCH 1/3] Html helper pure HTML 5 mode without support old standarts --- framework/yii/helpers/base/Html.php | 61 +++--------- tests/unit/framework/base/FormatterTest.php | 2 +- tests/unit/framework/helpers/HtmlTest.php | 147 ++++++++++++---------------- 3 files changed, 79 insertions(+), 131 deletions(-) diff --git a/framework/yii/helpers/base/Html.php b/framework/yii/helpers/base/Html.php index 8d1a384..d8cf675 100644 --- a/framework/yii/helpers/base/Html.php +++ b/framework/yii/helpers/base/Html.php @@ -21,11 +21,6 @@ use yii\base\Model; class Html { /** - * @var boolean whether to close void (empty) elements. Defaults to true. - * @see voidElements - */ - public static $closeVoidElements = true; - /** * @var array list of void elements (element name => 1) * @see closeVoidElements * @see http://www.w3.org/TR/html-markup/syntax.html#void-element @@ -49,12 +44,6 @@ class Html 'wbr' => 1, ); /** - * @var boolean whether to show the values of boolean attributes in element tags. - * If false, only the attribute names will be generated. - * @see booleanAttributes - */ - public static $showBooleanAttributeValues = true; - /** * @var array list of boolean attributes. The presence of a boolean attribute on * an element represents the true value, and the absence of the attribute represents the false value. * @see showBooleanAttributeValues @@ -165,11 +154,10 @@ class Html */ public static function tag($name, $content = '', $options = array()) { - $html = '<' . $name . static::renderTagAttributes($options); if (isset(static::$voidElements[strtolower($name)])) { - return $html . (static::$closeVoidElements ? ' />' : '>'); + return static::beginTag($name, $options); } else { - return $html . ">$content"; + return static::beginTag($name, $options) . $content . static::endTag($name); } } @@ -185,7 +173,7 @@ class Html */ public static function beginTag($name, $options = array()) { - return '<' . $name . static::renderTagAttributes($options) . '>'; + return "<$name" . static::renderTagAttributes($options) . '>'; } /** @@ -201,16 +189,6 @@ class Html } /** - * Encloses the given content within a CDATA tag. - * @param string $content the content to be enclosed within the CDATA tag - * @return string the CDATA tag with the enclosed content. - */ - public static function cdata($content) - { - return ''; - } - - /** * Generates a style tag. * @param string $content the style content * @param array $options the tag options in terms of name-value pairs. These will be rendered as @@ -221,10 +199,7 @@ class Html */ public static function style($content, $options = array()) { - if (!isset($options['type'])) { - $options['type'] = 'text/css'; - } - return static::tag('style', "/**/", $options); + return static::tag('style', $content, $options); } /** @@ -238,10 +213,7 @@ class Html */ public static function script($content, $options = array()) { - if (!isset($options['type'])) { - $options['type'] = 'text/javascript'; - } - return static::tag('script', "/**/", $options); + return static::tag('script', $content, $options); } /** @@ -256,7 +228,6 @@ class Html public static function cssFile($url, $options = array()) { $options['rel'] = 'stylesheet'; - $options['type'] = 'text/css'; $options['href'] = static::url($url); return static::tag('link', '', $options); } @@ -272,7 +243,6 @@ class Html */ public static function jsFile($url, $options = array()) { - $options['type'] = 'text/javascript'; $options['src'] = static::url($url); return static::tag('script', '', $options); } @@ -313,7 +283,10 @@ class Html // we use hidden fields to add them back foreach (explode('&', substr($action, $pos + 1)) as $pair) { if (($pos1 = strpos($pair, '=')) !== false) { - $hiddenInputs[] = static::hiddenInput(urldecode(substr($pair, 0, $pos1)), urldecode(substr($pair, $pos1 + 1))); + $hiddenInputs[] = static::hiddenInput( + urldecode(substr($pair, 0, $pos1)), + urldecode(substr($pair, $pos1 + 1)) + ); } else { $hiddenInputs[] = static::hiddenInput(urldecode($pair), ''); } @@ -395,7 +368,7 @@ class Html if (!isset($options['alt'])) { $options['alt'] = ''; } - return static::tag('img', null, $options); + return static::tag('img', '', $options); } /** @@ -424,14 +397,10 @@ class Html * @param array $options the tag options in terms of name-value pairs. These 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. - * 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($content = 'Button', $options = array()) { - if (!isset($options['type'])) { - $options['type'] = 'button'; - } return static::tag('button', $content, $options); } @@ -482,7 +451,7 @@ class Html $options['type'] = $type; $options['name'] = $name; $options['value'] = $value; - return static::tag('input', null, $options); + return static::tag('input', '', $options); } /** @@ -497,7 +466,7 @@ class Html { $options['type'] = 'button'; $options['value'] = $label; - return static::tag('input', null, $options); + return static::tag('input', '', $options); } /** @@ -512,7 +481,7 @@ class Html { $options['type'] = 'submit'; $options['value'] = $label; - return static::tag('input', null, $options); + return static::tag('input', '', $options); } /** @@ -526,7 +495,7 @@ class Html { $options['type'] = 'reset'; $options['value'] = $label; - return static::tag('input', null, $options); + return static::tag('input', '', $options); } /** @@ -1315,7 +1284,7 @@ class Html foreach ($attributes as $name => $value) { if (isset(static::$booleanAttributes[strtolower($name)])) { if ($value || strcasecmp($name, $value) === 0) { - $html .= static::$showBooleanAttributeValues ? " $name=\"$name\"" : " $name"; + $html .= " $name"; } } elseif ($value !== null) { $html .= " $name=\"" . static::encode($value) . '"'; diff --git a/tests/unit/framework/base/FormatterTest.php b/tests/unit/framework/base/FormatterTest.php index e9a909f..87a41c9 100644 --- a/tests/unit/framework/base/FormatterTest.php +++ b/tests/unit/framework/base/FormatterTest.php @@ -96,7 +96,7 @@ class FormatterTest extends TestCase public function testAsImage() { $value = 'http://sample.com/img.jpg'; - $this->assertSame("\"\"", $this->formatter->asImage($value)); + $this->assertSame("\"\"", $this->formatter->asImage($value)); } public function testAsBoolean() diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index 28cc86f..14f7fc3 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -44,24 +44,11 @@ class HtmlTest extends TestCase public function testTag() { - $this->assertEquals('
', Html::tag('br')); - $this->assertEquals('', Html::tag('span')); - $this->assertEquals('
content
', Html::tag('div', 'content')); - $this->assertEquals('', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); - - Html::$closeVoidElements = false; - $this->assertEquals('
', Html::tag('br')); $this->assertEquals('', Html::tag('span')); $this->assertEquals('
content
', Html::tag('div', 'content')); $this->assertEquals('', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); - - Html::$closeVoidElements = true; - - $this->assertEquals('', Html::tag('span', '', array('disabled' => true))); - Html::$showBooleanAttributeValues = false; $this->assertEquals('', Html::tag('span', '', array('disabled' => true))); - Html::$showBooleanAttributeValues = true; } public function testBeginTag() @@ -76,36 +63,30 @@ class HtmlTest extends TestCase $this->assertEquals('', Html::endTag('span')); } - public function testCdata() - { - $data = 'test<>'; - $this->assertEquals('', Html::cdata($data)); - } - public function testStyle() { $content = 'a <>'; - $this->assertEquals("", Html::style($content)); - $this->assertEquals("", Html::style($content, array('type' => 'text/less'))); + $this->assertEquals("", Html::style($content)); + $this->assertEquals("", Html::style($content, array('type' => 'text/less'))); } public function testScript() { $content = 'a <>'; - $this->assertEquals("", Html::script($content)); - $this->assertEquals("", Html::script($content, array('type' => 'text/js'))); + $this->assertEquals("", Html::script($content)); + $this->assertEquals("", Html::script($content, array('type' => 'text/js'))); } public function testCssFile() { - $this->assertEquals('', Html::cssFile('http://example.com')); - $this->assertEquals('', Html::cssFile('')); + $this->assertEquals('', Html::cssFile('http://example.com')); + $this->assertEquals('', Html::cssFile('')); } public function testJsFile() { - $this->assertEquals('', Html::jsFile('http://example.com')); - $this->assertEquals('', Html::jsFile('')); + $this->assertEquals('', Html::jsFile('http://example.com')); + $this->assertEquals('', Html::jsFile('')); } public function testBeginForm() @@ -113,8 +94,8 @@ class HtmlTest extends TestCase $this->assertEquals('
', Html::beginForm()); $this->assertEquals('', Html::beginForm('/example', 'get')); $hiddens = array( - '', - '', + '', + '', ); $this->assertEquals('' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get')); } @@ -139,9 +120,9 @@ class HtmlTest extends TestCase public function testImg() { - $this->assertEquals('', Html::img('/example')); - $this->assertEquals('', Html::img('')); - $this->assertEquals('something', Html::img('/example', array('alt' => 'something', 'width' => 10))); + $this->assertEquals('', Html::img('/example')); + $this->assertEquals('', Html::img('')); + $this->assertEquals('something', Html::img('/example', array('alt' => 'something', 'width' => 10))); } public function testLabel() @@ -153,8 +134,8 @@ class HtmlTest extends TestCase public function testButton() { - $this->assertEquals('', Html::button()); - $this->assertEquals('', Html::button('content<>', array('name' => 'test', 'value' => 'value'))); + $this->assertEquals('', Html::button()); + $this->assertEquals('', Html::button('content<>', array('name' => 'test', 'value' => 'value'))); $this->assertEquals('', Html::button('content<>', array('type' => 'submit', 'name' => 'test', 'value' => 'value', 'class' => "t"))); } @@ -172,50 +153,50 @@ class HtmlTest extends TestCase public function testInput() { - $this->assertEquals('', Html::input('text')); - $this->assertEquals('', Html::input('text', 'test', 'value', array('class' => 't'))); + $this->assertEquals('', Html::input('text')); + $this->assertEquals('', Html::input('text', 'test', 'value', array('class' => 't'))); } public function testButtonInput() { - $this->assertEquals('', Html::buttonInput()); - $this->assertEquals('', Html::buttonInput('text', array('name' => 'test', 'class' => 'a'))); + $this->assertEquals('', Html::buttonInput()); + $this->assertEquals('', Html::buttonInput('text', array('name' => 'test', 'class' => 'a'))); } public function testSubmitInput() { - $this->assertEquals('', Html::submitInput()); - $this->assertEquals('', Html::submitInput('text', array('name' => 'test', 'class' => 'a'))); + $this->assertEquals('', Html::submitInput()); + $this->assertEquals('', Html::submitInput('text', array('name' => 'test', 'class' => 'a'))); } public function testResetInput() { - $this->assertEquals('', Html::resetInput()); - $this->assertEquals('', Html::resetInput('text', array('name' => 'test', 'class' => 'a'))); + $this->assertEquals('', Html::resetInput()); + $this->assertEquals('', Html::resetInput('text', array('name' => 'test', 'class' => 'a'))); } public function testTextInput() { - $this->assertEquals('', Html::textInput('test')); - $this->assertEquals('', Html::textInput('test', 'value', array('class' => 't'))); + $this->assertEquals('', Html::textInput('test')); + $this->assertEquals('', Html::textInput('test', 'value', array('class' => 't'))); } public function testHiddenInput() { - $this->assertEquals('', Html::hiddenInput('test')); - $this->assertEquals('', Html::hiddenInput('test', 'value', array('class' => 't'))); + $this->assertEquals('', Html::hiddenInput('test')); + $this->assertEquals('', Html::hiddenInput('test', 'value', array('class' => 't'))); } public function testPasswordInput() { - $this->assertEquals('', Html::passwordInput('test')); - $this->assertEquals('', Html::passwordInput('test', 'value', array('class' => 't'))); + $this->assertEquals('', Html::passwordInput('test')); + $this->assertEquals('', Html::passwordInput('test', 'value', array('class' => 't'))); } public function testFileInput() { - $this->assertEquals('', Html::fileInput('test')); - $this->assertEquals('', Html::fileInput('test', 'value', array('class' => 't'))); + $this->assertEquals('', Html::fileInput('test')); + $this->assertEquals('', Html::fileInput('test', 'value', array('class' => 't'))); } public function testTextarea() @@ -226,16 +207,16 @@ class HtmlTest extends TestCase public function testRadio() { - $this->assertEquals('', Html::radio('test')); - $this->assertEquals('', Html::radio('test', true, array('class' => 'a', 'value' => null))); - $this->assertEquals('', Html::radio('test', true, array('class' => 'a' , 'uncheck' => '0', 'value' => 2))); + $this->assertEquals('', Html::radio('test')); + $this->assertEquals('', Html::radio('test', true, array('class' => 'a', 'value' => null))); + $this->assertEquals('', Html::radio('test', true, array('class' => 'a' , 'uncheck' => '0', 'value' => 2))); } public function testCheckbox() { - $this->assertEquals('', Html::checkbox('test')); - $this->assertEquals('', Html::checkbox('test', true, array('class' => 'a', 'value' => null))); - $this->assertEquals('', Html::checkbox('test', true, array('class' => 'a', 'uncheck' => '0', 'value' => 2))); + $this->assertEquals('', Html::checkbox('test')); + $this->assertEquals('', Html::checkbox('test', true, array('class' => 'a', 'value' => null))); + $this->assertEquals('', Html::checkbox('test', true, array('class' => 'a', 'uncheck' => '0', 'value' => 2))); } public function testDropDownList() @@ -256,7 +237,7 @@ EOD; $expected = << - + EOD; $this->assertEqualsWithoutLE($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); @@ -287,26 +268,26 @@ EOD; $expected = << - + EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', 'value2', $this->getDataItems())); $expected = << - - + + EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems())); $expected = << + EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', null, array(), array('multiple' => true))); $expected = << EOD; @@ -318,29 +299,29 @@ EOD; $this->assertEquals('', Html::checkboxList('test')); $expected = << text1 - + + EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', array('value2'), $this->getDataItems())); $expected = << text1<> - + + EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2())); $expected = <<
- +
+ EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( - 'separator' => "
\n", + 'separator' => "
\n", 'unselect' => '0', ))); $expected = <<text1 -1 +0 +1 EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( 'item' => function ($index, $label, $name, $checked, $value) { @@ -354,29 +335,29 @@ EOD; $this->assertEquals('', Html::radioList('test')); $expected = << text1 - + + EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', array('value2'), $this->getDataItems())); $expected = << text1<> - + + EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', array('value2'), $this->getDataItems2())); $expected = <<
- +
+ EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( - 'separator' => "
\n", + 'separator' => "
\n", 'unselect' => '0', ))); $expected = <<text1 -1 +0 +1 EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( 'item' => function ($index, $label, $name, $checked, $value) { @@ -401,11 +382,11 @@ EOD; ); $expected = <<please select<> - + - + @@ -432,9 +413,7 @@ EOD; { $this->assertEquals('', Html::renderTagAttributes(array())); $this->assertEquals(' name="test" value="1<>"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>'))); - Html::$showBooleanAttributeValues = false; $this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false))); - Html::$showBooleanAttributeValues = true; } public function testAddCssClass() From 5bf0ba562f4cce591a4a61de5f53e257222d7273 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Wed, 19 Jun 2013 20:22:06 +0400 Subject: [PATCH 2/3] Comments fixes --- framework/yii/helpers/base/Html.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/framework/yii/helpers/base/Html.php b/framework/yii/helpers/base/Html.php index d8cf675..bca17e3 100644 --- a/framework/yii/helpers/base/Html.php +++ b/framework/yii/helpers/base/Html.php @@ -22,7 +22,6 @@ class Html { /** * @var array list of void elements (element name => 1) - * @see closeVoidElements * @see http://www.w3.org/TR/html-markup/syntax.html#void-element */ public static $voidElements = array( @@ -46,7 +45,6 @@ class Html /** * @var array list of boolean attributes. The presence of a boolean attribute on * an element represents the true value, and the absence of the attribute represents the false value. - * @see showBooleanAttributeValues * @see http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes */ public static $booleanAttributes = array( From ef9ceafdca4cd6bad23701fafed800e30479d9d8 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 20 Jun 2013 00:03:22 +0400 Subject: [PATCH 3/3] Refactoring --- framework/yii/helpers/base/Html.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/framework/yii/helpers/base/Html.php b/framework/yii/helpers/base/Html.php index bca17e3..043fc42 100644 --- a/framework/yii/helpers/base/Html.php +++ b/framework/yii/helpers/base/Html.php @@ -152,11 +152,8 @@ class Html */ public static function tag($name, $content = '', $options = array()) { - if (isset(static::$voidElements[strtolower($name)])) { - return static::beginTag($name, $options); - } else { - return static::beginTag($name, $options) . $content . static::endTag($name); - } + $html = "<$name" . static::renderTagAttributes($options) . '>'; + return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content"; } /**