diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md
index c4824f4f17..57bb48e89e 100644
--- a/framework/CHANGELOG.md
+++ b/framework/CHANGELOG.md
@@ -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 #13513: Fixed RBAC migration to work correctly on Oracle DBMS (silverfire)
- 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
diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php
index a3fbbed6fb..eeabe3d26b 100644
--- a/framework/helpers/BaseHtml.php
+++ b/framework/helpers/BaseHtml.php
@@ -430,11 +430,22 @@ class BaseHtml
* 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.
* 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
*/
public static function img($src, $options = [])
{
$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'])) {
$options['alt'] = '';
}
diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php
index a83b4dd867..ea41b6f2c4 100644
--- a/tests/framework/helpers/HtmlTest.php
+++ b/tests/framework/helpers/HtmlTest.php
@@ -132,11 +132,98 @@ class HtmlTest extends TestCase
$this->assertEquals('test<>', Html::mailto('test<>', 'test>'));
}
- public function testImg()
+ /**
+ * @return array
+ */
+ public function imgDataProvider()
{
- $this->assertEquals('
', Html::img('/example'));
- $this->assertEquals('
', Html::img(''));
- $this->assertEquals('
', Html::img('/example', ['alt' => 'something', 'width' => 10]));
+ return [
+ [
+ '
',
+ '/example',
+ [],
+ ],
+ [
+ '
',
+ '',
+ [],
+ ],
+ [
+ '
',
+ '/example',
+ [
+ 'alt' => 'something',
+ 'width' => 10,
+ ],
+ ],
+ [
+ '
',
+ '/base-url',
+ [
+ 'srcset' => [
+ ],
+ ],
+ ],
+ [
+ '
',
+ '/base-url',
+ [
+ 'srcset' => [
+ '9001w' => '/example-9001w',
+ ],
+ ],
+ ],
+ [
+ '
',
+ '/base-url',
+ [
+ 'srcset' => [
+ '100w' => '/example-100w',
+ '500w' => '/example-500w',
+ '1500w' => '/example-1500w',
+ ],
+ ],
+ ],
+ [
+ '
',
+ '/base-url',
+ [
+ 'srcset' => [
+ '1x' => '/example-1x',
+ '2x' => '/example-2x',
+ '3x' => '/example-3x',
+ '4x' => '/example-4x',
+ '5x' => '/example-5x',
+ ],
+ ],
+ ],
+ [
+ '
',
+ '/base-url',
+ [
+ 'srcset' => [
+ '1.42x' => '/example-1.42x',
+ '2.0x' => '/example-2.0x',
+ '3.99999x' => '/example-3.99999x',
+ ],
+ ],
+ ],
+ [
+ '
',
+ '/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()