mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 13:02:24 +08:00
Fixes #13576: Added support of srcset to yii\helpers\Html::img()
This commit is contained in:
committed by
Alexander Makarov
parent
8e1a5eccde
commit
a26d28f8e3
@ -12,6 +12,7 @@ Yii Framework 2 Change Log
|
|||||||
- Bug #11404: `yii\base\Model::loadMultiple()` returns true even if `yii\base\Model::load()` returns false (zvook)
|
- Bug #11404: `yii\base\Model::loadMultiple()` returns true even if `yii\base\Model::load()` returns false (zvook)
|
||||||
- Bug #13513: Fixed RBAC migration to work correctly on Oracle DBMS (silverfire)
|
- Bug #13513: Fixed RBAC migration to work correctly on Oracle DBMS (silverfire)
|
||||||
- Enh #13550: Refactored unset call order in `yii\di\ServiceLocator::set()` (Lanrik)
|
- Enh #13550: Refactored unset call order in `yii\di\ServiceLocator::set()` (Lanrik)
|
||||||
|
- Enh #13576: Added support of `srcset` to `yii\helpers\Html::img()` (Kolyunya)
|
||||||
|
|
||||||
|
|
||||||
2.0.11.2 February 08, 2017
|
2.0.11.2 February 08, 2017
|
||||||
|
|||||||
@ -430,11 +430,22 @@ class BaseHtml
|
|||||||
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
|
* 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.
|
* If a value is null, the corresponding attribute will not be rendered.
|
||||||
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
|
||||||
|
* @since 2.0.12 It is possible to pass the "srcset" option as an array which keys are
|
||||||
|
* descriptors and values are URLs. All URLs will be processed by [[Url::to()]].
|
||||||
* @return string the generated image tag
|
* @return string the generated image tag
|
||||||
*/
|
*/
|
||||||
public static function img($src, $options = [])
|
public static function img($src, $options = [])
|
||||||
{
|
{
|
||||||
$options['src'] = Url::to($src);
|
$options['src'] = Url::to($src);
|
||||||
|
|
||||||
|
if (isset($options['srcset']) && is_array($options['srcset'])) {
|
||||||
|
$srcset = [];
|
||||||
|
foreach ($options['srcset'] as $descriptor => $url) {
|
||||||
|
$srcset[] = Url::to($url) . ' ' . $descriptor;
|
||||||
|
}
|
||||||
|
$options['srcset'] = implode(',', $srcset);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isset($options['alt'])) {
|
if (!isset($options['alt'])) {
|
||||||
$options['alt'] = '';
|
$options['alt'] = '';
|
||||||
}
|
}
|
||||||
|
|||||||
@ -132,11 +132,98 @@ class HtmlTest extends TestCase
|
|||||||
$this->assertEquals('<a href="mailto:test>">test<></a>', Html::mailto('test<>', 'test>'));
|
$this->assertEquals('<a href="mailto:test>">test<></a>', Html::mailto('test<>', 'test>'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testImg()
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function imgDataProvider()
|
||||||
{
|
{
|
||||||
$this->assertEquals('<img src="/example" alt="">', Html::img('/example'));
|
return [
|
||||||
$this->assertEquals('<img src="/test" alt="">', Html::img(''));
|
[
|
||||||
$this->assertEquals('<img src="/example" width="10" alt="something">', Html::img('/example', ['alt' => 'something', 'width' => 10]));
|
'<img src="/example" alt="">',
|
||||||
|
'/example',
|
||||||
|
[],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/test" alt="">',
|
||||||
|
'',
|
||||||
|
[],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/example" width="10" alt="something">',
|
||||||
|
'/example',
|
||||||
|
[
|
||||||
|
'alt' => 'something',
|
||||||
|
'width' => 10,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/base-url" alt="" srcset="">',
|
||||||
|
'/base-url',
|
||||||
|
[
|
||||||
|
'srcset' => [
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/base-url" alt="" srcset="/example-9001w 9001w">',
|
||||||
|
'/base-url',
|
||||||
|
[
|
||||||
|
'srcset' => [
|
||||||
|
'9001w' => '/example-9001w',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/base-url" alt="" srcset="/example-100w 100w,/example-500w 500w,/example-1500w 1500w">',
|
||||||
|
'/base-url',
|
||||||
|
[
|
||||||
|
'srcset' => [
|
||||||
|
'100w' => '/example-100w',
|
||||||
|
'500w' => '/example-500w',
|
||||||
|
'1500w' => '/example-1500w',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/base-url" alt="" srcset="/example-1x 1x,/example-2x 2x,/example-3x 3x,/example-4x 4x,/example-5x 5x">',
|
||||||
|
'/base-url',
|
||||||
|
[
|
||||||
|
'srcset' => [
|
||||||
|
'1x' => '/example-1x',
|
||||||
|
'2x' => '/example-2x',
|
||||||
|
'3x' => '/example-3x',
|
||||||
|
'4x' => '/example-4x',
|
||||||
|
'5x' => '/example-5x',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/base-url" alt="" srcset="/example-1.42x 1.42x,/example-2.0x 2.0x,/example-3.99999x 3.99999x">',
|
||||||
|
'/base-url',
|
||||||
|
[
|
||||||
|
'srcset' => [
|
||||||
|
'1.42x' => '/example-1.42x',
|
||||||
|
'2.0x' => '/example-2.0x',
|
||||||
|
'3.99999x' => '/example-3.99999x',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'<img src="/base-url" alt="" srcset="/example-1x 1x,/example-2x 2x,/example-3x 3x">',
|
||||||
|
'/base-url',
|
||||||
|
[
|
||||||
|
'srcset' => '/example-1x 1x,/example-2x 2x,/example-3x 3x',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider imgDataProvider
|
||||||
|
*/
|
||||||
|
public function testImg($expected, $src, $options)
|
||||||
|
{
|
||||||
|
$this->assertEquals($expected, Html::img($src, $options));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLabel()
|
public function testLabel()
|
||||||
|
|||||||
Reference in New Issue
Block a user