Bug 614586 - Implement string substitution in all console API methods; r=ddahl,Olli.Pettay

This commit is contained in:
Panos Astithas 2011-09-15 11:20:22 -07:00
Родитель c383a9623d
Коммит caa0212d06
2 изменённых файлов: 58 добавлений и 7 удалений

Просмотреть файл

@ -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.
*

Просмотреть файл

@ -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);