3 <!DOCTYPE bindings SYSTEM "chrome://global/locale/console.dtd">
5 <bindings id="consoleBindings"
6 xmlns="http://www.mozilla.org/xbl"
7 xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
8 xmlns:xbl="http://www.mozilla.org/xbl">
10 <binding id="console-box" extends="xul:box">
12 <xul:stringbundle src="chrome://global/locale/console.properties" role="string-bundle"/>
13 <xul:vbox class="console-box-internal">
14 <xul:vbox class="console-rows" role="console-rows" xbl:inherits="dir=sortOrder"/>
19 <field name="limit" readonly="true">
22 <field name="_showChromeErrors">-1</field>
24 <property name="showChromeErrors">
26 if (this._showChromeErrors != -1)
27 return this._showChromeErrors;
28 var pref = Components.classes['@mozilla.org/preferences-service;1'].getService();
29 pref = pref.QueryInterface(Components.interfaces.nsIPrefBranch);
32 return this._showChromeErrors = pref.getBoolPref("javascript.options.showInConsole");
35 return this._showChromeErrors = false;
40 <property name="count" readonly="true">
41 <getter>return this.mCount</getter>
44 <property name="mode">
45 <getter>return this.mMode;</getter>
47 if (this.mode != val) {
48 this.mMode = val || "All";
49 this.setAttribute("mode", this.mMode);
50 this.selectedItem = null;
56 <property name="sortOrder">
57 <getter>return this.getAttribute("sortOrder");</getter>
58 <setter>this.setAttribute("sortOrder", val); return val;</setter>
60 <field name="mSelectedItem">null</field>
61 <property name="selectedItem">
62 <getter>return this.mSelectedItem</getter>
64 if (this.mSelectedItem)
65 this.mSelectedItem.removeAttribute("selected");
67 this.mSelectedItem = val;
69 val.setAttribute("selected", "true");
71 // Update edit commands
72 window.updateCommands("focus");
81 this.mConsoleListener = {
83 observe : function(aObject) { this.console.appendItem(aObject); }
86 this.mConsoleRowBox = document.getAnonymousElementByAttribute(this, "role", "console-rows");
87 this.mStrBundle = document.getAnonymousElementByAttribute(this, "role", "string-bundle");
90 var isupports = Components.classes['@mozilla.org/consoleservice;1'].getService();
91 this.mCService = isupports.QueryInterface(Components.interfaces.nsIConsoleService);
92 this.mCService.registerListener(this.mConsoleListener);
95 "Unable to display errors - couldn't get Console Service component. " +
96 "(Missing @mozilla.org/consoleservice;1)");
100 this.mMode = this.getAttribute("mode") || "All";
102 this.appendInitialItems();
103 window.controllers.insertControllerAt(0, this._controller);
107 <method name="destroy">
109 this.mCService.unregisterListener(this.mConsoleListener);
110 window.controllers.removeController(this._controller);
114 <method name="appendInitialItems">
116 var out = {}; // Throwaway references to support 'out' parameters.
117 this.mCService.getMessageArray(out, {});
118 var messages = out.value;
120 // In case getMessageArray returns 0-length array as null
124 var limit = messages.length - this.limit;
125 if (limit < 0) limit = 0;
127 // Checks if console ever been cleared
128 for (var i = messages.length - 1; i >= limit; --i)
129 if (!messages[i].message)
132 // Populate with messages after latest "clear"
133 while (++i < messages.length)
134 this.appendItem(messages[i]);
138 <method name="appendItem">
139 <parameter name="aObject"/>
142 // Try to QI it to a script error to get more info
143 var scriptError = aObject.QueryInterface(Components.interfaces.nsIScriptError);
145 // filter chrome urls
146 if (!this.showChromeErrors && scriptError.sourceName.substr(0, 9) == "chrome://")
149 this.appendError(scriptError);
152 // Try to QI it to a console message
153 var msg = aObject.QueryInterface(Components.interfaces.nsIConsoleMessage);
155 this.appendMessage(msg.message);
156 else // observed a null/"clear" message
159 // Give up and append the object itself as a string
160 this.appendMessage(aObject);
166 <method name="appendError">
167 <parameter name="aObject"/>
169 var row = this.createConsoleRow();
170 var nsIScriptError = Components.interfaces.nsIScriptError;
172 // Is this error actually just a non-fatal warning?
173 var warning = aObject.flags & nsIScriptError.warningFlag != 0;
175 var typetext = warning ? "typeWarning" : "typeError";
176 row.setAttribute("typetext", this.mStrBundle.getString(typetext));
177 row.setAttribute("type", warning ? "warning" : "error");
178 row.setAttribute("msg", aObject.errorMessage);
179 row.setAttribute("category", aObject.category);
180 if (aObject.lineNumber || aObject.sourceName) {
181 row.setAttribute("href", aObject.sourceName);
182 row.setAttribute("line", aObject.lineNumber);
184 row.setAttribute("hideSource", "true");
186 if (aObject.sourceLine) {
187 row.setAttribute("code", aObject.sourceLine.replace(/\s/g, " "));
188 if (aObject.columnNumber) {
189 row.setAttribute("col", aObject.columnNumber);
190 row.setAttribute("errorDots", this.repeatChar(" ", aObject.columnNumber));
191 row.setAttribute("errorCaret", " ");
193 row.setAttribute("hideCaret", "true");
196 row.setAttribute("hideCode", "true");
198 this.appendConsoleRow(row);
202 <method name="appendMessage">
203 <parameter name="aMessage"/>
204 <parameter name="aType"/>
206 var row = this.createConsoleRow();
207 row.setAttribute("type", aType || "message");
208 row.setAttribute("msg", aMessage);
209 this.appendConsoleRow(row);
213 <method name="clear">
215 // add a "clear" message (mainly for other listeners)
216 this.mCService.logStringMessage(null);
217 this.mCService.reset();
221 <method name="copySelectedItem">
223 if (this.mSelectedItem) try {
224 const clipURI = "@mozilla.org/widget/clipboardhelper;1";
225 const clipI = Components.interfaces.nsIClipboardHelper;
226 var clipboard = Components.classes[clipURI].getService(clipI);
228 clipboard.copyString(this.mSelectedItem.toString());
230 // Unable to copy anything, die quietly
235 <method name="createConsoleRow">
237 var row = document.createElement("box");
238 row.setAttribute("class", "console-row");
239 row._IsConsoleRow = true;
240 row._ConsoleBox = this;
245 <method name="appendConsoleRow">
246 <parameter name="aRow"/>
248 this.mConsoleRowBox.appendChild(aRow);
249 if (++this.mCount > this.limit) this.deleteFirst();
253 <method name="deleteFirst">
255 var node = this.mConsoleRowBox.firstChild;
256 this.mConsoleRowBox.removeChild(node);
261 <method name="clearConsole">
263 if (this.mCount == 0) // already clear
267 var newRows = this.mConsoleRowBox.cloneNode(false);
268 this.mConsoleRowBox.parentNode.replaceChild(newRows, this.mConsoleRowBox);
269 this.mConsoleRowBox = newRows;
270 this.selectedItem = null;
274 <!-- UTILITY FUNCTIONS -->
276 <method name="repeatChar">
277 <parameter name="aChar"/>
278 <parameter name="aCol"/>
283 for (var i = 2; i < aCol; i += i)
286 return aChar + aChar.slice(0, aCol - aChar.length);
290 <constructor> this.init(); </constructor>
291 <destructor> this.destroy(); </destructor>
293 <!-- Command controller for the copy command -->
294 <field name="_controller"><![CDATA[({
297 QueryInterface: function(aIID) {
298 if (aIID.equals(Components.interfaces.nsIController) ||
299 aIID.equals(Components.interfaces.nsISupports))
301 throw Components.results.NS_NOINTERFACE;
304 supportsCommand: function(aCommand) {
305 return aCommand == "cmd_copy";
308 isCommandEnabled: function(aCommand) {
309 return aCommand == "cmd_copy" && this._outer.selectedItem;
312 doCommand: function(aCommand) {
313 if (aCommand == "cmd_copy")
314 this._outer.copySelectedItem();
317 onEvent: function() { }
322 <handler event="mousedown"><![CDATA[
323 if (event.button == 0 || event.button == 2) {
324 var target = event.originalTarget;
326 while (target && !("_IsConsoleRow" in target))
327 target = target.parentNode;
330 this.selectedItem = target;
336 <binding id="error" extends="xul:box">
338 <xul:box class="console-row-internal-box" flex="1">
339 <xul:box class="console-row-icon" align="center" xbl:inherits="selected">
340 <xul:image class="console-icon" xbl:inherits="src,type"/>
342 <xul:vbox class="console-row-content" xbl:inherits="selected" flex="1">
343 <xul:box class="console-row-msg" align="start">
344 <xul:label class="label" xbl:inherits="value=typetext"/>
345 <xul:description class="console-error-msg" xbl:inherits="xbl:text=msg" flex="1"/>
347 <xul:box class="console-row-file" xbl:inherits="hidden=hideSource">
348 <xul:label class="label" value="&errFile.label;"/>
349 <xul:box class="console-error-source" xbl:inherits="href,line"/>
350 <xul:spacer flex="1"/>
351 <xul:hbox class="lineNumberRow" xbl:inherits="line">
352 <xul:label class="label" value="&errLine.label;"/>
353 <xul:label class="label" xbl:inherits="value=line"/>
356 <xul:vbox class="console-row-code" xbl:inherits="selected,hidden=hideCode">
357 <xul:label class="monospace console-code" xbl:inherits="value=code" crop="end"/>
358 <xul:box xbl:inherits="hidden=hideCaret">
359 <xul:label class="monospace console-dots" xbl:inherits="value=errorDots"/>
360 <xul:label class="monospace console-caret" xbl:inherits="value=errorCaret"/>
361 <xul:spacer flex="1"/>
370 <method name="toString">
372 var msg = this.getAttribute("typetext") + " " + this.getAttribute("msg");
374 var strBundle = this._ConsoleBox.mStrBundle;
376 if (this.hasAttribute("line") && this.hasAttribute("href")) {
377 msg += "\n" + strBundle.getFormattedString("errFile",
378 [this.getAttribute("href")]) + "\n";
379 if (this.hasAttribute("col")) {
380 msg += strBundle.getFormattedString("errLineCol",
381 [this.getAttribute("line"), this.getAttribute("col")]);
383 msg += strBundle.getFormattedString("errLine", [this.getAttribute("line")]);
386 if (this.hasAttribute("code"))
387 msg += "\n" + strBundle.getString("errCode") + "\n" + this.getAttribute("code");
396 <binding id="message" extends="xul:box">
398 <xul:box class="console-internal-box" flex="1">
399 <xul:box class="console-row-icon" align="center">
400 <xul:image class="console-icon" xbl:inherits="src,type"/>
402 <xul:vbox class="console-row-content" xbl:inherits="selected" flex="1">
403 <xul:vbox class="console-row-msg" flex="1">
404 <xul:description class="console-msg-text" xbl:inherits="xbl:text=msg"/>
411 <method name="toString">
413 return this.getAttribute("msg");
419 <binding id="console-error-source" extends="xul:box">
421 <xul:label class="text-link" xbl:inherits="href,value=href" crop="right"/>
425 <handler event="click" phase="capturing" button="0" preventdefault="true">
427 var url = this.getAttribute("href");
428 var line = getAttribute("line");
429 gViewSourceUtils.viewSource(url, null, null, line);