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