[icinga-checkins] icinga.org: icingaweb2/master: Don' t destroy links by adding zero width space characters to plugin output

git at icinga.org git at icinga.org
Thu Jun 2 17:58:35 CEST 2016


Module: icingaweb2
Branch: master
Commit: 10d2b9c51d8660ca732490976b2f628a40cecd21
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=10d2b9c51d8660ca732490976b2f628a40cecd21

Author: Alexander A. Klimov <alexander.klimov at netways.de>
Date:   Thu May 19 18:38:36 2016 +0200

Don't destroy links by adding zero width space characters to plugin output

refs #11796

---

 .../application/views/helpers/PluginOutput.php     |   41 +++++++++++++++++---
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/modules/monitoring/application/views/helpers/PluginOutput.php b/modules/monitoring/application/views/helpers/PluginOutput.php
index ba3013a..b78417d 100644
--- a/modules/monitoring/application/views/helpers/PluginOutput.php
+++ b/modules/monitoring/application/views/helpers/PluginOutput.php
@@ -27,6 +27,16 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract
         '@@@@@@',
     );
 
+    /**
+     * @var string
+     */
+    protected $zeroWidthSpace;
+
+    public function __construct()
+    {
+        $this->zeroWidthSpace = html_entity_decode('​', ENT_NOQUOTES, 'UTF-8');
+    }
+
     public function pluginOutput($output, $raw = false)
     {
         if (empty($output)) {
@@ -50,15 +60,11 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract
             );
             $isHtml = false;
         }
-        $output = $this->fixLinks($output);
+        $output = $this->fixLinksAndWrapping($output);
         // Help browsers to break words in plugin output
         $output = trim($output);
         // Add space after comma where missing
         $output = preg_replace('/,[^\s]/', ', ', $output);
-        // Add zero width space after ')', ']', ':', '.', '_' and '-' if not surrounded by whitespaces
-        $output = preg_replace('/([^\s])([\\)\\]:._-])([^\s])/', '$1$2​$3', $output);
-        // Add zero width space before '(' and '[' if not surrounded by whitespaces
-        $output = preg_replace('/([^\s])([([])([^\s])/', '$1​$2$3', $output);
 
         if (! $raw) {
             if ($isHtml) {
@@ -70,13 +76,14 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract
         return $output;
     }
 
-    protected function fixLinks($html)
+    protected function fixLinksAndWrapping($html)
     {
 
         $ret = array();
         $dom = new DOMDocument;
         $dom->loadXML('<div>' . $html . '</div>', LIBXML_NOERROR | LIBXML_NOWARNING);
         $dom->preserveWhiteSpace = false;
+
         $links = $dom->getElementsByTagName('a');
         foreach ($links as $tag)
         {
@@ -93,6 +100,9 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract
             }
             //$ret[$tag->getAttribute('href')] = $tag->childNodes->item(0)->nodeValue;
         }
+
+        $this->fixWrappingRecursive($dom);
+
         return substr($dom->saveHTML(), 5, -7);
     }
 
@@ -119,4 +129,23 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract
         }
         return self::$purifier;
     }
+
+    /**
+     * Add zero width space to all text in the DOM to make wrapping easier for the user agent
+     *
+     * @param   DOMNode $node
+     */
+    protected function fixWrappingRecursive(DOMNode $node)
+    {
+        if ($node instanceof DOMText) {
+            // Add zero width space after ')', ']', ':', '.', '_' and '-' if not surrounded by whitespaces
+            $data = preg_replace('/([^\s])([\\)\\]:._-])([^\s])/', '$1$2' . $this->zeroWidthSpace . '$3', $node->data);
+            // Add zero width space before '(' and '[' if not surrounded by whitespaces
+            $node->data = preg_replace('/([^\s])([([])([^\s])/', '$1' . $this->zeroWidthSpace . '$2$3', $data);
+        } elseif ($node->childNodes !== null) {
+            foreach ($node->childNodes as $childNode) {
+                $this->fixWrappingRecursive($childNode);
+            }
+        }
+    }
 }



More information about the icinga-checkins mailing list