diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 739da0a1a8..21a9416c58 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 7326b8d049..bf264d48a5 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -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 "\n" . static::tag('link', '', $options) . "\n"; + } 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 "\n" . static::tag('script', '', $options) . "\n"; + } 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. diff --git a/framework/web/AssetBundle.php b/framework/web/AssetBundle.php index 60cfac242c..3d79b0f31a 100644 --- a/framework/web/AssetBundle.php +++ b/framework/web/AssetBundle.php @@ -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 = []; diff --git a/framework/web/View.php b/framework/web/View.php index a1c1e7909f..8818d36fa5 100644 --- a/framework/web/View.php +++ b/framework/web/View.php @@ -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. diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index ada3a9e4ad..e5f4f50836 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -85,12 +85,14 @@ class HtmlTest extends TestCase { $this->assertEquals('', Html::cssFile('http://example.com')); $this->assertEquals('', Html::cssFile('')); + $this->assertEquals("\n" . '' . "\n", Html::cssFile('http://example.com', ['condition' => 'IE 9'])); } public function testJsFile() { $this->assertEquals('', Html::jsFile('http://example.com')); $this->assertEquals('', Html::jsFile('')); + $this->assertEquals("\n" . '' . "\n", Html::jsFile('http://example.com', ['condition' => 'IE 9'])); } public function testBeginForm()