Skip to content

Commit

Permalink
Merge pull request #1708 from bolt/fix/dont-let-vue-parse-twig-in-con…
Browse files Browse the repository at this point in the history
…tent

Don't let Vue parse Twig tags when editing content
  • Loading branch information
I-Valchev authored Aug 13, 2020
2 parents 7751fef + 5c0a2a8 commit 621971e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Controller/Backend/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function save(?Content $content = null): Response
$event = new ContentEvent($content);
$this->dispatcher->dispatch($event, ContentEvent::PRE_SAVE);

/* Note: Doctrine also calls preUpdate() -> Event/Listener/FieldFillListener.php */
$this->em->persist($content);
$this->em->flush();

Expand Down
12 changes: 11 additions & 1 deletion src/Event/Listener/FieldFillListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ private function clean($value): array

foreach ($value as $key => $v) {
if ($v instanceof Markup) {
$v = $this->trimZeroWidthWhitespace((string) $v);
// todo: Figure out how to preserve original encoding
$v = new Markup($this->sanitiser->clean((string) $v), 'UTF-8');
$v = new Markup($this->sanitiser->clean($v), 'UTF-8');
} elseif (is_string($v)) {
$v = $this->trimZeroWidthWhitespace($v);
$v = $this->sanitiser->clean($v);
}

Expand All @@ -70,6 +72,14 @@ private function clean($value): array
return $result;
}

/**
* Remove the 'zero width space' from `{{` and `}}`, added in the editor.
*/
public function trimZeroWidthWhitespace(string $string): string
{
return preg_replace('/([{}])[\x{200B}-\x{200D}\x{FEFF}]([{}])/u', '$1$2', $string);
}

public function postLoad(LifecycleEventArgs $args): void
{
$entity = $args->getEntity();
Expand Down
5 changes: 5 additions & 0 deletions templates/_partials/fields/_base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
{% if value is iterable and field|type != "select" and field|type != "collection" %}
{% set value = value|first %}
{% endif %}

{# We're adding zero width spaces in `{{` and `}}`, to ensure Vue doesn't get its grubby paws on it. #}
{% if value is string %}
{% set value = value|replace({'{{': '{​{', '}}': '}​}'}) %}
{% endif %}
{% endif %}

{# Set the class #}
Expand Down

0 comments on commit 621971e

Please sign in to comment.