From c3f2eec095b6a7243372f293fbbbfe5f5ad22f80 Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 7 Mar 2025 18:22:02 +0000 Subject: [PATCH 1/5] Add encodeValue to DataField.php --- src/Field/DataField.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Field/DataField.php b/src/Field/DataField.php index dd74bb697..86559a388 100644 --- a/src/Field/DataField.php +++ b/src/Field/DataField.php @@ -39,6 +39,8 @@ final class DataField * a function accepting data and returning the array. * Example array: `['class' => 'value', 'data-type' => 'text']` * Example closure: `fn($data) => ['class' => $data->status . '-value']` + * + * @param bool $encodeValue Whether the value is HTML encoded */ public function __construct( public readonly string $name = '', @@ -47,7 +49,8 @@ public function __construct( public readonly string $labelTag = '', public readonly mixed $value = null, public readonly string $valueTag = '', - public readonly array|Closure $valueAttributes = [] + public readonly array|Closure $valueAttributes = [], + public readonly bool $encodeValue = true, ) { } } From 4b5da630f59c9d6c320a77ca249dba224a491044 Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 7 Mar 2025 18:27:32 +0000 Subject: [PATCH 2/5] Update DetailView.php Encode field values or leave raw according to the field encodeValue parameter --- src/DetailView.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DetailView.php b/src/DetailView.php index abf4d8fb4..c8aff6f35 100644 --- a/src/DetailView.php +++ b/src/DetailView.php @@ -336,7 +336,10 @@ private function normalizeColumns(array $fields): array 'label' => Html::encode($field->label === '' ? $field->name : $field->label), 'labelAttributes' => $this->renderAttributes($labelAttributes), 'labelTag' => $labelTag, - 'value' => Html::encodeAttribute($this->renderValue($field->name, $field->value)), + 'value' => $field->encodeValue + ? Html::encodeAttribute($this->renderValue($field->name, $field->value)) + : $this->renderValue($field->name, $field->value) + , 'valueAttributes' => $this->renderAttributes($valueAttributes), 'valueTag' => $valueTag, ]; From 7542f342395ecc8b9b81fddf99eea2f44a5ccafd Mon Sep 17 00:00:00 2001 From: Chris Yates Date: Fri, 7 Mar 2025 19:40:24 +0000 Subject: [PATCH 3/5] Add testNonEncodeValue() --- tests/DetailView/BaseTest.php | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/DetailView/BaseTest.php b/tests/DetailView/BaseTest.php index e000e6fcb..2cc202d66 100644 --- a/tests/DetailView/BaseTest.php +++ b/tests/DetailView/BaseTest.php @@ -19,6 +19,47 @@ final class BaseTest extends TestCase { use TestTrait; + /** + * @throws InvalidConfigException + * @throws NotFoundException + * @throws NotInstantiableException + * @throws CircularReferenceException + */ + public function testNonEncodedValue(): void + { + Assert::equalsWithoutLE( + << +
+
+
id
+
1
+
+
+
value
+
tests 1
+
+
+
total
+
10
+
+
+ + HTML, + DetailView::widget() + ->fields( + new DataField('id'), + new DataField( + name: 'value', + encodeValue: false + ), + new DataField('total'), + ) + ->data(['id' => 1, 'value' => '
tests 1
', 'total' => '10']) + ->render(), + ); + } + /** * @throws InvalidConfigException * @throws NotFoundException From 3ab8d796f753cace2b72198aa97b97746d1bb856 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 8 Mar 2025 12:24:54 +0300 Subject: [PATCH 4/5] Update tests/DetailView/BaseTest.php --- tests/DetailView/BaseTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/DetailView/BaseTest.php b/tests/DetailView/BaseTest.php index 2cc202d66..79b0bc6c8 100644 --- a/tests/DetailView/BaseTest.php +++ b/tests/DetailView/BaseTest.php @@ -19,12 +19,6 @@ final class BaseTest extends TestCase { use TestTrait; - /** - * @throws InvalidConfigException - * @throws NotFoundException - * @throws NotInstantiableException - * @throws CircularReferenceException - */ public function testNonEncodedValue(): void { Assert::equalsWithoutLE( From f5b1f1ce86556e334c9a891021eea09da6e78434 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 8 Mar 2025 12:25:00 +0300 Subject: [PATCH 5/5] Update src/DetailView.php --- src/DetailView.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DetailView.php b/src/DetailView.php index c8aff6f35..53514e741 100644 --- a/src/DetailView.php +++ b/src/DetailView.php @@ -338,7 +338,7 @@ private function normalizeColumns(array $fields): array 'labelTag' => $labelTag, 'value' => $field->encodeValue ? Html::encodeAttribute($this->renderValue($field->name, $field->value)) - : $this->renderValue($field->name, $field->value) + : (string) $this->renderValue($field->name, $field->value) , 'valueAttributes' => $this->renderAttributes($valueAttributes), 'valueTag' => $valueTag,