From caa0212d0601aad729fd3547c4e244460f89eedf Mon Sep 17 00:00:00 2001 From: Panos Astithas Date: Thu, 15 Sep 2011 11:20:22 -0700 Subject: [PATCH] Bug 614586 - Implement string substitution in all console API methods; r=ddahl,Olli.Pettay --- dom/base/ConsoleAPI.js | 48 ++++++++++++++++++-- dom/tests/browser/browser_ConsoleAPITests.js | 17 ++++++- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/dom/base/ConsoleAPI.js b/dom/base/ConsoleAPI.js index 484e8f32e637..f0b80ac5d77a 100644 --- a/dom/base/ConsoleAPI.js +++ b/dom/base/ConsoleAPI.js @@ -1,3 +1,5 @@ +/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * @@ -71,19 +73,19 @@ ConsoleAPI.prototype = { let chromeObject = { // window.console API log: function CA_log() { - self.notifyObservers(outerID, innerID, "log", arguments); + self.notifyObservers(outerID, innerID, "log", self.processArguments(arguments)); }, info: function CA_info() { - self.notifyObservers(outerID, innerID, "info", arguments); + self.notifyObservers(outerID, innerID, "info", self.processArguments(arguments)); }, warn: function CA_warn() { - self.notifyObservers(outerID, innerID, "warn", arguments); + self.notifyObservers(outerID, innerID, "warn", self.processArguments(arguments)); }, error: function CA_error() { - self.notifyObservers(outerID, innerID, "error", arguments); + self.notifyObservers(outerID, innerID, "error", self.processArguments(arguments)); }, debug: function CA_debug() { - self.notifyObservers(outerID, innerID, "log", arguments); + self.notifyObservers(outerID, innerID, "log", self.processArguments(arguments)); }, trace: function CA_trace() { self.notifyObservers(outerID, innerID, "trace", self.getStackTrace()); @@ -168,6 +170,42 @@ ConsoleAPI.prototype = { "console-api-log-event", aOuterWindowID); }, + /** + * Process the console API call arguments in order to perform printf-like + * string substitution. + * TODO: object substitution should display an interactive property list (bug + * 685815) and width and precision qualifiers should be taken into account + * (bug 685813). + * + * @param mixed aArguments + * The arguments given to the console API call. + **/ + processArguments: function CA_processArguments(aArguments) { + if (aArguments.length < 2) { + return aArguments; + } + let args = Array.prototype.slice.call(aArguments); + let format = args.shift(); + // Format specification regular expression. + let pattern = /%(\d*).?(\d*)[a-zA-Z]/g; + let processed = format.replace(pattern, function CA_PA_substitute(spec) { + switch (spec[spec.length-1]) { + case "o": + case "s": + return args.shift().toString(); + case "d": + case "i": + return parseInt(args.shift()); + case "f": + return parseFloat(args.shift()); + default: + return spec; + }; + }); + args.unshift(processed); + return args; + }, + /** * Build the stacktrace array for the console.trace() call. * diff --git a/dom/tests/browser/browser_ConsoleAPITests.js b/dom/tests/browser/browser_ConsoleAPITests.js index 1d97a05e32da..9307e14c9855 100644 --- a/dom/tests/browser/browser_ConsoleAPITests.js +++ b/dom/tests/browser/browser_ConsoleAPITests.js @@ -161,8 +161,21 @@ function observeConsoleTest() { expect("info", "arg", "extra arg"); win.console.info("arg", "extra arg"); - expect("warn", "arg", "extra arg", 1); - win.console.warn("arg", "extra arg", 1); + // We don't currently support width and precision qualifiers, but we don't + // choke on them either. + expect("warn", "Lesson 1: PI is approximately equal to 3.14159"); + win.console.warn("Lesson %d: %s is approximately equal to %1.2f", + 1, + "PI", + 3.14159); + expect("log", "%d, %s, %l"); + win.console.log("%d, %s, %l"); + expect("log", "%a %b %c"); + win.console.log("%a %b %c"); + expect("log", "%a %b %c", "a", "b"); + win.console.log("%a %b %c", "a", "b"); + expect("log", "2, a, %l", 3); + win.console.log("%d, %s, %l", 2, "a", 3); expect("dir", win.toString()); win.console.dir(win);