Parcourir la source

[ZF-9343] Zend_Filter_StripTags:

- fixed stripping comments

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21552 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas il y a 16 ans
Parent
commit
17f45d6116
2 fichiers modifiés avec 28 ajouts et 3 suppressions
  1. 16 2
      library/Zend/Filter/StripTags.php
  2. 12 1
      tests/Zend/Filter/StripTagsTest.php

+ 16 - 2
library/Zend/Filter/StripTags.php

@@ -1,4 +1,5 @@
 <?php
 <?php
+
 /**
 /**
  * Zend Framework
  * Zend Framework
  *
  *
@@ -35,6 +36,11 @@ require_once 'Zend/Filter/Interface.php';
 class Zend_Filter_StripTags implements Zend_Filter_Interface
 class Zend_Filter_StripTags implements Zend_Filter_Interface
 {
 {
     /**
     /**
+     * Unique ID prefix used for allowing comments
+     */
+    const UNIQUE_ID_PREFIX = '__Zend_Filter_StripTags__';
+
+    /**
      * Whether comments are allowed
      * Whether comments are allowed
      *
      *
      * If false (the default), then comments are removed from the input string.
      * If false (the default), then comments are removed from the input string.
@@ -231,14 +237,22 @@ class Zend_Filter_StripTags implements Zend_Filter_Interface
      */
      */
     public function filter($value)
     public function filter($value)
     {
     {
+        $value = (string) $value;
+
         // Strip HTML comments first
         // Strip HTML comments first
-        $valueCopy = preg_replace('#<!--(?:[^<]+|<(?!\!--))*?(--\s*>)#s', '', (string) $value);
+        while (strpos($value, '<!--') !== false) {
+            $pos   = strrpos($value, '<!--');
+            $start = substr($value, 0, $pos);
+            $value = substr($value, $pos);
+            $value = preg_replace('/<(?:!(?:--[\s\S]*?--\s*)?(>))/us', '',  $value);
+            $value = $start . $value;
+        }
 
 
         // Initialize accumulator for filtered data
         // Initialize accumulator for filtered data
         $dataFiltered = '';
         $dataFiltered = '';
         // Parse the input data iteratively as regular pre-tag text followed by a
         // Parse the input data iteratively as regular pre-tag text followed by a
         // tag; either may be empty strings
         // tag; either may be empty strings
-        preg_match_all('/([^<]*)(<?[^>]*>?)/', (string) $valueCopy, $matches);
+        preg_match_all('/([^<]*)(<?[^>]*>?)/', (string) $value, $matches);
         // Iterate over each set of matches
         // Iterate over each set of matches
         foreach ($matches[1] as $index => $preTag) {
         foreach ($matches[1] as $index => $preTag) {
             // If the pre-tag text is non-empty, strip any ">" characters from it
             // If the pre-tag text is non-empty, strip any ">" characters from it

+ 12 - 1
tests/Zend/Filter/StripTagsTest.php

@@ -457,7 +457,7 @@ class Zend_Filter_StripTagsTest extends PHPUnit_Framework_TestCase
     public function testSpecifyingCommentsAllowedShouldStillStripNestedComments()
     public function testSpecifyingCommentsAllowedShouldStillStripNestedComments()
     {
     {
         $input    = '<a> <!-- <b> <!-- <c> --> <d> --> <e>';
         $input    = '<a> <!-- <b> <!-- <c> --> <d> --> <e>';
-        $expected = '   ';
+        $expected = '  ';
         $this->_filter->setCommentsAllowed(true);
         $this->_filter->setCommentsAllowed(true);
         $this->assertEquals($expected, $this->_filter->filter($input));
         $this->assertEquals($expected, $this->_filter->filter($input));
     }
     }
@@ -563,6 +563,17 @@ class Zend_Filter_StripTagsTest extends PHPUnit_Framework_TestCase
         $expected = 'äöüäöü';
         $expected = 'äöüäöü';
         $this->assertEquals($expected, $this->_filter->filter($input));
         $this->assertEquals($expected, $this->_filter->filter($input));
     }
     }
+
+    /**
+     * @group ZF-9434
+     */
+    public function testCommentWithTagInSameLine()
+    {
+        $input    = 'test <!-- testcomment --> test <div>div-content</div>';
+        $expected = 'test  test div-content';
+        $this->assertEquals($expected, $this->_filter->filter($input));
+    }
+
 }
 }
 
 
 // Call Zend_Filter_StripTagsTest::main() if this source file is executed directly.
 // Call Zend_Filter_StripTagsTest::main() if this source file is executed directly.