Bug 1048035 - Remove occurences of deprecated parseHeadersWithArray from calendar code;r+a=philipp
authorMakeMyDay <makemyday@gmx-topmail.de>
Thu, 06 Aug 2015 10:23:25 +0200
changeset 26262 987e9543647fa525308ddce0123f0d8af6477beb
parent 26261 dd5111841d45745b1c275ad6e0658889dcc3c5d9
child 26263 8e324eaa1acce37164b4d56e58c2367c19900adb
push id1850
push userclokep@gmail.com
push dateWed, 08 Mar 2017 19:29:12 +0000
treeherdercomm-esr52@028df196b2d9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1048035
Bug 1048035 - Remove occurences of deprecated parseHeadersWithArray from calendar code;r+a=philipp
calendar/base/content/dialogs/calendar-event-dialog-attendees.xml
calendar/base/modules/calItipUtils.jsm
calendar/base/modules/calUtils.jsm
--- a/calendar/base/content/dialogs/calendar-event-dialog-attendees.xml
+++ b/calendar/base/content/dialogs/calendar-event-dialog-attendees.xml
@@ -237,51 +237,33 @@
               input.setAttribute("id", "attendeeCol3#" + rowNumber);
           }
 
           if (!aAttendee) {
               aAttendee = this.createAttendee();
           }
 
           // construct the display string from common name and/or email address.
-          var inputValue = aAttendee.commonName;
-          var regexp = new RegExp("^mailto:(.*)", "i");
-          if (inputValue) {
+          let cn = aAttendee.commonName || "";
+          let inputValue = cal.removeMailTo(aAttendee.id || "");
+          if (cn.length) {
               // Make the commonName appear in quotes if it contains a
               // character that could confuse the header parser
-              if (inputValue.search(/[,;<>@]/) != -1) {
-                  inputValue = '"' + inputValue + '"';
+              if (cn.search(/[,;<>@]/) != -1) {
+                  cn = '"' + cn + '"';
               }
-              var email = aAttendee.id;
-              if (email && email.length) {
-                  if (regexp.test(email)) {
-                      inputValue += ' <' + RegExp.$1 + '>';
-                  } else {
-                      inputValue += ' <' + email + '>';
-                  }
-              }
-          } else {
-              var email = aAttendee.id;
-              if (email && email.length) {
-                  if (regexp.test(email)) {
-                      inputValue = RegExp.$1;
-                  } else {
-                      inputValue = email;
-                  }
-              }
+              inputValue = (inputValue.length) ? cn + ' <' + inputValue + '>' : cn;
           }
 
-          // remove leading spaces
-          while (inputValue && inputValue[0] == " " ) {
-              inputValue = inputValue.substring(1, inputValue.length);
-          }
+          // trim spaces if any
+          inputValue = inputValue.trim();
 
           // don't set value with null, otherwise autocomplete stops working,
           // but make sure attendee and dirty are set
-          if (inputValue && inputValue != "") {
+          if (inputValue.length) {
 	          input.setAttribute("value", inputValue);
 	          input.value = inputValue;
           }
           input.attendee = aAttendee;
           input.setAttribute("dirty", "true");
 
           if (aAttendee) {
               // Set up userType
@@ -392,66 +374,50 @@
         ]]></body>
       </method>
 
       <property name="attendees">
         <getter><![CDATA[
           let attendees = [];
           let inputField;
 
-
-          function copyNameAttributes(email, name, attendee) {
-              // If the new address has no 'mailto'-prefix but seems
-              // to look like an email-address, we prepend the prefix.
-              // This also allows for non-email-addresses.
-              if (email.toLowerCase().indexOf("mailto:") != 0) {
-                  if (email.indexOf("@") >= 0) {
-                      email = "MAILTO:" + email;
-                  }
-              }
-              attendee.id = email;
-
-              if (name && name.length > 0) {
-                  attendee.commonName = name
-              }
-              return attendee;
-          }
-
           for (let i = 1; inputField = this.getInputElement(i); i++) {
               if (inputField && inputField.value != "") {
                   // the inputfield already has a reference to the attendee
                   // object, we just need to fill in the name.
                   let attendee = inputField.attendee.clone();
                   if (attendee.isOrganizer) {
                       continue;
                   }
 
                   attendee.role = this.getRoleElement(i).getAttribute("role");
                   //attendee.participationStatus = this.getStatusElement(i).getAttribute("status");
                   let userType = this.getUserTypeElement(i).getAttribute("cutype");
                   attendee.userType = (userType == "INDIVIDUAL" ? null : userType) // INDIVIDUAL is the default
 
-                  // break the list of potentially many attendees back into individual names
-                  let emailAddresses = {};
-                  let names = {};
-                  let fullNames = {};
-
-                  MailServices.headerParser.parseHeadersWithArray(inputField.value,
-                                                                  emailAddresses,
-                                                                  names,
-                                                                  fullNames);
-
-                  // We need to expand the attendees, this happens in case the
-                  // user entered comma-separated attendees in one field and
+                  // break the list of potentially many attendees back into individual names. This
+                  // is required in case the user entered comma-separated attendees in one field and
                   // then clicked OK without switching to the next line.
-                  for (var j = 0; j < emailAddresses.value.length; j++) {
-                      if (j > 0) attendee = attendee.clone();
-                      copyNameAttributes(emailAddresses.value[j], names.value[j], attendee);
+                  let parsedInput = MailServices.headerParser.makeFromDisplayAddress(inputField.value);
+                  let j = 0;
+                  let addAttendee = function(aAddress) {
+                      if (j > 0) {
+                          attendee = attendee.clone();
+                      }
+                      attendee.id = cal.prependMailTo(aAddress.email);
+                      if (aAddress.name.length > 0) {
+                          if (aAddress.name.startsWith('"') && aAddress.name.endsWith('"')) {
+                              aAddress.name.slice(1, -1);
+                          }
+                          attendee.commonName = aAddress.name;
+                      }
                       attendees.push(attendee);
-                  }
+                      j++;
+                  };
+                  parsedInput.forEach(addAttendee);
               }
           }
 
           return attendees;
         ]]></getter>
       </property>
 
       <property name="organizer">
@@ -464,65 +430,39 @@
                   let attendee = inputField.attendee.clone();
 
                   //attendee.role = this.getRoleElement(i).getAttribute("role");
                   attendee.participationStatus = this.getStatusElement(i).getAttribute("status");
                   // Organizers do not have a CUTYPE
                   attendee.userType = null;
 
                   // break the list of potentially many attendees back into individual names
-                  let emailAddresses = {};
-                  let names = {};
-                  let fullNames = {};
-
-                  MailServices.headerParser.parseHeadersWithArray(inputField.value,
-                                                                  emailAddresses,
-                                                                  names,
-                                                                  fullNames);
-
-                  if (emailAddresses.value.length > 0) {
-                      // if the new address has no 'mailto'-prefix but seems
-                      // to look like an email-address, we prepend the prefix.
-                      // this also allows for non-email-addresses.
-                      let email = emailAddresses.value[0];
-                      if (email.toLowerCase().indexOf("mailto:") != 0) {
-                          if (email.indexOf("@") >= 0) {
-                              email = "MAILTO:" + email;
-                          }
-                      }
-                      attendee.id = email;
+                  let parsedInput = MailServices.headerParser.makeFromDisplayAddress(inputField.value);
+                  if (parsedInput[0].email > 0) {
+                      attendee.id = cal.prependMailTo(parsedInput[0].email);
                   }
-                  if (names.value.length > 0) {
-                      attendee.commonName = names.value[0];
+                  if (parsedInput[0].name.length > 0) {
+                      attendee.commonName = parsedInput[0].name;
                   }
 
                   if (attendee.isOrganizer) {
                       return attendee;
                   }
               }
           }
 
           return null;
         ]]></getter>
       </property>
 
       <method name="_resolveListByName">
         <parameter name="value"/>
         <body><![CDATA[
-            let emailAddresses = {};
-            let names = {};
-            let fullNames = {};
-            MailServices.headerParser.parseHeadersWithArray(value,
-                                                            emailAddresses,
-                                                            names,
-                                                            fullNames);
-
-            let abDir = this._findListInAddrBooks(names.value[0]);
-
-            return abDir;
+            let entries = MailServices.headerParser.makeFromDisplayAddress(value);
+            return (entries.length) ? this._findListInAddrBooks(entries[0].name) : null;
           ]]></body>
     </method>
 
     <method name="_findListInAddrBooks">
         <parameter name="entryname"/>
         <body><![CDATA[
             let allAddressBooks = MailServices.ab.directories;
 
@@ -691,37 +631,18 @@
       <method name="onModify">
         <body><![CDATA[
           let list = [];
           for (let i = 1; i <= this.mMaxAttendees; i++) {
               // retrieve the string from the appropriate row
               let input = this.getInputElement(i);
               if (input && input.value) {
                   // parse the string to break this down to individual names and addresses
-                  let email = "";
-                  let emailAddresses = {};
-                  let names = {};
-                  let fullNames = {};
-
-                  MailServices.headerParser.parseHeadersWithArray(input.value,
-                                                                  emailAddresses,
-                                                                  names,
-                                                                  fullNames);
-
-                  if (emailAddresses.value.length > 0) {
-                      // if the new address has no 'mailto'-prefix but seems
-                      // to look like an email-address, we prepend the prefix.
-                      // this also allows for non-email-addresses.
-                      email = emailAddresses.value[0];
-                      if (email.toLowerCase().indexOf("mailto:") != 0) {
-                          if (email.indexOf("@") >= 0) {
-                              email = "MAILTO:" + email;
-                          }
-                      }
-                  }
+                  let parsedInput = MailServices.headerParser.makeFromDisplayAddress(input.value);
+                  let email = cal.prependMailTo(parsedInput[0].email);
 
                   let isdirty = false;
                   if (input.hasAttribute("dirty")) {
                       isdirty = input.getAttribute("dirty");
                   }
                   input.removeAttribute("dirty");
                   let entry = {
                       dirty: isdirty,
--- a/calendar/base/modules/calItipUtils.jsm
+++ b/calendar/base/modules/calItipUtils.jsm
@@ -361,32 +361,29 @@ cal.itip = {
         } else {
             // Build a map of usable email addresses
             for (let i = 0; i < identities.length; i++) {
                 let identity = identities.queryElementAt(i, Components.interfaces.nsIMsgIdentity);
                 emailMap[identity.email.toLowerCase()] = true;
             }
         }
 
-        let hdrParser = MailServices.headerParser;
-        let emails = {};
-
         // First check the recipient list
-        hdrParser.parseHeadersWithArray(aMsgHdr.recipients, emails, {}, {});
-        for each (let recipient in emails.value) {
-            if (recipient.toLowerCase() in emailMap) {
+        let toList = MailServices.headerParser.makeFromDisplayAddress(aMsgHdr.recipients);
+        for (let recipient of toList) {
+            if (recipient.name.toLowerCase() in emailMap) {
                 // Return the first found recipient
                 return recipient;
             }
         }
 
         // Maybe we are in the CC list?
-        hdrParser.parseHeadersWithArray(aMsgHdr.ccList, emails, {}, {});
-        for each (let recipient in emails.value) {
-            if (recipient.toLowerCase() in emailMap) {
+        let ccList = MailServices.headerParser.makeFromDisplayAddress(aMsgHdr.ccList);
+        for (let recipient of ccList) {
+            if (recipient.name.toLowerCase() in emailMap) {
                 // Return the first found recipient
                 return recipient;
             }
         }
 
         // Hrmpf. Looks like delegation or maybe Bcc.
         return null;
     },
--- a/calendar/base/modules/calUtils.jsm
+++ b/calendar/base/modules/calUtils.jsm
@@ -283,16 +283,36 @@ let cal = {
                 case "TENTATIVE":
                     return true;
             }
         }
         return false;
     },
 
     /**
+     * Prepends a mailto: prefix to an email address like string
+     *
+     * @param  {string}        the string to prepend the prefix if not already there
+     * @return {string}        the string with prefix
+     */
+    prependMailTo: function(aId) {
+        return (!aId.search(/^MAILTO:/i) && aId.includes("@")) ? "MAILTO:" + aId : aId;
+    },
+
+    /**
+     * Removes an existing mailto: prefix from an attendee id
+     *
+     * @param  {string}       the string to remove the prefix from if any
+     * @return {string}       the string without prefix
+     */
+    removeMailTo: function(aId) {
+        return aId.replace(/^mailto:/i, "");
+    },
+
+    /**
      * Shortcut function to get the invited attendee of an item.
      */
     getInvitedAttendee: function cal_getInvitedAttendee(aItem, aCalendar) {
         if (!aCalendar) {
             aCalendar = aItem.calendar;
         }
         let invitedAttendee = null;
         let calendar = cal.wrapInstance(aCalendar, Components.interfaces.calISchedulingSupport);