From f14f252a1633c9297f33a98ef7f4ccf6db0efaef Mon Sep 17 00:00:00 2001
From: Brion Vibber <brion@status.net>
Date: Thu, 12 Aug 2010 11:39:42 -0700
Subject: [PATCH] TinyMCE: fixes to attachment handling

---
 plugins/TinyMCE/TinyMCEPlugin.php | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/plugins/TinyMCE/TinyMCEPlugin.php b/plugins/TinyMCE/TinyMCEPlugin.php
index f67d6ea139..47d3d059f2 100644
--- a/plugins/TinyMCE/TinyMCEPlugin.php
+++ b/plugins/TinyMCE/TinyMCEPlugin.php
@@ -148,8 +148,7 @@ class TinyMCEPlugin extends Plugin
                         $this->formatAttachment($img, $media);
                     }
                 }
-                $html = $dom->saveHTML();
-                $options['rendered'] = $html;
+                $options['rendered'] = $this->saveHtml($dom);
             }
 
             // The regular code will append the short URL to the plaintext content.
@@ -166,29 +165,26 @@ class TinyMCEPlugin extends Plugin
      */
     private function formatAttachment($img, $media)
     {
+        $parent = $img->parentNode;
         $dom = $img->ownerDocument;
+
         $link = $dom->createElement('a');
         $link->setAttribute('href', $media->fileurl);
+        $link->setAttribute('title', File::url($media->filename));
 
         if ($this->isEmbeddable($media)) {
-            common_log(LOG_INFO, 'QQQQQ');
             // Fix the the <img> attributes and wrap the link around it...
             $this->insertImage($img, $media);
-            common_log(LOG_INFO, 'QQQQQ A!');
-            try {
-                $dom->replaceChild($link, $img); //it dies in here?!
-            } catch (Exception $wtf) {
-                common_log(LOG_ERR, 'QQQ WTF? ' . $wtf->getMessage());
-            }
-            common_log(LOG_INFO, 'QQQQQ B!');
+            $parent->replaceChild($link, $img); //it dies in here?!
             $link->appendChild($img);
-            common_log(LOG_INFO, 'QQQQQ C!');
         } else {
-            common_log(LOG_INFO, 'QQQQQ X');
             // Not an image? Replace it with a text link.
+            $link->setAttribute('rel', 'external');
+            $link->setAttribute('class', 'attachment');
+            $link->setAttribute('id', 'attachment-' . $media->fileRecord->id);
             $text = $dom->createTextNode($media->shortUrl());
             $link->appendChild($text);
-            $dom->replaceChild($link, $img);
+            $parent->replaceChild($link, $img);
         }
     }
 
@@ -265,6 +261,15 @@ class TinyMCEPlugin extends Plugin
         }
     }
 
+    private function saveHtml($dom)
+    {
+        $html = $dom->saveHTML();
+        // hack to remove surrounding crap added to the dom
+        // all we wanted was a fragment
+        $stripped = preg_replace('/^.*<body[^>]*>(.*)<\/body.*$/is', '$1', $html);
+        return $stripped;
+    }
+
     function _inlineScript()
     {
         $path = common_path('plugins/TinyMCE/js/tiny_mce.js');
-- 
2.39.5