From bcb3f3b1070dc6ddb9125d619b860e2bb071ee64 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 19 Aug 2010 12:21:45 -0600 Subject: [PATCH] Bug #9187: Add callback parameter to the Html2text filter. --- .../Text_Filter/lib/Horde/Text/Filter/Html2text.php | 17 ++++++++++++++--- framework/Text_Filter/package.xml | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/framework/Text_Filter/lib/Horde/Text/Filter/Html2text.php b/framework/Text_Filter/lib/Horde/Text/Filter/Html2text.php index 1689d70ea..b89ab29d0 100644 --- a/framework/Text_Filter/lib/Horde/Text/Filter/Html2text.php +++ b/framework/Text_Filter/lib/Horde/Text/Filter/Html2text.php @@ -2,9 +2,13 @@ /** * Takes HTML and converts it to formatted, plain text. * - * Parameters: + * Optional parameters to constructor: *
- * charset - (string) The charset to use for html_entity_decode() calls.
+ * callback - (callback) Callback triggered on every node. Passed the
+ *            DOMDocument object and the DOMNode object. If the callback
+ *            returns non-null, add this text to the output and skip further
+ *            processing of the node.
+ * charset - (string) The charset of the text.
  * width - (integer) The wrapping width. Set to 0 to not wrap.
  * 
* @@ -55,6 +59,7 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base * @var array */ protected $_params = array( + 'callback' => null, 'charset' => 'UTF-8', 'width' => 75 ); @@ -154,6 +159,12 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base if ($node->hasChildNodes()) { foreach ($node->childNodes as $child) { + if ($this->_params['callback'] && + ($txt = call_user_func($this->_params['callback'], $doc, $child)) !== null) { + $out .= $txt; + continue; + } + if ($child instanceof DOMElement) { switch (strtolower($child->tagName)) { case 'h1': @@ -308,7 +319,7 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base if (!$child->nextSibling) { $tmp = rtrim($tmp); } - $out .= html_entity_decode($tmp, ENT_QUOTES, $this->_params['charset']); + $out = $tmp; } } } diff --git a/framework/Text_Filter/package.xml b/framework/Text_Filter/package.xml index a1f1e07ea..a5d26df71 100644 --- a/framework/Text_Filter/package.xml +++ b/framework/Text_Filter/package.xml @@ -34,8 +34,8 @@ beta LGPL - -* XSS filter now uses PHP DOM parser to process incoming text. + * Add callback parameter to the Html2text filter. + * XSS filter now uses PHP DOM parser to process incoming text. * Remove Horde/Core dependency. * Add Horde_Text_Filter_Exception::. * Html2text converter now uses XML parser to generate output. -- 2.11.0