From 66785a8b5f3e46212d803217dd23c7ece749fa80 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Wed, 2 Mar 2011 14:43:57 -0500 Subject: [PATCH] Allow \c/\C in regexp options and so forth. --- common/content/dactyl.js | 2 +- common/content/events.js | 6 +++--- common/content/hints.js | 4 ++-- common/modules/commands.jsm | 6 +----- common/modules/completion.jsm | 4 +++- common/modules/highlight.jsm | 6 ++++-- common/modules/options.jsm | 2 +- common/modules/styles.jsm | 2 +- common/modules/util.jsm | 38 ++++++++++++++++++++++++++--------- 9 files changed, 45 insertions(+), 25 deletions(-) diff --git a/common/content/dactyl.js b/common/content/dactyl.js index cfa484f8..45302e7a 100644 --- a/common/content/dactyl.js +++ b/common/content/dactyl.js @@ -1276,7 +1276,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), { let urls; if (options["urlseparator"]) - urls = util.splitLiteral(str, RegExp("\\s*" + options["urlseparator"] + "\\s*")); + urls = util.splitLiteral(str, util.regexp("\\s*" + options["urlseparator"] + "\\s*")); else urls = [str]; diff --git a/common/content/events.js b/common/content/events.js index ef7a742a..86c908cf 100644 --- a/common/content/events.js +++ b/common/content/events.js @@ -300,6 +300,8 @@ var EventHive = Class("EventHive", Contexts.Hive, { * @param {function} callback The function to call when the event is received. * @param {boolean} capture When true, listen during the capture * phase, otherwise during the bubbling phase. + * @param {boolean} allowUntrusted When true, allow capturing of + * untrusted events. */ listen: function (target, event, callback, capture, allowUntrusted) { if (!isObject(event)) @@ -821,9 +823,7 @@ var Events = Module("events", { unknownOk = true; let out = []; - let re = RegExp("<.*?>?>|[^<]|<(?!.*>)", "g"); - let match; - while ((match = re.exec(input))) { + for (let match in util.regexp.iterate(/<.*?>?>|[^<]|<(?!.*>)/g, input)) { let evt_str = match[0]; let evt_obj = { ctrlKey: false, shiftKey: false, altKey: false, metaKey: false, keyCode: 0, charCode: 0, type: "keypress" }; diff --git a/common/content/hints.js b/common/content/hints.js index 443f0f8e..5183e0a7 100644 --- a/common/content/hints.js +++ b/common/content/hints.js @@ -847,8 +847,8 @@ var Hints = Module("hints", { * hints that match as above. */ function wordStartsWithMatcher(hintString, allowWordOverleaping) { //{{{ - let hintStrings = tokenize(/\s+/, hintString); - let wordSplitRegexp = RegExp(options["wordseparators"]); + let hintStrings = tokenize(/\s+/, hintString); + let wordSplitRegexp = util.regexp(options["wordseparators"]); /** * Match a set of characters to the start of words. diff --git a/common/modules/commands.jsm b/common/modules/commands.jsm index 43a3310d..5e941ccc 100644 --- a/common/modules/commands.jsm +++ b/common/modules/commands.jsm @@ -15,10 +15,6 @@ defineModule("commands", { use: ["config", "messages", "options", "services", "template"] }, this); -let base = util.regexp.escape(Components.stack.filename.replace(/[^\/]+$/, "")); -let re = RegExp("^(resource://dactyl|" + base + ")\\S+( -> resource://dactyl(?!-content/eval.js)\\S+)?$"); -let isDactyl = function isDactyl(frame) re.test(frame.filename); - /** * A structure representing the options available for a command. * @@ -692,7 +688,7 @@ var Commands = Module("commands", { repeat: null, add: function add() { - util.assert(isDactyl(Components.stack.caller), + util.assert(util.isDactyl(Components.stack.caller), "User scripts may not add builtin commands. Please use group.commands.add instead."); return this.builtin._add.apply(this.builtin, arguments); }, diff --git a/common/modules/completion.jsm b/common/modules/completion.jsm index abcebb30..1cc24f6f 100644 --- a/common/modules/completion.jsm +++ b/common/modules/completion.jsm @@ -903,7 +903,9 @@ var Completion = Module("completion", { url: function url(context, complete) { if (this.options["urlseparator"]) - var skip = RegExp("^.*" + this.options["urlseparator"] + "\\s*").exec(context.filter); + var skip = util.regexp("^.*" + this.options["urlseparator"] + "\\s*") + .exec(context.filter); + if (skip) context.advance(skip[0].length); diff --git a/common/modules/highlight.jsm b/common/modules/highlight.jsm index 48ba697a..35288942 100644 --- a/common/modules/highlight.jsm +++ b/common/modules/highlight.jsm @@ -406,12 +406,14 @@ var Highlights = Module("Highlight", { completion: function (dactyl, modules) { const { completion, config, io } = modules; completion.colorScheme = function colorScheme(context) { + let extRe = RegExp("\\." + config.fileExtension + "$"); + context.title = ["Color Scheme", "Runtime Path"]; - context.keys = { text: function (f) f.leafName.replace(RegExp("\\." + config.fileExtension + "$"), ""), description: ".parent.path" }; + context.keys = { text: function (f) f.leafName.replace(extRe, ""), description: ".parent.path" }; context.completions = array.flatten( io.getRuntimeDirectories("colors").map( function (dir) dir.readDirectory().filter( - function (file) RegExp("\\." + config.fileExtension + "$").test(file.leafName)))); + function (file) extRe.test(file.leafName)))); }; diff --git a/common/modules/options.jsm b/common/modules/options.jsm index 06776b71..05c8cfdd 100644 --- a/common/modules/options.jsm +++ b/common/modules/options.jsm @@ -425,7 +425,7 @@ var Option = Class("Option", { flags = this && this.regexpFlags || ""; let [, bang, val] = /^(!?)(.*)/.exec(value); - let re = RegExp(Option.dequote(val), flags); + let re = util.regexp(Option.dequote(val), flags); re.bang = bang; re.result = result !== undefined ? result : !bang; re.toString = function () Option.unparseRegexp(this, keepQuotes); diff --git a/common/modules/styles.jsm b/common/modules/styles.jsm index 183fc1f2..2a716025 100644 --- a/common/modules/styles.jsm +++ b/common/modules/styles.jsm @@ -413,7 +413,7 @@ var Styles = Module("Styles", { if (filter === "*") var test = function test(uri) true; else if (!/^(?:[a-z-]+:|[a-z-.]+$)/.test(filter)) { - let re = RegExp(filter); + let re = util.regexp(filter); test = function test(uri) re.test(uri.spec); } else if (/[*]$/.test(filter)) { diff --git a/common/modules/util.jsm b/common/modules/util.jsm index 753e8ef0..bd30a87a 100644 --- a/common/modules/util.jsm +++ b/common/modules/util.jsm @@ -177,20 +177,18 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), * @returns {RegExp} */ charListToRegexp: function charListToRegexp(list, accepted) { - let list = list.replace(/\s+/g, ""); + list = list.replace(/\s+/g, ""); // check for chars not in the accepted range this.assert(RegExp("^[" + accepted + "-]+$").test(list), - "Character list outside the range " + accepted.quote()); + "Character list outside the range " + accepted.quote()); // check for illegal ranges - let matches = list.match(RegExp("[" + accepted + "]-[" + accepted + "]", "g")); - if (matches) { - for (let match in values(matches)) - this.assert(match[0] <= match[2], - "Invalid character range: " + list.match(match + ".*")[0]); - } - return RegExp("[" + list + "]"); + for (let [match] in this.regexp.iterate(/.-./g, list)) + this.assert(match.charCodeAt(0) <= match.charCodeAt(2), + "Invalid character range: " + list.slice(list.indexOf(match))) + + return RegExp("[" + util.regexp.escape(list) + "]"); }, get chromePackages() { @@ -822,6 +820,18 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), bottom: Math.min(r1.bottom, r2.bottom) }), + /** + * Returns true if the given stack frame resides in Dactyl code. + * + * @param {nsIStackFrame} frame + * @returns {boolean} + */ + isDactyl: Class.memoize(function () { + let base = util.regexp.escape(Components.stack.filename.replace(/[^\/]+$/, "")); + let re = RegExp("^(resource://dactyl|" + base + ")\\S+( -> resource://dactyl(?!-content/eval.js)\\S+)?$"); + return function isDactyl(frame) re.test(frame.filename); + }), + /** * Returns true if *url* is in the domain *domain*. * @@ -1340,6 +1350,16 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), if (isinstance(expr, ["RegExp"])) expr = expr.source; + expr = String.replace(expr, /\\(.)/, function (m, m1) { + if (m1 === "c") + flags = flags.replace(/i/g, "") + "i"; + else if (m === "C") + flags = flags.replace(/i/g, ""); + else + return m; + return ""; + }); + // Replace replacement . if (tokens) expr = String.replace(expr, /(\(?P)?<(\w+)>/g, function (m, n1, n2) !n1 && set.has(tokens, n2) ? tokens[n2].dactylSource || tokens[n2].source || tokens[n2] : m);