Skip to content

Commit c409264

Browse files
Fix BaseArrayHelper::htmlDecode() (#19386)
* Fix BaseArrayHelper::htmlDecode() Add missed second argument on recursive calling. * Fix BaseArrayHelper::htmlDecode() `htmlspecialchars_decode()` flags must be same to `htmlspecialchars()` in `BaseArrayHelper::htmlEncode()` * Update ArrayHelperTest.php * Update ArrayHelperTest.php * Update ArrayHelperTest.php * Update CHANGELOG.md * test workflow fix
1 parent f458263 commit c409264

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Yii Framework 2 Change Log
2626
- Bug #19237: Fix OCI PHP 8.1 passing `null` to trim() (longthanhtran)
2727
- Bug #19312: Fix PHP 8.1 error when passing null to `yii\helpers\BaseInflector` (WinterSilence)
2828
- Bug #19368: Fix PHP 8.1 error when `$fileMimeType` is `null` in `yii\validators\FileValidator::validateMimeType()` (bizley)
29+
- Bug #19386: Fix recursive calling `yii\helpers\BaseArrayHelper::htmlDecode()` (WinterSilence)
2930

3031

3132
2.0.45 February 11, 2022

framework/helpers/BaseArrayHelper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,14 @@ public static function htmlEncode($data, $valuesOnly = true, $charset = null)
726726

727727
/**
728728
* Decodes HTML entities into the corresponding characters in an array of strings.
729+
*
729730
* Only array values will be decoded by default.
730731
* If a value is an array, this method will also decode it recursively.
731732
* Only string values will be decoded.
733+
*
732734
* @param array $data data to be decoded
733-
* @param bool $valuesOnly whether to decode array values only. If false,
734-
* both the array keys and array values will be decoded.
735+
* @param bool $valuesOnly whether to decode array values only. If `false`,
736+
* then both the array keys and array values will be decoded.
735737
* @return array the decoded data
736738
* @see https://www.php.net/manual/en/function.htmlspecialchars-decode.php
737739
*/
@@ -740,12 +742,12 @@ public static function htmlDecode($data, $valuesOnly = true)
740742
$d = [];
741743
foreach ($data as $key => $value) {
742744
if (!$valuesOnly && is_string($key)) {
743-
$key = htmlspecialchars_decode($key, ENT_QUOTES);
745+
$key = htmlspecialchars_decode($key, ENT_QUOTES | ENT_SUBSTITUTE);
744746
}
745747
if (is_string($value)) {
746-
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
748+
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES | ENT_SUBSTITUTE);
747749
} elseif (is_array($value)) {
748-
$d[$key] = static::htmlDecode($value);
750+
$d[$key] = static::htmlDecode($value, $valuesOnly);
749751
} else {
750752
$d[$key] = $value;
751753
}

tests/framework/helpers/ArrayHelperTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,29 +1185,35 @@ public function testHtmlDecode()
11851185
3 => 'blank',
11861186
[
11871187
'<>' => 'a&lt;&gt;b',
1188+
'&lt;a&gt;' => '&lt;a href=&quot;index.php?a=1&amp;b=2&quot;&gt;link&lt;/a&gt;',
11881189
'23' => true,
11891190
],
11901191
];
1191-
$this->assertEquals([
1192+
1193+
$expected = [
11921194
'abc' => '123',
11931195
'&lt;' => '>',
11941196
'cde' => false,
11951197
3 => 'blank',
11961198
[
11971199
'<>' => 'a<>b',
1200+
'&lt;a&gt;' => '<a href="index.php?a=1&b=2">link</a>',
11981201
'23' => true,
11991202
],
1200-
], ArrayHelper::htmlDecode($array));
1201-
$this->assertEquals([
1203+
];
1204+
$this->assertEquals($expected, ArrayHelper::htmlDecode($array));
1205+
$expected = [
12021206
'abc' => '123',
12031207
'<' => '>',
12041208
'cde' => false,
12051209
3 => 'blank',
12061210
[
12071211
'<>' => 'a<>b',
1212+
'<a>' => '<a href="index.php?a=1&b=2">link</a>',
12081213
'23' => true,
12091214
],
1210-
], ArrayHelper::htmlDecode($array, false));
1215+
];
1216+
$this->assertEquals($expected, ArrayHelper::htmlDecode($array, false));
12111217
}
12121218

12131219
public function testIsIn()

0 commit comments

Comments
 (0)