Commit 4fcd5003 by Qiang Xue

Fixes #2906: Added support for using conditional comments for js and css files…

Fixes #2906: Added support for using conditional comments for js and css files registered through asset bundles and Html helper
parent b13d1e80
......@@ -23,6 +23,7 @@ Yii Framework 2 Change Log
- Bug #3236: Return value for DateTime->format('U') casted to double to allow correct date formatting (pgaultier)
- Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue)
- Enh #2837: Error page now shows arguments in stack trace method calls (samdark)
- Enh #2906: Added support for using conditional comments for js and css files registered through asset bundles and Html helper (exromany, qiangxue)
- Enh #3008: Added `Html::errorSummary()` (qiangxue)
- Enh #3088: The debug and gii modules will manage their own URL rules now (hiltonjanfield, qiangxue)
- Enh #3103: debugger panel is now not displayed when printing a page (githubjeka)
......
......@@ -193,13 +193,18 @@ class BaseHtml
/**
* Generates a link tag that refers to an external CSS file.
* @param array|string $url the URL of the external CSS file. This parameter will be processed by [[\yii\helpers\Url::to()]].
* @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.
* @param array|string $url the URL of the external CSS file. This parameter will be processed by [[Url::to()]].
* @param array $options the tag options in terms of name-value pairs. The following option is specially handled:
*
* - condition: specifies the conditional comments for IE, e.g., `lt IE 9`. When this is specified,
* the generated `script` tag will be enclosed within the conditional comments. This is mainly useful
* for supporting old versions of IE browsers.
*
* The rest of the options will be rendered as the attributes of the resulting link tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
* @return string the generated link tag
* @see \yii\helpers\Url::to()
* @see Url::to()
*/
public static function cssFile($url, $options = [])
{
......@@ -208,29 +213,45 @@ class BaseHtml
}
$options['href'] = Url::to($url);
return static::tag('link', '', $options);
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return "<!--[if $condition]-->\n" . static::tag('link', '', $options) . "\n<![endif]-->";
} else {
return static::tag('link', '', $options);
}
}
/**
* Generates a script tag that refers to an external JavaScript file.
* @param string $url the URL of the external JavaScript file. This parameter will be processed by [[\yii\helpers\Url::to()]].
* @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.
* @param string $url the URL of the external JavaScript file. This parameter will be processed by [[Url::to()]].
* @param array $options the tag options in terms of name-value pairs. The following option is specially handled:
*
* - condition: specifies the conditional comments for IE, e.g., `lt IE 9`. When this is specified,
* the generated `script` tag will be enclosed within the conditional comments. This is mainly useful
* for supporting old versions of IE browsers.
*
* The rest of the options will be rendered as the attributes of the resulting script tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
* @return string the generated script tag
* @see \yii\helpers\Url::to()
* @see Url::to()
*/
public static function jsFile($url, $options = [])
{
$options['src'] = Url::to($url);
return static::tag('script', '', $options);
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return "<!--[if $condition]-->\n" . static::tag('script', '', $options) . "\n<![endif]-->";
} else {
return static::tag('script', '', $options);
}
}
/**
* Generates a form start tag.
* @param array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Url::to()]].
* @param array|string $action the form action URL. This parameter will be processed by [[Url::to()]].
* @param string $method the form submission method, such as "post", "get", "put", "delete" (case-insensitive).
* Since most browsers only support "post" and "get", if other methods are given, they will
* be simulated using "post", and a hidden input will be added which contains the actual method type.
......@@ -301,7 +322,7 @@ class BaseHtml
* @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code
* such as an image tag. If this is coming from end users, you should consider [[encode()]]
* it to prevent XSS attacks.
* @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[\yii\helpers\Url::to()]]
* @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[Url::to()]]
* and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute
* will not be generated.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as
......@@ -342,7 +363,7 @@ class BaseHtml
/**
* Generates an image tag.
* @param array|string $src the image URL. This parameter will be processed by [[\yii\helpers\Url::to()]].
* @param array|string $src the image URL. This parameter will be processed by [[Url::to()]].
* @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.
......
......@@ -95,12 +95,12 @@ class AssetBundle extends Object
*/
public $css = [];
/**
* @var array the options that will be passed to [[\yii\web\View::registerJsFile()]]
* @var array the options that will be passed to [[View::registerJsFile()]]
* when registering the JS files in this bundle.
*/
public $jsOptions = [];
/**
* @var array the options that will be passed to [[\yii\web\View::registerCssFile()]]
* @var array the options that will be passed to [[View::registerCssFile()]]
* when registering the CSS files in this bundle.
*/
public $cssOptions = [];
......
......@@ -362,6 +362,7 @@ class View extends \yii\base\View
* @param string $url the CSS file to be registered.
* @param array $depends the names of the asset bundles that this CSS file depends on
* @param array $options the HTML attributes for the link tag.
* Please refer to [[Html::cssFile()]] for supported options.
* @param string $key the key that identifies the CSS script file. If null, it will use
* $url as the key. If two CSS files are registered with the same key, the latter
* will overwrite the former.
......@@ -422,6 +423,8 @@ class View extends \yii\base\View
* - [[POS_BEGIN]]: at the beginning of the body section
* - [[POS_END]]: at the end of the body section. This is the default value.
*
* Please refer to [[Html::jsFile()]] for other supported options.
*
* @param string $key the key that identifies the JS script file. If null, it will use
* $url as the key. If two JS files are registered with the same key, the latter
* will overwrite the former.
......
......@@ -85,12 +85,14 @@ class HtmlTest extends TestCase
{
$this->assertEquals('<link href="http://example.com" rel="stylesheet">', Html::cssFile('http://example.com'));
$this->assertEquals('<link href="/test" rel="stylesheet">', Html::cssFile(''));
$this->assertEquals("<!--[if IE 9]-->\n" . '<link href="http://example.com" rel="stylesheet">' . "\n<![endif]-->", Html::cssFile('http://example.com', ['condition' => 'IE 9']));
}
public function testJsFile()
{
$this->assertEquals('<script src="http://example.com"></script>', Html::jsFile('http://example.com'));
$this->assertEquals('<script src="/test"></script>', Html::jsFile(''));
$this->assertEquals("<!--[if IE 9]-->\n" . '<script src="http://example.com"></script>' . "\n<![endif]-->", Html::jsFile('http://example.com', ['condition' => 'IE 9']));
}
public function testBeginForm()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment