mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-16 06:17:56 +08:00
Html WIP
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
namespace yii\util;
|
namespace yii\util;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
use yii\base\InvalidParamException;
|
use yii\base\InvalidParamException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -279,4 +280,35 @@ class ArrayHelper
|
|||||||
$args[] = &$array;
|
$args[] = &$array;
|
||||||
call_user_func_array('array_multisort', $args);
|
call_user_func_array('array_multisort', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes special characters in an array of strings into HTML entities.
|
||||||
|
* Both the array keys and values will be encoded if needed.
|
||||||
|
* If a value is an array, this method will also encode it recursively.
|
||||||
|
* @param array $data data to be encoded
|
||||||
|
* @param string $charset the charset that the data is using. If not set,
|
||||||
|
* [[\yii\base\Application::charset]] will be used.
|
||||||
|
* @return array the encoded data
|
||||||
|
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
|
||||||
|
*/
|
||||||
|
public static function htmlEncode($data, $charset = null)
|
||||||
|
{
|
||||||
|
if ($charset === null) {
|
||||||
|
$charset = Yii::$app->charset;
|
||||||
|
}
|
||||||
|
$d = array();
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
if (is_string($key)) {
|
||||||
|
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
|
||||||
|
}
|
||||||
|
if (is_string($value)) {
|
||||||
|
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
|
||||||
|
} elseif (is_array($value)) {
|
||||||
|
$value = static::htmlEncode($value);
|
||||||
|
}
|
||||||
|
$d[$key] = $value;
|
||||||
|
}
|
||||||
|
return $d;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -16,183 +16,154 @@ use Yii;
|
|||||||
class Html
|
class Html
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var integer the counter for generating automatic input field names.
|
* @var boolean whether to close void (empty) elements. Defaults to true.
|
||||||
|
* @see voidElements
|
||||||
*/
|
*/
|
||||||
public static $count = 0;
|
public static $closeVoidElements = true;
|
||||||
/**
|
/**
|
||||||
* @var boolean whether to close single tags. Defaults to true. Can be set to false for HTML5.
|
* @var array list of void elements (element name => 1)
|
||||||
|
* @see http://www.w3.org/TR/html-markup/syntax.html#void-element
|
||||||
*/
|
*/
|
||||||
public static $closeSingleTags = true;
|
public static $voidElements = array(
|
||||||
|
'area' => 1,
|
||||||
|
'base' => 1,
|
||||||
|
'br' => 1,
|
||||||
|
'col' => 1,
|
||||||
|
'command' => 1,
|
||||||
|
'embed' => 1,
|
||||||
|
'hr' => 1,
|
||||||
|
'img' => 1,
|
||||||
|
'input' => 1,
|
||||||
|
'keygen' => 1,
|
||||||
|
'link' => 1,
|
||||||
|
'meta' => 1,
|
||||||
|
'param' => 1,
|
||||||
|
'source' => 1,
|
||||||
|
'track' => 1,
|
||||||
|
'wbr' => 1,
|
||||||
|
);
|
||||||
/**
|
/**
|
||||||
* @var boolean whether to render special attributes value. Defaults to true. Can be set to false for HTML5.
|
* @var boolean whether to render special attributes value. Defaults to true. Can be set to false for HTML5.
|
||||||
*/
|
*/
|
||||||
public static $renderSpecialAttributesValue = true;
|
public static $renderSpecialAttributesValue = true;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes special characters into HTML entities.
|
* Encodes special characters into HTML entities.
|
||||||
* The {@link CApplication::charset application charset} will be used for encoding.
|
* The [[yii\base\Application::charset|application charset]] will be used for encoding.
|
||||||
* @param string $text data to be encoded
|
* @param string $content the content to be encoded
|
||||||
* @return string the encoded data
|
* @return string the encoded content
|
||||||
|
* @see decode
|
||||||
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
|
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
|
||||||
*/
|
*/
|
||||||
public static function encode($text)
|
public static function encode($content)
|
||||||
{
|
{
|
||||||
return htmlspecialchars($text, ENT_QUOTES, Yii::$app->charset);
|
return htmlspecialchars($content, ENT_QUOTES, Yii::$app->charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes special HTML entities back to the corresponding characters.
|
* Decodes special HTML entities back to the corresponding characters.
|
||||||
* This is the opposite of {@link encode()}.
|
* This is the opposite of [[encode()]].
|
||||||
* @param string $text data to be decoded
|
* @param string $content the content to be decoded
|
||||||
* @return string the decoded data
|
* @return string the decoded content
|
||||||
|
* @see encode
|
||||||
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
|
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
|
||||||
*/
|
*/
|
||||||
public static function decode($text)
|
public static function decode($content)
|
||||||
{
|
{
|
||||||
return htmlspecialchars_decode($text, ENT_QUOTES);
|
return htmlspecialchars_decode($content, ENT_QUOTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes special characters in an array of strings into HTML entities.
|
* Generates a complete HTML tag.
|
||||||
* Both the array keys and values will be encoded if needed.
|
* @param string $name the tag name
|
||||||
* If a value is an array, this method will also encode it recursively.
|
* @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded.
|
||||||
* The {@link CApplication::charset application charset} will be used for encoding.
|
* @param array $attributes the element attributes. The values will be HTML-encoded using [[encode()]].
|
||||||
* @param array $data data to be encoded
|
* Attributes whose value is null will be ignored and not put in the tag returned.
|
||||||
* @return array the encoded data
|
* @return string the generated HTML tag
|
||||||
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
|
* @see beginTag
|
||||||
|
* @see endTag
|
||||||
*/
|
*/
|
||||||
public static function encodeArray($data)
|
public static function tag($name, $content = '', $attributes = array())
|
||||||
{
|
{
|
||||||
$d = array();
|
$html = '<' . $name . static::renderAttributes($attributes);
|
||||||
foreach ($data as $key => $value) {
|
if (isset(static::$voidElements[strtolower($name)])) {
|
||||||
if (is_string($key)) {
|
return $html . (static::$closeVoidElements ? ' />' : '>');
|
||||||
$key = htmlspecialchars($key, ENT_QUOTES, Yii::$app->charset);
|
|
||||||
}
|
|
||||||
if (is_string($value)) {
|
|
||||||
$value = htmlspecialchars($value, ENT_QUOTES, Yii::$app->charset);
|
|
||||||
} elseif (is_array($value)) {
|
|
||||||
$value = static::encodeArray($value);
|
|
||||||
}
|
|
||||||
$d[$key] = $value;
|
|
||||||
}
|
|
||||||
return $d;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates an HTML element.
|
|
||||||
* @param string $tag the tag name
|
|
||||||
* @param array $htmlOptions the element attributes. The values will be HTML-encoded using {@link encode()}.
|
|
||||||
* If an 'encode' attribute is given and its value is false,
|
|
||||||
* the rest of the attribute values will NOT be HTML-encoded.
|
|
||||||
* Since version 1.1.5, attributes whose value is null will not be rendered.
|
|
||||||
* @param mixed $content the content to be enclosed between open and close element tags. It will not be HTML-encoded.
|
|
||||||
* If false, it means there is no body content.
|
|
||||||
* @param boolean $closeTag whether to generate the close tag.
|
|
||||||
* @return string the generated HTML element tag
|
|
||||||
*/
|
|
||||||
public static function tag($tag, $htmlOptions = array(), $content = false, $closeTag = true)
|
|
||||||
{
|
|
||||||
$html = '<' . $tag . static::renderAttributes($htmlOptions);
|
|
||||||
if ($content === false) {
|
|
||||||
return $closeTag && static::$closeSingleTags ? $html . ' />' : $html . '>';
|
|
||||||
} else {
|
} else {
|
||||||
return $closeTag ? $html . '>' . $content . '</' . $tag . '>' : $html . '>' . $content;
|
return $html . ">$content</$name>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates an open HTML element.
|
* Generates a start tag.
|
||||||
* @param string $tag the tag name
|
* @param string $name the tag name
|
||||||
* @param array $htmlOptions the element attributes. The values will be HTML-encoded using {@link encode()}.
|
* @param array $attributes the element attributes. The values will be HTML-encoded using [[encode()]].
|
||||||
* If an 'encode' attribute is given and its value is false,
|
* Attributes whose value is null will be ignored and not put in the tag returned.
|
||||||
* the rest of the attribute values will NOT be HTML-encoded.
|
* @return string the generated start tag
|
||||||
* Since version 1.1.5, attributes whose value is null will not be rendered.
|
* @see endTag
|
||||||
* @return string the generated HTML element tag
|
* @see tag
|
||||||
*/
|
*/
|
||||||
public static function openTag($tag, $htmlOptions = array())
|
public static function beginTag($name, $attributes = array())
|
||||||
{
|
{
|
||||||
return '<' . $tag . static::renderAttributes($htmlOptions) . '>';
|
return '<' . $name . static::renderAttributes($attributes) . '>';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a close HTML element.
|
* Generates an end tag.
|
||||||
* @param string $tag the tag name
|
* @param string $name the tag name
|
||||||
* @return string the generated HTML element tag
|
* @return string the generated end tag
|
||||||
|
* @see beginTag
|
||||||
|
* @see tag
|
||||||
*/
|
*/
|
||||||
public static function closeTag($tag)
|
public static function endTag($name)
|
||||||
{
|
{
|
||||||
return '</' . $tag . '>';
|
return !static::$closeVoidElements && isset(static::$voidElements[strtolower($name)]) ? '' : "</$name>";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encloses the given string within a CDATA tag.
|
* Encloses the given content within a CDATA tag.
|
||||||
* @param string $text the string to be enclosed
|
* @param string $content the content to be enclosed within the CDATA tag
|
||||||
* @return string the CDATA tag with the enclosed content.
|
* @return string the CDATA tag with the enclosed content.
|
||||||
*/
|
*/
|
||||||
public static function cdata($text)
|
public static function cdata($content)
|
||||||
{
|
{
|
||||||
return '<![CDATA[' . $text . ']]>';
|
return '<![CDATA[' . $content . ']]>';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a meta tag that can be inserted in the head section of HTML page.
|
* Generates a style tag.
|
||||||
* @param string $content content attribute of the meta tag
|
* @param string $content the style content
|
||||||
* @param string $name name attribute of the meta tag. If null, the attribute will not be generated
|
* @param array $attributes the attributes of the style tag. The values will be HTML-encoded using [[encode()]].
|
||||||
* @param string $httpEquiv http-equiv attribute of the meta tag. If null, the attribute will not be generated
|
* Attributes whose value is null will be ignored and not put in the tag returned.
|
||||||
* @param array $options other options in name-value pairs (e.g. 'scheme', 'lang')
|
* If the attributes does not contain "type", a default one with value "text/css" will be used.
|
||||||
* @return string the generated meta tag
|
* @return string the generated style tag
|
||||||
*/
|
*/
|
||||||
public static function metaTag($content, $name = null, $httpEquiv = null, $options = array())
|
public static function style($content, $attributes = array())
|
||||||
{
|
{
|
||||||
if ($name !== null) {
|
if (!isset($attributes['type'])) {
|
||||||
$options['name'] = $name;
|
$attributes['type'] = 'text/css';
|
||||||
}
|
}
|
||||||
if ($httpEquiv !== null) {
|
return static::beginTag('style', $attributes)
|
||||||
$options['http-equiv'] = $httpEquiv;
|
. "\n/*<![CDATA[*/\n{$content}\n/*]]>*/\n"
|
||||||
}
|
. static::endTag('style');
|
||||||
$options['content'] = $content;
|
|
||||||
return static::tag('meta', $options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a link tag that can be inserted in the head section of HTML page.
|
* Generates a script tag.
|
||||||
* Do not confuse this method with {@link link()}. The latter generates a hyperlink.
|
* @param string $content the script content
|
||||||
* @param string $relation rel attribute of the link tag. If null, the attribute will not be generated.
|
* @param array $attributes the attributes of the script tag. The values will be HTML-encoded using [[encode()]].
|
||||||
* @param string $type type attribute of the link tag. If null, the attribute will not be generated.
|
* Attributes whose value is null will be ignored and not put in the tag returned.
|
||||||
* @param string $href href attribute of the link tag. If null, the attribute will not be generated.
|
* If the attributes does not contain "type", a default one with value "text/javascript" will be used.
|
||||||
* @param string $media media attribute of the link tag. If null, the attribute will not be generated.
|
* @return string the generated script tag
|
||||||
* @param array $options other options in name-value pairs
|
|
||||||
* @return string the generated link tag
|
|
||||||
*/
|
*/
|
||||||
public static function linkTag($relation = null, $type = null, $href = null, $media = null, $options = array())
|
public static function script($content, $attributes = array())
|
||||||
{
|
{
|
||||||
if ($relation !== null) {
|
if (!isset($attributes['type'])) {
|
||||||
$options['rel'] = $relation;
|
$attributes['type'] = 'text/javascript';
|
||||||
}
|
}
|
||||||
if ($type !== null) {
|
return static::beginTag('script', $attributes)
|
||||||
$options['type'] = $type;
|
. "\n/*<![CDATA[*/\n{$content}\n/*]]>*/\n"
|
||||||
}
|
. static::endTag('script');
|
||||||
if ($href !== null) {
|
|
||||||
$options['href'] = $href;
|
|
||||||
}
|
|
||||||
if ($media !== null) {
|
|
||||||
$options['media'] = $media;
|
|
||||||
}
|
|
||||||
return static::tag('link', $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encloses the given CSS content with a CSS tag.
|
|
||||||
* @param string $text the CSS content
|
|
||||||
* @param string $media the media that this CSS should apply to.
|
|
||||||
* @return string the CSS properly enclosed
|
|
||||||
*/
|
|
||||||
public static function css($text, $media = '')
|
|
||||||
{
|
|
||||||
if ($media !== '') {
|
|
||||||
$media = ' media="' . $media . '"';
|
|
||||||
}
|
|
||||||
return "<style type=\"text/css\"{$media}>\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</style>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user