diff --git a/common/content/buffer.js b/common/content/buffer.js index fd8f11de..c65bf311 100644 --- a/common/content/buffer.js +++ b/common/content/buffer.js @@ -101,7 +101,7 @@ function Buffer() //{{{ if (win.scrollMaxX > 0 || win.scrollMaxY > 0) return win; - for (let frame in util.Array.iterator(win.frames)) + for (let frame in util.Array.itervalues(win.frames)) if (frame.scrollMaxX > 0 || frame.scrollMaxY > 0) return frame; @@ -174,17 +174,7 @@ function Buffer() //{{{ } catch (e) { liberator.reportError(e) } }, - completer: function(context) { - context.anchored = false; - context.generate = function() { - let names = util.Array.uniq( - util.Array.flatten( - 'more1 more2 more3 more4 more5 unicode'.split(' ').map(function(key) - options.getPref('intl.charsetmenu.browser.' + key).split(', ')))); - let bundle = document.getElementById('liberator-charset-bundle'); - return names.map(function(name) [name, bundle.getString(name.toLowerCase() + '.title')]) - }; - } + completer: function(context) completion.charset(context) }); // FIXME: Most certainly belongs elsewhere. @@ -803,7 +793,7 @@ function Buffer() //{{{ const ACCESS_READ = Ci.nsICache.ACCESS_READ; let cacheKey = doc.location.toString().replace(/#.*$/, ""); - for (let proto in util.Array.iterator(["HTTP", "FTP"])) + for (let proto in util.Array.itervalues(["HTTP", "FTP"])) { try { diff --git a/common/content/commands.js b/common/content/commands.js index 6cc8ab97..b6a2bdd7 100644 --- a/common/content/commands.js +++ b/common/content/commands.js @@ -324,7 +324,7 @@ function Commands() //{{{ __iterator__: function () { let sorted = exCommands.sort(function (a, b) a.name > b.name); - return util.Array.iterator(sorted); + return util.Array.itervalues(sorted); }, add: function (names, description, action, extra) @@ -480,7 +480,7 @@ function Commands() //{{{ argCount = "*"; var args = []; // parsed options - args.__iterator__ = function () util.Array.iterator2(this); + args.__iterator__ = function () util.Array.iteritems(this); args.string = str; // for access to the unparsed string args.literalArg = ""; @@ -919,12 +919,10 @@ function Commands() //{{{ { command: this.name, bang: true, - // Yeah, this is a bit scary. Perhaps I'll fix it when I'm - // awake. - options: util.Array.assocToObj( - util.map({ argCount: "-nargs", bang: "-bang", count: "-count" }, - function ([k, v]) k in cmd && cmd[k] != "0" && [v, typeof cmd[k] == "boolean" ? null : cmd[k]]) - .filter(util.identity)), + options: util.Array.toObject( + [[v, typeof cmd[k] == "boolean" ? null : cmd[k]] + for ([k, v] in Iterator({ argCount: "-nargs", bang: "-bang", count: "-count" })) + if (k in cmd && cmd[k] != "0")]), arguments: [cmd.name], literalArg: cmd.replacementText } diff --git a/common/content/completion.js b/common/content/completion.js index 34eed959..af9c57a0 100644 --- a/common/content/completion.js +++ b/common/content/completion.js @@ -291,7 +291,7 @@ CompletionContext.prototype = { set completions(items) { // Accept a generator - if (!("length" in items)) + if ({}.toString.call(items) != '[object Array]') items = [x for (x in Iterator(items))]; delete this.cache.filtered; delete this.cache.filter; @@ -1454,6 +1454,18 @@ function Completion() //{{{ }); }, + charset: function(context) { + context.anchored = false; + context.generate = function() { + let names = util.Array( + 'more1 more2 more3 more4 more5 unicode'.split(' ').map(function(key) + options.getPref('intl.charsetmenu.browser.' + key).split(', '))) + .flatten().uniq(); + let bundle = document.getElementById('liberator-charset-bundle'); + return names.map(function(name) [name, bundle.getString(name.toLowerCase() + '.title')]); + }; + }, + colorScheme: function colorScheme(context) { let colors = []; diff --git a/common/content/events.js b/common/content/events.js index ddf874bc..2ca809b9 100644 --- a/common/content/events.js +++ b/common/content/events.js @@ -180,7 +180,7 @@ function AutoCommands() //{{{ return { - __iterator__: function () util.Array.iterator(store), + __iterator__: function () util.Array.itervalues(store), /** * Adds a new autocommand. cmd will be executed when one of the diff --git a/common/content/modes.js b/common/content/modes.js index 25b65b6b..e4a5c0c3 100644 --- a/common/content/modes.js +++ b/common/content/modes.js @@ -129,7 +129,7 @@ const modes = (function () //{{{ NONE: 0, - __iterator__: function () util.Array.iterator(this.all), + __iterator__: function () util.Array.itervalues(this.all), get all() mainModes.slice(), diff --git a/common/content/style.js b/common/content/style.js index 78cf6770..69daac67 100644 --- a/common/content/style.js +++ b/common/content/style.js @@ -488,10 +488,10 @@ function Styles(name, store, serial) return namespace + "@-moz-document " + selectors + "{\n" + css + "\n}\n"; } } -let (array = util.Array) +let (array = utiltte.Array) { Styles.prototype = { - get sites() array.uniq(array.flatten([v.sites for ([k, v] in this.userSheets)])), + get sites() array([v.sites for ([k, v] in this.userSheets)]).flatten().uniq(), completeSite: function (context, content) { let compl = []; diff --git a/common/content/template.js b/common/content/template.js index 01a013c0..5529e696 100644 --- a/common/content/template.js +++ b/common/content/template.js @@ -15,7 +15,7 @@ const template = { map: function map(iter, fn, sep, interruptable) { if (iter.length) // FIXME: Kludge? - iter = util.Array.iterator(iter); + iter = util.Array.itervalues(iter); let ret = <>; let n = 0; for each (let i in Iterator(iter)) diff --git a/common/content/ui.js b/common/content/ui.js index 8aeaa838..53b41354 100644 --- a/common/content/ui.js +++ b/common/content/ui.js @@ -1867,7 +1867,7 @@ function ItemList(id) //{{{ for (let [i, row] in Iterator(context.getRows(start, end, doc))) nodes[i] = row; - for (let [i, row] in util.Array.iterator2(nodes)) + for (let [i, row] in util.Array.iteritems(nodes)) { if (!row) continue; @@ -2197,7 +2197,7 @@ function StatusLine() //{{{ // tab numbers set if (options.get("guioptions").has("n", "N")) { - for (let [i, tab] in util.Array.iterator2(getBrowser().mTabs)) + for (let [i, tab] in util.Array.iteritems(getBrowser().mTabs)) tab.setAttribute("ordinal", i + 1); } diff --git a/common/content/util.js b/common/content/util.js index 6515d930..d00cd280 100644 --- a/common/content/util.js +++ b/common/content/util.js @@ -33,96 +33,6 @@ const NS = Namespace("liberator", "http://vimperator.org/namespaces/liberator"); default xml namespace = XHTML; const util = { //{{{ - - /** - * Array utility methods. - * @singleton - */ - Array: { - /** - * Converts an array to an object. As in lisp, an assoc is an - * array of key-value pairs, which maps directly to an object, - * as such: - * [["a", "b"], ["c", "d"]] -> { a: "b", c: "d" } - * - * @param {Array[]} assoc - * @... {string} 0 - Key - * @... 1 - Value - */ - assocToObj: function assocToObj(assoc) - { - let obj = {}; - assoc.forEach(function ([k, v]) { obj[k] = v }); - return obj; - }, - - /** - * Flattens an array, such that all elements of the array are - * joined into a single array: - * [["foo", ["bar"]], ["baz"], "quux"] -> ["foo", ["bar"], "baz", "quux"] - * - * @param {Array} ary - * @returns {Array} - */ - flatten: function flatten(ary) Array.concat.apply([], ary), - - /** - * Returns an Iterator for an array's values. - * - * @param {Array} ary - * @returns {Iterator(Object)} - */ - iterator: function iterator(ary) - { - let length = ary.length; - for (let i = 0; i < length; i++) - yield ary[i]; - }, - - /** - * Returns an Iterator for an array's indices and values. - * - * @param {Array} ary - * @returns {Iterator([{number}, {Object}])} - */ - iterator2: function (ary) - { - let length = ary.length; - for (let i = 0; i < length; i++) - yield [i, ary[i]]; - }, - - /** - * Filters out all duplicates from an array. If - * unsorted is false, the array is sorted before - * duplicates are removed. - * - * @param {Array} ary - * @param {boolean} unsorted - * @returns {Array} - */ - uniq: function uniq(ary, unsorted) - { - let ret = []; - if (unsorted) - { - for (let [,item] in Iterator(ary)) - if (ret.indexOf(item) == -1) - ret.push(item); - } - else - { - for (let [,item] in Iterator(ary.sort())) - { - if (item != last || !ret.length) - ret.push(item); - var last = item; - } - } - return ret; - } - }, - /** * Returns a shallow copy of obj. * @@ -730,6 +640,106 @@ const util = { //{{{ } }; //}}} +/** + * Array utility methods. + * @singleton + */ +util.Array = function Array(ary) { + var obj = { + __proto__: ary, + __iterator__: function() this.iteritems(), + __noSuchMethod__: function (meth, args) { + let res = util.Array(util.Array[meth].apply(null, [this.__proto__].concat(args))) + if (res instanceof Array) + return util.Array(res); + return res; + } + }; + return obj; +} +/** + * Converts an array to an object. As in lisp, an assoc is an + * array of key-value pairs, which maps directly to an object, + * as such: + * [["a", "b"], ["c", "d"]] -> { a: "b", c: "d" } + * + * @param {Array[]} assoc + * @... {string} 0 - Key + * @... 1 - Value + */ +util.Array.toObject = function toObject(assoc) +{ + let obj = {}; + assoc.forEach(function ([k, v]) { obj[k] = v }); + return obj; +}; + +/** + * Flattens an array, such that all elements of the array are + * joined into a single array: + * [["foo", ["bar"]], ["baz"], "quux"] -> ["foo", ["bar"], "baz", "quux"] + * + * @param {Array} ary + * @returns {Array} + */ +util.Array.flatten = function flatten(ary) Array.concat.apply([], ary), + +/** + * Returns an Iterator for an array's values. + * + * @param {Array} ary + * @returns {Iterator(Object)} + */ +util.Array.itervalues = function itervalues(ary) +{ + let length = ary.length; + for (let i = 0; i < length; i++) + yield ary[i]; +}; + +/** + * Returns an Iterator for an array's indices and values. + * + * @param {Array} ary + * @returns {Iterator([{number}, {Object}])} + */ +util.Array.iteritems = function iteritems(ary) +{ + let length = ary.length; + for (let i = 0; i < length; i++) + yield [i, ary[i]]; +}; + +/** + * Filters out all duplicates from an array. If + * unsorted is false, the array is sorted before + * duplicates are removed. + * + * @param {Array} ary + * @param {boolean} unsorted + * @returns {Array} + */ +util.Array.uniq = function uniq(ary, unsorted) +{ + let ret = []; + if (unsorted) + { + for (let [,item] in Iterator(ary)) + if (ret.indexOf(item) == -1) + ret.push(item); + } + else + { + for (let [,item] in Iterator(ary.sort())) + { + if (item != last || !ret.length) + ret.push(item); + var last = item; + } + } + return ret; +}; + function Struct() { let self = this instanceof Struct ? this : new Struct();