blob: a6e2c496d490730bc41e1371fa5ef0d24c846018 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2007, 2008 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 Computer, 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 * @implements {Console.ConsoleViewportElement}
32 * @unrestricted
33 */
34Console.ConsoleViewMessage = class {
35 /**
36 * @param {!SDK.ConsoleMessage} consoleMessage
37 * @param {!Components.Linkifier} linkifier
38 * @param {!ProductRegistry.BadgePool} badgePool
39 * @param {number} nestingLevel
Erik Luo840be6b2018-12-03 20:54:2740 * @param {function(!Common.Event)} onResize
Blink Reformat4c46d092018-04-07 15:32:3741 */
Erik Luo840be6b2018-12-03 20:54:2742 constructor(consoleMessage, linkifier, badgePool, nestingLevel, onResize) {
Blink Reformat4c46d092018-04-07 15:32:3743 this._message = consoleMessage;
44 this._linkifier = linkifier;
45 this._badgePool = badgePool;
46 this._repeatCount = 1;
47 this._closeGroupDecorationCount = 0;
48 this._nestingLevel = nestingLevel;
Erik Luo31c21f62018-12-13 03:39:3949 /** @type {!Array<{element: !Element, forceSelect: function()}>} */
Erik Luo383f21d2018-11-07 23:16:3750 this._selectableChildren = [];
Erik Luo840be6b2018-12-03 20:54:2751 this._messageResized = onResize;
Blink Reformat4c46d092018-04-07 15:32:3752
53 /** @type {?DataGrid.DataGrid} */
54 this._dataGrid = null;
55 this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter();
56 this._searchRegex = null;
57 /** @type {?UI.Icon} */
58 this._messageLevelIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:0059 this._traceExpanded = false;
60 /** @type {?function(boolean)} */
61 this._expandTrace = null;
Blink Reformat4c46d092018-04-07 15:32:3762 }
63
64 /**
65 * @override
66 * @return {!Element}
67 */
68 element() {
69 return this.toMessageElement();
70 }
71
72 /**
Blink Reformat4c46d092018-04-07 15:32:3773 * @override
74 */
75 wasShown() {
76 if (this._dataGrid)
77 this._dataGrid.updateWidths();
78 this._isVisible = true;
79 }
80
81 onResize() {
82 if (!this._isVisible)
83 return;
84 if (this._dataGrid)
85 this._dataGrid.onResize();
86 }
87
88 /**
89 * @override
90 */
91 willHide() {
92 this._isVisible = false;
Erik Luo4b002322018-07-30 21:23:3193 this._cachedHeight = this.element().offsetHeight;
Blink Reformat4c46d092018-04-07 15:32:3794 }
95
96 /**
97 * @return {number}
98 */
99 fastHeight() {
100 if (this._cachedHeight)
101 return this._cachedHeight;
102 // This value reflects the 18px min-height of .console-message, plus the
103 // 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
104 const defaultConsoleRowHeight = 19;
105 if (this._message.type === SDK.ConsoleMessage.MessageType.Table) {
106 const table = this._message.parameters[0];
107 if (table && table.preview)
108 return defaultConsoleRowHeight * table.preview.properties.length;
109 }
110 return defaultConsoleRowHeight;
111 }
112
113 /**
114 * @return {!SDK.ConsoleMessage}
115 */
116 consoleMessage() {
117 return this._message;
118 }
119
120 /**
121 * @return {!Element}
122 */
123 _buildTableMessage() {
124 const formattedMessage = createElementWithClass('span', 'source-code');
Erik Luo5976c8c2018-07-24 02:03:09125 this._anchorElement = this._buildMessageAnchor();
126 if (this._anchorElement)
127 formattedMessage.appendChild(this._anchorElement);
Blink Reformat4c46d092018-04-07 15:32:37128 const badgeElement = this._buildMessageBadge();
129 if (badgeElement)
130 formattedMessage.appendChild(badgeElement);
131
132 let table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null;
133 if (table)
134 table = this._parameterToRemoteObject(table);
135 if (!table || !table.preview)
Erik Luo16e3e382018-11-09 02:56:01136 return this._buildMessage();
Blink Reformat4c46d092018-04-07 15:32:37137
138 const rawValueColumnSymbol = Symbol('rawValueColumn');
139 const columnNames = [];
140 const preview = table.preview;
141 const rows = [];
142 for (let i = 0; i < preview.properties.length; ++i) {
143 const rowProperty = preview.properties[i];
144 let rowSubProperties;
145 if (rowProperty.valuePreview)
146 rowSubProperties = rowProperty.valuePreview.properties;
147 else if (rowProperty.value)
148 rowSubProperties = [{name: rawValueColumnSymbol, type: rowProperty.type, value: rowProperty.value}];
149 else
150 continue;
151
152 const rowValue = {};
153 const maxColumnsToRender = 20;
154 for (let j = 0; j < rowSubProperties.length; ++j) {
155 const cellProperty = rowSubProperties[j];
156 let columnRendered = columnNames.indexOf(cellProperty.name) !== -1;
157 if (!columnRendered) {
158 if (columnNames.length === maxColumnsToRender)
159 continue;
160 columnRendered = true;
161 columnNames.push(cellProperty.name);
162 }
163
164 if (columnRendered) {
165 const cellElement = this._renderPropertyPreviewOrAccessor(table, [rowProperty, cellProperty]);
166 cellElement.classList.add('console-message-nowrap-below');
167 rowValue[cellProperty.name] = cellElement;
168 }
169 }
170 rows.push([rowProperty.name, rowValue]);
171 }
172
173 const flatValues = [];
174 for (let i = 0; i < rows.length; ++i) {
175 const rowName = rows[i][0];
176 const rowValue = rows[i][1];
177 flatValues.push(rowName);
178 for (let j = 0; j < columnNames.length; ++j)
179 flatValues.push(rowValue[columnNames[j]]);
180 }
181 columnNames.unshift(Common.UIString('(index)'));
182 const columnDisplayNames = columnNames.map(name => name === rawValueColumnSymbol ? Common.UIString('Value') : name);
183
184 if (flatValues.length) {
185 this._dataGrid = DataGrid.SortableDataGrid.create(columnDisplayNames, flatValues);
186 this._dataGrid.setStriped(true);
187
188 const formattedResult = createElementWithClass('span', 'console-message-text');
189 const tableElement = formattedResult.createChild('div', 'console-message-formatted-table');
190 const dataGridContainer = tableElement.createChild('span');
191 tableElement.appendChild(this._formatParameter(table, true, false));
192 dataGridContainer.appendChild(this._dataGrid.element);
193 formattedMessage.appendChild(formattedResult);
194 this._dataGrid.renderInline();
195 }
196 return formattedMessage;
197 }
198
199 /**
200 * @return {!Element}
201 */
202 _buildMessage() {
203 let messageElement;
204 let messageText = this._message.messageText;
205 if (this._message.source === SDK.ConsoleMessage.MessageSource.ConsoleAPI) {
206 switch (this._message.type) {
207 case SDK.ConsoleMessage.MessageType.Trace:
208 messageElement = this._format(this._message.parameters || ['console.trace']);
209 break;
210 case SDK.ConsoleMessage.MessageType.Clear:
211 messageElement = createElementWithClass('span', 'console-info');
212 if (Common.moduleSetting('preserveConsoleLog').get())
213 messageElement.textContent = Common.UIString('console.clear() was prevented due to \'Preserve log\'');
214 else
215 messageElement.textContent = Common.UIString('Console was cleared');
216 messageElement.title =
Lorne Mitchell81a1a972019-03-06 18:13:13217 ls`Clear all messages with ${UI.shortcutRegistry.shortcutTitleForAction('console.clear')}`;
Blink Reformat4c46d092018-04-07 15:32:37218 break;
Blink Reformat4c46d092018-04-07 15:32:37219 case SDK.ConsoleMessage.MessageType.Dir: {
220 const obj = this._message.parameters ? this._message.parameters[0] : undefined;
221 const args = ['%O', obj];
222 messageElement = this._format(args);
223 break;
224 }
225 case SDK.ConsoleMessage.MessageType.Profile:
226 case SDK.ConsoleMessage.MessageType.ProfileEnd:
227 messageElement = this._format([messageText]);
228 break;
Pavel Feldman9f0f0a32018-12-18 02:09:13229 case SDK.ConsoleMessage.MessageType.Assert:
230 this._messagePrefix = ls`Assertion failed: `;
231 // Fall through.
Blink Reformat4c46d092018-04-07 15:32:37232 default: {
233 if (this._message.parameters && this._message.parameters.length === 1 &&
234 this._message.parameters[0].type === 'string')
235 messageElement = this._tryFormatAsError(/** @type {string} */ (this._message.parameters[0].value));
236 const args = this._message.parameters || [messageText];
237 messageElement = messageElement || this._format(args);
238 }
239 }
240 } else {
Erik Luofc2214f2018-11-21 19:54:58241 if (this._message.source === SDK.ConsoleMessage.MessageSource.Network) {
242 messageElement = this._formatAsNetworkRequest() || this._format([messageText]);
243 } else {
Blink Reformat4c46d092018-04-07 15:32:37244 const messageInParameters =
245 this._message.parameters && messageText === /** @type {string} */ (this._message.parameters[0]);
246 if (this._message.source === SDK.ConsoleMessage.MessageSource.Violation)
247 messageText = Common.UIString('[Violation] %s', messageText);
248 else if (this._message.source === SDK.ConsoleMessage.MessageSource.Intervention)
249 messageText = Common.UIString('[Intervention] %s', messageText);
250 else if (this._message.source === SDK.ConsoleMessage.MessageSource.Deprecation)
251 messageText = Common.UIString('[Deprecation] %s', messageText);
252 const args = this._message.parameters || [messageText];
253 if (messageInParameters)
254 args[0] = messageText;
255 messageElement = this._format(args);
256 }
257 }
258 messageElement.classList.add('console-message-text');
259
260 const formattedMessage = createElementWithClass('span', 'source-code');
Erik Luo5976c8c2018-07-24 02:03:09261 this._anchorElement = this._buildMessageAnchor();
262 if (this._anchorElement)
263 formattedMessage.appendChild(this._anchorElement);
Blink Reformat4c46d092018-04-07 15:32:37264 const badgeElement = this._buildMessageBadge();
265 if (badgeElement)
266 formattedMessage.appendChild(badgeElement);
267 formattedMessage.appendChild(messageElement);
268 return formattedMessage;
269 }
270
271 /**
272 * @return {?Element}
273 */
Erik Luofc2214f2018-11-21 19:54:58274 _formatAsNetworkRequest() {
275 const request = SDK.NetworkLog.requestForConsoleMessage(this._message);
276 if (!request)
277 return null;
278 const messageElement = createElement('span');
279 if (this._message.level === SDK.ConsoleMessage.MessageLevel.Error) {
280 messageElement.createTextChild(request.requestMethod + ' ');
Erik Luo182bece2018-11-29 03:15:22281 const linkElement = Components.Linkifier.linkifyRevealable(request, request.url(), request.url());
282 // Focus is handled by the viewport.
283 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39284 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22285 messageElement.appendChild(linkElement);
Erik Luofc2214f2018-11-21 19:54:58286 if (request.failed)
287 messageElement.createTextChildren(' ', request.localizedFailDescription);
288 if (request.statusCode !== 0)
289 messageElement.createTextChildren(' ', String(request.statusCode));
290 if (request.statusText)
291 messageElement.createTextChildren(' (', request.statusText, ')');
292 } else {
Erik Luoad5f3942019-03-26 20:53:44293 const messageText = this._message.messageText;
294 const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
295 let linkElement;
296 if (url === request.url()) {
297 linkElement = Components.Linkifier.linkifyRevealable(
298 /** @type {!SDK.NetworkRequest} */ (request), url, request.url());
299 } else {
300 linkElement = Components.Linkifier.linkifyURL(url, {text, lineNumber, columnNumber});
301 }
Erik Luo182bece2018-11-29 03:15:22302 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39303 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22304 return linkElement;
305 });
Erik Luofc2214f2018-11-21 19:54:58306 messageElement.appendChild(fragment);
307 }
308 return messageElement;
309 }
310
311 /**
312 * @return {?Element}
313 */
Blink Reformat4c46d092018-04-07 15:32:37314 _buildMessageAnchor() {
315 let anchorElement = null;
316 if (this._message.scriptId) {
317 anchorElement = this._linkifyScriptId(
318 this._message.scriptId, this._message.url || '', this._message.line, this._message.column);
319 } else if (this._message.stackTrace && this._message.stackTrace.callFrames.length) {
320 anchorElement = this._linkifyStackTraceTopFrame(this._message.stackTrace);
321 } else if (this._message.url && this._message.url !== 'undefined') {
322 anchorElement = this._linkifyLocation(this._message.url, this._message.line, this._message.column);
323 }
324
325 // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
326 if (anchorElement) {
327 const anchorWrapperElement = createElementWithClass('span', 'console-message-anchor');
328 anchorWrapperElement.appendChild(anchorElement);
329 anchorWrapperElement.createTextChild(' ');
330 return anchorWrapperElement;
331 }
332 return null;
333 }
334
335 /**
336 * @return {?Element}
337 */
338 _buildMessageBadge() {
339 const badgeElement = this._badgeElement();
340 if (!badgeElement)
341 return null;
342 badgeElement.classList.add('console-message-badge');
343 return badgeElement;
344 }
345
346 /**
347 * @return {?Element}
348 */
349 _badgeElement() {
350 if (this._message._url)
351 return this._badgePool.badgeForURL(new Common.ParsedURL(this._message._url));
352 if (this._message.stackTrace) {
353 let stackTrace = this._message.stackTrace;
354 while (stackTrace) {
355 for (const callFrame of this._message.stackTrace.callFrames) {
356 if (callFrame.url)
357 return this._badgePool.badgeForURL(new Common.ParsedURL(callFrame.url));
358 }
359 stackTrace = stackTrace.parent;
360 }
361 }
362 if (!this._message.executionContextId)
363 return null;
364 const runtimeModel = this._message.runtimeModel();
365 if (!runtimeModel)
366 return null;
367 const executionContext = runtimeModel.executionContext(this._message.executionContextId);
368 if (!executionContext || !executionContext.frameId)
369 return null;
370 const resourceTreeModel = executionContext.target().model(SDK.ResourceTreeModel);
371 if (!resourceTreeModel)
372 return null;
373 const frame = resourceTreeModel.frameForId(executionContext.frameId);
374 if (!frame || !frame.parentFrame)
375 return null;
376 return this._badgePool.badgeForFrame(frame);
377 }
378
379 /**
380 * @return {!Element}
381 */
382 _buildMessageWithStackTrace() {
383 const toggleElement = createElementWithClass('div', 'console-message-stack-trace-toggle');
384 const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
385
386 const messageElement = this._buildMessage();
387 const icon = UI.Icon.create('smallicon-triangle-right', 'console-message-expand-icon');
388 const clickableElement = contentElement.createChild('div');
389 clickableElement.appendChild(icon);
Erik Luob5bfff42018-09-20 02:52:39390 // Intercept focus to avoid highlight on click.
391 clickableElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37392
393 clickableElement.appendChild(messageElement);
394 const stackTraceElement = contentElement.createChild('div');
395 const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
396 this._message.runtimeModel().target(), this._linkifier, this._message.stackTrace);
Erik Luo182bece2018-11-29 03:15:22397 stackTraceElement.appendChild(stackTracePreview.element);
398 for (const linkElement of stackTracePreview.links) {
399 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39400 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22401 }
Blink Reformat4c46d092018-04-07 15:32:37402 stackTraceElement.classList.add('hidden');
Erik Luo8ef5d0c2018-09-25 21:16:00403 this._expandTrace = expand => {
Blink Reformat4c46d092018-04-07 15:32:37404 icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right');
405 stackTraceElement.classList.toggle('hidden', !expand);
Erik Luo8ef5d0c2018-09-25 21:16:00406 this._traceExpanded = expand;
407 };
Blink Reformat4c46d092018-04-07 15:32:37408
409 /**
Erik Luo8ef5d0c2018-09-25 21:16:00410 * @this {!Console.ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37411 * @param {?Event} event
412 */
413 function toggleStackTrace(event) {
Erik Luob5bfff42018-09-20 02:52:39414 if (UI.isEditing() || contentElement.hasSelection())
Blink Reformat4c46d092018-04-07 15:32:37415 return;
Erik Luo8ef5d0c2018-09-25 21:16:00416 this._expandTrace(stackTraceElement.classList.contains('hidden'));
Blink Reformat4c46d092018-04-07 15:32:37417 event.consume();
418 }
419
Erik Luo8ef5d0c2018-09-25 21:16:00420 clickableElement.addEventListener('click', toggleStackTrace.bind(this), false);
Blink Reformat4c46d092018-04-07 15:32:37421 if (this._message.type === SDK.ConsoleMessage.MessageType.Trace)
Erik Luo8ef5d0c2018-09-25 21:16:00422 this._expandTrace(true);
Blink Reformat4c46d092018-04-07 15:32:37423
Erik Luo8ef5d0c2018-09-25 21:16:00424 toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true);
Blink Reformat4c46d092018-04-07 15:32:37425 return toggleElement;
426 }
427
428 /**
429 * @param {string} url
430 * @param {number} lineNumber
431 * @param {number} columnNumber
432 * @return {?Element}
433 */
434 _linkifyLocation(url, lineNumber, columnNumber) {
435 if (!this._message.runtimeModel())
436 return null;
437 return this._linkifier.linkifyScriptLocation(
438 this._message.runtimeModel().target(), null, url, lineNumber, columnNumber);
439 }
440
441 /**
442 * @param {!Protocol.Runtime.StackTrace} stackTrace
443 * @return {?Element}
444 */
445 _linkifyStackTraceTopFrame(stackTrace) {
446 if (!this._message.runtimeModel())
447 return null;
448 return this._linkifier.linkifyStackTraceTopFrame(this._message.runtimeModel().target(), stackTrace);
449 }
450
451 /**
452 * @param {string} scriptId
453 * @param {string} url
454 * @param {number} lineNumber
455 * @param {number} columnNumber
456 * @return {?Element}
457 */
458 _linkifyScriptId(scriptId, url, lineNumber, columnNumber) {
459 if (!this._message.runtimeModel())
460 return null;
461 return this._linkifier.linkifyScriptLocation(
462 this._message.runtimeModel().target(), scriptId, url, lineNumber, columnNumber);
463 }
464
465 /**
466 * @param {!SDK.RemoteObject|!Protocol.Runtime.RemoteObject|string} parameter
467 * @return {!SDK.RemoteObject}
468 */
469 _parameterToRemoteObject(parameter) {
470 if (parameter instanceof SDK.RemoteObject)
471 return parameter;
472 const runtimeModel = this._message.runtimeModel();
473 if (!runtimeModel)
474 return SDK.RemoteObject.fromLocalObject(parameter);
475 if (typeof parameter === 'object')
476 return runtimeModel.createRemoteObject(parameter);
477 return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
478 }
479
480 /**
481 * @param {!Array.<!SDK.RemoteObject|string>} rawParameters
482 * @return {!Element}
483 */
484 _format(rawParameters) {
485 // This node is used like a Builder. Values are continually appended onto it.
486 const formattedResult = createElement('span');
Pavel Feldman9f0f0a32018-12-18 02:09:13487 if (this._messagePrefix)
488 formattedResult.createChild('span').textContent = this._messagePrefix;
Blink Reformat4c46d092018-04-07 15:32:37489 if (!rawParameters.length)
490 return formattedResult;
491
492 // Formatting code below assumes that parameters are all wrappers whereas frontend console
493 // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
494 // FIXME: Only pass runtime wrappers here.
495 let parameters = [];
496 for (let i = 0; i < rawParameters.length; ++i)
497 parameters[i] = this._parameterToRemoteObject(rawParameters[i]);
498
499 // There can be string log and string eval result. We distinguish between them based on message type.
500 const shouldFormatMessage =
501 SDK.RemoteObject.type((/** @type {!Array.<!SDK.RemoteObject>} **/ (parameters))[0]) === 'string' &&
502 (this._message.type !== SDK.ConsoleMessage.MessageType.Result ||
503 this._message.level === SDK.ConsoleMessage.MessageLevel.Error);
504
505 // Multiple parameters with the first being a format string. Save unused substitutions.
506 if (shouldFormatMessage) {
507 const result = this._formatWithSubstitutionString(
508 /** @type {string} **/ (parameters[0].description), parameters.slice(1), formattedResult);
509 parameters = result.unusedSubstitutions;
510 if (parameters.length)
511 formattedResult.createTextChild(' ');
512 }
513
514 // Single parameter, or unused substitutions from above.
515 for (let i = 0; i < parameters.length; ++i) {
516 // Inline strings when formatting.
517 if (shouldFormatMessage && parameters[i].type === 'string')
Erik Luo383f21d2018-11-07 23:16:37518 formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description));
Blink Reformat4c46d092018-04-07 15:32:37519 else
520 formattedResult.appendChild(this._formatParameter(parameters[i], false, true));
521 if (i < parameters.length - 1)
522 formattedResult.createTextChild(' ');
523 }
524 return formattedResult;
525 }
526
527 /**
528 * @param {!SDK.RemoteObject} output
529 * @param {boolean=} forceObjectFormat
530 * @param {boolean=} includePreview
531 * @return {!Element}
532 */
533 _formatParameter(output, forceObjectFormat, includePreview) {
534 if (output.customPreview())
535 return (new ObjectUI.CustomPreviewComponent(output)).element;
536
537 const type = forceObjectFormat ? 'object' : (output.subtype || output.type);
538 let element;
539 switch (type) {
540 case 'error':
541 element = this._formatParameterAsError(output);
542 break;
543 case 'function':
544 element = this._formatParameterAsFunction(output, includePreview);
545 break;
546 case 'array':
547 case 'arraybuffer':
548 case 'blob':
549 case 'dataview':
550 case 'generator':
551 case 'iterator':
552 case 'map':
553 case 'object':
554 case 'promise':
555 case 'proxy':
556 case 'set':
557 case 'typedarray':
558 case 'weakmap':
559 case 'weakset':
560 element = this._formatParameterAsObject(output, includePreview);
561 break;
562 case 'node':
563 element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false);
564 break;
565 case 'string':
566 element = this._formatParameterAsString(output);
567 break;
568 case 'boolean':
569 case 'date':
570 case 'null':
571 case 'number':
572 case 'regexp':
573 case 'symbol':
574 case 'undefined':
575 case 'bigint':
576 element = this._formatParameterAsValue(output);
577 break;
578 default:
579 element = this._formatParameterAsValue(output);
580 console.error('Tried to format remote object of unknown type.');
581 }
582 element.classList.add('object-value-' + type);
583 element.classList.add('source-code');
584 return element;
585 }
586
587 /**
588 * @param {!SDK.RemoteObject} obj
589 * @return {!Element}
590 */
591 _formatParameterAsValue(obj) {
592 const result = createElement('span');
593 const description = obj.description || '';
594 if (description.length > Console.ConsoleViewMessage._MaxTokenizableStringLength)
Erik Luo5fb33bd2018-06-04 23:23:52595 result.appendChild(UI.createExpandableText(description, Console.ConsoleViewMessage._LongStringVisibleLength));
Blink Reformat4c46d092018-04-07 15:32:37596 else
597 result.createTextChild(description);
598 if (obj.objectId)
599 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false);
600 return result;
601 }
602
603 /**
604 * @param {!SDK.RemoteObject} obj
605 * @param {boolean=} includePreview
606 * @return {!Element}
607 */
608 _formatParameterAsObject(obj, includePreview) {
609 const titleElement = createElementWithClass('span', 'console-object');
610 if (includePreview && obj.preview) {
611 titleElement.classList.add('console-object-preview');
612 this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
613 } else if (obj.type === 'function') {
614 const functionElement = titleElement.createChild('span');
615 ObjectUI.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
616 titleElement.classList.add('object-value-function');
617 } else {
618 titleElement.createTextChild(obj.description || '');
619 }
620
621 if (!obj.hasChildren || obj.customPreview())
622 return titleElement;
623
624 const note = titleElement.createChild('span', 'object-state-note info-note');
625 if (this._message.type === SDK.ConsoleMessage.MessageType.QueryObjectResult)
626 note.title = ls`This value will not be collected until console is cleared.`;
627 else
628 note.title = ls`Value below was evaluated just now.`;
629
630 const section = new ObjectUI.ObjectPropertiesSection(obj, titleElement, this._linkifier);
631 section.element.classList.add('console-view-object-properties-section');
632 section.enableContextMenu();
Erik Luocc14b812018-11-03 01:33:09633 section.setShowSelectionOnKeyboardFocus(true, true);
Erik Luo383f21d2018-11-07 23:16:37634 this._selectableChildren.push(section);
Erik Luo840be6b2018-12-03 20:54:27635 section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
636 section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
637 section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
Blink Reformat4c46d092018-04-07 15:32:37638 return section.element;
639 }
640
641 /**
642 * @param {!SDK.RemoteObject} func
643 * @param {boolean=} includePreview
644 * @return {!Element}
645 */
646 _formatParameterAsFunction(func, includePreview) {
647 const result = createElement('span');
648 SDK.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this));
649 return result;
650
651 /**
652 * @param {!SDK.RemoteObject} targetFunction
653 * @this {Console.ConsoleViewMessage}
654 */
655 function formatTargetFunction(targetFunction) {
656 const functionElement = createElement('span');
Joey Arhard78a58f2018-12-05 01:59:45657 const promise = ObjectUI.ObjectPropertiesSection.formatObjectAsFunction(
658 targetFunction, functionElement, true, includePreview);
Blink Reformat4c46d092018-04-07 15:32:37659 result.appendChild(functionElement);
660 if (targetFunction !== func) {
661 const note = result.createChild('span', 'object-info-state-note');
662 note.title = Common.UIString('Function was resolved from bound function.');
663 }
664 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false);
Joey Arhard78a58f2018-12-05 01:59:45665 promise.then(() => this._formattedParameterAsFunctionForTest());
Blink Reformat4c46d092018-04-07 15:32:37666 }
667 }
668
Joey Arhard78a58f2018-12-05 01:59:45669 _formattedParameterAsFunctionForTest() {
670 }
671
Blink Reformat4c46d092018-04-07 15:32:37672 /**
673 * @param {!SDK.RemoteObject} obj
674 * @param {!Event} event
675 */
676 _contextMenuEventFired(obj, event) {
677 const contextMenu = new UI.ContextMenu(event);
678 contextMenu.appendApplicableItems(obj);
679 contextMenu.show();
680 }
681
682 /**
683 * @param {?SDK.RemoteObject} object
684 * @param {!Array.<!Protocol.Runtime.PropertyPreview>} propertyPath
685 * @return {!Element}
686 */
687 _renderPropertyPreviewOrAccessor(object, propertyPath) {
688 const property = propertyPath.peekLast();
689 if (property.type === 'accessor')
690 return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name), false);
691 return this._previewFormatter.renderPropertyPreview(
692 property.type, /** @type {string} */ (property.subtype), property.value);
693 }
694
695 /**
696 * @param {!SDK.RemoteObject} remoteObject
697 * @return {!Element}
698 */
699 _formatParameterAsNode(remoteObject) {
700 const result = createElement('span');
701
702 const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel);
703 if (!domModel)
704 return result;
Erik Luo54fdd912018-11-01 17:57:01705 domModel.pushObjectAsNodeToFrontend(remoteObject).then(async node => {
Blink Reformat4c46d092018-04-07 15:32:37706 if (!node) {
707 result.appendChild(this._formatParameterAsObject(remoteObject, false));
708 return;
709 }
Erik Luo54fdd912018-11-01 17:57:01710 const renderResult = await UI.Renderer.render(/** @type {!Object} */ (node));
Erik Luofc6a6302018-11-02 06:48:52711 if (renderResult) {
Erik Luo840be6b2018-12-03 20:54:27712 if (renderResult.tree) {
Erik Luo383f21d2018-11-07 23:16:37713 this._selectableChildren.push(renderResult.tree);
Erik Luo840be6b2018-12-03 20:54:27714 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
715 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
716 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
717 }
Erik Luofc6a6302018-11-02 06:48:52718 result.appendChild(renderResult.node);
719 } else {
720 result.appendChild(this._formatParameterAsObject(remoteObject, false));
721 }
Erik Luo54fdd912018-11-01 17:57:01722 this._formattedParameterAsNodeForTest();
Blink Reformat4c46d092018-04-07 15:32:37723 });
724
725 return result;
726 }
727
728 _formattedParameterAsNodeForTest() {
729 }
730
731 /**
732 * @param {!SDK.RemoteObject} output
733 * @return {!Element}
734 */
735 _formatParameterAsString(output) {
736 const span = createElement('span');
Erik Luo383f21d2018-11-07 23:16:37737 span.appendChild(this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37738
739 const result = createElement('span');
740 result.createChild('span', 'object-value-string-quote').textContent = '"';
741 result.appendChild(span);
742 result.createChild('span', 'object-value-string-quote').textContent = '"';
743 return result;
744 }
745
746 /**
747 * @param {!SDK.RemoteObject} output
748 * @return {!Element}
749 */
750 _formatParameterAsError(output) {
751 const result = createElement('span');
752 const errorSpan = this._tryFormatAsError(output.description || '');
Erik Luo383f21d2018-11-07 23:16:37753 result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37754 return result;
755 }
756
757 /**
758 * @param {!SDK.RemoteObject} output
759 * @return {!Element}
760 */
761 _formatAsArrayEntry(output) {
762 return this._previewFormatter.renderPropertyPreview(output.type, output.subtype, output.description);
763 }
764
765 /**
766 * @param {?SDK.RemoteObject} object
767 * @param {!Array.<string>} propertyPath
768 * @param {boolean} isArrayEntry
769 * @return {!Element}
770 */
771 _formatAsAccessorProperty(object, propertyPath, isArrayEntry) {
772 const rootElement = ObjectUI.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
773 object, propertyPath, onInvokeGetterClick.bind(this));
774
775 /**
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18776 * @param {!SDK.CallFunctionResult} result
Blink Reformat4c46d092018-04-07 15:32:37777 * @this {Console.ConsoleViewMessage}
778 */
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18779 function onInvokeGetterClick(result) {
780 const wasThrown = result.wasThrown;
781 const object = result.object;
782 if (!object)
Blink Reformat4c46d092018-04-07 15:32:37783 return;
784 rootElement.removeChildren();
785 if (wasThrown) {
786 const element = rootElement.createChild('span');
787 element.textContent = Common.UIString('<exception>');
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18788 element.title = /** @type {string} */ (object.description);
Blink Reformat4c46d092018-04-07 15:32:37789 } else if (isArrayEntry) {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18790 rootElement.appendChild(this._formatAsArrayEntry(object));
Blink Reformat4c46d092018-04-07 15:32:37791 } else {
792 // Make a PropertyPreview from the RemoteObject similar to the backend logic.
793 const maxLength = 100;
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18794 const type = object.type;
795 const subtype = object.subtype;
Blink Reformat4c46d092018-04-07 15:32:37796 let description = '';
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18797 if (type !== 'function' && object.description) {
Blink Reformat4c46d092018-04-07 15:32:37798 if (type === 'string' || subtype === 'regexp')
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18799 description = object.description.trimMiddle(maxLength);
Blink Reformat4c46d092018-04-07 15:32:37800 else
Tim van der Lippeffa78622019-09-16 12:07:12801 description = object.description.trimEndWithMaxLength(maxLength);
Blink Reformat4c46d092018-04-07 15:32:37802 }
803 rootElement.appendChild(this._previewFormatter.renderPropertyPreview(type, subtype, description));
804 }
805 }
806
807 return rootElement;
808 }
809
810 /**
811 * @param {string} format
812 * @param {!Array.<!SDK.RemoteObject>} parameters
813 * @param {!Element} formattedResult
814 */
815 _formatWithSubstitutionString(format, parameters, formattedResult) {
816 const formatters = {};
817
818 /**
819 * @param {boolean} force
820 * @param {boolean} includePreview
821 * @param {!SDK.RemoteObject} obj
822 * @return {!Element}
823 * @this {Console.ConsoleViewMessage}
824 */
825 function parameterFormatter(force, includePreview, obj) {
826 return this._formatParameter(obj, force, includePreview);
827 }
828
829 function stringFormatter(obj) {
830 return obj.description;
831 }
832
833 function floatFormatter(obj) {
834 if (typeof obj.value !== 'number')
835 return 'NaN';
836 return obj.value;
837 }
838
839 function integerFormatter(obj) {
Alexey Kozyatinskiybeb38de2018-08-10 18:54:19840 if (obj.type === 'bigint')
841 return obj.description;
Blink Reformat4c46d092018-04-07 15:32:37842 if (typeof obj.value !== 'number')
843 return 'NaN';
844 return Math.floor(obj.value);
845 }
846
847 function bypassFormatter(obj) {
848 return (obj instanceof Node) ? obj : '';
849 }
850
851 let currentStyle = null;
852 function styleFormatter(obj) {
853 currentStyle = {};
854 const buffer = createElement('span');
855 buffer.setAttribute('style', obj.description);
856 for (let i = 0; i < buffer.style.length; i++) {
857 const property = buffer.style[i];
858 if (isWhitelistedProperty(property))
859 currentStyle[property] = buffer.style[property];
860 }
861 }
862
863 function isWhitelistedProperty(property) {
864 // Make sure that allowed properties do not interfere with link visibility.
865 const prefixes = [
866 'background', 'border', 'color', 'font', 'line', 'margin', 'padding', 'text', '-webkit-background',
867 '-webkit-border', '-webkit-font', '-webkit-margin', '-webkit-padding', '-webkit-text'
868 ];
869 for (let i = 0; i < prefixes.length; i++) {
870 if (property.startsWith(prefixes[i]))
871 return true;
872 }
873 return false;
874 }
875
876 // Firebug uses %o for formatting objects.
877 formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */);
878 formatters.s = stringFormatter;
879 formatters.f = floatFormatter;
880 // Firebug allows both %i and %d for formatting integers.
881 formatters.i = integerFormatter;
882 formatters.d = integerFormatter;
883
884 // Firebug uses %c for styling the message.
885 formatters.c = styleFormatter;
886
887 // Support %O to force object formatting, instead of the type-based %o formatting.
888 formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */);
889
890 formatters._ = bypassFormatter;
891
892 /**
893 * @param {!Element} a
894 * @param {*} b
895 * @this {!Console.ConsoleViewMessage}
Erik Luo17926392018-05-17 22:06:12896 * @return {!Element}
Blink Reformat4c46d092018-04-07 15:32:37897 */
898 function append(a, b) {
899 if (b instanceof Node) {
900 a.appendChild(b);
Erik Luo17926392018-05-17 22:06:12901 return a;
902 }
903 if (typeof b === 'undefined')
904 return a;
905 if (!currentStyle) {
Erik Luo383f21d2018-11-07 23:16:37906 a.appendChild(this._linkifyStringAsFragment(String(b)));
Erik Luo17926392018-05-17 22:06:12907 return a;
908 }
909 const lines = String(b).split('\n');
910 for (let i = 0; i < lines.length; i++) {
911 const line = lines[i];
Erik Luo383f21d2018-11-07 23:16:37912 const lineFragment = this._linkifyStringAsFragment(line);
Erik Luo17926392018-05-17 22:06:12913 const wrapper = createElement('span');
914 wrapper.style.setProperty('contain', 'paint');
915 wrapper.style.setProperty('display', 'inline-block');
916 wrapper.style.setProperty('max-width', '100%');
917 wrapper.appendChild(lineFragment);
918 applyCurrentStyle(wrapper);
919 for (const child of wrapper.children) {
920 if (child.classList.contains('devtools-link'))
921 this._applyForcedVisibleStyle(child);
Blink Reformat4c46d092018-04-07 15:32:37922 }
Erik Luo17926392018-05-17 22:06:12923 a.appendChild(wrapper);
924 if (i < lines.length - 1)
925 a.appendChild(createElement('br'));
Blink Reformat4c46d092018-04-07 15:32:37926 }
927 return a;
928 }
929
930 /**
931 * @param {!Element} element
932 */
933 function applyCurrentStyle(element) {
934 for (const key in currentStyle)
935 element.style[key] = currentStyle[key];
936 }
937
938 // String.format does treat formattedResult like a Builder, result is an object.
939 return String.format(format, parameters, formatters, formattedResult, append.bind(this));
940 }
941
942 /**
943 * @param {!Element} element
944 */
945 _applyForcedVisibleStyle(element) {
946 element.style.setProperty('-webkit-text-stroke', '0', 'important');
947 element.style.setProperty('text-decoration', 'underline', 'important');
948
949 const themedColor = UI.themeSupport.patchColorText('rgb(33%, 33%, 33%)', UI.ThemeSupport.ColorUsage.Foreground);
950 element.style.setProperty('color', themedColor, 'important');
951
952 let backgroundColor = 'hsl(0, 0%, 100%)';
953 if (this._message.level === SDK.ConsoleMessage.MessageLevel.Error)
954 backgroundColor = 'hsl(0, 100%, 97%)';
955 else if (this._message.level === SDK.ConsoleMessage.MessageLevel.Warning || this._shouldRenderAsWarning())
956 backgroundColor = 'hsl(50, 100%, 95%)';
957 const themedBackgroundColor =
958 UI.themeSupport.patchColorText(backgroundColor, UI.ThemeSupport.ColorUsage.Background);
959 element.style.setProperty('background-color', themedBackgroundColor, 'important');
960 }
961
962 /**
963 * @return {boolean}
964 */
965 matchesFilterRegex(regexObject) {
966 regexObject.lastIndex = 0;
Erik Luo5976c8c2018-07-24 02:03:09967 const contentElement = this.contentElement();
968 const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : '';
969 return (anchorText && regexObject.test(anchorText.trim())) ||
970 regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
Blink Reformat4c46d092018-04-07 15:32:37971 }
972
973 /**
974 * @param {string} filter
975 * @return {boolean}
976 */
977 matchesFilterText(filter) {
978 const text = this.contentElement().deepTextContent();
979 return text.toLowerCase().includes(filter.toLowerCase());
980 }
981
982 updateTimestamp() {
983 if (!this._contentElement)
984 return;
985
986 if (Common.moduleSetting('consoleTimestampsEnabled').get()) {
987 if (!this._timestampElement)
988 this._timestampElement = createElementWithClass('span', 'console-timestamp');
Rayan Kansoaca06e72019-03-27 11:57:06989 this._timestampElement.textContent = UI.formatTimestamp(this._message.timestamp, false) + ' ';
990 this._timestampElement.title = UI.formatTimestamp(this._message.timestamp, true);
Blink Reformat4c46d092018-04-07 15:32:37991 this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild);
992 } else if (this._timestampElement) {
993 this._timestampElement.remove();
994 delete this._timestampElement;
995 }
Blink Reformat4c46d092018-04-07 15:32:37996 }
997
998 /**
999 * @return {number}
1000 */
1001 nestingLevel() {
1002 return this._nestingLevel;
1003 }
1004
1005 /**
1006 * @param {boolean} inSimilarGroup
1007 * @param {boolean=} isLast
1008 */
1009 setInSimilarGroup(inSimilarGroup, isLast) {
1010 this._inSimilarGroup = inSimilarGroup;
1011 this._lastInSimilarGroup = inSimilarGroup && !!isLast;
1012 if (this._similarGroupMarker && !inSimilarGroup) {
1013 this._similarGroupMarker.remove();
1014 this._similarGroupMarker = null;
1015 } else if (this._element && !this._similarGroupMarker && inSimilarGroup) {
1016 this._similarGroupMarker = createElementWithClass('div', 'nesting-level-marker');
1017 this._element.insertBefore(this._similarGroupMarker, this._element.firstChild);
1018 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1019 }
1020 }
1021
1022 /**
1023 * @return {boolean}
1024 */
1025 isLastInSimilarGroup() {
1026 return this._inSimilarGroup && this._lastInSimilarGroup;
1027 }
1028
1029 resetCloseGroupDecorationCount() {
1030 if (!this._closeGroupDecorationCount)
1031 return;
1032 this._closeGroupDecorationCount = 0;
1033 this._updateCloseGroupDecorations();
1034 }
1035
1036 incrementCloseGroupDecorationCount() {
1037 ++this._closeGroupDecorationCount;
1038 this._updateCloseGroupDecorations();
1039 }
1040
1041 _updateCloseGroupDecorations() {
1042 if (!this._nestingLevelMarkers)
1043 return;
1044 for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) {
1045 const marker = this._nestingLevelMarkers[i];
1046 marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount);
1047 }
1048 }
1049
1050 /**
Erik Luo0b8282e2018-10-08 20:37:461051 * @return {number}
1052 */
1053 _focusedChildIndex() {
Erik Luo383f21d2018-11-07 23:16:371054 if (!this._selectableChildren.length)
Erik Luo0b8282e2018-10-08 20:37:461055 return -1;
Erik Luo383f21d2018-11-07 23:16:371056 return this._selectableChildren.findIndex(child => child.element.hasFocus());
Erik Luo0b8282e2018-10-08 20:37:461057 }
1058
1059 /**
Erik Luo8ef5d0c2018-09-25 21:16:001060 * @param {!Event} event
1061 */
1062 _onKeyDown(event) {
1063 if (UI.isEditing() || !this._element.hasFocus() || this._element.hasSelection())
1064 return;
1065 if (this.maybeHandleOnKeyDown(event))
1066 event.consume(true);
1067 }
1068
1069 /**
1070 * @protected
1071 * @param {!Event} event
1072 */
1073 maybeHandleOnKeyDown(event) {
1074 // Handle trace expansion.
Erik Luo0b8282e2018-10-08 20:37:461075 const focusedChildIndex = this._focusedChildIndex();
1076 const isWrapperFocused = focusedChildIndex === -1;
1077 if (this._expandTrace && isWrapperFocused) {
Erik Luo8ef5d0c2018-09-25 21:16:001078 if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) {
1079 this._expandTrace(!this._traceExpanded);
1080 return true;
1081 }
1082 }
Erik Luo383f21d2018-11-07 23:16:371083 if (!this._selectableChildren.length)
Erik Luo0b8282e2018-10-08 20:37:461084 return false;
1085
1086 if (event.key === 'ArrowLeft') {
1087 this._element.focus();
1088 return true;
1089 }
1090 if (event.key === 'ArrowRight') {
Erik Luo182bece2018-11-29 03:15:221091 if (isWrapperFocused && this._selectNearestVisibleChild(0))
Erik Luo0b8282e2018-10-08 20:37:461092 return true;
Erik Luo0b8282e2018-10-08 20:37:461093 }
1094 if (event.key === 'ArrowUp') {
Erik Luo182bece2018-11-29 03:15:221095 const firstVisibleChild = this._nearestVisibleChild(0);
1096 if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
Erik Luo0b8282e2018-10-08 20:37:461097 this._element.focus();
1098 return true;
Erik Luo182bece2018-11-29 03:15:221099 } else if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461100 return true;
1101 }
1102 }
1103 if (event.key === 'ArrowDown') {
Erik Luo182bece2018-11-29 03:15:221104 if (isWrapperFocused && this._selectNearestVisibleChild(0))
Erik Luo0b8282e2018-10-08 20:37:461105 return true;
Erik Luo182bece2018-11-29 03:15:221106 if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1))
Erik Luo0b8282e2018-10-08 20:37:461107 return true;
Erik Luo0b8282e2018-10-08 20:37:461108 }
Erik Luo8ef5d0c2018-09-25 21:16:001109 return false;
1110 }
1111
Erik Luo182bece2018-11-29 03:15:221112 /**
1113 * @param {number} fromIndex
1114 * @param {boolean=} backwards
1115 * @return {boolean}
1116 */
1117 _selectNearestVisibleChild(fromIndex, backwards) {
1118 const nearestChild = this._nearestVisibleChild(fromIndex, backwards);
1119 if (nearestChild) {
Erik Luo31c21f62018-12-13 03:39:391120 nearestChild.forceSelect();
Erik Luo182bece2018-11-29 03:15:221121 return true;
1122 }
1123 return false;
1124 }
1125
1126 /**
1127 * @param {number} fromIndex
1128 * @param {boolean=} backwards
Erik Luo31c21f62018-12-13 03:39:391129 * @return {?{element: !Element, forceSelect: function()}}
Erik Luo182bece2018-11-29 03:15:221130 */
1131 _nearestVisibleChild(fromIndex, backwards) {
1132 const childCount = this._selectableChildren.length;
1133 if (fromIndex < 0 || fromIndex >= childCount)
1134 return null;
1135 const direction = backwards ? -1 : 1;
1136 let index = fromIndex;
1137
1138 while (!this._selectableChildren[index].element.offsetParent) {
1139 index += direction;
1140 if (index < 0 || index >= childCount)
1141 return null;
1142 }
1143 return this._selectableChildren[index];
1144 }
1145
Erik Luo0b8282e2018-10-08 20:37:461146 focusLastChildOrSelf() {
Erik Luo182bece2018-11-29 03:15:221147 if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */))
Erik Luo0b8282e2018-10-08 20:37:461148 this._element.focus();
1149 }
1150
1151 /**
Blink Reformat4c46d092018-04-07 15:32:371152 * @return {!Element}
1153 */
1154 contentElement() {
1155 if (this._contentElement)
1156 return this._contentElement;
1157
1158 const contentElement = createElementWithClass('div', 'console-message');
1159 if (this._messageLevelIcon)
1160 contentElement.appendChild(this._messageLevelIcon);
1161 this._contentElement = contentElement;
1162
1163 let formattedMessage;
1164 const shouldIncludeTrace = !!this._message.stackTrace &&
1165 (this._message.source === SDK.ConsoleMessage.MessageSource.Network ||
1166 this._message.source === SDK.ConsoleMessage.MessageSource.Violation ||
1167 this._message.level === SDK.ConsoleMessage.MessageLevel.Error ||
1168 this._message.level === SDK.ConsoleMessage.MessageLevel.Warning ||
1169 this._message.type === SDK.ConsoleMessage.MessageType.Trace);
1170 if (this._message.runtimeModel() && shouldIncludeTrace)
1171 formattedMessage = this._buildMessageWithStackTrace();
1172 else if (this._message.type === SDK.ConsoleMessage.MessageType.Table)
1173 formattedMessage = this._buildTableMessage();
1174 else
1175 formattedMessage = this._buildMessage();
1176 contentElement.appendChild(formattedMessage);
1177
1178 this.updateTimestamp();
1179 return this._contentElement;
1180 }
1181
1182 /**
1183 * @return {!Element}
1184 */
1185 toMessageElement() {
1186 if (this._element)
1187 return this._element;
1188
1189 this._element = createElement('div');
Pavel Feldmandb310912019-01-30 00:31:201190 this._element.tabIndex = -1;
1191 this._element.addEventListener('keydown', this._onKeyDown.bind(this));
Blink Reformat4c46d092018-04-07 15:32:371192 this.updateMessageElement();
1193 return this._element;
1194 }
1195
1196 updateMessageElement() {
1197 if (!this._element)
1198 return;
1199
1200 this._element.className = 'console-message-wrapper';
1201 this._element.removeChildren();
1202 if (this._message.isGroupStartMessage())
1203 this._element.classList.add('console-group-title');
1204 if (this._message.source === SDK.ConsoleMessage.MessageSource.ConsoleAPI)
1205 this._element.classList.add('console-from-api');
1206 if (this._inSimilarGroup) {
1207 this._similarGroupMarker = this._element.createChild('div', 'nesting-level-marker');
1208 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1209 }
1210
1211 this._nestingLevelMarkers = [];
1212 for (let i = 0; i < this._nestingLevel; ++i)
1213 this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker'));
1214 this._updateCloseGroupDecorations();
1215 this._element.message = this;
1216
1217 switch (this._message.level) {
1218 case SDK.ConsoleMessage.MessageLevel.Verbose:
1219 this._element.classList.add('console-verbose-level');
Blink Reformat4c46d092018-04-07 15:32:371220 break;
1221 case SDK.ConsoleMessage.MessageLevel.Info:
1222 this._element.classList.add('console-info-level');
1223 if (this._message.type === SDK.ConsoleMessage.MessageType.System)
1224 this._element.classList.add('console-system-type');
1225 break;
1226 case SDK.ConsoleMessage.MessageLevel.Warning:
1227 this._element.classList.add('console-warning-level');
Blink Reformat4c46d092018-04-07 15:32:371228 break;
1229 case SDK.ConsoleMessage.MessageLevel.Error:
1230 this._element.classList.add('console-error-level');
Blink Reformat4c46d092018-04-07 15:32:371231 break;
1232 }
Erik Luofd3e7d42018-09-25 02:12:351233 this._updateMessageLevelIcon();
Blink Reformat4c46d092018-04-07 15:32:371234 if (this._shouldRenderAsWarning())
1235 this._element.classList.add('console-warning-level');
1236
1237 this._element.appendChild(this.contentElement());
1238 if (this._repeatCount > 1)
1239 this._showRepeatCountElement();
1240 }
1241
1242 /**
1243 * @return {boolean}
1244 */
1245 _shouldRenderAsWarning() {
1246 return (this._message.level === SDK.ConsoleMessage.MessageLevel.Verbose ||
1247 this._message.level === SDK.ConsoleMessage.MessageLevel.Info) &&
1248 (this._message.source === SDK.ConsoleMessage.MessageSource.Violation ||
1249 this._message.source === SDK.ConsoleMessage.MessageSource.Deprecation ||
1250 this._message.source === SDK.ConsoleMessage.MessageSource.Intervention ||
1251 this._message.source === SDK.ConsoleMessage.MessageSource.Recommendation);
1252 }
1253
Erik Luofd3e7d42018-09-25 02:12:351254 _updateMessageLevelIcon() {
1255 let iconType = '';
1256 let accessibleName = '';
1257 if (this._message.level === SDK.ConsoleMessage.MessageLevel.Warning) {
1258 iconType = 'smallicon-warning';
1259 accessibleName = ls`Warning`;
1260 } else if (this._message.level === SDK.ConsoleMessage.MessageLevel.Error) {
1261 iconType = 'smallicon-error';
1262 accessibleName = ls`Error`;
1263 }
Blink Reformat4c46d092018-04-07 15:32:371264 if (!iconType && !this._messageLevelIcon)
1265 return;
1266 if (iconType && !this._messageLevelIcon) {
1267 this._messageLevelIcon = UI.Icon.create('', 'message-level-icon');
1268 if (this._contentElement)
1269 this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild);
1270 }
1271 this._messageLevelIcon.setIconType(iconType);
Erik Luofd3e7d42018-09-25 02:12:351272 UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371273 }
1274
1275 /**
1276 * @return {number}
1277 */
1278 repeatCount() {
1279 return this._repeatCount || 1;
1280 }
1281
1282 resetIncrementRepeatCount() {
1283 this._repeatCount = 1;
1284 if (!this._repeatCountElement)
1285 return;
1286
1287 this._repeatCountElement.remove();
1288 if (this._contentElement)
1289 this._contentElement.classList.remove('repeated-message');
1290 delete this._repeatCountElement;
1291 }
1292
1293 incrementRepeatCount() {
1294 this._repeatCount++;
1295 this._showRepeatCountElement();
1296 }
1297
1298 /**
1299 * @param {number} repeatCount
1300 */
1301 setRepeatCount(repeatCount) {
1302 this._repeatCount = repeatCount;
1303 this._showRepeatCountElement();
1304 }
1305
1306 _showRepeatCountElement() {
1307 if (!this._element)
1308 return;
1309
1310 if (!this._repeatCountElement) {
Joel Einbinder7fbe24c2019-01-24 05:19:011311 this._repeatCountElement = createElementWithClass('span', 'console-message-repeat-count', 'dt-small-bubble');
Blink Reformat4c46d092018-04-07 15:32:371312 switch (this._message.level) {
1313 case SDK.ConsoleMessage.MessageLevel.Warning:
1314 this._repeatCountElement.type = 'warning';
1315 break;
1316 case SDK.ConsoleMessage.MessageLevel.Error:
1317 this._repeatCountElement.type = 'error';
1318 break;
1319 case SDK.ConsoleMessage.MessageLevel.Verbose:
1320 this._repeatCountElement.type = 'verbose';
1321 break;
1322 default:
1323 this._repeatCountElement.type = 'info';
1324 }
1325 if (this._shouldRenderAsWarning())
1326 this._repeatCountElement.type = 'warning';
1327
1328 this._element.insertBefore(this._repeatCountElement, this._contentElement);
1329 this._contentElement.classList.add('repeated-message');
1330 }
1331 this._repeatCountElement.textContent = this._repeatCount;
Erik Luofd3e7d42018-09-25 02:12:351332 let accessibleName = ls`Repeat ${this._repeatCount}`;
1333 if (this._message.level === SDK.ConsoleMessage.MessageLevel.Warning)
1334 accessibleName = ls`Warning ${accessibleName}`;
1335 else if (this._message.level === SDK.ConsoleMessage.MessageLevel.Error)
1336 accessibleName = ls`Error ${accessibleName}`;
1337 UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371338 }
1339
1340 get text() {
1341 return this._message.messageText;
1342 }
1343
1344 /**
1345 * @return {string}
1346 */
1347 toExportString() {
1348 const lines = [];
1349 const nodes = this.contentElement().childTextNodes();
1350 const messageContent = nodes.map(Components.Linkifier.untruncatedNodeText).join('');
1351 for (let i = 0; i < this.repeatCount(); ++i)
1352 lines.push(messageContent);
1353 return lines.join('\n');
1354 }
1355
1356 /**
1357 * @param {?RegExp} regex
1358 */
1359 setSearchRegex(regex) {
1360 if (this._searchHiglightNodeChanges && this._searchHiglightNodeChanges.length)
1361 UI.revertDomChanges(this._searchHiglightNodeChanges);
1362 this._searchRegex = regex;
1363 this._searchHighlightNodes = [];
1364 this._searchHiglightNodeChanges = [];
1365 if (!this._searchRegex)
1366 return;
1367
1368 const text = this.contentElement().deepTextContent();
1369 let match;
1370 this._searchRegex.lastIndex = 0;
1371 const sourceRanges = [];
1372 while ((match = this._searchRegex.exec(text)) && match[0])
1373 sourceRanges.push(new TextUtils.SourceRange(match.index, match[0].length));
1374
1375 if (sourceRanges.length) {
1376 this._searchHighlightNodes =
1377 UI.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHiglightNodeChanges);
1378 }
1379 }
1380
1381 /**
1382 * @return {?RegExp}
1383 */
1384 searchRegex() {
1385 return this._searchRegex;
1386 }
1387
1388 /**
1389 * @return {number}
1390 */
1391 searchCount() {
1392 return this._searchHighlightNodes.length;
1393 }
1394
1395 /**
1396 * @return {!Element}
1397 */
1398 searchHighlightNode(index) {
1399 return this._searchHighlightNodes[index];
1400 }
1401
1402 /**
1403 * @param {string} string
1404 * @return {?Element}
1405 */
1406 _tryFormatAsError(string) {
1407 /**
1408 * @param {string} prefix
1409 */
1410 function startsWith(prefix) {
1411 return string.startsWith(prefix);
1412 }
1413
1414 const errorPrefixes =
1415 ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError'];
1416 if (!this._message.runtimeModel() || !errorPrefixes.some(startsWith))
1417 return null;
1418 const debuggerModel = this._message.runtimeModel().debuggerModel();
1419 const baseURL = this._message.runtimeModel().target().inspectedURL();
1420
1421 const lines = string.split('\n');
1422 const links = [];
1423 let position = 0;
1424 for (let i = 0; i < lines.length; ++i) {
1425 position += i > 0 ? lines[i - 1].length + 1 : 0;
1426 const isCallFrameLine = /^\s*at\s/.test(lines[i]);
1427 if (!isCallFrameLine && links.length)
1428 return null;
1429
1430 if (!isCallFrameLine)
1431 continue;
1432
1433 let openBracketIndex = -1;
1434 let closeBracketIndex = -1;
Yang Guo39256bd2019-07-18 06:02:251435 const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g;
1436 const inBrackets = /\([^\)\(]+\)/g;
1437 let lastMatch = null;
1438 let currentMatch;
1439 while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i])))
1440 lastMatch = currentMatch;
1441 if (!lastMatch) {
1442 while ((currentMatch = inBrackets.exec(lines[i])))
1443 lastMatch = currentMatch;
1444 }
1445 if (lastMatch) {
1446 openBracketIndex = lastMatch.index;
1447 closeBracketIndex = lastMatch.index + lastMatch[0].length - 1;
Blink Reformat4c46d092018-04-07 15:32:371448 }
1449 const hasOpenBracket = openBracketIndex !== -1;
1450 const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3;
1451 const right = hasOpenBracket ? closeBracketIndex : lines[i].length;
1452 const linkCandidate = lines[i].substring(left, right);
1453 const splitResult = Common.ParsedURL.splitLineAndColumn(linkCandidate);
1454 if (!splitResult)
1455 return null;
1456
1457 if (splitResult.url === '<anonymous>')
1458 continue;
1459 let url = parseOrScriptMatch(splitResult.url);
1460 if (!url && Common.ParsedURL.isRelativeURL(splitResult.url))
1461 url = parseOrScriptMatch(Common.ParsedURL.completeURL(baseURL, splitResult.url));
1462 if (!url)
1463 return null;
1464
1465 links.push({
1466 url: url,
1467 positionLeft: position + left,
1468 positionRight: position + right,
1469 lineNumber: splitResult.lineNumber,
1470 columnNumber: splitResult.columnNumber
1471 });
1472 }
1473
1474 if (!links.length)
1475 return null;
1476
1477 const formattedResult = createElement('span');
1478 let start = 0;
1479 for (let i = 0; i < links.length; ++i) {
Erik Luo383f21d2018-11-07 23:16:371480 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft)));
Erik Luo182bece2018-11-29 03:15:221481 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
1482 debuggerModel.target(), null, links[i].url, links[i].lineNumber, links[i].columnNumber);
1483 scriptLocationLink.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391484 this._selectableChildren.push({element: scriptLocationLink, forceSelect: () => scriptLocationLink.focus()});
Erik Luo182bece2018-11-29 03:15:221485 formattedResult.appendChild(scriptLocationLink);
Blink Reformat4c46d092018-04-07 15:32:371486 start = links[i].positionRight;
1487 }
1488
1489 if (start !== string.length)
Erik Luo383f21d2018-11-07 23:16:371490 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start)));
Blink Reformat4c46d092018-04-07 15:32:371491
1492 return formattedResult;
1493
1494 /**
1495 * @param {?string} url
1496 * @return {?string}
1497 */
1498 function parseOrScriptMatch(url) {
1499 if (!url)
1500 return null;
1501 const parsedURL = url.asParsedURL();
1502 if (parsedURL)
1503 return parsedURL.url;
1504 if (debuggerModel.scriptsForSourceURL(url).length)
1505 return url;
1506 return null;
1507 }
1508 }
1509
1510 /**
1511 * @param {string} string
1512 * @param {function(string,string,number=,number=):!Node} linkifier
1513 * @return {!DocumentFragment}
1514 */
Erik Luofc2214f2018-11-21 19:54:581515 _linkifyWithCustomLinkifier(string, linkifier) {
Blink Reformat4c46d092018-04-07 15:32:371516 if (string.length > Console.ConsoleViewMessage._MaxTokenizableStringLength)
Erik Luo5fb33bd2018-06-04 23:23:521517 return UI.createExpandableText(string, Console.ConsoleViewMessage._LongStringVisibleLength);
Blink Reformat4c46d092018-04-07 15:32:371518 const container = createDocumentFragment();
Erik Luofc2214f2018-11-21 19:54:581519 const tokens = Console.ConsoleViewMessage._tokenizeMessageText(string);
Blink Reformat4c46d092018-04-07 15:32:371520 for (const token of tokens) {
Erik Luofc2214f2018-11-21 19:54:581521 if (!token.text)
1522 continue;
Blink Reformat4c46d092018-04-07 15:32:371523 switch (token.type) {
1524 case 'url': {
1525 const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text);
1526 const splitResult = Common.ParsedURL.splitLineAndColumn(realURL);
1527 let linkNode;
1528 if (splitResult)
1529 linkNode = linkifier(token.text, splitResult.url, splitResult.lineNumber, splitResult.columnNumber);
1530 else
1531 linkNode = linkifier(token.text, token.value);
1532 container.appendChild(linkNode);
1533 break;
1534 }
1535 default:
1536 container.appendChild(createTextNode(token.text));
1537 break;
1538 }
1539 }
1540 return container;
1541 }
1542
1543 /**
Blink Reformat4c46d092018-04-07 15:32:371544 * @param {string} string
1545 * @return {!DocumentFragment}
1546 */
Erik Luo383f21d2018-11-07 23:16:371547 _linkifyStringAsFragment(string) {
Erik Luofc2214f2018-11-21 19:54:581548 return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => {
Erik Luo383f21d2018-11-07 23:16:371549 const linkElement = Components.Linkifier.linkifyURL(url, {text, lineNumber, columnNumber});
1550 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391551 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo383f21d2018-11-07 23:16:371552 return linkElement;
Blink Reformat4c46d092018-04-07 15:32:371553 });
1554 }
1555
1556 /**
1557 * @param {string} string
1558 * @return {!Array<{type: string, text: (string|undefined)}>}
1559 */
1560 static _tokenizeMessageText(string) {
1561 if (!Console.ConsoleViewMessage._tokenizerRegexes) {
1562 const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f';
1563 const linkStringRegex = new RegExp(
1564 '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes +
1565 '"\')}\\],:;.!?]',
1566 'u');
1567 const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/;
1568 const timeRegex = /took [\d]+ms/;
1569 const eventRegex = /'\w+' event/;
1570 const milestoneRegex = /\sM[6-7]\d/;
1571 const autofillRegex = /\(suggested: \"[\w-]+\"\)/;
1572 const handlers = new Map();
1573 handlers.set(linkStringRegex, 'url');
1574 handlers.set(pathLineRegex, 'url');
1575 handlers.set(timeRegex, 'time');
1576 handlers.set(eventRegex, 'event');
1577 handlers.set(milestoneRegex, 'milestone');
1578 handlers.set(autofillRegex, 'autofill');
1579 Console.ConsoleViewMessage._tokenizerRegexes = Array.from(handlers.keys());
1580 Console.ConsoleViewMessage._tokenizerTypes = Array.from(handlers.values());
1581 }
1582 if (string.length > Console.ConsoleViewMessage._MaxTokenizableStringLength)
1583 return [{text: string, type: undefined}];
1584 const results = TextUtils.TextUtils.splitStringByRegexes(string, Console.ConsoleViewMessage._tokenizerRegexes);
1585 return results.map(
1586 result => ({text: result.value, type: Console.ConsoleViewMessage._tokenizerTypes[result.regexIndex]}));
1587 }
1588
1589 /**
1590 * @return {string}
1591 */
1592 groupKey() {
1593 if (!this._groupKey)
1594 this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle();
1595 return this._groupKey;
1596 }
1597
1598 /**
1599 * @return {string}
1600 */
1601 groupTitle() {
1602 const tokens = Console.ConsoleViewMessage._tokenizeMessageText(this._message.messageText);
1603 const result = tokens.reduce((acc, token) => {
1604 let text = token.text;
1605 if (token.type === 'url')
1606 text = Common.UIString('<URL>');
1607 else if (token.type === 'time')
1608 text = Common.UIString('took <N>ms');
1609 else if (token.type === 'event')
1610 text = Common.UIString('<some> event');
1611 else if (token.type === 'milestone')
1612 text = Common.UIString(' M<XX>');
1613 else if (token.type === 'autofill')
1614 text = Common.UIString('<attribute>');
1615 return acc + text;
1616 }, '');
1617 return result.replace(/[%]o/g, '');
1618 }
1619};
1620
1621/**
1622 * @unrestricted
1623 */
1624Console.ConsoleGroupViewMessage = class extends Console.ConsoleViewMessage {
1625 /**
1626 * @param {!SDK.ConsoleMessage} consoleMessage
1627 * @param {!Components.Linkifier} linkifier
1628 * @param {!ProductRegistry.BadgePool} badgePool
1629 * @param {number} nestingLevel
Erik Luo8ef5d0c2018-09-25 21:16:001630 * @param {function()} onToggle
Erik Luo840be6b2018-12-03 20:54:271631 * @param {function(!Common.Event)} onResize
Blink Reformat4c46d092018-04-07 15:32:371632 */
Erik Luo840be6b2018-12-03 20:54:271633 constructor(consoleMessage, linkifier, badgePool, nestingLevel, onToggle, onResize) {
Blink Reformat4c46d092018-04-07 15:32:371634 console.assert(consoleMessage.isGroupStartMessage());
Erik Luo840be6b2018-12-03 20:54:271635 super(consoleMessage, linkifier, badgePool, nestingLevel, onResize);
Blink Reformat4c46d092018-04-07 15:32:371636 this._collapsed = consoleMessage.type === SDK.ConsoleMessage.MessageType.StartGroupCollapsed;
1637 /** @type {?UI.Icon} */
1638 this._expandGroupIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:001639 this._onToggle = onToggle;
Blink Reformat4c46d092018-04-07 15:32:371640 }
1641
1642 /**
1643 * @param {boolean} collapsed
1644 */
Erik Luo8ef5d0c2018-09-25 21:16:001645 _setCollapsed(collapsed) {
Blink Reformat4c46d092018-04-07 15:32:371646 this._collapsed = collapsed;
1647 if (this._expandGroupIcon)
1648 this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down');
Erik Luo8ef5d0c2018-09-25 21:16:001649 this._onToggle.call(null);
Blink Reformat4c46d092018-04-07 15:32:371650 }
1651
1652 /**
1653 * @return {boolean}
1654 */
1655 collapsed() {
1656 return this._collapsed;
1657 }
1658
1659 /**
1660 * @override
Erik Luo8ef5d0c2018-09-25 21:16:001661 * @param {!Event} event
1662 */
1663 maybeHandleOnKeyDown(event) {
Erik Luo0b8282e2018-10-08 20:37:461664 const focusedChildIndex = this._focusedChildIndex();
1665 if (focusedChildIndex === -1) {
1666 if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) {
1667 this._setCollapsed(!this._collapsed);
1668 return true;
1669 }
Erik Luo8ef5d0c2018-09-25 21:16:001670 }
1671 return super.maybeHandleOnKeyDown(event);
1672 }
1673
1674 /**
1675 * @override
Blink Reformat4c46d092018-04-07 15:32:371676 * @return {!Element}
1677 */
1678 toMessageElement() {
1679 if (!this._element) {
1680 super.toMessageElement();
Erik Luo8ef5d0c2018-09-25 21:16:001681 const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down';
1682 this._expandGroupIcon = UI.Icon.create(iconType, 'expand-group-icon');
Erik Luob5bfff42018-09-20 02:52:391683 // Intercept focus to avoid highlight on click.
1684 this._contentElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:371685 if (this._repeatCountElement)
1686 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
1687 else
1688 this._element.insertBefore(this._expandGroupIcon, this._contentElement);
Erik Luo8ef5d0c2018-09-25 21:16:001689 this._element.addEventListener('click', () => this._setCollapsed(!this._collapsed));
Blink Reformat4c46d092018-04-07 15:32:371690 }
1691 return this._element;
1692 }
1693
1694 /**
1695 * @override
1696 */
1697 _showRepeatCountElement() {
1698 super._showRepeatCountElement();
1699 if (this._repeatCountElement && this._expandGroupIcon)
1700 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
1701 }
1702};
1703
1704/**
1705 * @const
1706 * @type {number}
1707 */
1708Console.ConsoleViewMessage.MaxLengthForLinks = 40;
1709
1710Console.ConsoleViewMessage._MaxTokenizableStringLength = 10000;
1711
1712Console.ConsoleViewMessage._LongStringVisibleLength = 5000;