Bug 1367933 - properly read attributes from offscreen listitems of junk whitelisted address books in Junk Settings. r=jorgk
authoraceman <acelists@atlas.sk>
Sun, 28 May 2017 07:09:00 +0200
changeset 28231 b8d187a8af7e03716ec91c4bff5b2f73905b4051
parent 28230 d5dd2b8171f8b8d66c1cb63a3010ccae0f87da44
child 28232 4aeb9d455545b3654becf90c24ba45dfe1ca37c6
push id1966
push userclokep@gmail.com
push dateMon, 12 Jun 2017 16:57:35 +0000
treeherdercomm-beta@32d9b8d10da1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorgk
bugs1367933
Bug 1367933 - properly read attributes from offscreen listitems of junk whitelisted address books in Junk Settings. r=jorgk
mailnews/base/prefs/content/am-junk.js
--- a/mailnews/base/prefs/content/am-junk.js
+++ b/mailnews/base/prefs/content/am-junk.js
@@ -52,54 +52,48 @@ function onInit(aPageId, aServerId)
   document.getElementById("actionFolderPopup").selectFolder(folder);
 
   var currentArray = [];
   if (document.getElementById("server.useWhiteList").checked)
     currentArray = document.getElementById("server.whiteListAbURI").value.split(" ");
 
   // set up the whitelist UI
   var wList = document.getElementById("whiteListAbURI");
-
   // Ensure the whitelist is empty
-  while (wList.hasChildNodes()) {
-    wList.lastChild.remove();
+  for (let i = wList.itemCount - 1; i >= 0; i--) {
+    wList.removeItemAt(i);
   }
 
   // Populate the listbox with address books
   let abItems = [];
   for (let ab in fixIterator(MailServices.ab.directories,
                              Components.interfaces.nsIAbDirectory)) {
     // We skip mailing lists and remote address books.
     if (ab.isMailList || ab.isRemote)
       continue;
 
-    let abItem = document.createElement("listitem");
-    abItem.setAttribute("type", "checkbox");
-    abItem.setAttribute("class", "listitem-iconic");
-    abItem.setAttribute("label", ab.dirName);
-    abItem.setAttribute("value", ab.URI);
-
-    // Due to bug 448582, we have to use setAttribute to set the
-    // checked value of the listitem.
-    abItem.setAttribute("checked", (currentArray.indexOf(ab.URI) != -1));
-
-    abItems.push(abItem);
+    abItems.push({ label: ab.dirName, URI: ab.URI });
   }
 
   // Sort the list
   function sortFunc(a, b) {
-    return a.getAttribute("label").toLowerCase()
-           > b.getAttribute("label").toLowerCase();
+    return a.label.localeCompare(b.label);
   }
-
   abItems.sort(sortFunc);
 
   // And then append each item to the listbox
-  for (let i = 0; i < abItems.length; i++)
-    wList.appendChild(abItems[i]);
+  for (let abItem of abItems) {
+    let item = wList.appendItem(abItem.label, abItem.URI);
+    item.setAttribute("type", "checkbox");
+    item.setAttribute("class", "listitem-iconic");
+
+    // Due to bug 448582, we have to use setAttribute to set the
+    // checked value of the listitem.
+    item.setAttribute("checked", currentArray.includes(abItem.URI));
+  }
 
   // enable or disable the whitelist
   onAdaptiveJunkToggle();
 
   // set up trusted IP headers
   var serverFilterList = document.getElementById("useServerFilterList");
   serverFilterList.value =
     document.getElementById("server.serverFilterName").value;
@@ -153,18 +147,19 @@ function onAdaptiveJunkToggle()
   onCheckItem("whiteListAbURI", ["server.spamLevel.visible"]);
   onCheckItem("whiteListLabel", ["server.spamLevel.visible"]);
 
   // Enable/disable individual listbox rows.
   // Setting enable/disable on the parent listbox does not seem to work.
   let wList = document.getElementById("whiteListAbURI");
   let wListDisabled = wList.disabled;
 
-  for (let i = 0; i < wList.getRowCount(); i++)
-    wList.getItemAtIndex(i).disabled = wListDisabled;
+  for (let i = 0; i < wList.getRowCount(); i++) {
+    wList.getItemAtIndex(i).setAttribute("disabled", wListDisabled);
+  }
 }
 
 /**
  * Called when someone checks or unchecks the "move new junk messages to"
  * Enable/disable the radio group accordingly.
  */
 function updateJunkTargetsAndRetention() {
   onCheckItem("server.moveTargetMode", ["server.moveOnSpam"]);
@@ -202,18 +197,21 @@ function onSave()
  */
 function onSaveWhiteList()
 {
   var wList = document.getElementById("whiteListAbURI");
   var wlArray = [];
 
   for (var i = 0; i < wList.getRowCount(); i++)
   {
+    // Due to bug 448582, do not trust any properties of the listitems
+    // as they may not return the right value or may even not exist.
+    // Always get the attributes only.
     var wlNode = wList.getItemAtIndex(i);
-    if (wlNode.checked) {
+    if (wlNode.getAttribute("checked") == "true") {
       let abURI = wlNode.getAttribute("value");
       wlArray.push(abURI);
     }
   }
   var wlValue = wlArray.join(" ");
   document.getElementById("server.whiteListAbURI").setAttribute("value", wlValue);
   document.getElementById("server.useWhiteList").checked = (wlValue != "");
 }
@@ -232,20 +230,19 @@ function onActionTargetChange(aEvent, aW
 /**
  * Enumerates over the "ISPDL" directories, calling buildServerFilterListFromDir
  * for each one.
  */
 function buildServerFilterMenuList()
 {
   const KEY_ISP_DIRECTORY_LIST = "ISPDL";
   let ispHeaderList = document.getElementById("useServerFilterList");
+
   // Ensure the menulist is empty.
-  while (ispHeaderList.hasChildNodes()) {
-    ispHeaderList.lastChild.remove();
-  }
+  ispHeaderList.removeAllItems();
 
   // Now walk through the isp directories looking for sfd files.
   let ispDirectories = Services.dirsvc.get(KEY_ISP_DIRECTORY_LIST,
                                            Components.interfaces.nsISimpleEnumerator);
 
   let menuEntries = [];
   while (ispDirectories.hasMoreElements())
   {