759d6ac751fb1c1fddb5f6056c7da47ca4c6293f
[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
285         if (this._isExpandable(output))
286             span.classList.add("expandable");
287
288         formatter.call(this, output, span, forceObjectFormat);
289         return span;
290     },
291
292     _formatParameterAsValue: function(value, elem)
293     {
294         elem.appendChild(WebInspector.FormattedValue.createElementForRemoteObject(value));
295     },
296
297     _formatParameterAsObject: function(obj, elem, forceExpansion)
298     {
299         // FIXME: Intialize component with "$n" instead of "obj". Or, an existing property path.
300         var objectTree = new WebInspector.ObjectTreeView(obj, WebInspector.ObjectTreeView.Mode.Properties, null, forceExpansion);
301         elem.appendChild(objectTree.element);
302     },
303
304     _formatParameterAsString: function(output, elem)
305     {
306         var span = WebInspector.FormattedValue.createLinkifiedElementString(output.description);
307         elem.appendChild(span);
308     },
309
310     _formatParameterAsNode: function(object, elem)
311     {
312         var span = WebInspector.FormattedValue.createElementForNode(object);
313         elem.appendChild(span);
314     },
315
316     _formatParameterAsArray: function(arr, elem)
317     {
318         // FIXME: Intialize component with "$n" instead of "obj". Or, an existing property path.
319         var objectTree = new WebInspector.ObjectTreeView(arr, WebInspector.ObjectTreeView.Mode.Properties);
320         elem.appendChild(objectTree.element);
321     },
322
323     _userProvidedColumnNames: function(columnNamesArgument)
324     {
325         if (!columnNamesArgument)
326             return null;
327
328         var remoteObject = WebInspector.RemoteObject.fromPayload(columnNamesArgument);
329
330         // Single primitive argument.
331         if (remoteObject.type === "string" || remoteObject.type === "number")
332             return [String(columnNamesArgument.value)];
333
334         // Ignore everything that is not an array with property previews.
335         if (remoteObject.type !== "object" || remoteObject.subtype !== "array" || !remoteObject.preview || !remoteObject.preview.propertyPreviews)
336             return null;
337
338         // Array. Look into the preview and get string values.
339         var extractedColumnNames = [];
340         for (var propertyPreview of remoteObject.preview.propertyPreviews) {
341             if (propertyPreview.type === "string" || propertyPreview.type === "number")
342                 extractedColumnNames.push(String(propertyPreview.value));
343         }
344
345         return extractedColumnNames.length ? extractedColumnNames : null;
346     },
347
348     _formatParameterAsTable: function(parameters)
349     {
350         var element = document.createElement("span");
351         var table = parameters[0];
352         if (!table || !table.preview)
353             return element;
354
355         var rows = [];
356         var columnNames = [];
357         var flatValues = [];
358         var preview = table.preview;
359         var userProvidedColumnNames = false;
360
361         // User provided columnNames.
362         var extractedColumnNames = this._userProvidedColumnNames(parameters[1]);
363         if (extractedColumnNames) {
364             userProvidedColumnNames = true;
365             columnNames = extractedColumnNames;
366         }
367
368         // Check first for valuePreviews in the properties meaning this was an array of objects.
369         if (preview.propertyPreviews) {
370             for (var i = 0; i < preview.propertyPreviews.length; ++i) {
371                 var rowProperty = preview.propertyPreviews[i];
372                 var rowPreview = rowProperty.valuePreview;
373                 if (!rowPreview)
374                     continue;
375
376                 var rowValue = {};
377                 const maxColumnsToRender = 10;
378                 for (var j = 0; j < rowPreview.propertyPreviews.length; ++j) {
379                     var cellProperty = rowPreview.propertyPreviews[j];
380                     var columnRendered = columnNames.contains(cellProperty.name);
381                     if (!columnRendered) {
382                         if (userProvidedColumnNames || columnNames.length === maxColumnsToRender)
383                             continue;
384                         columnRendered = true;
385                         columnNames.push(cellProperty.name);
386                     }
387
388                     rowValue[cellProperty.name] = WebInspector.FormattedValue.createElementForPropertyPreview(cellProperty);
389                 }
390                 rows.push([rowProperty.name, rowValue]);
391             }
392         }
393
394         // If there were valuePreviews, convert to a flat list.
395         if (rows.length) {
396             const emDash = "\u2014";
397             columnNames.unshift(WebInspector.UIString("(Index)"));
398             for (var i = 0; i < rows.length; ++i) {
399                 var rowName = rows[i][0];
400                 var rowValue = rows[i][1];
401                 flatValues.push(rowName);
402                 for (var j = 1; j < columnNames.length; ++j) {
403                     var columnName = columnNames[j];
404                     if (!(columnName in rowValue))
405                         flatValues.push(emDash);
406                     else
407                         flatValues.push(rowValue[columnName]);
408                 }
409             }
410         }
411
412         // If there were no value Previews, then check for an array of values.
413         if (!flatValues.length && preview.propertyPreviews) {
414             for (var i = 0; i < preview.propertyPreviews.length; ++i) {
415                 var rowProperty = preview.propertyPreviews[i];
416                 if (!("value" in rowProperty))
417                     continue;
418
419                 if (!columnNames.length) {
420                     columnNames.push(WebInspector.UIString("Index"));
421                     columnNames.push(WebInspector.UIString("Value"));
422                 }
423
424                 flatValues.push(rowProperty.name);
425                 flatValues.push(WebInspector.FormattedValue.createElementForPropertyPreview(rowProperty));
426             }
427         }
428
429         // If lossless or not table data, output the object so full data can be gotten.
430         if (!preview.lossless || !flatValues.length) {
431             element.appendChild(this._formatParameter(table));
432             if (!flatValues.length)
433                 return element;
434         }
435
436         var dataGridContainer = element.createChild("span");
437         var dataGrid = WebInspector.DataGrid.createSortableDataGrid(columnNames, flatValues);
438         dataGrid.element.classList.add("inline");
439         dataGridContainer.appendChild(dataGrid.element);
440
441         return element;
442     },
443
444     _formatWithSubstitutionString: function(parameters, formattedResult)
445     {
446         var formatters = {};
447
448         function parameterFormatter(force, obj)
449         {
450             return this._formatParameter(obj, force);
451         }
452
453         function stringFormatter(obj)
454         {
455             return obj.description;
456         }
457
458         function floatFormatter(obj)
459         {
460             if (typeof obj.value !== "number")
461                 return parseFloat(obj.description);
462             return obj.value;
463         }
464
465         function integerFormatter(obj)
466         {
467             if (typeof obj.value !== "number")
468                 return parseInt(obj.description);
469             return Math.floor(obj.value);
470         }
471
472         var currentStyle = null;
473         function styleFormatter(obj)
474         {
475             currentStyle = {};
476             var buffer = document.createElement("span");
477             buffer.setAttribute("style", obj.description);
478             for (var i = 0; i < buffer.style.length; i++) {
479                 var property = buffer.style[i];
480                 if (isWhitelistedProperty(property))
481                     currentStyle[property] = buffer.style[property];
482             }
483         }
484
485         function isWhitelistedProperty(property)
486         {
487             var prefixes = ["background", "border", "color", "font", "line", "margin", "padding", "text", "-webkit-background", "-webkit-border", "-webkit-font", "-webkit-margin", "-webkit-padding", "-webkit-text"];
488             for (var i = 0; i < prefixes.length; i++) {
489                 if (property.startsWith(prefixes[i]))
490                     return true;
491             }
492             return false;
493         }
494
495         // Firebug uses %o for formatting objects.
496         formatters.o = parameterFormatter.bind(this, false);
497         formatters.s = stringFormatter;
498         formatters.f = floatFormatter;
499
500         // Firebug allows both %i and %d for formatting integers.
501         formatters.i = integerFormatter;
502         formatters.d = integerFormatter;
503
504         // Firebug uses %c for styling the message.
505         formatters.c = styleFormatter;
506
507         // Support %O to force object formatting, instead of the type-based %o formatting.
508         formatters.O = parameterFormatter.bind(this, true);
509
510         function append(a, b)
511         {
512             if (b instanceof Node)
513                 a.appendChild(b);
514             else if (b) {
515                 var toAppend = WebInspector.linkifyStringAsFragment(b.toString());
516                 if (currentStyle) {
517                     var wrapper = document.createElement("span");
518                     for (var key in currentStyle)
519                         wrapper.style[key] = currentStyle[key];
520                     wrapper.appendChild(toAppend);
521                     toAppend = wrapper;
522                 }
523                 var span = document.createElement("span");
524                 span.className = "type-string";
525                 span.appendChild(toAppend);
526                 a.appendChild(span);
527             }
528             return a;
529         }
530
531         // String.format does treat formattedResult like a Builder, result is an object.
532         return String.format(parameters[0].description, parameters.slice(1), formatters, formattedResult, append);
533     },
534
535     decorateMessageElement: function(element)
536     {
537         if (this._element)
538             return this._element;
539
540         element.message = this;
541         element.classList.add("console-message");
542
543         this._element = element;
544
545         switch (this.level) {
546             case WebInspector.ConsoleMessage.MessageLevel.Tip:
547                 element.classList.add("console-tip-level");
548                 element.setAttribute("data-labelprefix", WebInspector.UIString("Tip: "));
549                 break;
550             case WebInspector.ConsoleMessage.MessageLevel.Log:
551                 element.classList.add("console-log-level");
552                 element.setAttribute("data-labelprefix", WebInspector.UIString("Log: "));
553                 break;
554             case WebInspector.ConsoleMessage.MessageLevel.Debug:
555                 element.classList.add("console-debug-level");
556                 element.setAttribute("data-labelprefix", WebInspector.UIString("Debug: "));
557                 break;
558             case WebInspector.ConsoleMessage.MessageLevel.Warning:
559                 element.classList.add("console-warning-level");
560                 element.setAttribute("data-labelprefix", WebInspector.UIString("Warning: "));
561                 break;
562             case WebInspector.ConsoleMessage.MessageLevel.Error:
563                 element.classList.add("console-error-level");
564                 element.setAttribute("data-labelprefix", WebInspector.UIString("Error: "));
565                 break;
566         }
567
568         if (this.type === WebInspector.ConsoleMessage.MessageType.StartGroup || this.type === WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed)
569             element.classList.add("console-group-title");
570
571         element.appendChild(this.formattedMessage);
572
573         if (this.repeatCount > 1)
574             this.updateRepeatCount();
575
576         return element;
577     },
578
579     toMessageElement: function()
580     {
581         if (this._element)
582             return this._element;
583
584         var element = document.createElement("div");
585
586         return this.decorateMessageElement(element);
587     },
588
589     _populateStackTraceTreeElement: function(parentTreeElement)
590     {
591         for (var i = 0; i < this._stackTrace.length; i++) {
592             var frame = this._stackTrace[i];
593
594             var content = document.createElement("div");
595             var messageTextElement = document.createElement("span");
596             messageTextElement.className = "console-message-text";
597             var functionName = frame.functionName || WebInspector.UIString("(anonymous function)");
598             messageTextElement.appendChild(document.createTextNode(functionName));
599             content.appendChild(messageTextElement);
600
601             if (frame.url && !this._shouldHideURL(frame.url)) {
602                 var urlElement = this._linkifyCallFrame(frame);
603                 content.appendChild(urlElement);
604             }
605
606             var treeElement = new TreeElement(content);
607             parentTreeElement.appendChild(treeElement);
608         }
609     },
610
611     updateRepeatCount: function() {
612         if (!this.repeatCountElement) {
613             this.repeatCountElement = document.createElement("span");
614             this.repeatCountElement.className = "bubble";
615
616             this._element.insertBefore(this.repeatCountElement, this._element.firstChild);
617         }
618         this.repeatCountElement.textContent = this.repeatCount;
619     },
620
621     toString: function()
622     {
623         var sourceString;
624         switch (this.source) {
625             case WebInspector.ConsoleMessage.MessageSource.HTML:
626                 sourceString = "HTML";
627                 break;
628             case WebInspector.ConsoleMessage.MessageSource.XML:
629                 sourceString = "XML";
630                 break;
631             case WebInspector.ConsoleMessage.MessageSource.JS:
632                 sourceString = "JS";
633                 break;
634             case WebInspector.ConsoleMessage.MessageSource.Network:
635                 sourceString = "Network";
636                 break;
637             case WebInspector.ConsoleMessage.MessageSource.ConsoleAPI:
638                 sourceString = "ConsoleAPI";
639                 break;
640             case WebInspector.ConsoleMessage.MessageSource.Other:
641                 sourceString = "Other";
642                 break;
643         }
644
645         var typeString;
646         switch (this.type) {
647             case WebInspector.ConsoleMessage.MessageType.Log:
648                 typeString = "Log";
649                 break;
650             case WebInspector.ConsoleMessage.MessageType.Dir:
651                 typeString = "Dir";
652                 break;
653             case WebInspector.ConsoleMessage.MessageType.DirXML:
654                 typeString = "Dir XML";
655                 break;
656             case WebInspector.ConsoleMessage.MessageType.Trace:
657                 typeString = "Trace";
658                 break;
659             case WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed:
660             case WebInspector.ConsoleMessage.MessageType.StartGroup:
661                 typeString = "Start Group";
662                 break;
663             case WebInspector.ConsoleMessage.MessageType.EndGroup:
664                 typeString = "End Group";
665                 break;
666             case WebInspector.ConsoleMessage.MessageType.Assert:
667                 typeString = "Assert";
668                 break;
669             case WebInspector.ConsoleMessage.MessageType.Result:
670                 typeString = "Result";
671                 break;
672         }
673
674         return sourceString + " " + typeString + " " + this.levelString + ": " + this.formattedMessage.textContent + "\n" + this.url + " line " + this.line;
675     },
676
677     get text()
678     {
679         return this._messageText;
680     },
681
682     isEqual: function(msg)
683     {
684         if (!msg)
685             return false;
686
687         if (this._stackTrace) {
688             if (!msg._stackTrace)
689                 return false;
690             var l = this._stackTrace;
691             var r = msg._stackTrace;
692             for (var i = 0; i < l.length; i++) {
693                 if (l[i].url !== r[i].url ||
694                     l[i].functionName !== r[i].functionName ||
695                     l[i].lineNumber !== r[i].lineNumber ||
696                     l[i].columnNumber !== r[i].columnNumber)
697                     return false;
698             }
699         }
700
701         return (this.source === msg.source)
702             && (this.type === msg.type)
703             && (this.level === msg.level)
704             && (this.line === msg.line)
705             && (this.url === msg.url)
706             && (this.message === msg.message)
707             && (this._request === msg._request);
708     },
709
710     get stackTrace()
711     {
712         return this._stackTrace;
713     },
714
715     clone: function()
716     {
717         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);
718     },
719
720     get levelString()
721     {
722         switch (this.level) {
723             case WebInspector.ConsoleMessage.MessageLevel.Tip:
724                 return "Tip";
725             case WebInspector.ConsoleMessage.MessageLevel.Log:
726                 return "Log";
727             case WebInspector.ConsoleMessage.MessageLevel.Warning:
728                 return "Warning";
729             case WebInspector.ConsoleMessage.MessageLevel.Debug:
730                 return "Debug";
731             case WebInspector.ConsoleMessage.MessageLevel.Error:
732                 return "Error";
733         }
734     },
735
736     get clipboardPrefixString()
737     {
738         return "[" + this.levelString + "] ";
739     },
740
741     toClipboardString: function(isPrefixOptional)
742     {
743         var isTrace = this._shouldDumpStackTrace();
744
745         var clipboardString = "";
746         if (this._formattedMessage && !isTrace)
747             clipboardString = this._formattedMessage.querySelector("span").innerText;
748         else
749             clipboardString = this.type === WebInspector.ConsoleMessage.MessageType.Trace ? "console.trace()" : this._message || this._messageText;
750
751         if (!isPrefixOptional || this.enforcesClipboardPrefixString)
752             clipboardString = this.clipboardPrefixString + clipboardString;
753
754         if (isTrace) {
755             this._stackTrace.forEach(function(frame) {
756                 clipboardString += "\n\t" + (frame.functionName || WebInspector.UIString("(anonymous function)"));
757                 if (frame.url)
758                     clipboardString += " (" + WebInspector.displayNameForURL(frame.url) + ", line " + frame.lineNumber + ")";
759             });
760         } else {
761             var repeatString = this.repeatCount > 1 ? "x" + this.repeatCount : "";
762
763             var urlLine = "";
764             if (this.url) {
765                 var components = [WebInspector.displayNameForURL(this.url), "line " + this.line];
766                 if (repeatString)
767                     components.push(repeatString);
768                 urlLine = " (" + components.join(", ") + ")";
769             } else if (repeatString)
770                 urlLine = " (" + repeatString + ")";
771
772             if (urlLine) {
773                 var lines = clipboardString.split("\n");
774                 lines[0] += urlLine;
775                 clipboardString = lines.join("\n");
776             }
777         }
778
779         return clipboardString;
780     }
781 };
782
783 WebInspector.ConsoleMessageImpl.prototype.__proto__ = WebInspector.ConsoleMessage.prototype;