Bug 394322 - Fix various forwarding issues: <html attr>, without <html>, content before <html>. r=aceman a=jorgk BETA_60_CONTINUATION
authorJorg K <jorgk@jorgk.com>
Sun, 24 Jun 2018 18:20:43 +0200
branchBETA_60_CONTINUATION
changeset 32013 b78ee64b973d
parent 32012 c26f70927c76
child 32014 516fa9132deb
push id384
push userclokep@gmail.com
push date2018-06-26 01:38 +0000
reviewersaceman, jorgk
bugs394322
Bug 394322 - Fix various forwarding issues: <html attr>, without <html>, content before <html>. r=aceman a=jorgk
mail/test/mozmill/composition/content-utf8-alt-rel.eml
mail/test/mozmill/composition/content-utf8-alt-rel2.eml
mail/test/mozmill/composition/content-utf8-rel-alt.eml
mail/test/mozmill/composition/test-forward-utf8.js
mailnews/mime/src/mimeTextHTMLParsed.cpp
mailnews/mime/src/mimedrft.cpp
mailnews/mime/src/mimethsa.cpp
--- a/mail/test/mozmill/composition/content-utf8-alt-rel.eml
+++ b/mail/test/mozmill/composition/content-utf8-alt-rel.eml
@@ -17,17 +17,18 @@ Content-Transfer-Encoding: 8bit
 Content-Type: multipart/related;
  boundary="------------related"
 
 
 --------------related
 Content-Type: text/html; charset=UTF-8
 Content-Transfer-Encoding: 8bit
 
-<html>
+<html attr>
+  <!-- This also needs to work when the html tag has an attribute -->
   <head>
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
   </head>
   <body bgcolor="#FFFFFF" text="#000000">
     áóúäöüß<br>
     <img src="cid:part1" alt=""><br>
   </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/mail/test/mozmill/composition/content-utf8-alt-rel2.eml
@@ -0,0 +1,46 @@
+From: test <test@example.com>
+Subject: test multipart, alternative first
+To: test2 <test2@example.com>
+Date: Sat, 27 Feb 2016 17:11:45 +0100
+MIME-Version: 1.0
+Content-Type: multipart/alternative;
+ boundary="------------alternative"
+
+This is a multi-part message in MIME format.
+--------------alternative
+Content-Type: text/plain; charset=UTF-8; format=flowed
+Content-Transfer-Encoding: 8bit
+
+áóúäöüß
+
+--------------alternative
+Content-Type: multipart/related;
+ boundary="------------related"
+
+
+--------------related
+Content-Type: text/html; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+áóúäöüß<br>
+<html>
+  <!-- This also needs to work when there is content before the html tag :-( -->
+  <head>
+    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
+  </head>
+  <body bgcolor="#FFFFFF" text="#000000">
+    <img src="cid:part1" alt=""><br>
+  </body>
+</html>
+
+--------------related
+Content-Type: image/png
+Content-Transfer-Encoding: base64
+Content-ID: <part1>
+
+iVBORw0KGgoAAAANSUhEUgAAAAYAAAALCAIAAADTMGvBAAAAEUlEQVQImWPgsi5DQwxDWggA
+lCEwN+YGfiYAAAAASUVORK5CYII=
+--------------related--
+
+--------------alternative--
+
--- a/mail/test/mozmill/composition/content-utf8-rel-alt.eml
+++ b/mail/test/mozmill/composition/content-utf8-rel-alt.eml
@@ -13,25 +13,24 @@ Content-Type: text/plain; charset=UTF-8;
 Content-Transfer-Encoding: 8bit
 
 áóúäöüß
 
 --------------alternative
 Content-Type: text/html; charset=UTF-8
 Content-Transfer-Encoding: 8bit
 
-<html>
+  <!-- This also needs to work when there is no html tag -->
   <head>
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
   </head>
   <body bgcolor="#FFFFFF" text="#000000">
     áóúäöüß<br>
     <img src="cid:part1" alt=""><br>
   </body>
-</html>
 
 --------------alternative
 
 --------------related
 Content-Type: image/png
 Content-Transfer-Encoding: base64
 Content-ID: <part1>
 
--- a/mail/test/mozmill/composition/test-forward-utf8.js
+++ b/mail/test/mozmill/composition/test-forward-utf8.js
@@ -100,14 +100,22 @@ function forwardViaFolder(aFilePath) {
 function test_utf8_forwarding_from_opened_file() {
   forwardDirect("./content-utf8-rel-only.eml");
   forwardDirect("./content-utf8-rel-alt.eml");
   forwardDirect("./content-utf8-alt-rel.eml");
 }
 
 function test_utf8_forwarding_from_via_folder() {
   forwardViaFolder("./content-utf8-rel-only.eml");
-  forwardViaFolder("./content-utf8-rel-alt.eml");
-  forwardViaFolder("./content-utf8-alt-rel.eml");
+  forwardViaFolder("./content-utf8-rel-alt.eml");   // Also tests HTML part without <html> tag.
+  forwardViaFolder("./content-utf8-alt-rel.eml");   // Also tests <html attr>.
+  forwardViaFolder("./content-utf8-alt-rel2.eml");  // Also tests content before <html>.
+
+  // Repeat the last three in simple HTML view.
+  Services.prefs.setIntPref("mailnews.display.html_as", 3);
+  forwardViaFolder("./content-utf8-rel-alt.eml");   // Also tests HTML part without <html> tag.
+  forwardViaFolder("./content-utf8-alt-rel.eml");   // Also tests <html attr>.
+  forwardViaFolder("./content-utf8-alt-rel2.eml");  // Also tests content before <html>.
 }
 
 function teardownModule() {
+  Services.prefs.clearUserPref("mailnews.display.html_as");
 }
--- a/mailnews/mime/src/mimeTextHTMLParsed.cpp
+++ b/mailnews/mime/src/mimeTextHTMLParsed.cpp
@@ -79,16 +79,18 @@ MimeInlineTextHTMLParsed_parse_eof(MimeO
   // We have to cache all lines and parse the whole document at once.
   // There's a useful sounding function parseFromStream(), but it only allows XML
   // mimetypes, not HTML. Methinks that's because the HTML soup parser
   // needs the entire doc to make sense of the gibberish that people write.
   if (!me || !me->complete_buffer)
     return 0;
 
   nsString& rawHTML = *(me->complete_buffer);
+  if (rawHTML.IsEmpty())
+    return 0;
   nsString parsed;
   nsresult rv;
 
   // Parse the HTML source.
   nsCOMPtr<nsIDOMDocument> document;
   nsCOMPtr<nsIDOMParser> parser = do_GetService(NS_DOMPARSER_CONTRACTID);
   rv = parser->ParseFromString(rawHTML.get(), "text/html",
                                getter_AddRefs(document));
--- a/mailnews/mime/src/mimedrft.cpp
+++ b/mailnews/mime/src/mimedrft.cpp
@@ -660,18 +660,18 @@ static void
 mime_insert_all_headers(char            **body,
                         MimeHeaders     *headers,
                         MSG_ComposeFormat composeFormat,
                         char            *mailcharset)
 {
   bool htmlEdit = (composeFormat == nsIMsgCompFormat::HTML);
   char *newBody = NULL;
   char *html_tag = nullptr;
-  if (*body)
-    html_tag = PL_strcasestr(*body, "<HTML>");
+  if (*body && PL_strncasecmp(*body, "<HTML", 5) == 0)
+    html_tag = PL_strchr(*body, '>') + 1;
   int i;
 
   if (!headers->done_p)
   {
     MimeHeaders_build_heads_list(headers);
     headers->done_p = true;
   }
 
@@ -755,17 +755,17 @@ mime_insert_all_headers(char            
     PR_Free(name);
   }
 
   if (htmlEdit)
   {
     NS_MsgSACat(&newBody, "</TABLE>");
     NS_MsgSACat(&newBody, MSG_LINEBREAK "<BR><BR>");
     if (html_tag)
-      NS_MsgSACat(&newBody, html_tag+6);
+      NS_MsgSACat(&newBody, html_tag);
     else if (*body)
         NS_MsgSACat(&newBody, *body);
   }
   else
   {
     NS_MsgSACat(&newBody, MSG_LINEBREAK MSG_LINEBREAK);
     if (*body)
       NS_MsgSACat(&newBody, *body);
@@ -796,18 +796,18 @@ mime_insert_normal_headers(char         
   nsCString reply_to(MimeHeaders_get(headers, HEADER_REPLY_TO, false, true));
   char *organization = MimeHeaders_get(headers, HEADER_ORGANIZATION, false, false);
   nsCString to(MimeHeaders_get(headers, HEADER_TO, false, true));
   nsCString cc(MimeHeaders_get(headers, HEADER_CC, false, true));
   char *newsgroups = MimeHeaders_get(headers, HEADER_NEWSGROUPS, false, true);
   char *followup_to = MimeHeaders_get(headers, HEADER_FOLLOWUP_TO, false, true);
   char *references = MimeHeaders_get(headers, HEADER_REFERENCES, false, true);
   const char *html_tag = nullptr;
-  if (*body)
-    html_tag = PL_strcasestr(*body, "<HTML>");
+  if (*body && PL_strncasecmp(*body, "<HTML", 5) == 0)
+    html_tag = PL_strchr(*body, '>') + 1;
   bool htmlEdit = composeFormat == nsIMsgCompFormat::HTML;
 
   if (from.IsEmpty())
     from.Adopt(MimeHeaders_get(headers, HEADER_SENDER, false, true));
   if (resent_from.IsEmpty())
     resent_from.Adopt(MimeHeaders_get(headers, HEADER_RESENT_SENDER, false,
       true));
 
@@ -923,17 +923,17 @@ mime_insert_normal_headers(char         
                       MimeGetNamedString(MIME_MHTML_REFERENCES),
                       mailcharset, htmlEdit);
   }
   if (htmlEdit)
   {
     NS_MsgSACat(&newBody, "</TABLE>");
     NS_MsgSACat(&newBody, MSG_LINEBREAK "<BR><BR>");
     if (html_tag)
-      NS_MsgSACat(&newBody, html_tag+6);
+      NS_MsgSACat(&newBody, html_tag);
     else if (*body)
         NS_MsgSACat(&newBody, *body);
   }
   else
   {
     NS_MsgSACat(&newBody, MSG_LINEBREAK MSG_LINEBREAK);
     if (*body)
       NS_MsgSACat(&newBody, *body);
@@ -964,18 +964,18 @@ mime_insert_micro_headers(char          
   nsCString from(MimeHeaders_get(headers, HEADER_FROM, false, true));
   nsCString resent_from(MimeHeaders_get(headers, HEADER_RESENT_FROM, false, true));
   char *date = MimeHeaders_get(headers, HEADER_DATE, false, true);
   nsCString to(MimeHeaders_get(headers, HEADER_TO, false, true));
   nsCString cc(MimeHeaders_get(headers, HEADER_CC, false, true));
   char *newsgroups = MimeHeaders_get(headers, HEADER_NEWSGROUPS, false,
                      true);
   const char *html_tag = nullptr;
-  if (*body)
-    html_tag = PL_strcasestr(*body, "<HTML>");
+  if (*body && PL_strncasecmp(*body, "<HTML", 5) == 0)
+    html_tag = PL_strchr(*body, '>') + 1;
   bool htmlEdit = composeFormat == nsIMsgCompFormat::HTML;
 
   if (from.IsEmpty())
     from.Adopt(MimeHeaders_get(headers, HEADER_SENDER, false, true));
   if (resent_from.IsEmpty())
     resent_from.Adopt(MimeHeaders_get(headers, HEADER_RESENT_SENDER, false, true));
   if (!date)
     date = MimeHeaders_get(headers, HEADER_RESENT_DATE, false, true);
@@ -1042,17 +1042,17 @@ mime_insert_micro_headers(char          
     mime_intl_insert_message_header_1(&newBody, newsgroups, HEADER_NEWSGROUPS,
                     MimeGetNamedString(MIME_MHTML_NEWSGROUPS),
                     mailcharset, htmlEdit);
   if (htmlEdit)
   {
     NS_MsgSACat(&newBody, "</TABLE>");
     NS_MsgSACat(&newBody, MSG_LINEBREAK "<BR><BR>");
     if (html_tag)
-      NS_MsgSACat(&newBody, html_tag+6);
+      NS_MsgSACat(&newBody, html_tag);
     else if (*body)
         NS_MsgSACat(&newBody, *body);
   }
   else
   {
     NS_MsgSACat(&newBody, MSG_LINEBREAK MSG_LINEBREAK);
     if (*body)
       NS_MsgSACat(&newBody, *body);
--- a/mailnews/mime/src/mimethsa.cpp
+++ b/mailnews/mime/src/mimethsa.cpp
@@ -85,16 +85,18 @@ MimeInlineTextHTMLSanitized_parse_eof(Mi
   // We have to cache all lines and parse the whole document at once.
   // There's a useful sounding function parseFromStream(), but it only allows XML
   // mimetypes, not HTML. Methinks that's because the HTML soup parser
   // needs the entire doc to make sense of the gibberish that people write.
   if (!me || !me->complete_buffer)
     return 0;
 
   nsString& cb = *(me->complete_buffer);
+  if (cb.IsEmpty())
+    return 0;
   nsString sanitized;
 
   // Sanitize.
   HTMLSanitize(cb, sanitized);
 
   // Write it out.
   NS_ConvertUTF16toUTF8 resultCStr(sanitized);
   MimeInlineTextHTML_insert_lang_div(obj, resultCStr);