Revision: 7333
          http://mahogany.svn.sourceforge.net/mahogany/?rev=7333&view=rev
Author:   vadz
Date:     2007-08-22 14:14:48 -0700 (Wed, 22 Aug 2007)

Log Message:
-----------
rewrote InsertRawContents() to only display the contents of the <body> of the 
embedded message instead of trying to preserve it entirely as this results in 
unexpected display with broken HTML messages

Modified Paths:
--------------
    trunk/M/src/modules/HtmlViewer.cpp

Modified: trunk/M/src/modules/HtmlViewer.cpp
===================================================================
--- trunk/M/src/modules/HtmlViewer.cpp  2007-08-22 21:03:54 UTC (rev 7332)
+++ trunk/M/src/modules/HtmlViewer.cpp  2007-08-22 21:14:48 UTC (rev 7333)
@@ -286,6 +286,9 @@
 class EncodingChanger : private AttributeChanger
 {
 public:
+   // change the encoding to the given one during this object lifetime
+   //
+   // does nothing if enc == wxFONTENCODING_SYSTEM
    EncodingChanger(wxFontEncoding enc, String& str)
       : AttributeChanger(str)
    {
@@ -299,10 +302,20 @@
          // could change it either to our own custom tag or to something
          // completely different, but for now leave it as is...
          DoChange(GetMetaString(wxFontMapper::GetEncodingName(enc)),
-                  GetMetaString(_T("iso-8859-1")));
+                  GetDefaultMeta());
       }
    }
 
+   // another version using the charset name, does nothing if it's empty
+   EncodingChanger(const String& charset, String& str)
+      : AttributeChanger(str)
+   {
+      if ( !charset.empty() )
+      {
+         DoChange(GetMetaString(charset), GetDefaultMeta());
+      }
+   }
+
 private:
    static String GetMetaString(const String& charset)
    {
@@ -312,6 +325,8 @@
       return s;
    }
 
+   static String GetDefaultMeta() { return GetMetaString(_T("iso-8859-1")); }
+
    DECLARE_NO_COPY_CLASS(EncodingChanger)
 };
 
@@ -1053,61 +1068,56 @@
 
 void HtmlViewer::InsertRawContents(const String& data)
 {
-   // collect all meta tags in the HTML document in this string
-   String metaTags;
+   // we're already inside body so we can't insert the entire HTML document
+   // here, so extract just its body and also take its charset
+   EncodingChanger
+      encChanger(wxHtmlParser::ExtractCharsetInformation(data), m_htmlText);
 
-   // we're already inside body so we can't insert the entire HTML document
-   // here, instead we're going to try to insert our existing contents in the
-   // beginning of the given document
-   const wxChar *pHtml = data.c_str();
-   const wxChar *pBody = pHtml;
-   while ( pBody )
+   // tag handler used to extract the <body> tag contents
+   class BodyTagHandler : public wxHtmlTagHandler
    {
-      pBody = wxStrchr(pBody, _T('<'));
-      if ( pBody )
+   public:
+      BodyTagHandler()
       {
-         // we also check for "<meta charset>" tag or, as it's simpler, for any
-         // "<meta>" tags at all, as otherwise we could have a wrong charset
-         if ( (pBody[1] == _T('m') || pBody[1] == _T('M')) &&
-              (pBody[2] == _T('e') || pBody[2] == _T('E')) &&
-              (pBody[3] == _T('t') || pBody[3] == _T('T')) &&
-              (pBody[4] == _T('a') || pBody[4] == _T('A')) )
-         {
-            const wxChar *pEnd = wxStrchr(pBody + 4, _T('>'));
-            if ( pEnd )
-            {
-               metaTags += String(pBody, pEnd + 1);
-            }
-         }
+         // by default, take all
+         m_begin = 0;
+         m_end = String::npos;
+      }
 
-         if ( (pBody[1] == _T('b') || pBody[1] == _T('B')) &&
-              (pBody[2] == _T('o') || pBody[2] == _T('O')) &&
-              (pBody[3] == _T('d') || pBody[3] == _T('D')) &&
-              (pBody[4] == _T('y') || pBody[4] == _T('Y')) )
-         {
-            pBody = wxStrchr(pBody + 4, _T('>'));
-            if ( pBody )
-            {
-               // skip '>'
-               pBody++;
-            }
+      virtual wxString GetSupportedTags() { return "BODY"; }
+      virtual bool HandleTag(const wxHtmlTag& tag)
+      {
+         m_begin = tag.GetBeginPos();
+         m_end = tag.GetEndPos1(); // position just before the closing tag
 
-            break;
-         }
-
-         pBody++;
+         return false;
       }
-   }
 
-   if ( pBody )
+      size_t GetBegin() const { return m_begin; }
+      size_t GetEnd() const { return m_end; }
+
+   private:
+      size_t m_begin,
+             m_end;
+   };
+
+   class BodyParser : public wxHtmlParser
    {
-      m_htmlText = String(pHtml, pBody) + m_htmlText + metaTags + pBody;
-   }
-   else // HTML fragment only?
-   {
-      m_htmlText += pHtml;
-   }
+   public:
+      BodyParser() { }
 
+      // provide stubs for base class pure virtual methods which we don't use
+      virtual wxObject* GetProduct() { return NULL; }
+      virtual void AddText(const wxString& WXUNUSED(txt)) { }
+   };
+
+   BodyTagHandler *handler = new BodyTagHandler;
+   BodyParser parser;
+   parser.AddTagHandler(handler);
+   parser.Parse(data);
+
+   m_htmlText += data.substr(handler->GetBegin(), handler->GetEnd());
+
    // set the flag for EndBody
    m_hasHtmlContents = true;
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Mahogany-cvsupdates mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mahogany-cvsupdates

Reply via email to