From bac9daa73c8bfe5669237539e6ee3c0525781795 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 15 Jun 2017 17:50:00 +0200 Subject: [PATCH 1/5] Do only not escape twit filter output when EditInPlace is false This will fix #100 --- Resources/config/edit_in_place.yml | 3 ++ Twig/TranslationExtension.php | 66 +++++++++++++++++++++++++++++- composer.json | 2 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/Resources/config/edit_in_place.yml b/Resources/config/edit_in_place.yml index 802b5688..6d89c949 100644 --- a/Resources/config/edit_in_place.yml +++ b/Resources/config/edit_in_place.yml @@ -26,6 +26,9 @@ services: class: Translation\Bundle\Twig\TranslationExtension arguments: - '@php_translator.edit_in_place.xtrans_html_translator' + calls: + - [setActivator, ['@php_translation.edit_in_place.activator']] + - [setRequestStack, ['@request_stack']] tags: - { name: 'twig.extension' } diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 57cc9668..b8d98385 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -11,6 +11,10 @@ namespace Translation\Bundle\Twig; +use Symfony\Component\HttpFoundation\RequestStack; +use Translation\Bundle\EditInPlace\ActivatorInterface; +use Twig\Environment; + /** * Override the `trans` functions `is_safe` option to allow HTML output from the * translator. This extension is used by for the EditInPlace feature. @@ -19,17 +23,75 @@ */ class TranslationExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension { + /** + * @var ActivatorInterface + */ + private $activator; + + /** + * @var RequestStack + */ + private $requestStack; + /** * {@inheritdoc} */ public function getFilters() { return [ - new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe' => ['html']]), - new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe' => ['html']]), + new \Twig_SimpleFilter('trans', [$this, 'transAutoEscape'], ['is_safe' => ['html'], 'needs_environment' => true]), + new \Twig_SimpleFilter('transchoice', [$this, 'transchoiceAutoEscape'], ['is_safe' => ['html'], 'needs_environment' => true]), ]; } + /** + * Escape output if the EditInPlace is disabled. + * + * @return bool + */ + private function escapeOutput() + { + return !$this->activator->checkRequest($this->requestStack->getMasterRequest()); + } + + public function transAutoEscape(Environment $env, $message, array $arguments = array(), $domain = null, $locale = null) + { + $value = $this->trans($message, $arguments, $domain, $locale); + + if ($this->escapeOutput()) { + return twig_escape_filter($env, $value, 'html', null, true); + } + + return $value; + } + + public function transchoiceAutoEscape(Environment $env, $message, $count, array $arguments = array(), $domain = null, $locale = null) + { + $value = $this->transchoice($message, $count, $arguments, $domain, $locale); + + if ($this->escapeOutput()) { + return twig_escape_filter($env, $value, 'html', null, true); + } + + return $value; + } + + /** + * @param ActivatorInterface $activator + */ + public function setActivator(ActivatorInterface $activator) + { + $this->activator = $activator; + } + + /** + * @param RequestStack $requestStack + */ + public function setRequestStack(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + /** * {@inheritdoc} */ diff --git a/composer.json b/composer.json index ad942acb..9aaf71fc 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "matthiasnoback/symfony-dependency-injection-test": "^1.0", "guzzlehttp/psr7": "^1.3", "nyholm/nsa": "^1.0", - "nyholm/symfony-bundle-test": "^1.0.1" + "nyholm/symfony-bundle-test": "^1.0.2" }, "suggest": { "php-http/httplug-bundle": "To easier configure your httplug clients." From e6d1b1997686b1ee1a10ff57cb31d23631d973b0 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 15 Jun 2017 17:51:46 +0200 Subject: [PATCH 2/5] cs --- Twig/TranslationExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index b8d98385..3d131d63 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -54,7 +54,7 @@ private function escapeOutput() return !$this->activator->checkRequest($this->requestStack->getMasterRequest()); } - public function transAutoEscape(Environment $env, $message, array $arguments = array(), $domain = null, $locale = null) + public function transAutoEscape(Environment $env, $message, array $arguments = [], $domain = null, $locale = null) { $value = $this->trans($message, $arguments, $domain, $locale); @@ -65,7 +65,7 @@ public function transAutoEscape(Environment $env, $message, array $arguments = a return $value; } - public function transchoiceAutoEscape(Environment $env, $message, $count, array $arguments = array(), $domain = null, $locale = null) + public function transchoiceAutoEscape(Environment $env, $message, $count, array $arguments = [], $domain = null, $locale = null) { $value = $this->transchoice($message, $count, $arguments, $domain, $locale); From 9922058d2d5885537bb072df1bf60c1375ca3fc1 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 15 Jun 2017 18:14:53 +0200 Subject: [PATCH 3/5] cs --- Twig/TranslationExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 3d131d63..f219431c 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -59,7 +59,7 @@ public function transAutoEscape(Environment $env, $message, array $arguments = [ $value = $this->trans($message, $arguments, $domain, $locale); if ($this->escapeOutput()) { - return twig_escape_filter($env, $value, 'html', null, true); + return twig_escape_filter($env, $value, 'html', null, true); } return $value; @@ -70,7 +70,7 @@ public function transchoiceAutoEscape(Environment $env, $message, $count, array $value = $this->transchoice($message, $count, $arguments, $domain, $locale); if ($this->escapeOutput()) { - return twig_escape_filter($env, $value, 'html', null, true); + return twig_escape_filter($env, $value, 'html', null, true); } return $value; From 8c32ac03f8c61984c708699bd90c48841f7cdb32 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 15 Jun 2017 18:28:09 +0200 Subject: [PATCH 4/5] Using is_safe_callback --- Twig/TranslationExtension.php | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index f219431c..d814a14c 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -39,41 +39,19 @@ class TranslationExtension extends \Symfony\Bridge\Twig\Extension\TranslationExt public function getFilters() { return [ - new \Twig_SimpleFilter('trans', [$this, 'transAutoEscape'], ['is_safe' => ['html'], 'needs_environment' => true]), - new \Twig_SimpleFilter('transchoice', [$this, 'transchoiceAutoEscape'], ['is_safe' => ['html'], 'needs_environment' => true]), + new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]), + new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]), ]; } /** * Escape output if the EditInPlace is disabled. * - * @return bool + * @return array */ - private function escapeOutput() + public function isSafe($node) { - return !$this->activator->checkRequest($this->requestStack->getMasterRequest()); - } - - public function transAutoEscape(Environment $env, $message, array $arguments = [], $domain = null, $locale = null) - { - $value = $this->trans($message, $arguments, $domain, $locale); - - if ($this->escapeOutput()) { - return twig_escape_filter($env, $value, 'html', null, true); - } - - return $value; - } - - public function transchoiceAutoEscape(Environment $env, $message, $count, array $arguments = [], $domain = null, $locale = null) - { - $value = $this->transchoice($message, $count, $arguments, $domain, $locale); - - if ($this->escapeOutput()) { - return twig_escape_filter($env, $value, 'html', null, true); - } - - return $value; + return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? []: ['html']; } /** From 2631a8ef7a5120f68de8712f22799e9d87290aee Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 15 Jun 2017 18:30:03 +0200 Subject: [PATCH 5/5] cs --- Twig/TranslationExtension.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index d814a14c..f92acb2f 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -13,7 +13,6 @@ use Symfony\Component\HttpFoundation\RequestStack; use Translation\Bundle\EditInPlace\ActivatorInterface; -use Twig\Environment; /** * Override the `trans` functions `is_safe` option to allow HTML output from the @@ -51,7 +50,7 @@ public function getFilters() */ public function isSafe($node) { - return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? []: ['html']; + return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? [] : ['html']; } /**