Skip to content

Commit

Permalink
Merge pull request #29400 from colemanw/unicodeHtml
Browse files Browse the repository at this point in the history
Afform - Fix incorrect html encoding when saving
  • Loading branch information
mlutfy authored Feb 18, 2024
2 parents 861e974 + 9afe93f commit c2cf1f4
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 c2cf1f4

Please sign in to comment.