Skip to content

Commit

Permalink
FIX HTMLEditorField is not able to show html or xml code examples (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
sabina-talipova committed May 21, 2024
1 parent 470293a commit f0aaba5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
14 changes: 13 additions & 1 deletion src/Forms/HTMLEditor/HTMLEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,19 @@ public function getSchemaStateDefaults()
*/
public function ValueEntities()
{
return htmlentities($this->Value() ?? '', ENT_COMPAT, 'UTF-8', false);
$entities = get_html_translation_table(HTML_ENTITIES);

foreach ($entities as $key => $value) {
$entities[$key] = "/" . $value . "/";
}

$value = preg_replace_callback($entities, function ($matches) {
// Don't apply double encoding to ampersand
$doubleEncoding = $matches[0] != '&';
return htmlentities($matches[0], ENT_COMPAT, 'UTF-8', $doubleEncoding);
}, $this->Value() ?? '');

return $value;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/View/Shortcodes/EmbedShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ protected static function videoEmbed($arguments, $content)
$arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;';
}

if (!empty($arguments['caption'])) {
$arguments['caption'] = htmlentities($arguments['caption'], ENT_QUOTES, 'UTF-8', false);
}

// override iframe dimension attributes provided by webservice with ones specified in shortcode arguments
foreach (['width', 'height'] as $attr) {
if (!($value = $arguments[$attr] ?? false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
>
{$Content}
<% if $Arguments.caption %>
<p class="caption">{$Arguments.caption}</p>
<p class="caption">{$Arguments.caption.RAW}</p>
<% end_if %>
</div>
37 changes: 24 additions & 13 deletions tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testCasting()
$inputText = "These are some unicodes: ä, ö, & ü";
$field = new HTMLEditorField("Test", "Test");
$field->setValue($inputText);
$this->assertStringContainsString('These are some unicodes: &auml;, &ouml;, &amp; &uuml;', $field->Field());
$this->assertStringContainsString('These are some unicodes: ä, ö, & ü', $field->Field());
// Test shortcodes
$inputText = "Shortcode: [file_link id=4]";
$field = new HTMLEditorField("Test", "Test");
Expand Down 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>&lt;strong&gt;The company &amp;amp; partners&lt;/strong&gt;</p>",
"<p>&amp;lt;strong&amp;gt;The company &amp;amp; partners&amp;lt;/strong&amp;gt;</p>"
],
];
}

$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 f0aaba5

Please sign in to comment.