mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
WIP #10764
This commit is contained in:
@ -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>";
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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="<>">', 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()
|
||||
|
@ -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
|
||||
*/
|
||||
|
Reference in New Issue
Block a user