From 5619e98eda55ceb860045fceb6b163b591e1c53c Mon Sep 17 00:00:00 2001 From: Mehdi Achour Date: Mon, 16 Nov 2015 15:12:14 +0100 Subject: [PATCH] Allow setting css options per CSS file in AssetBundle::, fixes #10158 --- framework/CHANGELOG.md | 1 + framework/web/AssetBundle.php | 22 +++++++++++++++----- tests/framework/web/AssetBundleTest.php | 27 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f9afa26ae5..ca2eabf396 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -62,6 +62,7 @@ Yii Framework 2 Change Log - Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark) - Chg #9953: `TimestampBehavior::getValue()` changed to make value processing consistent with `AttributeBehavior::getValue()` (silverfire) - New #10083: Added wrapper for PHP webserver (samdark) +- Enh #10158: Added the possibility to specify CSS options per file in `AssetBundle::$css` (machour) 2.0.6 August 05, 2015 --------------------- diff --git a/framework/web/AssetBundle.php b/framework/web/AssetBundle.php index bf197d2ceb..a40ebc77fc 100644 --- a/framework/web/AssetBundle.php +++ b/framework/web/AssetBundle.php @@ -8,6 +8,7 @@ namespace yii\web; use yii\base\Object; +use yii\helpers\ArrayHelper; use yii\helpers\Url; use Yii; @@ -86,8 +87,12 @@ class AssetBundle extends Object */ public $js = []; /** - * @var array list of CSS files that this bundle contains. Each CSS file can be specified - * in one of the three formats as explained in [[js]]. + * @var array list of CSS files that this bundle contains. Each CSS file can be specified in one + * of the following formats: + * + * - a string, with an absolute URL or a relative path, as explained in [[js]]. + * - an array, with a first entry being the URL or relative path, and a list of key => pair values + * that will be used to overwrite [[cssOptions]] settings. * * Note that only forward slash "/" can be used as directory separator. */ @@ -147,7 +152,10 @@ class AssetBundle extends Object $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions); } foreach ($this->css as $css) { - $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions); + $css = ArrayHelper::toArray($css); + $file = array_shift($css); + $options = ArrayHelper::merge($this->cssOptions, $css); + $view->registerCssFile($manager->getAssetUrl($this, $file), $options); } } @@ -170,8 +178,12 @@ class AssetBundle extends Object } } foreach ($this->css as $i => $css) { - if (Url::isRelative($css)) { - $this->css[$i] = $converter->convert($css, $this->basePath); + $css = ArrayHelper::toArray($css); + $file = array_shift($css); + if (Url::isRelative($file)) { + $css = ArrayHelper::merge($this->cssOptions, $css); + array_unshift($css, $converter->convert($file, $this->basePath)); + $this->css[$i] = $css; } } } diff --git a/tests/framework/web/AssetBundleTest.php b/tests/framework/web/AssetBundleTest.php index 2f67ebe6e2..ec030102c4 100644 --- a/tests/framework/web/AssetBundleTest.php +++ b/tests/framework/web/AssetBundleTest.php @@ -201,6 +201,21 @@ EOF; $expected = <<4 +EOF; + $this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php')); + } + + public function testCssOptions() + { + $view = $this->getView(); + + $this->assertEmpty($view->assetBundles); + TestAssetCssOptions::register($view); + + $expected = << + +234 EOF; $this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php')); } @@ -271,3 +286,15 @@ class TestAssetCircleB extends AssetBundle 'yiiunit\\framework\\web\\TestAssetCircleA' ]; } + +class TestAssetCssOptions extends AssetBundle +{ + public $basePath = '@testWebRoot/js'; + public $baseUrl = '@testWeb/js'; + public $css = [ + 'default_options.css', + ['tv.css', 'media' => 'tv'], + ['screen_and_print.css', 'media' => 'screen, print'] + ]; + public $cssOptions = ['media' => 'screen', 'hreflang' => 'en']; +}