Skip to content

Commit

Permalink
FIX Support pre tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed May 20, 2024
1 parent 50a0018 commit 4fdbe39
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/Forms/HTMLEditor/HTMLEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ public function getSchemaStateDefaults()
*/
public function ValueEntities()
{
return htmlentities($this->Value() ?? '', ENT_COMPAT, 'UTF-8', false);
$value = htmlentities($this->Value(), ENT_COMPAT, 'UTF-8', false);

$value = preg_replace_callback('/(?:&lt;pre.*?&gt;(?<replace>.*?)&lt;\/pre&gt;)/imsu', function ($matches) {
return str_replace($matches['replace'], htmlentities($matches['replace'], ENT_COMPAT, 'UTF-8', true), $matches[0]);
}, $value);

return $value;
}

/**
Expand Down
35 changes: 23 additions & 12 deletions tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,34 @@ public function testReadonlyField()
);
}

public function testValueEntities()
public function provideTestValueEntities()
{
$inputText = "The company &amp; partners";
$field = new HTMLEditorField("Content");
$field->setValue($inputText);

$this->assertEquals(
"The company &amp; partners",
$field->obj('ValueEntities')->forTemplate()
);
return [
"ampersand" => [
"The company &amp; partners",
"The company &amp; partners"
],
"double ampersand" => [
"The company &amp;amp; partners",
"The company &amp;amp; partners"
],
"left arrow and right arrow" => [
"<p><pre>&lt;strong&gt;The company and partners&lt;/strong&gt;</pre></p>",
"&lt;p&gt;&lt;pre&gt;&amp;lt;strong&amp;gt;The company and partners&amp;lt;/strong&amp;gt;&lt;/pre&gt;&lt;/p&gt;"
],
];
}

$inputText = "The company &amp;&amp; partners";
/**
* @dataProvider provideTestValueEntities
*/
public function testValueEntities(string $input, string $result)
{
$field = new HTMLEditorField("Content");
$field->setValue($inputText);
$field->setValue($input);

$this->assertEquals(
"The company &amp;&amp; partners",
$result,
$field->obj('ValueEntities')->forTemplate()
);
}
Expand Down

0 comments on commit 4fdbe39

Please sign in to comment.