diff --git a/src/Forms/HTMLEditor/HTMLEditorField.php b/src/Forms/HTMLEditor/HTMLEditorField.php
index db8d08a05ae..52d6f650e01 100644
--- a/src/Forms/HTMLEditor/HTMLEditorField.php
+++ b/src/Forms/HTMLEditor/HTMLEditorField.php
@@ -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('/(?:<pre.*?>(?.*?)<\/pre>)/imsu', function ($matches) {
+ return str_replace($matches['replace'], htmlentities($matches['replace'], ENT_COMPAT, 'UTF-8', true), $matches[0]);
+ }, $value);
+
+ return $value;
}
/**
diff --git a/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php b/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
index 2abe550aa5b..030fa749b26 100644
--- a/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
+++ b/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
@@ -210,23 +210,34 @@ public function testReadonlyField()
);
}
- public function testValueEntities()
+ public function provideTestValueEntities()
{
- $inputText = "The company & partners";
- $field = new HTMLEditorField("Content");
- $field->setValue($inputText);
-
- $this->assertEquals(
- "The company & partners",
- $field->obj('ValueEntities')->forTemplate()
- );
+ return [
+ "ampersand" => [
+ "The company & partners",
+ "The company & partners"
+ ],
+ "double ampersand" => [
+ "The company & partners",
+ "The company & partners"
+ ],
+ "left arrow and right arrow" => [
+ "<strong>The company and partners</strong>
",
+ "<p><pre><strong>The company and partners</strong></pre></p>"
+ ],
+ ];
+ }
- $inputText = "The company && partners";
+ /**
+ * @dataProvider provideTestValueEntities
+ */
+ public function testValueEntities(string $input, string $result)
+ {
$field = new HTMLEditorField("Content");
- $field->setValue($inputText);
+ $field->setValue($input);
$this->assertEquals(
- "The company && partners",
+ $result,
$field->obj('ValueEntities')->forTemplate()
);
}