From 85eede1ee2d6349ce2bd4adafe35eb8bdb27b1e1 Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Mon, 6 Jul 2015 23:55:51 +1000 Subject: [PATCH] Add util.iterFrames. --- common/modules/buffer.jsm | 12 ++++-------- common/modules/util.jsm | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/common/modules/buffer.jsm b/common/modules/buffer.jsm index a34dea9c..f450402f 100644 --- a/common/modules/buffer.jsm +++ b/common/modules/buffer.jsm @@ -363,17 +363,13 @@ var Buffer = Module("Buffer", { /** * Returns a list of all frames in the given window or current buffer. */ - allFrames: function allFrames(win, focusedFirst) { - let frames = []; - (function rec(frame) { - if (true || frame.document.body instanceof Ci.nsIDOMHTMLBodyElement) - frames.push(frame); - Array.forEach(frame.frames, rec); - })(win || this.win); + allFrames: function allFrames(win=this.win, focusedFirst) { + let frames = iter(util.iterFrames(win)).toArray(); if (focusedFirst) return frames.filter(f => f === this.focusedFrame).concat( frames.filter(f => f !== this.focusedFrame)); + return frames; }, @@ -2337,7 +2333,7 @@ var Buffer = Module("Buffer", { if (count >= 1 || !elem || !events.isContentNode(elem)) { let xpath = ["frame", "iframe", "input", "xul:textbox", "textarea[not(@disabled) and not(@readonly)]"]; - let frames = buffer.allFrames(null, true); + let frames = buffer.allFrames(undefined, true); let elements = Ary.flatten(frames.map(win => [m for (m of DOM.XPath(xpath, win.document))])) .filter(function (elem) { diff --git a/common/modules/util.jsm b/common/modules/util.jsm index 5c3475ee..dbf0925e 100644 --- a/common/modules/util.jsm +++ b/common/modules/util.jsm @@ -1704,6 +1704,19 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), } }, + /** + * Iterates, depth-first, over the given window and all of its descendant + * frames. + * + * @param {nsIDOMWindow} win The root window. + * @returns {Generator(nsIDOMWindow)} + */ + iterFrames: function* iterFrames(win) { + yield win; + for (let i = 0; i < win.frames.length; i++) + yield* iterFrames(win.frames[i]); + }, + /** * Returns a list of all domains and subdomains of documents in the * given window and all of its descendant frames. @@ -1712,16 +1725,14 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), * @returns {[string]} The visible domains. */ visibleHosts: function visibleHosts(win) { - let res = [], - seen = new RealSet; - (function rec(frame) { + let res = [], seen = new RealSet; + for (let frame of this.iterFrames(win)) { try { if (frame.location.hostname) res = res.concat(util.subdomains(frame.location.hostname)); } catch (e) {} - Array.forEach(frame.frames, rec); - })(win); + } return res.filter(h => !seen.add(h)); }, @@ -1733,15 +1744,13 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), * @returns {[nsIURI]} The visible URIs. */ visibleURIs: function visibleURIs(win) { - let res = [], - seen = new RealSet; - (function rec(frame) { + let res = [], seen = new RealSet; + for (let frame of this.iterFrames(win)) { try { - res = res.concat(util.newURI(frame.location.href)); + res.push(util.newURI(frame.location.href)); } catch (e) {} - Array.forEach(frame.frames, rec); - })(win); + } return res.filter(h => !seen.add(h.spec)); },