Bug 1668848 - Cleanup, rectify and improve readibility and performance of recipientAddPills(). r=aleca a=wsmwk
authorThomas Duellmann <bugzilla2007@duellmann24.net>
Thu, 08 Oct 2020 13:52:41 +0300
changeset 40827 9921e1929e744e2f5e64163f036c215f2960fc1f
parent 40826 4fede0d63a34ce1d50eadb6f8c31beff4f3f8191
child 40828 752ebbceaa18c910f4c08cd51ce7d05e4b1bad80
push id406
push userthunderbird@calypsoblue.org
push dateMon, 19 Oct 2020 19:46:44 +0000
reviewersaleca, wsmwk
bugs1668848
Bug 1668848 - Cleanup, rectify and improve readibility and performance of recipientAddPills(). r=aleca a=wsmwk Change function name from recipientAddPill() to recipientAddPills(). Rename 'element' argument to 'input', also for some other functions. Avoid repeated runs of addRecipientsToIgnoreList() for all addresses of input. Improve various function comments.
mail/components/compose/content/MsgComposeCommands.js
mail/components/compose/content/addressingWidgetOverlay.js
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -190,17 +190,17 @@ const inputObserver = {
       // Interrupt if there's no input proxy, or the input doesn't have an ID,
       // the latter meaning that the autocomplete event was triggered within an
       // already existing pill, so we don't want to create a new pill.
       if (!input || !input.id) {
         return;
       }
 
       // Trigger the pill creation.
-      recipientAddPill(document.getElementById(input.id));
+      recipientAddPills(document.getElementById(input.id));
     }
   },
 };
 
 function InitializeGlobalVariables() {
   gMessenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
 
   gMsgCompose = null;
@@ -5006,17 +5006,23 @@ function OutputFormatMenuSelect(target) 
     }
 
     SetCompositionAsPerDeliveryFormat(gSendFormat);
     gMsgCompose.compFields.deliveryFormat = gSendFormat;
     gContentChanged = currentSendFormat != gSendFormat;
   }
 }
 
-// walk through the recipients list and add them to the inline spell checker ignore list
+/**
+ * Walk through a plain text list of recipients and add them to the inline spell
+ * checker ignore list, e.g. to avoid that known recipient names get marked
+ * wrong in message body.
+ *
+ * @param {string} aAddressesToAdd - A (comma-separated) recipient(s) string.
+ */
 function addRecipientsToIgnoreList(aAddressesToAdd) {
   if (gSpellChecker.enabled) {
     // break the list of potentially many recipients back into individual names
     let addresses = MailServices.headerParser.parseEncodedHeader(
       aAddressesToAdd
     );
     let tokenizedNames = [];
 
--- a/mail/components/compose/content/addressingWidgetOverlay.js
+++ b/mail/components/compose/content/addressingWidgetOverlay.js
@@ -118,79 +118,79 @@ function CompFields2Recipients(msgCompFi
       );
       if (msgReplyTo.length) {
         showAddressRow(
           document.getElementById("addr_reply"),
           "addressRowReply"
         );
         input.focus();
         input.value = msgReplyTo.join(", ");
-        recipientAddPill(input, true);
+        recipientAddPills(input, true);
       }
     }
 
     if (typeof msgCompFields.to == "string") {
       let input = document.getElementById("toAddrInput");
       recipientClearPills(input);
 
       let msgTo = MailServices.headerParser.parseEncodedHeaderW(
         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);
+        recipientAddPills(input, true);
       }
     }
 
     if (typeof msgCompFields.cc == "string") {
       let input = document.getElementById("ccAddrInput");
       recipientClearPills(input);
 
       let msgCC = MailServices.headerParser.parseEncodedHeaderW(
         msgCompFields.cc
       );
       if (msgCC.length) {
         showAddressRow(document.getElementById("addr_cc"), "addressRowCc");
         input.focus();
         input.value = msgCC.join(", ");
-        recipientAddPill(input, true);
+        recipientAddPills(input, true);
       }
     }
 
     if (typeof msgCompFields.bcc == "string") {
       let input = document.getElementById("bccAddrInput");
       recipientClearPills(input);
 
       let msgBCC = MailServices.headerParser.parseEncodedHeaderW(
         msgCompFields.bcc
       );
       if (msgBCC.length) {
         showAddressRow(document.getElementById("addr_bcc"), "addressRowBcc");
         input.focus();
         input.value = msgBCC.join(", ");
-        recipientAddPill(input, true);
+        recipientAddPills(input, true);
       }
     }
 
     if (typeof msgCompFields.newsgroups == "string") {
       let input = document.getElementById("newsgroupsAddrInput");
       recipientClearPills(input);
 
       if (msgCompFields.newsgroups) {
         showAddressRow(
           document.getElementById("addr_newsgroups"),
           "addressRowNewsgroups"
         );
         input.focus();
         input.value = msgCompFields.newsgroups;
-        recipientAddPill(input, true);
+        recipientAddPills(input, true);
       }
     }
 
     if (typeof msgCompFields.followupTo == "string") {
       let input = document.getElementById("followupAddrInput");
       recipientClearPills(input);
 
       let msgFollowupTo = MailServices.headerParser.parseEncodedHeaderW(
@@ -198,17 +198,17 @@ function CompFields2Recipients(msgCompFi
       );
       if (msgFollowupTo.length) {
         showAddressRow(
           document.getElementById("addr_followup"),
           "addressRowFollowup"
         );
         input.focus();
         input.value = msgFollowupTo.join(", ");
-        recipientAddPill(input, true);
+        recipientAddPills(input, true);
       }
     }
 
     // Add the sender to our spell check ignore list.
     if (gCurrentIdentity) {
       addRecipientsToIgnoreList(gCurrentIdentity.fullAddress);
     }
 
@@ -725,85 +725,84 @@ function addressInputOnBeforeHandleKeyDo
         event.preventDefault();
         input.closest("mail-recipients-area").moveFocusToPreviousElement(input);
       }
       break;
   }
 }
 
 /**
- * Add a new "address-pill" to the parent recipient container.
+ * Add one or more <mail-address-pill> elements to the containing address row.
  *
- * @param {Element} element - The element that triggered the keypress event.
+ * @param {Element} input - Address input where "autocomplete-did-enter-text"
+ *   was observed, and/or to whose containing address row pill(s) will be added.
  * @param {boolean} [automatic=false] - Set to true if the change of recipients
  *   was invoked programmatically and should not be considered a change of
  *   message content.
  */
-function recipientAddPill(element, automatic = false) {
-  if (!element.value.trim()) {
+function recipientAddPills(input, automatic = false) {
+  if (!input.value.trim()) {
     return;
   }
 
-  let addresses = MailServices.headerParser.makeFromDisplayAddress(
-    element.value
-  );
+  let addresses = MailServices.headerParser.makeFromDisplayAddress(input.value);
   let recipientArea = document.getElementById("recipientsContainer");
 
   for (let address of addresses) {
-    recipientArea.createRecipientPill(element, address);
-
-    // Be sure to add the user add recipient to our ignore list
-    // when the user hits enter in an autocomplete widget...
-    addRecipientsToIgnoreList(element.value);
+    recipientArea.createRecipientPill(input, address);
   }
 
+  // Add the just added recipient address(es) to the spellcheck ignore list.
+  addRecipientsToIgnoreList(input.value.trim());
+
   // Reset the input element.
-  element.removeAttribute("nomatch");
-  element.setAttribute("size", 1);
-  element.value = "";
+  input.removeAttribute("nomatch");
+  input.setAttribute("size", 1);
+  input.value = "";
 
   // We need to detach the autocomplete Controller to prevent the input
   // to be filled with the previously selected address when the "blur" event
   // gets triggered.
-  element.detachController();
+  input.detachController();
   // Attach it again to enable autocomplete.
-  element.attachController();
+  input.attachController();
 
   if (!automatic) {
-    element
+    input
       .closest(".address-container")
       .classList.add("addressing-field-edited");
     onRecipientsChanged();
   }
 
   calculateHeaderHeight();
-  udpateAddressingInputAriaLabel(element.closest(".address-row"));
+  udpateAddressingInputAriaLabel(input.closest(".address-row"));
 }
 
 /**
- * Remove existing "address-pill" elements from the parent recipient container.
+ * Remove all <mail-address-pill> elements from the containing address row.
  *
- * @param {HTMLElement} element - The input element in the container to clear.
+ * @param {Element} input - The address 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")) {
+function recipientClearPills(input) {
+  for (let pill of input
+    .closest(".address-container")
+    .querySelectorAll("mail-address-pill")) {
     pill.remove();
   }
-  udpateAddressingInputAriaLabel(element.closest(".address-row"));
+  udpateAddressingInputAriaLabel(input.closest(".address-row"));
 }
 
 /**
  * Handle focus event of address inputs: Force a focused styling on the closest
  * address container of the currently focused input element.
  *
- * @param {HTMLElement} element - The input element receiving focus.
+ * @param {Element} input - The address input element receiving focus.
  */
-function addressInputOnFocus(element) {
-  element.closest(".address-container").setAttribute("focused", "true");
+function addressInputOnFocus(input) {
+  input.closest(".address-container").setAttribute("focused", "true");
   deselectAllPills();
 }
 
 /**
  * Deselect any previously selected pills.
  */
 function deselectAllPills() {
   for (let pill of document.querySelectorAll(`mail-address-pill[selected]`)) {
@@ -844,17 +843,17 @@ function addressInputOnBlur(input) {
     MailServices.ab.mailListNameExists(listNames[0].name);
 
   if (
     address &&
     (isValidAddress(address) ||
       isMailingList ||
       input.classList.contains("news-input"))
   ) {
-    recipientAddPill(input);
+    recipientAddPills(input);
   }
 
   // Trim any remaining input for which we didn't create a pill.
   if (input.value.trim()) {
     input.value = input.value.trim();
   }
 }