Skip to content

Commit

Permalink
Afform - Fix incorrect html encoding when saving
Browse files Browse the repository at this point in the history
Fixes  https://lab.civicrm.org/dev/core/-/issues/4977

This reverts 0ae1b99 AND 4a439e7 back to the original version which used a simple str_replace.

The problem with 4a439e7 is that function mode is deprecated in php 8.2.
The problem with 0ae1b99 is that it was wrong and broke the html encoding of the file.

So this takes us all the way back to the original version of the function, which worked fine for what it did.
  • Loading branch information
colemanw committed Feb 15, 2024
1 parent 6f59c81 commit 9afe93f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ext/afform/core/CRM/Afform/ArrayHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ protected function decodeAttrValue($type, $txtAttrValue) {
* @return string
*/
public function replaceUnicodeChars($markup) {
return htmlspecialchars_decode(htmlentities($markup, ENT_COMPAT, 'utf-8', FALSE));
$replace = [
["\xc2\xa0", ' '],
];
return str_replace(array_column($replace, 0), array_column($replace, 1), $markup);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ public function testGetLayoutWithEmptyNode() {
$this->assertArrayNotHasKey('#children', $layout[0]['#children'][0]);
}

public function testGetHtmlEncoding(): void {
Afform::create(FALSE)
->setLayoutFormat('shallow')
->addValue('name', $this->formName)
->addValue('title', 'Test Form')
->addValue('layout', [
[
'#tag' => 'af-form',
'ctrl' => 'afform',
'#children' => [
[
'#tag' => 'af-entity',
'data' => "{contact_type: 'Individual', source: 'This isn\\'t \"quotes\"'}",
'type' => 'Contact',
'name' => 'Individual1',
],
],
],
])
->execute();

$html = Afform::get(FALSE)
->addWhere('name', '=', $this->formName)
->setLayoutFormat('html')
->execute()->single()['layout'];

$expected = <<<HTML
data="{contact_type: 'Individual', source: 'This isn\'t &quot;quotes&quot;'}"
HTML;

$this->assertStringContainsString($expected, $html);
}

public function testAfformAutocomplete(): void {
// Use a numeric title to test that the "search by id" feature
// doesn't kick in for Afforms (which don't have a numeric "id")
Expand Down

0 comments on commit 9afe93f

Please sign in to comment.