Преглед изворни кода

Added ability to add filters to existing tags ZF-8862

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20406 44c647ce-9c0f-0410-b52a-842ac1e357ba
kokx пре 16 година
родитељ
комит
cc021c82d7
2 измењених фајлова са 126 додато и 18 уклоњено
  1. 93 7
      library/Zend/Markup/Renderer/RendererAbstract.php
  2. 33 11
      tests/Zend/Markup/BbcodeAndHtmlTest.php

+ 93 - 7
library/Zend/Markup/Renderer/RendererAbstract.php

@@ -226,13 +226,13 @@ abstract class Zend_Markup_Renderer_RendererAbstract
             );
         } else {
             if ($type && array_key_exists('empty', $options) && $options['empty']) {
-                // add a single replace tag
+                // add a single replace markup
                 $options['type']   = $type;
                 $options['filter'] = $filter;
 
                 $this->_markups[$name] = $options;
             } else {
-                // add a replace tag
+                // add a replace markup
                 $options['type']   = $type;
                 $options['filter'] = $filter;
 
@@ -437,7 +437,7 @@ abstract class Zend_Markup_Renderer_RendererAbstract
     }
 
     /**
-     * Get the tag name
+     * Get the markup name
      *
      * @param Zend_Markup_Token
      *
@@ -450,8 +450,19 @@ abstract class Zend_Markup_Renderer_RendererAbstract
             return false;
         }
 
-        // process the aliases
-        while (($type = $this->_getMarkupType($name)) 
+        return $this->_resolveMarkupName($name);
+    }
+
+    /**
+     * Resolve aliases for a markup name
+     *
+     * @param string $name
+     *
+     * @return string
+     */
+    protected function _resolveMarkupName($name)
+    {
+        while (($type = $this->_getMarkupType($name))
                && ($type & self::TYPE_ALIAS)
         ) {
             $name = $this->_markups[$name]['name'];
@@ -462,8 +473,8 @@ abstract class Zend_Markup_Renderer_RendererAbstract
 
     /**
      * Retrieve markup type
-     * 
-     * @param  string $name 
+     *
+     * @param  string $name
      * @return false|int
      */
     protected function _getMarkupType($name)
@@ -546,9 +557,84 @@ abstract class Zend_Markup_Renderer_RendererAbstract
     }
 
     /**
+     * Get the filter for an existing markup
+     *
+     * @param string $markup
+     *
+     * @return Zend_Filter_Interface
+     */
+    public function getFilter($markup)
+    {
+        $markup = $this->_resolveMarkupName($markup);
+
+        if (!isset($this->_markups[$markup]['filter'])
+            || !($this->_markups[$markup]['filter'] instanceof Zend_Filter_Interface)
+        ) {
+            if (isset($this->_markups[$markup]['filter']) && $this->_markups[$markup]['filter']) {
+                $this->_markups[$markup]['filter'] = $this->getDefaultFilter();
+            }
+        }
+
+        return $this->_markups[$markup]['filter'];
+    }
+
+    /**
+     * Add a filter for an existing markup
+     *
+     * @param Zend_Filter_Interface $filter
+     * @param string $markup
+     * @param string $placement
+     *
+     * @return Zend_Markup_Renderer_RendererAbstract
+     */
+    public function addFilter(Zend_Filter_Interface $filter, $markup, $placement = Zend_Filter::CHAIN_APPEND)
+    {
+        $markup = $this->_resolveMarkupName($markup);
+
+        $oldFilter = $this->getFilter($markup);
+
+        // if this filter is the default filter, clone it first
+        if ($oldFilter === $this->getDefaultFilter()) {
+            $oldFilter = clone $oldFilter;
+        }
+
+        if (!($oldFilter instanceof Zend_Filter)) {
+            $this->_markups[$markup]['filter'] = new Zend_Filter();
+
+            if ($oldFilter instanceof Zend_Filter_Interface) {
+                $this->_markups[$markup]['filter']->addFilter($oldFilter);
+            }
+        } else {
+            $this->_markups[$markup]['filter'] = $oldFilter;
+        }
+
+        $this->_markups[$markup]['filter']->addFilter($filter, $placement);
+
+        return $this;
+    }
+
+    /**
+     * Set the filter for an existing
+     *
+     * @param Zend_Filter_Interface $filter
+     * @param string $markup
+     *
+     * @return Zend_Markup_Renderer_RendererAbstract
+     */
+    public function setFilter(Zend_Filter_Interface $filter, $markup)
+    {
+        $markup = $this->_resolveMarkupName($markup);
+
+        $this->_markups[$markup]['filter'] = $filter;
+
+        return $this;
+    }
+
+    /**
      * Set the default filters
      *
      * @return void
      */
     abstract public function addDefaultFilters();
+
 }

+ 33 - 11
tests/Zend/Markup/BbcodeAndHtmlTest.php

@@ -174,7 +174,7 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
             $this->_markup->render('[suppp]test aap[/suppp]test'));
     }
 
-    public function testHtmlUrlTitleIsRenderedCorrectly() 
+    public function testHtmlUrlTitleIsRenderedCorrectly()
     {
         $this->assertEquals(
             '<a href="http://exampl.com" title="foo">test</a>',
@@ -182,7 +182,7 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
         );
     }
 
-    public function testValueLessAttributeDoesNotThrowNotice() 
+    public function testValueLessAttributeDoesNotThrowNotice()
     {
         // Notice: Uninitialized string offset: 42
         // in Zend/Markup/Parser/Bbcode.php on line 316
@@ -207,12 +207,12 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
         $this->_markup->render('[url="http://framework.zend.com/"title');
     }
 
-    public function testHrTagWorks() 
+    public function testHrTagWorks()
     {
         $this->assertEquals('foo<hr />bar', $this->_markup->render('foo[hr]bar'));
     }
 
-    public function testFunkyCombos() 
+    public function testFunkyCombos()
     {
         $expected = '<span style="text-decoration: underline;">a[/b][hr]b'
                   . '<strong>c</strong></span><strong>d</strong>[/u]e';
@@ -220,26 +220,26 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($expected, $outcome);
     }
 
-    public function testImgSrcsConstraints() 
+    public function testImgSrcsConstraints()
     {
         $this->assertEquals('F/\!ZLrFz',$this->_markup->render('F[img]/\!ZLrFz[/img]'));
     }
 
-    public function testColorConstraintsAndJs() 
+    public function testColorConstraintsAndJs()
     {
         $input = "<kokx> i think you mean? [color=\"onclick='foobar();'\"]your text[/color] DASPRiD";
         $expected = "&lt;kokx&gt; i think you mean? <span>your text</span> DASPRiD";
         $this->assertEquals($expected, $this->_markup->render($input));
     }
 
-    public function testNeverEndingAttribute() 
+    public function testNeverEndingAttribute()
     {
         $input = "[color=\"green]your text[/color]";
         $expected = '<span>your text</span>';
         $this->assertEquals($expected, $this->_markup->render($input));
     }
 
-    public function testTreatmentNonTags() 
+    public function testTreatmentNonTags()
     {
         $input = '[span][acronym][h1][h2][h3][h4][h5][h6][nothing]'
                . '[/h6][/h5][/h4][/h3][/h2][/h1][/acronym][/span]';
@@ -248,7 +248,7 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($expected, $this->_markup->render($input));
     }
 
-    public function testListItems() 
+    public function testListItems()
     {
         $input = "[list][*]Foo*bar (item 1)\n[*]Item 2\n[*]Trimmed (Item 3)\n[/list]";
         $expected = "<ul><li>Foo*bar (item 1)</li><li>Item 2</li><li>Trimmed (Item 3)</li></ul>";
@@ -274,7 +274,7 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
         }
     }
 
-    public function testHtmlTags() 
+    public function testHtmlTags()
     {
         $m = $this->_markup;
 
@@ -318,7 +318,7 @@ class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
                                 $this->_markup->render('[b]foo[i]bar[/b]kokx[/i]'));
     }
 
-    public function testHtmlAliases() 
+    public function testHtmlAliases()
     {
         $m = $this->_markup;
 
@@ -421,6 +421,28 @@ BBCODE;
             $m->render("filter\n[suppp]filter\n&\nfilter[/suppp]"));
     }
 
+    public function testSetFilterForExistingMarkup()
+    {
+        $m = $this->_markup;
+
+        $filter = new Zend_Filter_StringToUpper();
+
+        $m->setFilter($filter, 'strong');
+
+        $this->assertEquals('<strong>FOO&BAR</strong>baz', $m->render('[b]foo&bar[/b]baz'));
+    }
+
+    public function testAddFilterForExistingMarkup()
+    {
+        $m = $this->_markup;
+
+        $filter = new Zend_Filter_StringToUpper();
+
+        $m->addFilter($filter, 'i', Zend_Filter::CHAIN_PREPEND);
+
+        $this->assertEquals('<em>FOO&amp;BAR</em>baz', $m->render('[i]foo&bar[/i]baz'));
+    }
+
 }
 
 // Call Zend_Markup_BbcodeAndHtmlTest::main()