Bug 1512977 - Part 2: Stop relying on x-mac-croatian for testing. r=mkmelin a=jorgk
authorJorg K <jorgk@jorgk.com>
Tue, 11 Dec 2018 23:01:43 +0100
changeset 33876 5a1af52c188e4c092576ff67bef9d5dbc07bb265
parent 33875 e2a06105446af2ccbcad99fc49b90e607ca07208
child 33877 514f894df2cb5915e059f1ef896abd5a481aef65
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersmkmelin, jorgk
bugs1512977
Bug 1512977 - Part 2: Stop relying on x-mac-croatian for testing. r=mkmelin a=jorgk
mail/test/mozmill/composition/test-charset-edit.js
mailnews/compose/src/nsMsgCompose.cpp
mailnews/mime/src/mimemoz2.cpp
--- a/mail/test/mozmill/composition/test-charset-edit.js
+++ b/mail/test/mozmill/composition/test-charset-edit.js
@@ -80,31 +80,31 @@ function getMsgHeaders(aMsgHdr, aGetText
                                                         false,
                                                         "",
                                                         false);
   utils.waitFor(() => handler._done);
   return aGetText ? handler._text : handler._data;
 }
 
 /**
- * Test that if we reply to a message in x-mac-croatian, we don't try to compose
- * in x-mac-croatian. Instead, we should be using the default charset (set to
+ * Test that if we reply to a message in an invalid charset, we don't try to compose
+ * in that charset. Instead, we should be using the default charset (set to
  * not be UTF-8 in this test).
  */
 function test_wrong_reply_charset() {
   let folder = gDrafts;
   let msg0 = create_message({
     bodyPart: new SyntheticPartLeaf("Some text",
-      {charset: "x-mac-croatian"})
+      {charset: "invalid-charset"})
   });
   add_message_to_folder(folder, msg0);
   be_in_folder(folder);
   let msg = select_click_row(0);
   assert_selected_and_displayed(mc, msg);
-  assert_equals(getMsgHeaders(msg).get("").charset, "x-mac-croatian");
+  assert_equals(getMsgHeaders(msg).get("").charset, "invalid-charset");
 
   let rwc = open_compose_with_reply();
   // Ctrl+S = save as draft.
   rwc.keypress(null, "s", {shiftKey: false, accelKey: true});
   close_compose_window(rwc);
 
   let draftMsg = select_click_row(1);
   assert_equals(getMsgHeaders(draftMsg).get("").charset, "windows-1252");
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -1608,21 +1608,31 @@ static nsresult fixCharset(nsCString &aC
   nsresult rv;
   nsCOMPtr<nsICharsetConverterManager> ccm =
     do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCString charset(aCharset);
   rv = ccm->GetCharsetAlias(charset.get(), aCharset);
 
+  // Replace an unrecognized charset with the default.
+  if (NS_FAILED(rv)) {
+    nsCOMPtr<nsIPrefBranch> prefs (do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
+    NS_ENSURE_SUCCESS(rv, rv);
+    nsString defaultCharset;
+    NS_GetLocalizedUnicharPreferenceWithDefault(prefs, "mailnews.send_default_charset",
+                                                NS_LITERAL_STRING("UTF-8"), defaultCharset);
+    LossyCopyUTF16toASCII(defaultCharset, aCharset);
+    return NS_OK;
+  }
+
   // Don't accept UTF-16 ever. UTF-16 should never be selected as an
   // outgoing encoding for e-mail. MIME can't handle those messages
   // encoded in ASCII-incompatible encodings.
-  if (NS_FAILED(rv) ||
-      StringBeginsWith(aCharset, NS_LITERAL_CSTRING("UTF-16"))) {
+  if (StringBeginsWith(aCharset, NS_LITERAL_CSTRING("UTF-16"))) {
     aCharset.AssignLiteral("UTF-8");
   }
   return NS_OK;
 }
 
 // This used to be called BEFORE editor was created
 //  (it did the loadURI that triggered editor creation)
 // It is called from JS after editor creation
--- a/mailnews/mime/src/mimemoz2.cpp
+++ b/mailnews/mime/src/mimemoz2.cpp
@@ -825,20 +825,17 @@ int ConvertToUTF8(const char *stringToUs
     rv = CopyUTF7toUTF16(nsDependentCString(stringToUse, inLength), utf16);
     if (NS_FAILED(rv))
       return -1;
     CopyUTF16toUTF8(utf16, outString);
     return 0;
   }
 
   auto encoding = mozilla::Encoding::ForLabel(newCharset);
-  if (!encoding) {
-    // Assume input is UTF-8.
-    encoding = UTF_8_ENCODING;
-  }
+  NS_ENSURE_TRUE(encoding, -1);  // Impossible since GetCharsetAlias() already checked.
 
   rv = encoding->DecodeWithoutBOMHandling(nsDependentCSubstring(stringToUse, inLength), outString);
   return NS_SUCCEEDED(rv) ? 0 : -1;
 }
 
 static int
 mime_convert_charset (const char *input_line, int32_t input_length,
                       const char *input_charset,