Allow setting css options per CSS file in AssetBundle::, fixes #10158

This commit is contained in:
Mehdi Achour
2015-11-16 15:12:14 +01:00
parent 61acf28338
commit 5619e98eda
3 changed files with 45 additions and 5 deletions

View File

@@ -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
---------------------

View File

@@ -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;
}
}
}

View File

@@ -201,6 +201,21 @@ EOF;
$expected = <<<EOF
123<script src="/js/jquery.js"></script>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 = <<<EOF
1<link href="/js/default_options.css" rel="stylesheet" media="screen" hreflang="en">
<link href="/js/tv.css" rel="stylesheet" media="tv" hreflang="en">
<link href="/js/screen_and_print.css" rel="stylesheet" media="screen, print" hreflang="en">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'];
}