Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BaseArrayHelper::htmlDecode() #19386

Merged
merged 8 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Yii Framework 2 Change Log
- Bug #19237: Fix OCI PHP 8.1 passing `null` to trim() (longthanhtran)
- Bug #19312: Fix PHP 8.1 error when passing null to `yii\helpers\BaseInflector` (WinterSilence)
- Bug #19368: Fix PHP 8.1 error when `$fileMimeType` is `null` in `yii\validators\FileValidator::validateMimeType()` (bizley)
- Bug #19386: Fix recursive calling `yii\helpers\BaseArrayHelper::htmlDecode()` (WinterSilence)


2.0.45 February 11, 2022
Expand Down
12 changes: 7 additions & 5 deletions framework/helpers/BaseArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,14 @@ public static function htmlEncode($data, $valuesOnly = true, $charset = null)

/**
* Decodes HTML entities into the corresponding characters in an array of strings.
*
* Only array values will be decoded by default.
* If a value is an array, this method will also decode it recursively.
* Only string values will be decoded.
*
* @param array $data data to be decoded
* @param bool $valuesOnly whether to decode array values only. If false,
* both the array keys and array values will be decoded.
* @param bool $valuesOnly whether to decode array values only. If `false`,
* then both the array keys and array values will be decoded.
* @return array the decoded data
* @see https://www.php.net/manual/en/function.htmlspecialchars-decode.php
*/
Expand All @@ -740,12 +742,12 @@ public static function htmlDecode($data, $valuesOnly = true)
$d = [];
foreach ($data as $key => $value) {
if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars_decode($key, ENT_QUOTES);
$key = htmlspecialchars_decode($key, ENT_QUOTES | ENT_SUBSTITUTE);
}
if (is_string($value)) {
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES | ENT_SUBSTITUTE);
} elseif (is_array($value)) {
$d[$key] = static::htmlDecode($value);
$d[$key] = static::htmlDecode($value, $valuesOnly);
} else {
$d[$key] = $value;
}
Expand Down
14 changes: 10 additions & 4 deletions tests/framework/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,29 +1185,35 @@ public function testHtmlDecode()
3 => 'blank',
[
'<>' => 'a&lt;&gt;b',
WinterSilence marked this conversation as resolved.
Show resolved Hide resolved
'&lt;a&gt;' => '&lt;a href=&quot;index.php?a=1&amp;b=2&quot;&gt;link&lt;/a&gt;',
'23' => true,
],
];
$this->assertEquals([

$expected = [
'abc' => '123',
'&lt;' => '>',
'cde' => false,
3 => 'blank',
[
'<>' => 'a<>b',
'&lt;a&gt;' => '<a href="index.php?a=1&b=2">link</a>',
'23' => true,
],
], ArrayHelper::htmlDecode($array));
$this->assertEquals([
];
$this->assertEquals($expected, ArrayHelper::htmlDecode($array));
$expected = [
'abc' => '123',
'<' => '>',
'cde' => false,
3 => 'blank',
[
'<>' => 'a<>b',
'<a>' => '<a href="index.php?a=1&b=2">link</a>',
'23' => true,
],
], ArrayHelper::htmlDecode($array, false));
];
$this->assertEquals($expected, ArrayHelper::htmlDecode($array, false));
}

public function testIsIn()
Expand Down