Bug 1612369 - Remove old addressing entries when Compfields2Recipients is called. r=aleca
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 03 Feb 2020 13:44:02 +1300
changeset 37187 26bc58b449520c4b92ded793cf93575aee0f6925
parent 37186 5a831b099713be5a0f1a5c3174a31d7eb5a92600
child 37188 8e4805dafb1a1a680864ed8eb42dc43fbd43351e
push id2552
push userclokep@gmail.com
push dateMon, 10 Feb 2020 21:24:16 +0000
treeherdercomm-beta@f95a6f4408a3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaleca
bugs1612369
Bug 1612369 - Remove old addressing entries when Compfields2Recipients is called. r=aleca
mail/components/compose/content/addressingWidgetOverlay.js
--- a/mail/components/compose/content/addressingWidgetOverlay.js
+++ b/mail/components/compose/content/addressingWidgetOverlay.js
@@ -13,17 +13,17 @@ var { DisplayNameUtils } = ChromeUtils.i
 var gDragService = Cc["@mozilla.org/widget/dragservice;1"].getService(
   Ci.nsIDragService
 );
 
 /**
  * Convert all the written recipients into string and store them into the
  * msgCompFields array to be printed in the message header.
  *
- * @param {Array} msgCompFields - The array containing all the recipients.
+ * @param {Object} msgCompFields - An object to receive the recipients.
  */
 function Recipients2CompFields(msgCompFields) {
   if (!msgCompFields) {
     throw new Error(
       "Message Compose Error: msgCompFields is null (ExtractRecipients)"
     );
   }
 
@@ -91,102 +91,126 @@ function Recipients2CompFields(msgCompFi
   msgCompFields.replyTo = addrReply;
   msgCompFields.newsgroups = addrNg;
   msgCompFields.followupTo = addrFollow;
 }
 
 /**
  * Convert all the recipients coming from a message header into pills.
  *
- * @param {Array} msgCompFields - The array containing all the recipients.
+ * @param {Object} msgCompFields - An object containing all the recipients. If
+ *                                 any property is not a string, it is ignored.
  */
 function CompFields2Recipients(msgCompFields) {
   if (msgCompFields) {
-    let msgReplyTo = msgCompFields.replyTo
-      ? MailServices.headerParser.parseEncodedHeader(msgCompFields.replyTo)
-      : null;
-    let msgTo = msgCompFields.to
-      ? MailServices.headerParser.parseEncodedHeader(msgCompFields.to)
-      : null;
-    let msgCC = msgCompFields.cc
-      ? MailServices.headerParser.parseEncodedHeader(msgCompFields.cc)
-      : null;
-    let msgBCC = msgCompFields.bcc
-      ? MailServices.headerParser.parseEncodedHeader(msgCompFields.bcc)
-      : null;
-    let msgNewsgroups = msgCompFields.newsgroups;
-    let msgFollowupTo = msgCompFields.followupTo
-      ? MailServices.headerParser.parseEncodedHeader(msgCompFields.followupTo)
-      : null;
-
     // Populate all the recipients with the proper values.
     // We need to force the focus() on each input to trigger the attachment
     // of the autocomplete mController.
-    if (msgReplyTo) {
-      showAddressRow(document.getElementById("addr_reply"), "addressRowReply");
+    if (typeof msgCompFields.replyTo == "string") {
       let input = document.getElementById("replyAddrInput");
-      input.focus();
-      input.value = msgReplyTo.join(", ");
-      recipientAddPill(input, true);
+      recipientClearPills(input);
+
+      let msgReplyTo = MailServices.headerParser.parseEncodedHeader(
+        msgCompFields.replyTo
+      );
+      if (msgReplyTo.length) {
+        showAddressRow(
+          document.getElementById("addr_reply"),
+          "addressRowReply"
+        );
+        input.focus();
+        input.value = msgReplyTo.join(", ");
+        recipientAddPill(input, true);
+      }
     }
 
-    if (msgTo) {
+    if (typeof msgCompFields.to == "string") {
       let input = document.getElementById("toAddrInput");
-      if (input.closest(".address-row").classList.contains("hidden")) {
-        showAddressRow(document.getElementById("addr_to"), "addressRowTo");
+      recipientClearPills(input);
+
+      let msgTo = MailServices.headerParser.parseEncodedHeader(
+        msgCompFields.to
+      );
+      if (msgTo.length) {
+        if (input.closest(".address-row").classList.contains("hidden")) {
+          showAddressRow(document.getElementById("addr_to"), "addressRowTo");
+        }
+        input.focus();
+        input.value = msgTo.join(", ");
+        recipientAddPill(input, true);
       }
-      input.focus();
-      input.value = msgTo.join(", ");
-      recipientAddPill(input, true);
     }
 
-    if (msgCC) {
-      showAddressRow(document.getElementById("addr_cc"), "addressRowCc");
+    if (typeof msgCompFields.cc == "string") {
       let input = document.getElementById("ccAddrInput");
-      input.focus();
-      input.value = msgCC.join(", ");
-      recipientAddPill(input, true);
+      recipientClearPills(input);
+
+      let msgCC = MailServices.headerParser.parseEncodedHeader(
+        msgCompFields.cc
+      );
+      if (msgCC.length) {
+        showAddressRow(document.getElementById("addr_cc"), "addressRowCc");
+        input.focus();
+        input.value = msgCC.join(", ");
+        recipientAddPill(input, true);
+      }
     }
 
-    if (msgBCC) {
-      showAddressRow(document.getElementById("addr_bcc"), "addressRowBcc");
+    if (typeof msgCompFields.bcc == "string") {
       let input = document.getElementById("bccAddrInput");
-      input.focus();
-      input.value = msgBCC.join(", ");
-      recipientAddPill(input, true);
+      recipientClearPills(input);
+
+      let msgBCC = MailServices.headerParser.parseEncodedHeader(
+        msgCompFields.bcc
+      );
+      if (msgBCC.length) {
+        showAddressRow(document.getElementById("addr_bcc"), "addressRowBcc");
+        input.focus();
+        input.value = msgBCC.join(", ");
+        recipientAddPill(input, true);
+      }
     }
 
-    if (msgNewsgroups) {
-      showAddressRow(
-        document.getElementById("addr_newsgroups"),
-        "addressRowNewsgroups"
-      );
+    if (typeof msgCompFields.newsgroups == "string") {
       let input = document.getElementById("newsgroupsAddrInput");
-      input.focus();
-      input.value = msgNewsgroups;
-      recipientAddPill(input, true);
+      recipientClearPills(input);
+
+      if (msgCompFields.newsgroups) {
+        showAddressRow(
+          document.getElementById("addr_newsgroups"),
+          "addressRowNewsgroups"
+        );
+        input.focus();
+        input.value = msgCompFields.newsgroups;
+        recipientAddPill(input, true);
+      }
     }
 
-    if (msgFollowupTo) {
-      showAddressRow(
-        document.getElementById("addr_followup"),
-        "addressRowFollowup"
+    if (typeof msgCompFields.followupTo == "string") {
+      let input = document.getElementById("followupAddrInput");
+      recipientClearPills(input);
+
+      let msgFollowupTo = MailServices.headerParser.parseEncodedHeader(
+        msgCompFields.followupTo
       );
-      let input = document.getElementById("followupAddrInput");
-      input.focus();
-      input.value = msgFollowupTo.join(", ");
-      recipientAddPill(input, true);
+      if (msgFollowupTo.length) {
+        showAddressRow(
+          document.getElementById("addr_followup"),
+          "addressRowFollowup"
+        );
+        input.focus();
+        input.value = msgFollowupTo.join(", ");
+        recipientAddPill(input, true);
+      }
     }
 
-    // CompFields2Recipients is called whenever a user replies or edits an existing message. We want to
-    // add all of the non-empty recipients for this message to the ignore list for spell check
-    let currentAddress = gCurrentIdentity ? gCurrentIdentity.fullAddress : "";
-    addRecipientsToIgnoreList(
-      [currentAddress, msgTo, msgCC, msgBCC].filter(adr => adr).join(", ")
-    );
+    // Add the sender to our spell check ignore list.
+    if (gCurrentIdentity) {
+      addRecipientsToIgnoreList(gCurrentIdentity.fullAddress);
+    }
   }
 }
 
 /**
  * Update the recipients area UI to show News related fields and hide
  * Mail releated fields.
  */
 function updateUIforNNTPAccount() {
@@ -574,16 +598,28 @@ function recipientAddPill(element, autom
   // Attach it again to enable autocomplete.
   element.attachController();
 
   onRecipientsChanged(automatic);
   calculateHeaderHeight();
 }
 
 /**
+ * Remove existing "address-pill" elements from the parent recipient container.
+ *
+ * @param {HTMLElement} element - The input element in the container to clear.
+ */
+function recipientClearPills(element) {
+  let container = element.closest(".address-container");
+  for (let pill of container.querySelectorAll("mail-address-pill")) {
+    pill.remove();
+  }
+}
+
+/**
  * Force a focused styling on the recipient container of the currently
  * selected input element.
  *
  * @param {HTMLElement} element - The element receiving focus.
  */
 function highlightAddressContainer(element) {
   element.closest(".address-container").setAttribute("focused", "true");
   deselectAllPills();