Bug #8062: Fix parsing links to appear in new window
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 30 Jun 2010 21:47:54 +0000 (15:47 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 30 Jun 2010 21:51:50 +0000 (15:51 -0600)
Make tests re: link to new window work again (this test now requires a
working Horde installation, but that is fine for me).

imp/lib/Mime/Viewer/Html.php
imp/lib/tests/mime_viewer_html.phpt

index e46487b..78f3ff9 100644 (file)
 class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
 {
     /**
+     * The window target to use for links.
+     * Needed for testing purposes.
+     *
+     * @var string
+     */
+    public $newwinTarget = null;
+
+    /**
      * This driver's display capabilities.
      *
      * @var array
@@ -213,30 +221,12 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
         /* Convert links to open in new windows. First we hide all
          * mailto: links, links that have an "#xyz" anchor and ignore
          * all links that already have a target. */
-        $target = 'target_' . uniqid(mt_rand());
-        $data = preg_replace(
-            array('/<a\b([^>]*\s+href=["\']?(#|mailto:))/i',
-                  '/<a\b([^>]*)\s+target=["\']?[^>"\'\s]*["\']?/i',
-                  '/<a\s/i',
-                  '/<area\b([^>]*\s+href=["\']?(#|mailto:))/i',
-                  '/<area\b([^>]*)\s+target=["\']?[^>"\'\s]*["\']?/i',
-                  '/<area\s/i',
-                  "/\x01/",
-                  "/\x02/"),
-            array("<\x01\\1",
-                  "<\x01 \\1 target=\"" . $target . "\"",
-                  '<a target="' . $target . '" ',
-                  "<\x02\\1",
-                  "<\x02 \\1 target=\"" . $target . "\"",
-                  '<area target="' . $target . '" ',
-                  'a ',
-                  'area '),
-            $data);
+        $data = $this->openLinksInNewWindow($data);
 
         /* If displaying inline (in IFRAME), tables with 100% height seems to
          * confuse many browsers re: the iframe internal height. */
         if ($inline) {
-            $data = preg_replace('/<table\b([^>]*)\s+height=["\']?100\%["\']?/i', '<table \\1', $data);
+            $data = preg_replace('/<table\b([^>]*)\bheight=["\']?100\%["\']?/i', '<table \\1', $data);
         }
 
         /* Turn mailto: links into our own compose links. */
@@ -276,6 +266,40 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
     }
 
     /**
+     * Scans HTML data and alters links to open in a new window.
+     * In public function so that it can be tested.
+     *
+     * @param string $data  Data in.
+     *
+     * @return string  Altered data.
+     */
+    public function openLinksInNewWindow($data)
+    {
+        $target = is_null($this->newwinTarget)
+            ? 'target_' . uniqid(mt_rand())
+            : $this->newwinTarget;
+
+        return preg_replace(
+            array('/<a\b([^>]*\bhref=["\']?(#|mailto:))/i',
+                  '/<a\b([^>]*)\btarget=["\']?[^>"\'\s]*["\']?/i',
+                  '/<a\b/i',
+                  '/<area\b([^>]*\bhref=["\']?(#|mailto:))/i',
+                  '/<area\b([^>]*)\btarget=["\']?[^>"\'\s]*["\']?/i',
+                  '/<area\b/i',
+                  "/\x01/",
+                  "/\x02/"),
+            array("<\x01\\1",
+                  "<\x01\\1target=\"" . $target . "\"",
+                  '<a target="' . $target . '"',
+                  "<\x02\\1",
+                  "<\x02\\1target=\"" . $target . "\"",
+                  '<area target="' . $target . '"',
+                  'a',
+                  'area'),
+            $data);
+    }
+
+    /**
      * TODO
      */
     protected function _mailtoCallback($m)
index 948828a..560b0b8 100644 (file)
@@ -1,35 +1,32 @@
 --TEST--
- tests.
+IMP HTML MIME Viewer tests.
 --FILE--
 <?php
 
-require_once 'Horde.php';
-require_once 'Horde/Mime/Viewer/Driver.php';
-require_once 'Horde/Mime/Viewer/Html.php';
-require_once dirname(dirname(__FILE__)) . '/Mime/Viewer/Html.php';
+require_once dirname(__FILE__) . '/../Application.php';
+Horde_Registry::appInit('imp', array(
+    'authentication' => 'none',
+    'cli' => true
+));
 
-class MockRegistry {
-    function getImageDir()
-    {
-        return '';
-    }
-}
+$mock_part = new Horde_Mime_Part();
+$mock_part->setType('text/html');
 
-$registry = new MockRegistry();
-$mock_part = null;
-$v = new IMP_Horde_Mime_Viewer_html($mock_part);
+$v = Horde_Mime_Viewer::factory($mock_part);
+$v->newwinTarget = '_blank';
 
 // Test regex for converting links to open in a new window.
-echo $v->_openLinksInNewWindow('foo') . "\n";
-echo $v->_openLinksInNewWindow('example@example.com') . "\n";
-echo $v->_openLinksInNewWindow('foo <a href="#bar">Anchor</a>') . "\n";
-echo $v->_openLinksInNewWindow('foo <a href="http://www.example.com/">example</a>') . "\n";
-echo $v->_openLinksInNewWindow('foo <a target="foo" href="http://www.example.com/">example</a>') . "\n";
-echo $v->_openLinksInNewWindow('foo <a href="http://www.example.com/" target="foo">example</a>') . "\n";
-echo $v->_openLinksInNewWindow('foo <a mailto="example@example.com">Example Email</a>') . "\n";
-echo $v->_openLinksInNewWindow('<map name="Map"><area shape="rect" coords="32,-2,293,29" href="http://www.example.com/"></map>') . "\n";
-echo $v->_openLinksInNewWindow('<map name="Map"><area shape="rect" coords="32,-2,293,29" href="http://www.example.com/" target="foo"></map>') . "\n";
+echo $v->openLinksInNewWindow('foo') . "\n";
+echo $v->openLinksInNewWindow('example@example.com') . "\n";
+echo $v->openLinksInNewWindow('foo <a href="#bar">Anchor</a>') . "\n";
+echo $v->openLinksInNewWindow('foo <a href="http://www.example.com/">example</a>') . "\n";
+echo $v->openLinksInNewWindow('foo <a target="foo" href="http://www.example.com/">example</a>') . "\n";
+echo $v->openLinksInNewWindow('foo <a href="http://www.example.com/" target="foo">example</a>') . "\n";
+echo $v->openLinksInNewWindow('foo <a mailto="example@example.com">Example Email</a>') . "\n";
+echo $v->openLinksInNewWindow('<map name="Map"><area shape="rect" coords="32,-2,293,29" href="http://www.example.com/"></map>') . "\n";
+echo $v->openLinksInNewWindow('<map name="Map"><area shape="rect" coords="32,-2,293,29" href="http://www.example.com/" target="foo"></map>') . "\n";
 echo "\n";
+exit;
 
 // Test regex for hiding images.
 echo preg_replace_callback($v->_img_regex, array($v, '_blockImages'),
@@ -56,11 +53,11 @@ foo
 example@example.com
 foo <a href="#bar">Anchor</a>
 foo <a target="_blank" href="http://www.example.com/">example</a>
-foo <a   target="_blank" href="http://www.example.com/">example</a>
-foo <a  href="http://www.example.com/"  target="_blank">example</a>
+foo <a target="_blank" href="http://www.example.com/">example</a>
+foo <a href="http://www.example.com/" target="_blank">example</a>
 foo <a target="_blank" mailto="example@example.com">Example Email</a>
 <map name="Map"><area target="_blank" shape="rect" coords="32,-2,293,29" href="http://www.example.com/"></map>
-<map name="Map"><area  shape="rect" coords="32,-2,293,29" href="http://www.example.com/"  target="_blank"></map>
+<map name="Map"><area shape="rect" coords="32,-2,293,29" href="http://www.example.com/" target="_blank"></map>
 
 <img src="/spacer_red.png" blocked="http%3A%2F%2Fexample.com%2Fimage.png">
 <img src="/spacer_red.png" blocked="http%3A%2F%2Fexample.com%2Fimage.png" />