1e61feca4fdf221ee4c4113b2306970f415af45a
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ConsoleMessageImpl.js
1 /*
2  * Copyright (C) 2011 Google Inc.  All rights reserved.
3  * Copyright (C) 2007, 2008, 2013 Apple Inc.  All rights reserved.
4  * Copyright (C) 2009 Joseph Pecoraro
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1.  Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  * 2.  Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
16  *     its contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 WebInspector.ConsoleMessageImpl = function(source, level, message, linkifier, type, url, line, column, repeatCount, parameters, stackTrace, request)
32 {
33     WebInspector.ConsoleMessage.call(this, source, level, url, line, column, repeatCount);
34
35     this._linkifier = linkifier;
36     this.type = type || WebInspector.ConsoleMessage.MessageType.Log;
37     this._messageText = message;
38     this._parameters = parameters;
39     this._stackTrace = stackTrace;
40     this._request = request;
41
42     this._customFormatters = {
43         "object": this._formatParameterAsObject,
44         "error": this._formatParameterAsObject,
45         "map": this._formatParameterAsObject,
46         "set": this._formatParameterAsObject,
47         "weakmap": this._formatParameterAsObject,
48         "array":  this._formatParameterAsArray,
49         "node":   this._formatParameterAsNode,
50         "string": this._formatParameterAsString
51     };
52 };
53
54 WebInspector.ConsoleMessageImpl.prototype = {
55
56     enforcesClipboardPrefixString: true,
57
58     _formatMessage: function()
59     {
60         this._formattedMessage = document.createElement("span");
61         this._formattedMessage.className = "console-message-text";
62
63         var messageText;
64         if (this.source === WebInspector.ConsoleMessage.MessageSource.ConsoleAPI) {
65             switch (this.type) {
66                 case WebInspector.ConsoleMessage.MessageType.Trace:
67                     messageText = document.createTextNode("console.trace()");
68                     break;
69                 case WebInspector.ConsoleMessage.MessageType.Assert:
70                     var args = [WebInspector.UIString("Assertion failed:")];
71                     if (this._parameters)
72                         args = args.concat(this._parameters);
73                     messageText = this._format(args);
74                     break;
75                 case WebInspector.ConsoleMessage.MessageType.Dir:
76                     var obj = this._parameters ? this._parameters[0] : undefined;
77                     var args = ["%O", obj];
78                     messageText = this._format(args);
79                     break;
80                 default:
81                     var args = this._parameters || [this._messageText];
82                     messageText = this._format(args);
83             }
84         } else if (this.source === WebInspector.ConsoleMessage.MessageSource.Network) {
85             if (this._request) {
86                 this._stackTrace = this._request.stackTrace;
87                 if (this._request.initiator && this._request.initiator.url) {
88                     this.url = this._request.initiator.url;
89                     this.line = this._request.initiator.lineNumber;
90                 }
91                 messageText = document.createElement("span");
92                 if (this.level === WebInspector.ConsoleMessage.MessageLevel.Error) {
93                     messageText.appendChild(document.createTextNode(this._request.requestMethod + " "));
94                     messageText.appendChild(WebInspector.linkifyRequestAsNode(this._request));
95                     if (this._request.failed)
96                         messageText.appendChild(document.createTextNode(" " + this._request.localizedFailDescription));
97                     else
98                         messageText.appendChild(document.createTextNode(" " + this._request.statusCode + " (" + this._request.statusText + ")"));
99                 } else {
100                     var fragment = WebInspector.linkifyStringAsFragmentWithCustomLinkifier(this._messageText, WebInspector.linkifyRequestAsNode.bind(null, this._request, ""));
101                     messageText.appendChild(fragment);
102                 }
103             } else {
104                 if (this.url) {
105                     var anchor = WebInspector.linkifyURLAsNode(this.url, this.url, "console-message-url");
106                     this._formattedMessage.appendChild(anchor);
107                 }
108                 messageText = this._format([this._messageText]);
109             }
110         } else {
111             var args = this._parameters || [this._messageText];
112             messageText = this._format(args);
113         }
114
115         if (this.source !== WebInspector.ConsoleMessage.MessageSource.Network || this._request) {
116             var firstNonNativeCallFrame = this._firstNonNativeCallFrame();
117             if (firstNonNativeCallFrame) {
118                 var urlElement = this._linkifyCallFrame(firstNonNativeCallFrame);
119                 this._formattedMessage.appendChild(urlElement);
120             } else if (this.url && !this._shouldHideURL(this.url)) {
121                 var urlElement = this._linkifyLocation(this.url, this.line, this.column);
122                 this._formattedMessage.appendChild(urlElement);
123             }
124         }
125
126         this._formattedMessage.appendChild(messageText);
127
128         if (this._shouldDumpStackTrace()) {
129             var ol = document.createElement("ol");
130             ol.className = "outline-disclosure";
131             var treeOutline = new TreeOutline(ol);
132
133             var content = this._formattedMessage;
134             var root = new TreeElement(content, null, true);
135             content.treeElementForTest = root;
136             treeOutline.appendChild(root);
137             if (this.type === WebInspector.ConsoleMessage.MessageType.Trace)
138                 root.expand();
139
140             this._populateStackTraceTreeElement(root);
141             this._formattedMessage = ol;
142         }
143
144         // This is used for inline message bubbles in SourceFrames, or other plain-text representations.
145         this._message = messageText.textContent;
146     },
147
148     _shouldDumpStackTrace: function()
149     {
150         return !!this._stackTrace && this._stackTrace.length && (this.source === WebInspector.ConsoleMessage.MessageSource.Network || this.level === WebInspector.ConsoleMessage.MessageLevel.Error || this.type === WebInspector.ConsoleMessage.MessageType.Trace);
151     },
152
153     _shouldHideURL: function(url)
154     {
155         return url === "undefined" || url === "[native code]";
156     },
157
158     _firstNonNativeCallFrame: function()
159     {
160         if (!this._stackTrace)
161             return null;
162
163         for (var i = 0; i < this._stackTrace.length; i++) {
164             var frame = this._stackTrace[i];
165             if (!frame.url || frame.url === "[native code]")
166                 continue;
167             return frame;
168         }
169
170         return null;
171     },
172
173     get message()
174     {
175         // force message formatting
176         var formattedMessage = this.formattedMessage;
177         return this._message;
178     },
179
180     get formattedMessage()
181     {
182         if (!this._formattedMessage)
183             this._formatMessage();
184         return this._formattedMessage;
185     },
186
187     _linkifyLocation: function(url, lineNumber, columnNumber)
188     {
189         // ConsoleMessage stack trace line numbers are one-based.
190         lineNumber = lineNumber ? lineNumber - 1 : 0;
191
192         return WebInspector.linkifyLocation(url, lineNumber, columnNumber, "console-message-url");
193     },
194
195     _linkifyCallFrame: function(callFrame)
196     {
197         return this._linkifyLocation(callFrame.url, callFrame.lineNumber, callFrame.columnNumber);
198     },
199
200     isErrorOrWarning: function()
201     {
202         return (this.level === WebInspector.ConsoleMessage.MessageLevel.Warning || this.level === WebInspector.ConsoleMessage.MessageLevel.Error);
203     },
204
205     _format: function(parameters)
206     {
207         // This node is used like a Builder. Values are continually appended onto it.
208         var formattedResult = document.createElement("span");
209         if (!parameters.length)
210             return formattedResult;
211
212         // Formatting code below assumes that parameters are all wrappers whereas frontend console
213         // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
214         for (var i = 0; i < parameters.length; ++i) {
215             // FIXME: Only pass runtime wrappers here.
216             if (parameters[i] instanceof WebInspector.RemoteObject)
217                 continue;
218
219             if (typeof parameters[i] === "object")
220                 parameters[i] = WebInspector.RemoteObject.fromPayload(parameters[i]);
221             else
222                 parameters[i] = WebInspector.RemoteObject.fromPrimitiveValue(parameters[i]);
223         }
224
225         // There can be string log and string eval result. We distinguish between them based on message type.
226         var shouldFormatMessage = WebInspector.RemoteObject.type(parameters[0]) === "string" && this.type !== WebInspector.ConsoleMessage.MessageType.Result;
227
228         if (shouldFormatMessage) {
229             // Multiple parameters with the first being a format string. Save unused substitutions.
230             var result = this._formatWithSubstitutionString(parameters, formattedResult);
231             parameters = result.unusedSubstitutions;
232             if (parameters.length)
233                 formattedResult.appendChild(document.createTextNode(" "));
234         }
235
236         if (this.type === WebInspector.ConsoleMessage.MessageType.Table) {
237             formattedResult.appendChild(this._formatParameterAsTable(parameters));
238             return formattedResult;
239         }
240
241         // Single parameter, or unused substitutions from above.
242         for (var i = 0; i < parameters.length; ++i) {
243             // Inline strings when formatting.
244             if (shouldFormatMessage && parameters[i].type === "string") {
245                 var span = document.createElement("span");
246                 span.classList.add("type-string");
247                 span.textContent = parameters[i].description;
248                 formattedResult.appendChild(span);
249             } else
250                 formattedResult.appendChild(this._formatParameter(parameters[i], false));
251
252             if (i < parameters.length - 1 && !this._isExpandable(parameters[i]))
253                 formattedResult.appendChild(document.createTextNode(" "));
254
255         }
256         return formattedResult;
257     },
258
259     _isExpandable: function(remoteObject) {
260         if (!remoteObject)
261             return false;
262
263         if (remoteObject.hasChildren && remoteObject.preview && remoteObject.preview.lossless)
264             return false;
265
266         return remoteObject.hasChildren;
267     },
268
269     _formatParameter: function(output, forceObjectFormat)
270     {
271         var type;
272         if (forceObjectFormat)
273             type = "object";
274         else if (output instanceof WebInspector.RemoteObject)
275             type = output.subtype || output.type;
276         else
277             type = typeof output;
278
279         var formatter = this._customFormatters[type];
280         if (!formatter)
281             formatter = this._formatParameterAsValue;
282
283         var span = document.createElement("span");
284         span.className = "console-formatted-" + type;
285
286         if (this._isExpandable(output))
287             span.classList.add("expandable");
288
289         formatter.call(this, output, span, forceObjectFormat);
290         return span;
291     },
292
293     _formatParameterAsValue: function(value, elem)
294     {
295         elem.appendChild(WebInspector.FormattedValue.createElementForRemoteObject(value));
296     },
297
298     _formatParameterAsObject: function(obj, elem, forceExpansion)
299     {
300         var objectTree = new WebInspector.ObjectTreeView(obj, WebInspector.ObjectTreeView.Mode.Properties, forceExpansion);
301         elem.appendChild(objectTree.element);
302     },
303
304     _formatParameterAsString: function(output, elem)
305     {
306         var span = WebInspector.FormattedValue.createLinkifiedElementString(output.description);
307         elem.classList.remove("console-formatted-string");
308         elem.appendChild(span);
309     },
310
311     _formatParameterAsNode: function(object, elem)
312     {
313         function printNode(nodeId)
314         {
315             if (!nodeId) {
316                 // Sometimes DOM is loaded after the sync message is being formatted, so we get no
317                 // nodeId here. So we fall back to object formatting here.
318                 this._formatParameterAsObject(object, elem, true);
319                 return;
320             }
321             var treeOutline = new WebInspector.DOMTreeOutline(false, false, true);
322             treeOutline.setVisible(true);
323             treeOutline.rootDOMNode = WebInspector.domTreeManager.nodeForId(nodeId);
324             treeOutline.element.classList.add("outline-disclosure");
325             if (!treeOutline.children[0].hasChildren)
326                 treeOutline.element.classList.add("single-node");
327             elem.appendChild(treeOutline.element);
328         }
329         object.pushNodeToFrontend(printNode.bind(this));
330     },
331
332     _formatParameterAsArray: function(arr, elem)
333     {
334         // FIXME: Array previews look poor. Keep doing what we currently do for arrays.
335         arr.deprecatedGetOwnProperties(this._printArray.bind(this, arr, elem));
336     },
337
338     _userProvidedColumnNames: function(columnNamesArgument)
339     {
340         if (!columnNamesArgument)
341             return null;
342
343         var remoteObject = WebInspector.RemoteObject.fromPayload(columnNamesArgument);
344
345         // Single primitive argument.
346         if (remoteObject.type === "string" || remoteObject.type === "number")
347             return [String(columnNamesArgument.value)];
348
349         // Ignore everything that is not an array with property previews.
350         if (remoteObject.type !== "object" || remoteObject.subtype !== "array" || !remoteObject.preview || !remoteObject.preview.propertyPreviews)
351             return null;
352
353         // Array. Look into the preview and get string values.
354         var extractedColumnNames = [];
355         for (var propertyPreview of remoteObject.preview.propertyPreviews) {
356             if (propertyPreview.type === "string" || propertyPreview.type === "number")
357                 extractedColumnNames.push(String(propertyPreview.value));
358         }
359
360         return extractedColumnNames.length ? extractedColumnNames : null;
361     },
362
363     _formatParameterAsTable: function(parameters)
364     {
365         var element = document.createElement("span");
366         var table = parameters[0];
367         if (!table || !table.preview)
368             return element;
369
370         var rows = [];
371         var columnNames = [];
372         var flatValues = [];
373         var preview = table.preview;
374         var userProvidedColumnNames = false;
375
376         // User provided columnNames.
377         var extractedColumnNames = this._userProvidedColumnNames(parameters[1]);
378         if (extractedColumnNames) {
379             userProvidedColumnNames = true;
380             columnNames = extractedColumnNames;
381         }
382
383         // Check first for valuePreviews in the properties meaning this was an array of objects.
384         if (preview.propertyPreviews) {
385             for (var i = 0; i < preview.propertyPreviews.length; ++i) {
386                 var rowProperty = preview.propertyPreviews[i];
387                 var rowPreview = rowProperty.valuePreview;
388                 if (!rowPreview)
389                     continue;
390
391                 var rowValue = {};
392                 const maxColumnsToRender = 10;
393                 for (var j = 0; j < rowPreview.propertyPreviews.length; ++j) {
394                     var cellProperty = rowPreview.propertyPreviews[j];
395                     var columnRendered = columnNames.contains(cellProperty.name);
396                     if (!columnRendered) {
397                         if (userProvidedColumnNames || columnNames.length === maxColumnsToRender)
398                             continue;
399                         columnRendered = true;
400                         columnNames.push(cellProperty.name);
401                     }
402
403                     rowValue[cellProperty.name] = WebInspector.FormattedValue.createElementForPropertyPreview(cellProperty);
404                 }
405                 rows.push([rowProperty.name, rowValue]);
406             }
407         }
408
409         // If there were valuePreviews, convert to a flat list.
410         if (rows.length) {
411             const emDash = "\u2014";
412             columnNames.unshift(WebInspector.UIString("(Index)"));
413             for (var i = 0; i < rows.length; ++i) {
414                 var rowName = rows[i][0];
415                 var rowValue = rows[i][1];
416                 flatValues.push(rowName);
417                 for (var j = 1; j < columnNames.length; ++j) {
418                     var columnName = columnNames[j];
419                     if (!(columnName in rowValue))
420                         flatValues.push(emDash);
421                     else
422                         flatValues.push(rowValue[columnName]);
423                 }
424             }
425         }
426
427         // If there were no value Previews, then check for an array of values.
428         if (!flatValues.length && preview.propertyPreviews) {
429             for (var i = 0; i < preview.propertyPreviews.length; ++i) {
430                 var rowProperty = preview.propertyPreviews[i];
431                 if (!("value" in rowProperty))
432                     continue;
433
434                 if (!columnNames.length) {
435                     columnNames.push(WebInspector.UIString("Index"));
436                     columnNames.push(WebInspector.UIString("Value"));
437                 }
438
439                 flatValues.push(rowProperty.name);
440                 flatValues.push(WebInspector.FormattedValue.createElementForPropertyPreview(rowProperty));
441             }
442         }
443
444         // If lossless or not table data, output the object so full data can be gotten.
445         if (!preview.lossless || !flatValues.length) {
446             element.appendChild(this._formatParameter(table));
447             if (!flatValues.length)
448                 return element;
449         }
450
451         var dataGridContainer = element.createChild("span");
452         var dataGrid = WebInspector.DataGrid.createSortableDataGrid(columnNames, flatValues);
453         dataGrid.element.classList.add("inline");
454         dataGridContainer.appendChild(dataGrid.element);
455
456         return element;
457     },
458
459     _printArray: function(array, elem, properties)
460     {
461         if (!properties)
462             return;
463
464         var elements = [];
465         for (var i = 0; i < properties.length; ++i) {
466             var property = properties[i];
467             var name = property.name;
468             if (!isNaN(name))
469                 elements[name] = this._formatAsArrayEntry(property.value);
470         }
471
472         elem.appendChild(document.createTextNode("["));
473         var lastNonEmptyIndex = -1;
474
475         function appendUndefined(elem, index)
476         {
477             if (index - lastNonEmptyIndex <= 1)
478                 return;
479             var span = elem.createChild("span", "console-formatted-undefined");
480             span.textContent = WebInspector.UIString("undefined × %d").format(index - lastNonEmptyIndex - 1);
481         }
482
483         var length = array.arrayLength();
484         for (var i = 0; i < length; ++i) {
485             var element = elements[i];
486             if (!element)
487                 continue;
488
489             if (i - lastNonEmptyIndex > 1) {
490                 appendUndefined(elem, i);
491                 elem.appendChild(document.createTextNode(", "));
492             }
493
494             elem.appendChild(element);
495             lastNonEmptyIndex = i;
496             if (i < length - 1)
497                 elem.appendChild(document.createTextNode(", "));
498         }
499         appendUndefined(elem, length);
500
501         elem.appendChild(document.createTextNode("]"));
502     },
503
504     _formatAsArrayEntry: function(output)
505     {
506         // Prevent infinite expansion of cross-referencing arrays.
507         return this._formatParameter(output, output.subtype && output.subtype === "array");
508     },
509
510     _formatWithSubstitutionString: function(parameters, formattedResult)
511     {
512         var formatters = {};
513
514         function parameterFormatter(force, obj)
515         {
516             return this._formatParameter(obj, force);
517         }
518
519         function stringFormatter(obj)
520         {
521             return obj.description;
522         }
523
524         function floatFormatter(obj)
525         {
526             if (typeof obj.value !== "number")
527                 return parseFloat(obj.description);
528             return obj.value;
529         }
530
531         function integerFormatter(obj)
532         {
533             if (typeof obj.value !== "number")
534                 return parseInt(obj.description);
535             return Math.floor(obj.value);
536         }
537
538         var currentStyle = null;
539         function styleFormatter(obj)
540         {
541             currentStyle = {};
542             var buffer = document.createElement("span");
543             buffer.setAttribute("style", obj.description);
544             for (var i = 0; i < buffer.style.length; i++) {
545                 var property = buffer.style[i];
546                 if (isWhitelistedProperty(property))
547                     currentStyle[property] = buffer.style[property];
548             }
549         }
550
551         function isWhitelistedProperty(property)
552         {
553             var prefixes = ["background", "border", "color", "font", "line", "margin", "padding", "text", "-webkit-background", "-webkit-border", "-webkit-font", "-webkit-margin", "-webkit-padding", "-webkit-text"];
554             for (var i = 0; i < prefixes.length; i++) {
555                 if (property.startsWith(prefixes[i]))
556                     return true;
557             }
558             return false;
559         }
560
561         // Firebug uses %o for formatting objects.
562         formatters.o = parameterFormatter.bind(this, false);
563         formatters.s = stringFormatter;
564         formatters.f = floatFormatter;
565
566         // Firebug allows both %i and %d for formatting integers.
567         formatters.i = integerFormatter;
568         formatters.d = integerFormatter;
569
570         // Firebug uses %c for styling the message.
571         formatters.c = styleFormatter;
572
573         // Support %O to force object formatting, instead of the type-based %o formatting.
574         formatters.O = parameterFormatter.bind(this, true);
575
576         function append(a, b)
577         {
578             if (b instanceof Node)
579                 a.appendChild(b);
580             else if (b) {
581                 var toAppend = WebInspector.linkifyStringAsFragment(b.toString());
582                 if (currentStyle) {
583                     var wrapper = document.createElement("span");
584                     for (var key in currentStyle)
585                         wrapper.style[key] = currentStyle[key];
586                     wrapper.appendChild(toAppend);
587                     toAppend = wrapper;
588                 }
589                 var span = document.createElement("span");
590                 span.className = "type-string";
591                 span.appendChild(toAppend);
592                 a.appendChild(span);
593             }
594             return a;
595         }
596
597         // String.format does treat formattedResult like a Builder, result is an object.
598         return String.format(parameters[0].description, parameters.slice(1), formatters, formattedResult, append);
599     },
600
601     decorateMessageElement: function(element)
602     {
603         if (this._element)
604             return this._element;
605
606         element.message = this;
607         element.classList.add("console-message");
608
609         this._element = element;
610
611         switch (this.level) {
612             case WebInspector.ConsoleMessage.MessageLevel.Tip:
613                 element.classList.add("console-tip-level");
614                 element.setAttribute("data-labelprefix", WebInspector.UIString("Tip: "));
615                 break;
616             case WebInspector.ConsoleMessage.MessageLevel.Log:
617                 element.classList.add("console-log-level");
618                 element.setAttribute("data-labelprefix", WebInspector.UIString("Log: "));
619                 break;
620             case WebInspector.ConsoleMessage.MessageLevel.Debug:
621                 element.classList.add("console-debug-level");
622                 element.setAttribute("data-labelprefix", WebInspector.UIString("Debug: "));
623                 break;
624             case WebInspector.ConsoleMessage.MessageLevel.Warning:
625                 element.classList.add("console-warning-level");
626                 element.setAttribute("data-labelprefix", WebInspector.UIString("Warning: "));
627                 break;
628             case WebInspector.ConsoleMessage.MessageLevel.Error:
629                 element.classList.add("console-error-level");
630                 element.setAttribute("data-labelprefix", WebInspector.UIString("Error: "));
631                 break;
632         }
633
634         if (this.type === WebInspector.ConsoleMessage.MessageType.StartGroup || this.type === WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed)
635             element.classList.add("console-group-title");
636
637         element.appendChild(this.formattedMessage);
638
639         if (this.repeatCount > 1)
640             this.updateRepeatCount();
641
642         return element;
643     },
644
645     toMessageElement: function()
646     {
647         if (this._element)
648             return this._element;
649
650         var element = document.createElement("div");
651
652         return this.decorateMessageElement(element);
653     },
654
655     _populateStackTraceTreeElement: function(parentTreeElement)
656     {
657         for (var i = 0; i < this._stackTrace.length; i++) {
658             var frame = this._stackTrace[i];
659
660             var content = document.createElement("div");
661             var messageTextElement = document.createElement("span");
662             messageTextElement.className = "console-message-text";
663             var functionName = frame.functionName || WebInspector.UIString("(anonymous function)");
664             messageTextElement.appendChild(document.createTextNode(functionName));
665             content.appendChild(messageTextElement);
666
667             if (frame.url && !this._shouldHideURL(frame.url)) {
668                 var urlElement = this._linkifyCallFrame(frame);
669                 content.appendChild(urlElement);
670             }
671
672             var treeElement = new TreeElement(content);
673             parentTreeElement.appendChild(treeElement);
674         }
675     },
676
677     updateRepeatCount: function() {
678         if (!this.repeatCountElement) {
679             this.repeatCountElement = document.createElement("span");
680             this.repeatCountElement.className = "bubble";
681
682             this._element.insertBefore(this.repeatCountElement, this._element.firstChild);
683         }
684         this.repeatCountElement.textContent = this.repeatCount;
685     },
686
687     toString: function()
688     {
689         var sourceString;
690         switch (this.source) {
691             case WebInspector.ConsoleMessage.MessageSource.HTML:
692                 sourceString = "HTML";
693                 break;
694             case WebInspector.ConsoleMessage.MessageSource.XML:
695                 sourceString = "XML";
696                 break;
697             case WebInspector.ConsoleMessage.MessageSource.JS:
698                 sourceString = "JS";
699                 break;
700             case WebInspector.ConsoleMessage.MessageSource.Network:
701                 sourceString = "Network";
702                 break;
703             case WebInspector.ConsoleMessage.MessageSource.ConsoleAPI:
704                 sourceString = "ConsoleAPI";
705                 break;
706             case WebInspector.ConsoleMessage.MessageSource.Other:
707                 sourceString = "Other";
708                 break;
709         }
710
711         var typeString;
712         switch (this.type) {
713             case WebInspector.ConsoleMessage.MessageType.Log:
714                 typeString = "Log";
715                 break;
716             case WebInspector.ConsoleMessage.MessageType.Dir:
717                 typeString = "Dir";
718                 break;
719             case WebInspector.ConsoleMessage.MessageType.DirXML:
720                 typeString = "Dir XML";
721                 break;
722             case WebInspector.ConsoleMessage.MessageType.Trace:
723                 typeString = "Trace";
724                 break;
725             case WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed:
726             case WebInspector.ConsoleMessage.MessageType.StartGroup:
727                 typeString = "Start Group";
728                 break;
729             case WebInspector.ConsoleMessage.MessageType.EndGroup:
730                 typeString = "End Group";
731                 break;
732             case WebInspector.ConsoleMessage.MessageType.Assert:
733                 typeString = "Assert";
734                 break;
735             case WebInspector.ConsoleMessage.MessageType.Result:
736                 typeString = "Result";
737                 break;
738         }
739
740         return sourceString + " " + typeString + " " + this.levelString + ": " + this.formattedMessage.textContent + "\n" + this.url + " line " + this.line;
741     },
742
743     get text()
744     {
745         return this._messageText;
746     },
747
748     isEqual: function(msg)
749     {
750         if (!msg)
751             return false;
752
753         if (this._stackTrace) {
754             if (!msg._stackTrace)
755                 return false;
756             var l = this._stackTrace;
757             var r = msg._stackTrace;
758             for (var i = 0; i < l.length; i++) {
759                 if (l[i].url !== r[i].url ||
760                     l[i].functionName !== r[i].functionName ||
761                     l[i].lineNumber !== r[i].lineNumber ||
762                     l[i].columnNumber !== r[i].columnNumber)
763                     return false;
764             }
765         }
766
767         return (this.source === msg.source)
768             && (this.type === msg.type)
769             && (this.level === msg.level)
770             && (this.line === msg.line)
771             && (this.url === msg.url)
772             && (this.message === msg.message)
773             && (this._request === msg._request);
774     },
775
776     get stackTrace()
777     {
778         return this._stackTrace;
779     },
780
781     clone: function()
782     {
783         return WebInspector.ConsoleMessage.create(this.source, this.level, this._messageText, this.type, this.url, this.line, this.column, this.repeatCount, this._parameters, this._stackTrace, this._request);
784     },
785
786     get levelString()
787     {
788         switch (this.level) {
789             case WebInspector.ConsoleMessage.MessageLevel.Tip:
790                 return "Tip";
791             case WebInspector.ConsoleMessage.MessageLevel.Log:
792                 return "Log";
793             case WebInspector.ConsoleMessage.MessageLevel.Warning:
794                 return "Warning";
795             case WebInspector.ConsoleMessage.MessageLevel.Debug:
796                 return "Debug";
797             case WebInspector.ConsoleMessage.MessageLevel.Error:
798                 return "Error";
799         }
800     },
801
802     get clipboardPrefixString()
803     {
804         return "[" + this.levelString + "] ";
805     },
806
807     toClipboardString: function(isPrefixOptional)
808     {
809         var isTrace = this._shouldDumpStackTrace();
810
811         var clipboardString = "";
812         if (this._formattedMessage && !isTrace)
813             clipboardString = this._formattedMessage.querySelector("span").innerText;
814         else
815             clipboardString = this.type === WebInspector.ConsoleMessage.MessageType.Trace ? "console.trace()" : this._message || this._messageText;
816
817         if (!isPrefixOptional || this.enforcesClipboardPrefixString)
818             clipboardString = this.clipboardPrefixString + clipboardString;
819
820         if (isTrace) {
821             this._stackTrace.forEach(function(frame) {
822                 clipboardString += "\n\t" + (frame.functionName || WebInspector.UIString("(anonymous function)"));
823                 if (frame.url)
824                     clipboardString += " (" + WebInspector.displayNameForURL(frame.url) + ", line " + frame.lineNumber + ")";
825             });
826         } else {
827             var repeatString = this.repeatCount > 1 ? "x" + this.repeatCount : "";
828
829             var urlLine = "";
830             if (this.url) {
831                 var components = [WebInspector.displayNameForURL(this.url), "line " + this.line];
832                 if (repeatString)
833                     components.push(repeatString);
834                 urlLine = " (" + components.join(", ") + ")";
835             } else if (repeatString)
836                 urlLine = " (" + repeatString + ")";
837
838             if (urlLine) {
839                 var lines = clipboardString.split("\n");
840                 lines[0] += urlLine;
841                 clipboardString = lines.join("\n");
842             }
843         }
844
845         return clipboardString;
846     }
847 };
848
849 WebInspector.ConsoleMessageImpl.prototype.__proto__ = WebInspector.ConsoleMessage.prototype;