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 807832 7d0ac4750e6efc35632c59a6311dfad55105215c
parent 807831 9ed698ccc2ddb529194a670457ffa4279df07855
child 807833 3c747d2bb4219892a91ea1fc7d116708e33c0a42
push id113224
push userbmo:gijskruitbosch+bugs@gmail.com
push dateFri, 15 Jun 2018 20:35:10 +0000
reviewersflorian
bugs1454378
milestone62.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
@@ -866,32 +866,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();
@@ -902,25 +900,23 @@ 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)
+    for (let item of items) {
+      if (item.localName != itemName)
         continue;
 
-      handler(blocklistElement, result);
+      handler(item, result);
     }
     return result;
   },
 
   _handleEmItemNode(blocklistElement, result) {
     if (!matchesOSABI(blocklistElement))
       return;
 
@@ -939,74 +935,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")
+        let prefElements = childElement.children;
+        for (let prefElement of prefElements) {
+          if (prefElement.localName !== "pref")
             continue;
           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));
@@ -1043,26 +1031,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);
@@ -1432,20 +1415,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
@@ -1541,20 +1522,18 @@ 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")
+      for (let versionRangeElement of targetAppElement.children) {
+        if (versionRangeElement.localName != "versionRange")
           continue;
         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));