Pārlūkot izejas kodu

[Form] Add StripTagsFilter EventListener

Benjamin Eberlei 14 gadi atpakaļ
vecāks
revīzija
864a3ec45f

+ 34 - 0
src/Symfony/Component/Form/EventListener/StripTagsFilter.php

@@ -0,0 +1,34 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\EventListener;
+
+use Symfony\Component\Form\Events;
+use Symfony\Component\Form\Event\FilterDataEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * Strip tags from incoming input.
+ *
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
+ */
+class StripTagsFilter implements EventSubscriberInterface
+{
+    public function filterBoundDataFromClient(FilterDataEvent $event)
+    {
+        $event->setData(strip_tags($event->getData()));
+    }
+
+    public static function getSubscribedEvents()
+    {
+        return Events::filterBoundDataFromClient;
+    }
+}

+ 30 - 0
tests/Symfony/Tests/Component/Form/EventListener/StripTagsFilterTest.php

@@ -0,0 +1,30 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Form\EventListener;
+
+use Symfony\Component\Form\Event\FilterDataEvent;
+use Symfony\Component\Form\EventListener\StripTagsFilter;
+
+class StripTagsFilterTest extends \PHPUnit_Framework_TestCase
+{
+    public function testStripTags()
+    {
+        $data = "<div><strong>Foo!</strong>Bar!<span>Baz!</span></table></body>";
+        $field = $this->getMock('Symfony\Component\Form\FieldInterface');
+        $event = new FilterDataEvent($field, $data);
+
+        $filter = new StripTagsFilter();
+        $filter->filterBoundDataFromClient($event);
+
+        $this->assertEquals('Foo!Bar!Baz!', $event->getData());
+    }
+}