This commit is contained in:
Daniel Gomez Pan
2016-02-07 16:39:41 +01:00
parent 2505e7ef63
commit cd7039e97c
5 changed files with 30 additions and 17 deletions

View File

@ -119,7 +119,7 @@ class BaseHtml
/**
* Generates a complete HTML tag.
* @param string $name the tag name
* @param string|boolean|null $name the tag name. If $name is null or false, the corresponding content will be rendered without any tag.
* @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 $options the HTML tag attributes (HTML options) in terms of name-value pairs.
@ -137,6 +137,9 @@ class BaseHtml
*/
public static function tag($name, $content = '', $options = [])
{
if ($name === null || $name === false) {
return $content;
}
$html = "<$name" . static::renderTagAttributes($options) . '>';
return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>";
}

View File

@ -103,12 +103,8 @@ class ListView extends BaseListView
}
$options = $this->itemOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div');
if ($tag !== false) {
$options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
return Html::tag($tag, $content, $options);
} else {
return $content;
}
}
}

View File

@ -176,11 +176,7 @@ class Menu extends Widget
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'ul');
if ($tag !== false) {
echo Html::tag($tag, $this->renderItems($items), $options);
} else {
echo $this->renderItems($items);
}
}
}
@ -221,12 +217,8 @@ class Menu extends Widget
'{items}' => $this->renderItems($item['items']),
]);
}
if ($tag === false) {
$lines[] = $menu;
} else {
$lines[] = Html::tag($tag, $menu, $options);
}
}
return implode("\n", $lines);
}

View File

@ -50,6 +50,8 @@ class HtmlTest extends TestCase
$this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', ['type' => 'text', 'name' => 'test', 'value' => '<>']));
$this->assertEquals('<span disabled></span>', Html::tag('span', '', ['disabled' => true]));
$this->assertEquals('test', Html::tag(false, 'test'));
$this->assertEquals('test', Html::tag(null, 'test'));
}
public function testBeginTag()

View File

@ -162,6 +162,26 @@ class BreadcrumbsTest extends \yiiunit\TestCase
$this->assertEquals('<li><a class="external" href="http://example.com">demo</a></li>' . "\n", $result);
}
public function testTag()
{
$this->breadcrumbs->homeLink = ['label' => 'home-link'];
$this->breadcrumbs->links = ['label' => 'My Home Page', 'url' => 'http://my.example.com/yii2/link/page'];
$this->breadcrumbs->itemTemplate = "{link}\n";
$this->breadcrumbs->activeItemTemplate = "{link}\n";
$this->breadcrumbs->tag = false;
$expectedHtml = "home-link\n"
. "My Home Page\n"
. "http://my.example.com/yii2/link/page\n";
ob_start();
$this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* Helper methods
*/