From faed7cd7dde68c034792f27bfa708516b3d60f2a Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 17 Mar 2015 15:14:17 -0400 Subject: [PATCH] Replace some 'for each..in' instances There are hundreds more, but these are all the ones that generate warnings in the console at startup. XPCOM/XBL ones don't seem to do so, so we can ignore those for now (and hopefully not bother with them on 4.0). Instances in translators do generate warnings. Addresses #656 --- chrome/content/zotero/browser.js | 13 ++++--- chrome/content/zotero/locateMenu.js | 47 +++++++++++++----------- chrome/content/zotero/zoteroPane.js | 56 ++++++++++++++++------------- resource/tinymce/note.html | 5 ++- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/chrome/content/zotero/browser.js b/chrome/content/zotero/browser.js index a2ab63738..59e3859ea 100644 --- a/chrome/content/zotero/browser.js +++ b/chrome/content/zotero/browser.js @@ -296,7 +296,8 @@ var Zotero_Browser = new function() { // ignore blacklisted domains try { if(doc.domain) { - for each(var blacklistedURL in _blacklist) { + for (let i = 0; i < _blacklist.length; i++) { + let blacklistedURL = _blacklist[i]; if(doc.domain.substr(doc.domain.length-blacklistedURL.length) == blacklistedURL) { Zotero.debug("Ignoring blacklisted URL "+doc.location); return; @@ -531,8 +532,8 @@ var Zotero_Browser = new function() { menuitem.setAttribute("class", "menuitem-iconic"); menuitem.addEventListener("command", _constructLookupFunction(tab, function(event, obj) { var urls = []; - for each(var item in obj.newItems) { - var url = Zotero.OpenURL.resolve(item); + for (let i = 0; i < obj.newItems.length; i++) { + var url = Zotero.OpenURL.resolve(obj.newItems[i]); if(url) urls.push(url); } ZoteroPane.loadURI(urls, event); @@ -837,7 +838,8 @@ Zotero_Browser.Tab.prototype.detectTranslators = function(rootDoc, doc) { Zotero_Browser.Tab.prototype._searchFrames = function(rootDoc, searchDoc) { if(rootDoc == searchDoc) return true; var frames = rootDoc.getElementsByTagName("frame"); - for each(var frame in frames) { + for (let i = 0; i < frames.length; i++) { + let frame = frames[i]; if(frame.contentDocument && (frame.contentDocument == searchDoc || this._searchFrames(frame.contentDocument, searchDoc))) { @@ -846,7 +848,8 @@ Zotero_Browser.Tab.prototype._searchFrames = function(rootDoc, searchDoc) { } var frames = rootDoc.getElementsByTagName("iframe"); - for each(var frame in frames) { + for (let i = 0; i < frames.length; i++) { + let frame = frames[i]; if(frame.contentDocument && (frame.contentDocument == searchDoc || this._searchFrames(frame.contentDocument, searchDoc))) { diff --git a/chrome/content/zotero/locateMenu.js b/chrome/content/zotero/locateMenu.js index 183fd8c3b..991ec957f 100644 --- a/chrome/content/zotero/locateMenu.js +++ b/chrome/content/zotero/locateMenu.js @@ -72,7 +72,7 @@ var Zotero_LocateMenu = new function() { } if(installableLocateEngines.length) { - for each(var locateEngine in installableLocateEngines) { + for (let locateEngine of installableLocateEngines) { var menuitem = document.createElement("menuitem"); menuitem.setAttribute("label", locateEngine.label); menuitem.setAttribute("class", "menuitem-iconic"); @@ -147,7 +147,7 @@ var Zotero_LocateMenu = new function() { var optionsToShow = {}; // check which view options are available - for each(var item in selectedItems) { + for (let item of selectedItems) { for(var viewOption in ViewOptions) { if(!optionsToShow[viewOption]) { optionsToShow[viewOption] = ViewOptions[viewOption].canHandleItem(item); @@ -172,7 +172,7 @@ var Zotero_LocateMenu = new function() { } if(addExtraOptions) { - for each(var viewOption in [key for(key in optionsToShow) if(key[0] === "_")]) { + for (let viewOption in optionsToShow) { if(viewOption[0] !== "_" || !optionsToShow[viewOption]) continue; locateMenu.insertBefore(_addViewOption(selectedItems, viewOption.substr(1), ViewOptions[viewOption], showIcons), lastNode); @@ -192,9 +192,9 @@ var Zotero_LocateMenu = new function() { var availableEngines = []; // check which engines can translate an item - for each(var engine in customEngines) { + for (let engine of customEngines) { // require a submission for at least one selected item - for each(var item in selectedItems) { + for (let item of selectedItems) { if(engine.getItemSubmission(item)) { availableEngines.push(engine); break; @@ -217,7 +217,7 @@ var Zotero_LocateMenu = new function() { locateFn = this.locateItem; } - for each(var engine in engines) { + for (let engine of engines) { var menuitem = _createMenuItem(engine.name, null, engine.description); menuitem.setAttribute("class", "menuitem-iconic"); menuitem.setAttribute("image", engine.icon); @@ -246,7 +246,7 @@ var Zotero_LocateMenu = new function() { if(!window.Zotero_Browser || !window.Zotero_Browser.tabbrowser) return locateEngines; var links = Zotero_Browser.tabbrowser.selectedBrowser.contentDocument.getElementsByTagName("link"); - for each(var link in links) { + for (let link of links) { if(!link.getAttribute) continue; var rel = link.getAttribute("rel"); if(rel && rel === "search") { @@ -287,7 +287,7 @@ var Zotero_LocateMenu = new function() { var urls = []; var postDatas = []; - for each(var item in selectedItems) { + for (let item of selectedItems) { var submission = selectedEngine.getItemSubmission(item); if(submission) { urls.push(submission.uri.spec); @@ -347,7 +347,7 @@ var Zotero_LocateMenu = new function() { this.handleItems = function(items, event) { var attachments = []; - for each(var item in items) { + for (let item of items) { var attachment = _getFirstAttachmentWithMIMEType(item, this._mimeTypes); if(attachment) attachments.push(attachment.id); } @@ -357,9 +357,12 @@ var Zotero_LocateMenu = new function() { function _getFirstAttachmentWithMIMEType(item, mimeTypes) { var attachments = (item.isAttachment() ? [item] : Zotero.Items.get(item.getBestAttachments())); - for each(var attachment in attachments) { - if(mimeTypes.indexOf(attachment.attachmentMIMEType) !== -1 - && attachment.attachmentLinkMode !== Zotero.Attachments.LINK_MODE_LINKED_URL) return attachment; + for (let i = 0; i < attachments.length; i++) { + let attachment = attachments[i]; + if (mimeTypes.indexOf(attachment.attachmentMIMEType) !== -1 + && attachment.attachmentLinkMode !== Zotero.Attachments.LINK_MODE_LINKED_URL) { + return attachment; + } } return false; } @@ -375,8 +378,8 @@ var Zotero_LocateMenu = new function() { this.canHandleItem = function(item) _getURL(item) !== false; this.handleItems = function(items, event) { - var urls = [_getURL(item) for each(item in items)]; - ZoteroPane_Local.loadURI([url for each(url in urls) if(url)], event); + var urls = [for (item of items) _getURL(item)]; + ZoteroPane_Local.loadURI([for (url of urls) if (url) url], event); } function _getURL(item) { @@ -394,7 +397,7 @@ var Zotero_LocateMenu = new function() { var attachments = item.getAttachments(); if(attachments) { // look through url fields for non-file:/// attachments - for each(var attachment in Zotero.Items.get(attachments)) { + for (let attachment of Zotero.Items.get(attachments)) { var urlField = attachment.getField('url'); if(urlField) return urlField; } @@ -440,7 +443,7 @@ var Zotero_LocateMenu = new function() { this.handleItems = function(items, event) { var attachments = []; - for each(var item in items) { + for (let item of items) { var attachment = _getFile(item); if(attachment) attachments.push(attachment.id); } @@ -450,7 +453,8 @@ var Zotero_LocateMenu = new function() { function _getFile(item) { var attachments = (item.isAttachment() ? [item] : Zotero.Items.get(item.getBestAttachments())); - for each(var attachment in attachments) { + for (let i = 0; i < attachments.length; i++) { + let attachment = attachments[i]; if(!ViewOptions.snapshot.canHandleItem(attachment) && !ViewOptions.pdf.canHandleItem(attachment) && attachment.attachmentLinkMode !== Zotero.Attachments.LINK_MODE_LINKED_URL) { @@ -478,7 +482,7 @@ var Zotero_LocateMenu = new function() { this.handleItems = function(items, event) { var attachments = []; - for each(var item in items) { + for (let item of items) { var attachment = _getBestNonNativeAttachment(item); if(attachment) attachments.push(attachment.id); } @@ -488,7 +492,8 @@ var Zotero_LocateMenu = new function() { function _getBestNonNativeAttachment(item) { var attachments = (item.isAttachment() ? [item] : Zotero.Items.get(item.getBestAttachments())); - for each(var attachment in attachments) { + for (let i = 0; i < attachments.length; i++) { + let attachment = attachments[i]; if(attachment.attachmentLinkMode !== Zotero.Attachments.LINK_MODE_LINKED_URL) { var file = attachment.getFile(); if(file) { @@ -534,7 +539,7 @@ var Zotero_LocateMenu = new function() { } this.handleItems = function(items, event) { - for each(var item in items) { + for (let item of items) { var attachment = _getBestFile(item); if(attachment) { ZoteroPane_Local.showAttachmentInFilesystem(attachment.id); @@ -562,7 +567,7 @@ var Zotero_LocateMenu = new function() { this.canHandleItem = function(item) item.isRegularItem(); this.handleItems = function(items, event) { var urls = []; - for each(var item in items) { + for (let item of items) { if(!item.isRegularItem()) continue; var url = Zotero.OpenURL.resolve(item); if(url) urls.push(url); diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 59bc16851..3f1fd12ed 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -911,7 +911,8 @@ var ZoteroPane = new function() } var newids = []; - for each(var id in ids) { + for (let i = 0; i < ids.length; i++) { + let id = ids[i]; id = parseInt(id); if (isNaN(id)) { continue; @@ -1442,7 +1443,7 @@ var ZoteroPane = new function() var disabled = !this.canEdit() || !(items.length == 1 && items[0].isRegularItem()); if (disabled) { - for each(var node in popup.childNodes) { + for (let node of popup.childNodes) { node.disabled = true; } return; @@ -1844,7 +1845,8 @@ var ZoteroPane = new function() // automatically converting text/html to plaintext rather than using // text/unicode. (That may be fixable, however.) var canCopy = false; - for each(var item in items) { + for (let i = 0; i < items.length; i++) { + let item = items[i]; if (item.isRegularItem()) { canCopy = true; break; @@ -2096,9 +2098,8 @@ var ZoteroPane = new function() ]; var m = {}; - var i = 0; - for each(var option in options) { - m[option] = i++; + for (let i = 0; i < options.length; i++) { + m[options[i]] = i; } var menu = document.getElementById('zotero-collectionmenu'); @@ -2205,7 +2206,8 @@ var ZoteroPane = new function() } // Hide and enable all actions by default (so if they're shown they're enabled) - for each(var pos in m) { + for (let i in m) { + let pos = m[i]; menu.childNodes[pos].setAttribute('hidden', true); menu.childNodes[pos].setAttribute('disabled', false); } @@ -2246,9 +2248,8 @@ var ZoteroPane = new function() ]; var m = {}; - var i = 0; - for each(var option in options) { - m[option] = i++; + for (let i = 0; i < options.length; i++) { + m[options[i]] = i; } var menu = document.getElementById('zotero-itemmenu'); @@ -2286,7 +2287,8 @@ var ZoteroPane = new function() canIndex = false; } - for each(var item in items) { + for (let i = 0; i < items.length; i++) { + let item = items[i]; if (canMerge && !item.isRegularItem() || itemGroup.isDuplicates()) { canMerge = false; } @@ -2318,7 +2320,8 @@ var ZoteroPane = new function() } var canCreateParent = true; - for each(var item in items) { + for (let i = 0; i < items.length; i++) { + let item = items[i]; if (!item.isTopLevelItem() || !item.isAttachment()) { canCreateParent = false; break; @@ -2416,10 +2419,9 @@ var ZoteroPane = new function() // Block certain actions on files if no access if (item.isImportedAttachment() && !itemGroup.filesEditable) { - var d = [m.deleteFromLibrary, m.createParent, m.renameAttachments]; - for each(var val in d) { - disable.push(val); - } + [m.deleteFromLibrary, m.createParent, m.renameAttachments].forEach(function (x) { + disable.push(x); + }); } } } @@ -2471,7 +2473,8 @@ var ZoteroPane = new function() menu.childNodes[m.reindexItem].setAttribute('label', Zotero.getString('pane.items.menu.reindexItem' + multiple)); // Hide and enable all actions by default (so if they're shown they're enabled) - for each(var pos in m) { + for (let i in m) { + let pos = m[i]; menu.childNodes[pos].setAttribute('hidden', true); menu.childNodes[pos].setAttribute('disabled', false); } @@ -2770,7 +2773,8 @@ var ZoteroPane = new function() uris = [uris]; } - for each(var uri in uris) { + for (let i = 0; i < uris.length; i++) { + let uri = uris[i]; // Ignore javascript: and data: URIs if (uri.match(/^(javascript|data):/)) { return; @@ -2925,7 +2929,7 @@ var ZoteroPane = new function() var itemGroup = self.collectionsView._getItemAtRow(self.collectionsView.selection.currentIndex); disabled = !itemGroup.editable; } - for each(var menuitem in menu.firstChild.childNodes) { + for (let menuitem of menu.firstChild.childNodes) { menuitem.disabled = disabled; } } @@ -3442,7 +3446,8 @@ var ZoteroPane = new function() } } - for each(var item in items) { + for (let i = 0; i < items.length; i++) { + let item = items[i]; if (item.isRegularItem()) { // Prefer local file attachments var uri = Components.classes["@mozilla.org/network/standard-url;1"] @@ -3510,7 +3515,8 @@ var ZoteroPane = new function() } } - for each(var itemID in itemIDs) { + for (let i = 0; i < itemIDs.length; i++) { + let itemID = itemIDs[i]; var item = Zotero.Items.get(itemID); if (!item.isAttachment()) { throw ("Item " + itemID + " is not an attachment in ZoteroPane_Local.viewAttachment()"); @@ -4101,12 +4107,12 @@ var ZoteroPane = new function() this.serializePersist = function() { if(!_unserialized) return; var serializedValues = {}; - for each(var el in document.getElementsByAttribute("zotero-persist", "*")) { + for (let el of document.getElementsByAttribute("zotero-persist", "*")) { if(!el.getAttribute) continue; var id = el.getAttribute("id"); if(!id) continue; var elValues = {}; - for each(var attr in el.getAttribute("zotero-persist").split(/[\s,]+/)) { + for (let attr of el.getAttribute("zotero-persist").split(/[\s,]+/)) { var attrValue = el.getAttribute(attr); elValues[attr] = attrValue; } @@ -4200,10 +4206,10 @@ var ZoteroPane = new function() "observe":function(aSubject, aTopic, aData) { if(aTopic == "zotero-reloaded") { Zotero.debug("Reloading Zotero pane"); - for each(var func in _reloadFunctions) func(aData); + for (let func of _reloadFunctions) func(aData); } else if(aTopic == "zotero-before-reload") { Zotero.debug("Zotero pane caught before-reload event"); - for each(var func in _beforeReloadFunctions) func(aData); + for (let func of _beforeReloadFunctions) func(aData); } } }; diff --git a/resource/tinymce/note.html b/resource/tinymce/note.html index 0b10dc1a5..8eac5725a 100644 --- a/resource/tinymce/note.html +++ b/resource/tinymce/note.html @@ -39,13 +39,12 @@ zoteroInit(ed); }); - var commands = ["Cut", "Copy", "Paste"]; - for each(var command in commands) { + ["Cut", "Copy", "Paste"].forEach(function (command) { let cmd = command; ed.addCommand(command, function (ui, value) { zoteroExecCommand(ed.getDoc(), cmd, ui, value); }); - } + }); }, fix_list_elements : true,