Bug 1454378 - switch to for...of loops and .children instead of manual .item() calls, r?florian draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 13 Jun 2018 11:53:33 -0700
changeset 810882 c7e45544d379cafab230c67213ff6f0ce193996e
parent 810881 766c1fadd8500ea64de77990e4282b0cf4a2d5f3
child 810883 82026d8bc8cea378e2d5740f7699b581f93bebf7
push id114146
push userbmo:gijskruitbosch+bugs@gmail.com
push dateTue, 26 Jun 2018 17:15:11 +0000
reviewersflorian
bugs1454378
milestone63.0a1
Bug 1454378 - switch to for...of loops and .children instead of manual .item() calls, r?florian MozReview-Commit-ID: 5nPrMVTz09m
toolkit/mozapps/extensions/Blocklist.jsm
--- a/toolkit/mozapps/extensions/Blocklist.jsm
+++ b/toolkit/mozapps/extensions/Blocklist.jsm
@@ -865,32 +865,30 @@ var Blocklist = {
     });
   },
 
   _loadBlocklistFromXML(doc) {
     this._addonEntries = [];
     this._gfxEntries = [];
     this._pluginEntries = [];
     try {
-      var childNodes = doc.documentElement.childNodes;
-      for (let element of childNodes) {
-        if (element.nodeType != doc.ELEMENT_NODE)
-          continue;
+      var children = doc.documentElement.children;
+      for (let element of children) {
         switch (element.localName) {
         case "emItems":
-          this._addonEntries = this._processItemNodes(element.childNodes, "emItem",
+          this._addonEntries = this._processItemNodes(element.children, "emItem",
                                                       this._handleEmItemNode);
           break;
         case "pluginItems":
-          this._pluginEntries = this._processItemNodes(element.childNodes, "pluginItem",
+          this._pluginEntries = this._processItemNodes(element.children, "pluginItem",
                                                        this._handlePluginItemNode);
           break;
         case "gfxItems":
           // Parse as simple list of objects.
-          this._gfxEntries = this._processItemNodes(element.childNodes, "gfxBlacklistEntry",
+          this._gfxEntries = this._processItemNodes(element.children, "gfxBlacklistEntry",
                                                     this._handleGfxBlacklistNode);
           break;
         default:
           LOG("Blocklist::_loadBlocklistFromXML: ignored entries " + element.localName);
         }
       }
       if (this._gfxEntries.length > 0) {
         this._notifyObserversBlocklistGFX();
@@ -901,25 +899,22 @@ var Blocklist = {
     // Dispatch to mainthread because consumers may try to construct nsIPluginHost
     // again based on this notification, while we were called from nsIPluginHost
     // anyway, leading to re-entrancy.
     Services.tm.dispatchToMainThread(function() {
       Services.obs.notifyObservers(null, "blocklist-loaded");
     });
   },
 
-  _processItemNodes(itemNodes, itemName, handler) {
+  _processItemNodes(items, itemName, handler) {
     var result = [];
-    for (var i = 0; i < itemNodes.length; ++i) {
-      var blocklistElement = itemNodes.item(i);
-      if (blocklistElement.nodeType != blocklistElement.ELEMENT_NODE ||
-          blocklistElement.localName != itemName)
-        continue;
-
-      handler(blocklistElement, result);
+    for (let item of items) {
+      if (item.localName == itemName) {
+        handler(item, result);
+      }
     }
     return result;
   },
 
   _handleEmItemNode(blocklistElement, result) {
     if (!matchesOSABI(blocklistElement))
       return;
 
@@ -938,74 +933,66 @@ var Blocklist = {
     }
 
     for (let filter of EXTENSION_BLOCK_FILTERS) {
       let attr = blocklistElement.getAttribute(filter);
       if (attr)
         blockEntry.attributes.set(filter, regExpCheck(attr));
     }
 
-    var childNodes = blocklistElement.childNodes;
+    var children = blocklistElement.children;
 
-    for (let x = 0; x < childNodes.length; x++) {
-      var childElement = childNodes.item(x);
-      if (childElement.nodeType != childElement.ELEMENT_NODE)
-        continue;
+    for (let childElement of children) {
       if (childElement.localName === "prefs") {
-        let prefElements = childElement.childNodes;
-        for (let i = 0; i < prefElements.length; i++) {
-          let prefElement = prefElements.item(i);
-          if (prefElement.nodeType != prefElement.ELEMENT_NODE ||
-              prefElement.localName !== "pref")
-            continue;
-          blockEntry.prefs.push(prefElement.textContent);
+        let prefElements = childElement.children;
+        for (let prefElement of prefElements) {
+          if (prefElement.localName == "pref") {
+            blockEntry.prefs.push(prefElement.textContent);
+          }
         }
-      } else if (childElement.localName === "versionRange")
+      } else if (childElement.localName === "versionRange") {
         blockEntry.versions.push(new BlocklistItemData(childElement));
+      }
     }
     // if only the extension ID is specified block all versions of the
     // extension for the current application.
     if (blockEntry.versions.length == 0)
       blockEntry.versions.push(new BlocklistItemData(null));
 
     blockEntry.blockID = blocklistElement.getAttribute("blockID");
 
     result.push(blockEntry);
   },
 
   _handlePluginItemNode(blocklistElement, result) {
     if (!matchesOSABI(blocklistElement))
       return;
 
-    var matchNodes = blocklistElement.childNodes;
+    let children = blocklistElement.children;
     var blockEntry = {
       matches: {},
       versions: [],
       blockID: null,
       infoURL: null,
     };
     var hasMatch = false;
-    for (var x = 0; x < matchNodes.length; ++x) {
-      var matchElement = matchNodes.item(x);
-      if (matchElement.nodeType != matchElement.ELEMENT_NODE)
-        continue;
-      if (matchElement.localName == "match") {
-        var name = matchElement.getAttribute("name");
-        var exp = matchElement.getAttribute("exp");
+    for (let childElement of children) {
+      if (childElement.localName == "match") {
+        var name = childElement.getAttribute("name");
+        var exp = childElement.getAttribute("exp");
         try {
           blockEntry.matches[name] = new RegExp(exp, "m");
           hasMatch = true;
         } catch (e) {
           // Ignore invalid regular expressions
         }
-      }
-      if (matchElement.localName == "versionRange") {
-        blockEntry.versions.push(new BlocklistItemData(matchElement));
-      } else if (matchElement.localName == "infoURL") {
-        blockEntry.infoURL = matchElement.textContent;
+      } else if (childElement.localName == "versionRange") {
+        blockEntry.versions.push(new BlocklistItemData(childElement));
+      } else if (childElement.localName == "infoURL") {
+        blockEntry.infoURL = childElement.textContent;
       }
     }
     // Plugin entries require *something* to match to an actual plugin
     if (!hasMatch)
       return;
     // Add a default versionRange if there wasn't one specified
     if (blockEntry.versions.length == 0)
       blockEntry.versions.push(new BlocklistItemData(null));
@@ -1042,26 +1029,21 @@ var Blocklist = {
     // But it is sometimes missing in test fixtures.
     if (blocklistElement.hasAttribute("blockID")) {
       blockEntry.blockID = blocklistElement.getAttribute("blockID");
     }
 
     // Trim helper (spaces, tabs, no-break spaces..)
     const trim = (s) => (s || "").replace(/(^[\s\uFEFF\xA0]+)|([\s\uFEFF\xA0]+$)/g, "");
 
-    for (let i = 0; i < blocklistElement.childNodes.length; ++i) {
-      var matchElement = blocklistElement.childNodes.item(i);
-      if (matchElement.nodeType != matchElement.ELEMENT_NODE)
-        continue;
-
+    for (let matchElement of blocklistElement.children) {
       let value;
       if (matchElement.localName == "devices") {
         value = [];
-        for (let j = 0; j < matchElement.childNodes.length; j++) {
-          const childElement = matchElement.childNodes.item(j);
+        for (let childElement of matchElement.children) {
           const childValue = trim(childElement.textContent);
           // Make sure no empty value is added.
           if (childValue) {
             if (/,/.test(childValue)) {
               // Devices can't contain comma.
               // (c.f serialization in _notifyObserversBlocklistGFX)
               const e = new Error(`Unsupported device name ${childValue}`);
               Cu.reportError(e);
@@ -1431,20 +1413,18 @@ function BlocklistItemData(versionRangeE
     this.vulnerabilityStatus = versionRangeElement.getAttribute("vulnerabilitystatus");
   } else {
     this.vulnerabilityStatus = VULNERABILITYSTATUS_NONE;
   }
   this.targetApps = { };
   var found = false;
 
   if (versionRangeElement) {
-    for (var i = 0; i < versionRangeElement.childNodes.length; ++i) {
-      var targetAppElement = versionRangeElement.childNodes.item(i);
-      if (targetAppElement.nodeType != targetAppElement.ELEMENT_NODE ||
-          targetAppElement.localName != "targetApplication")
+    for (let targetAppElement of versionRangeElement.children) {
+      if (targetAppElement.localName != "targetApplication")
         continue;
       found = true;
       // default to the current application if id is not provided.
       var appID = targetAppElement.hasAttribute("id") ? targetAppElement.getAttribute("id") : gApp.ID;
       this.targetApps[appID] = this.getBlocklistAppVersions(targetAppElement);
     }
   }
   // Default to all versions of the current application when no targetApplication
@@ -1540,22 +1520,20 @@ BlocklistItemData.prototype = {
    *        An array of JS objects with the following properties:
    *          "minVersion"  The minimum version in a version range (default = null).
    *          "maxVersion"  The maximum version in a version range (default = null).
    */
   getBlocklistAppVersions(targetAppElement) {
     var appVersions = [ ];
 
     if (targetAppElement) {
-      for (var i = 0; i < targetAppElement.childNodes.length; ++i) {
-        var versionRangeElement = targetAppElement.childNodes.item(i);
-        if (versionRangeElement.nodeType != versionRangeElement.ELEMENT_NODE ||
-            versionRangeElement.localName != "versionRange")
-          continue;
-        appVersions.push(this.getBlocklistVersionRange(versionRangeElement));
+      for (let versionRangeElement of targetAppElement.children) {
+        if (versionRangeElement.localName == "versionRange") {
+          appVersions.push(this.getBlocklistVersionRange(versionRangeElement));
+        }
       }
     }
     // return minVersion = null and maxVersion = null if no specific versionRange
     // elements were found
     if (appVersions.length == 0)
       appVersions.push(this.getBlocklistVersionRange(null));
     return appVersions;
   },