blob: e78be85a7ccb034a04d00b7010bcc81a2d1db1bd [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 */
Tim van der Lippe9b2f8712020-02-12 17:46:2230
31import * as Common from '../common/common.js';
32import * as Components from '../components/components.js';
33import * as DataGrid from '../data_grid/data_grid.js';
34import * as ObjectUI from '../object_ui/object_ui.js';
35import * as SDK from '../sdk/sdk.js';
36import * as TextUtils from '../text_utils/text_utils.js';
37import * as UI from '../ui/ui.js';
38
Tim van der Lippeeaacb722020-01-10 12:16:0039import {ConsoleViewportElement} from './ConsoleViewport.js'; // eslint-disable-line no-unused-vars
40
Blink Reformat4c46d092018-04-07 15:32:3741/**
Tim van der Lippeeaacb722020-01-10 12:16:0042 * @implements {ConsoleViewportElement}
Blink Reformat4c46d092018-04-07 15:32:3743 * @unrestricted
44 */
Tim van der Lippeeaacb722020-01-10 12:16:0045export class ConsoleViewMessage {
Blink Reformat4c46d092018-04-07 15:32:3746 /**
Tim van der Lippe9b2f8712020-02-12 17:46:2247 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
48 * @param {!Components.Linkifier.Linkifier} linkifier
Blink Reformat4c46d092018-04-07 15:32:3749 * @param {number} nestingLevel
Erik Luo840be6b2018-12-03 20:54:2750 * @param {function(!Common.Event)} onResize
Blink Reformat4c46d092018-04-07 15:32:3751 */
Tim van der Lippeb45d9a02019-11-05 17:24:4152 constructor(consoleMessage, linkifier, nestingLevel, onResize) {
Blink Reformat4c46d092018-04-07 15:32:3753 this._message = consoleMessage;
54 this._linkifier = linkifier;
Blink Reformat4c46d092018-04-07 15:32:3755 this._repeatCount = 1;
56 this._closeGroupDecorationCount = 0;
57 this._nestingLevel = nestingLevel;
Erik Luo31c21f62018-12-13 03:39:3958 /** @type {!Array<{element: !Element, forceSelect: function()}>} */
Erik Luo383f21d2018-11-07 23:16:3759 this._selectableChildren = [];
Erik Luo840be6b2018-12-03 20:54:2760 this._messageResized = onResize;
Blink Reformat4c46d092018-04-07 15:32:3761
Tim van der Lippe9b2f8712020-02-12 17:46:2262 /** @type {?DataGrid.DataGrid.DataGridImpl} */
Blink Reformat4c46d092018-04-07 15:32:3763 this._dataGrid = null;
Tim van der Lippe9b2f8712020-02-12 17:46:2264 this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter();
Blink Reformat4c46d092018-04-07 15:32:3765 this._searchRegex = null;
Tim van der Lippe9b2f8712020-02-12 17:46:2266 /** @type {?UI.Icon.Icon} */
Blink Reformat4c46d092018-04-07 15:32:3767 this._messageLevelIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:0068 this._traceExpanded = false;
69 /** @type {?function(boolean)} */
70 this._expandTrace = null;
John Emaubb2897a2019-10-04 17:37:3271 /** @type {?Element} */
72 this._anchorElement = null;
Blink Reformat4c46d092018-04-07 15:32:3773 }
74
75 /**
76 * @override
77 * @return {!Element}
78 */
79 element() {
80 return this.toMessageElement();
81 }
82
83 /**
Blink Reformat4c46d092018-04-07 15:32:3784 * @override
85 */
86 wasShown() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3487 if (this._dataGrid) {
Blink Reformat4c46d092018-04-07 15:32:3788 this._dataGrid.updateWidths();
Tim van der Lippe1d6e57a2019-09-30 11:55:3489 }
Blink Reformat4c46d092018-04-07 15:32:3790 this._isVisible = true;
91 }
92
93 onResize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3494 if (!this._isVisible) {
Blink Reformat4c46d092018-04-07 15:32:3795 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3496 }
97 if (this._dataGrid) {
Blink Reformat4c46d092018-04-07 15:32:3798 this._dataGrid.onResize();
Tim van der Lippe1d6e57a2019-09-30 11:55:3499 }
Blink Reformat4c46d092018-04-07 15:32:37100 }
101
102 /**
103 * @override
104 */
105 willHide() {
106 this._isVisible = false;
Erik Luo4b002322018-07-30 21:23:31107 this._cachedHeight = this.element().offsetHeight;
Blink Reformat4c46d092018-04-07 15:32:37108 }
109
110 /**
111 * @return {number}
112 */
113 fastHeight() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34114 if (this._cachedHeight) {
Blink Reformat4c46d092018-04-07 15:32:37115 return this._cachedHeight;
Tim van der Lippe1d6e57a2019-09-30 11:55:34116 }
Blink Reformat4c46d092018-04-07 15:32:37117 // This value reflects the 18px min-height of .console-message, plus the
118 // 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
119 const defaultConsoleRowHeight = 19;
Tim van der Lippe9b2f8712020-02-12 17:46:22120 if (this._message.type === SDK.ConsoleModel.MessageType.Table) {
Blink Reformat4c46d092018-04-07 15:32:37121 const table = this._message.parameters[0];
Tim van der Lippe1d6e57a2019-09-30 11:55:34122 if (table && table.preview) {
Blink Reformat4c46d092018-04-07 15:32:37123 return defaultConsoleRowHeight * table.preview.properties.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:34124 }
Blink Reformat4c46d092018-04-07 15:32:37125 }
126 return defaultConsoleRowHeight;
127 }
128
129 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22130 * @return {!SDK.ConsoleModel.ConsoleMessage}
Blink Reformat4c46d092018-04-07 15:32:37131 */
132 consoleMessage() {
133 return this._message;
134 }
135
136 /**
137 * @return {!Element}
138 */
139 _buildTableMessage() {
140 const formattedMessage = createElementWithClass('span', 'source-code');
Erik Luo5976c8c2018-07-24 02:03:09141 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34142 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09143 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34144 }
Blink Reformat4c46d092018-04-07 15:32:37145
146 let table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34147 if (table) {
Blink Reformat4c46d092018-04-07 15:32:37148 table = this._parameterToRemoteObject(table);
Tim van der Lippe1d6e57a2019-09-30 11:55:34149 }
150 if (!table || !table.preview) {
Erik Luo16e3e382018-11-09 02:56:01151 return this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:34152 }
Blink Reformat4c46d092018-04-07 15:32:37153
154 const rawValueColumnSymbol = Symbol('rawValueColumn');
155 const columnNames = [];
156 const preview = table.preview;
157 const rows = [];
158 for (let i = 0; i < preview.properties.length; ++i) {
159 const rowProperty = preview.properties[i];
160 let rowSubProperties;
Tim van der Lippe1d6e57a2019-09-30 11:55:34161 if (rowProperty.valuePreview) {
Blink Reformat4c46d092018-04-07 15:32:37162 rowSubProperties = rowProperty.valuePreview.properties;
Tim van der Lippe1d6e57a2019-09-30 11:55:34163 } else if (rowProperty.value) {
Blink Reformat4c46d092018-04-07 15:32:37164 rowSubProperties = [{name: rawValueColumnSymbol, type: rowProperty.type, value: rowProperty.value}];
Tim van der Lippe1d6e57a2019-09-30 11:55:34165 } else {
Blink Reformat4c46d092018-04-07 15:32:37166 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34167 }
Blink Reformat4c46d092018-04-07 15:32:37168
169 const rowValue = {};
170 const maxColumnsToRender = 20;
171 for (let j = 0; j < rowSubProperties.length; ++j) {
172 const cellProperty = rowSubProperties[j];
173 let columnRendered = columnNames.indexOf(cellProperty.name) !== -1;
174 if (!columnRendered) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34175 if (columnNames.length === maxColumnsToRender) {
Blink Reformat4c46d092018-04-07 15:32:37176 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 }
Blink Reformat4c46d092018-04-07 15:32:37178 columnRendered = true;
179 columnNames.push(cellProperty.name);
180 }
181
182 if (columnRendered) {
183 const cellElement = this._renderPropertyPreviewOrAccessor(table, [rowProperty, cellProperty]);
184 cellElement.classList.add('console-message-nowrap-below');
185 rowValue[cellProperty.name] = cellElement;
186 }
187 }
188 rows.push([rowProperty.name, rowValue]);
189 }
190
191 const flatValues = [];
192 for (let i = 0; i < rows.length; ++i) {
193 const rowName = rows[i][0];
194 const rowValue = rows[i][1];
195 flatValues.push(rowName);
Tim van der Lippe1d6e57a2019-09-30 11:55:34196 for (let j = 0; j < columnNames.length; ++j) {
Blink Reformat4c46d092018-04-07 15:32:37197 flatValues.push(rowValue[columnNames[j]]);
Tim van der Lippe1d6e57a2019-09-30 11:55:34198 }
Blink Reformat4c46d092018-04-07 15:32:37199 }
Tim van der Lippe9b2f8712020-02-12 17:46:22200 columnNames.unshift(Common.UIString.UIString('(index)'));
201 const columnDisplayNames =
202 columnNames.map(name => name === rawValueColumnSymbol ? Common.UIString.UIString('Value') : name);
Blink Reformat4c46d092018-04-07 15:32:37203
204 if (flatValues.length) {
Tim van der Lippe9b2f8712020-02-12 17:46:22205 this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create(columnDisplayNames, flatValues, ls`Console`);
Blink Reformat4c46d092018-04-07 15:32:37206 this._dataGrid.setStriped(true);
Anubha Mathurfbacf4e2019-10-28 19:08:03207 this._dataGrid.setFocusable(false);
Blink Reformat4c46d092018-04-07 15:32:37208
209 const formattedResult = createElementWithClass('span', 'console-message-text');
210 const tableElement = formattedResult.createChild('div', 'console-message-formatted-table');
211 const dataGridContainer = tableElement.createChild('span');
212 tableElement.appendChild(this._formatParameter(table, true, false));
213 dataGridContainer.appendChild(this._dataGrid.element);
214 formattedMessage.appendChild(formattedResult);
215 this._dataGrid.renderInline();
216 }
217 return formattedMessage;
218 }
219
220 /**
221 * @return {!Element}
222 */
223 _buildMessage() {
224 let messageElement;
225 let messageText = this._message.messageText;
Tim van der Lippe9b2f8712020-02-12 17:46:22226 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:37227 switch (this._message.type) {
Tim van der Lippe9b2f8712020-02-12 17:46:22228 case SDK.ConsoleModel.MessageType.Trace:
Blink Reformat4c46d092018-04-07 15:32:37229 messageElement = this._format(this._message.parameters || ['console.trace']);
230 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22231 case SDK.ConsoleModel.MessageType.Clear:
Blink Reformat4c46d092018-04-07 15:32:37232 messageElement = createElementWithClass('span', 'console-info');
Paul Lewis4b64b3f2020-01-23 11:41:20233 if (self.Common.settings.moduleSetting('preserveConsoleLog').get()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22234 messageElement.textContent =
235 Common.UIString.UIString('console.clear() was prevented due to \'Preserve log\'');
Tim van der Lippe1d6e57a2019-09-30 11:55:34236 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22237 messageElement.textContent = Common.UIString.UIString('Console was cleared');
Tim van der Lippe1d6e57a2019-09-30 11:55:34238 }
Blink Reformat4c46d092018-04-07 15:32:37239 messageElement.title =
Paul Lewis05eb37f2020-01-24 14:31:40240 ls`Clear all messages with ${self.UI.shortcutRegistry.shortcutTitleForAction('console.clear')}`;
Blink Reformat4c46d092018-04-07 15:32:37241 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22242 case SDK.ConsoleModel.MessageType.Dir: {
Blink Reformat4c46d092018-04-07 15:32:37243 const obj = this._message.parameters ? this._message.parameters[0] : undefined;
244 const args = ['%O', obj];
245 messageElement = this._format(args);
246 break;
247 }
Tim van der Lippe9b2f8712020-02-12 17:46:22248 case SDK.ConsoleModel.MessageType.Profile:
249 case SDK.ConsoleModel.MessageType.ProfileEnd:
Blink Reformat4c46d092018-04-07 15:32:37250 messageElement = this._format([messageText]);
251 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22252 case SDK.ConsoleModel.MessageType.Assert:
Pavel Feldman9f0f0a32018-12-18 02:09:13253 this._messagePrefix = ls`Assertion failed: `;
254 // Fall through.
Blink Reformat4c46d092018-04-07 15:32:37255 default: {
256 if (this._message.parameters && this._message.parameters.length === 1 &&
Tim van der Lippe1d6e57a2019-09-30 11:55:34257 this._message.parameters[0].type === 'string') {
Blink Reformat4c46d092018-04-07 15:32:37258 messageElement = this._tryFormatAsError(/** @type {string} */ (this._message.parameters[0].value));
Tim van der Lippe1d6e57a2019-09-30 11:55:34259 }
Blink Reformat4c46d092018-04-07 15:32:37260 const args = this._message.parameters || [messageText];
261 messageElement = messageElement || this._format(args);
262 }
263 }
264 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22265 if (this._message.source === SDK.ConsoleModel.MessageSource.Network) {
Erik Luofc2214f2018-11-21 19:54:58266 messageElement = this._formatAsNetworkRequest() || this._format([messageText]);
267 } else {
Blink Reformat4c46d092018-04-07 15:32:37268 const messageInParameters =
269 this._message.parameters && messageText === /** @type {string} */ (this._message.parameters[0]);
Tim van der Lippe9b2f8712020-02-12 17:46:22270 if (this._message.source === SDK.ConsoleModel.MessageSource.Violation) {
271 messageText = Common.UIString.UIString('[Violation] %s', messageText);
272 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Intervention) {
273 messageText = Common.UIString.UIString('[Intervention] %s', messageText);
274 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Deprecation) {
275 messageText = Common.UIString.UIString('[Deprecation] %s', messageText);
Tim van der Lippe1d6e57a2019-09-30 11:55:34276 }
Blink Reformat4c46d092018-04-07 15:32:37277 const args = this._message.parameters || [messageText];
Tim van der Lippe1d6e57a2019-09-30 11:55:34278 if (messageInParameters) {
Blink Reformat4c46d092018-04-07 15:32:37279 args[0] = messageText;
Tim van der Lippe1d6e57a2019-09-30 11:55:34280 }
Blink Reformat4c46d092018-04-07 15:32:37281 messageElement = this._format(args);
282 }
283 }
284 messageElement.classList.add('console-message-text');
285
286 const formattedMessage = createElementWithClass('span', 'source-code');
Erik Luo5976c8c2018-07-24 02:03:09287 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34288 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09289 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34290 }
Blink Reformat4c46d092018-04-07 15:32:37291 formattedMessage.appendChild(messageElement);
292 return formattedMessage;
293 }
294
295 /**
296 * @return {?Element}
297 */
Erik Luofc2214f2018-11-21 19:54:58298 _formatAsNetworkRequest() {
Tim van der Lippe9b2f8712020-02-12 17:46:22299 const request = SDK.NetworkLog.NetworkLog.requestForConsoleMessage(this._message);
Tim van der Lippe1d6e57a2019-09-30 11:55:34300 if (!request) {
Erik Luofc2214f2018-11-21 19:54:58301 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34302 }
Erik Luofc2214f2018-11-21 19:54:58303 const messageElement = createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22304 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofc2214f2018-11-21 19:54:58305 messageElement.createTextChild(request.requestMethod + ' ');
Tim van der Lippe9b2f8712020-02-12 17:46:22306 const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url());
Erik Luo182bece2018-11-29 03:15:22307 // Focus is handled by the viewport.
308 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39309 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22310 messageElement.appendChild(linkElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34311 if (request.failed) {
Erik Luofc2214f2018-11-21 19:54:58312 messageElement.createTextChildren(' ', request.localizedFailDescription);
Tim van der Lippe1d6e57a2019-09-30 11:55:34313 }
314 if (request.statusCode !== 0) {
Erik Luofc2214f2018-11-21 19:54:58315 messageElement.createTextChildren(' ', String(request.statusCode));
Tim van der Lippe1d6e57a2019-09-30 11:55:34316 }
317 if (request.statusText) {
Erik Luofc2214f2018-11-21 19:54:58318 messageElement.createTextChildren(' (', request.statusText, ')');
Tim van der Lippe1d6e57a2019-09-30 11:55:34319 }
Erik Luofc2214f2018-11-21 19:54:58320 } else {
Erik Luoad5f3942019-03-26 20:53:44321 const messageText = this._message.messageText;
322 const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
323 let linkElement;
324 if (url === request.url()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22325 linkElement = Components.Linkifier.Linkifier.linkifyRevealable(
326 /** @type {!SDK.NetworkRequest.NetworkRequest} */ (request), url, request.url());
Erik Luoad5f3942019-03-26 20:53:44327 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22328 linkElement = Components.Linkifier.Linkifier.linkifyURL(url, {text, lineNumber, columnNumber});
Erik Luoad5f3942019-03-26 20:53:44329 }
Erik Luo182bece2018-11-29 03:15:22330 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39331 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22332 return linkElement;
333 });
Erik Luofc2214f2018-11-21 19:54:58334 messageElement.appendChild(fragment);
335 }
336 return messageElement;
337 }
338
339 /**
340 * @return {?Element}
341 */
Blink Reformat4c46d092018-04-07 15:32:37342 _buildMessageAnchor() {
343 let anchorElement = null;
344 if (this._message.scriptId) {
345 anchorElement = this._linkifyScriptId(
346 this._message.scriptId, this._message.url || '', this._message.line, this._message.column);
347 } else if (this._message.stackTrace && this._message.stackTrace.callFrames.length) {
348 anchorElement = this._linkifyStackTraceTopFrame(this._message.stackTrace);
349 } else if (this._message.url && this._message.url !== 'undefined') {
350 anchorElement = this._linkifyLocation(this._message.url, this._message.line, this._message.column);
351 }
352
353 // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
354 if (anchorElement) {
John Emauf7e30fb2019-10-04 19:12:32355 anchorElement.tabIndex = -1;
356 this._selectableChildren.push({
357 element: anchorElement,
358 forceSelect: () => anchorElement.focus(),
359 });
Blink Reformat4c46d092018-04-07 15:32:37360 const anchorWrapperElement = createElementWithClass('span', 'console-message-anchor');
361 anchorWrapperElement.appendChild(anchorElement);
362 anchorWrapperElement.createTextChild(' ');
363 return anchorWrapperElement;
364 }
365 return null;
366 }
367
368 /**
Blink Reformat4c46d092018-04-07 15:32:37369 * @return {!Element}
370 */
371 _buildMessageWithStackTrace() {
372 const toggleElement = createElementWithClass('div', 'console-message-stack-trace-toggle');
373 const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
374
375 const messageElement = this._buildMessage();
Tim van der Lippe9b2f8712020-02-12 17:46:22376 const icon = UI.Icon.Icon.create('smallicon-triangle-right', 'console-message-expand-icon');
Blink Reformat4c46d092018-04-07 15:32:37377 const clickableElement = contentElement.createChild('div');
378 clickableElement.appendChild(icon);
Erik Luob5bfff42018-09-20 02:52:39379 // Intercept focus to avoid highlight on click.
380 clickableElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37381
382 clickableElement.appendChild(messageElement);
383 const stackTraceElement = contentElement.createChild('div');
384 const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
Jack Lynch7bc9bf22020-01-13 17:16:00385 this._message.runtimeModel().target(), this._linkifier, {stackTrace: this._message.stackTrace});
Erik Luo182bece2018-11-29 03:15:22386 stackTraceElement.appendChild(stackTracePreview.element);
387 for (const linkElement of stackTracePreview.links) {
Erik Luo31c21f62018-12-13 03:39:39388 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22389 }
Blink Reformat4c46d092018-04-07 15:32:37390 stackTraceElement.classList.add('hidden');
Brandon Goddard04a5a762019-12-10 16:45:53391 UI.ARIAUtils.markAsTreeitem(this.element());
392 UI.ARIAUtils.setExpanded(this.element(), false);
Erik Luo8ef5d0c2018-09-25 21:16:00393 this._expandTrace = expand => {
Blink Reformat4c46d092018-04-07 15:32:37394 icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right');
395 stackTraceElement.classList.toggle('hidden', !expand);
Brandon Goddard04a5a762019-12-10 16:45:53396 UI.ARIAUtils.setExpanded(this.element(), expand);
Erik Luo8ef5d0c2018-09-25 21:16:00397 this._traceExpanded = expand;
398 };
Blink Reformat4c46d092018-04-07 15:32:37399
400 /**
Tim van der Lippeeaacb722020-01-10 12:16:00401 * @this {!ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37402 * @param {?Event} event
403 */
404 function toggleStackTrace(event) {
Tim van der Lippe9b2f8712020-02-12 17:46:22405 if (UI.UIUtils.isEditing() || contentElement.hasSelection()) {
Blink Reformat4c46d092018-04-07 15:32:37406 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34407 }
Erik Luo8ef5d0c2018-09-25 21:16:00408 this._expandTrace(stackTraceElement.classList.contains('hidden'));
Blink Reformat4c46d092018-04-07 15:32:37409 event.consume();
410 }
411
Erik Luo8ef5d0c2018-09-25 21:16:00412 clickableElement.addEventListener('click', toggleStackTrace.bind(this), false);
Tim van der Lippe9b2f8712020-02-12 17:46:22413 if (this._message.type === SDK.ConsoleModel.MessageType.Trace) {
Erik Luo8ef5d0c2018-09-25 21:16:00414 this._expandTrace(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34415 }
Blink Reformat4c46d092018-04-07 15:32:37416
Erik Luo8ef5d0c2018-09-25 21:16:00417 toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true);
Blink Reformat4c46d092018-04-07 15:32:37418 return toggleElement;
419 }
420
421 /**
422 * @param {string} url
423 * @param {number} lineNumber
424 * @param {number} columnNumber
425 * @return {?Element}
426 */
427 _linkifyLocation(url, lineNumber, columnNumber) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34428 if (!this._message.runtimeModel()) {
Blink Reformat4c46d092018-04-07 15:32:37429 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34430 }
Blink Reformat4c46d092018-04-07 15:32:37431 return this._linkifier.linkifyScriptLocation(
Jack Lynch236e1d22020-01-07 20:33:51432 this._message.runtimeModel().target(), /* scriptId */ null, url, lineNumber, {columnNumber});
Blink Reformat4c46d092018-04-07 15:32:37433 }
434
435 /**
436 * @param {!Protocol.Runtime.StackTrace} stackTrace
437 * @return {?Element}
438 */
439 _linkifyStackTraceTopFrame(stackTrace) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34440 if (!this._message.runtimeModel()) {
Blink Reformat4c46d092018-04-07 15:32:37441 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34442 }
Blink Reformat4c46d092018-04-07 15:32:37443 return this._linkifier.linkifyStackTraceTopFrame(this._message.runtimeModel().target(), stackTrace);
444 }
445
446 /**
447 * @param {string} scriptId
448 * @param {string} url
449 * @param {number} lineNumber
450 * @param {number} columnNumber
451 * @return {?Element}
452 */
453 _linkifyScriptId(scriptId, url, lineNumber, columnNumber) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34454 if (!this._message.runtimeModel()) {
Blink Reformat4c46d092018-04-07 15:32:37455 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34456 }
Blink Reformat4c46d092018-04-07 15:32:37457 return this._linkifier.linkifyScriptLocation(
Jack Lynch236e1d22020-01-07 20:33:51458 this._message.runtimeModel().target(), scriptId, url, lineNumber, {columnNumber});
Blink Reformat4c46d092018-04-07 15:32:37459 }
460
461 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22462 * @param {!SDK.RemoteObject.RemoteObject|!Protocol.Runtime.RemoteObject|string} parameter
463 * @return {!SDK.RemoteObject.RemoteObject}
Blink Reformat4c46d092018-04-07 15:32:37464 */
465 _parameterToRemoteObject(parameter) {
Tim van der Lippe9b2f8712020-02-12 17:46:22466 if (parameter instanceof SDK.RemoteObject.RemoteObject) {
Blink Reformat4c46d092018-04-07 15:32:37467 return parameter;
Tim van der Lippe1d6e57a2019-09-30 11:55:34468 }
Blink Reformat4c46d092018-04-07 15:32:37469 const runtimeModel = this._message.runtimeModel();
Tim van der Lippe1d6e57a2019-09-30 11:55:34470 if (!runtimeModel) {
Tim van der Lippe9b2f8712020-02-12 17:46:22471 return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter);
Tim van der Lippe1d6e57a2019-09-30 11:55:34472 }
473 if (typeof parameter === 'object') {
Blink Reformat4c46d092018-04-07 15:32:37474 return runtimeModel.createRemoteObject(parameter);
Tim van der Lippe1d6e57a2019-09-30 11:55:34475 }
Blink Reformat4c46d092018-04-07 15:32:37476 return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
477 }
478
479 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22480 * @param {!Array.<!SDK.RemoteObject.RemoteObject|string>} rawParameters
Blink Reformat4c46d092018-04-07 15:32:37481 * @return {!Element}
482 */
483 _format(rawParameters) {
484 // This node is used like a Builder. Values are continually appended onto it.
485 const formattedResult = createElement('span');
Tim van der Lippe1d6e57a2019-09-30 11:55:34486 if (this._messagePrefix) {
Pavel Feldman9f0f0a32018-12-18 02:09:13487 formattedResult.createChild('span').textContent = this._messagePrefix;
Tim van der Lippe1d6e57a2019-09-30 11:55:34488 }
489 if (!rawParameters.length) {
Blink Reformat4c46d092018-04-07 15:32:37490 return formattedResult;
Tim van der Lippe1d6e57a2019-09-30 11:55:34491 }
Blink Reformat4c46d092018-04-07 15:32:37492
493 // Formatting code below assumes that parameters are all wrappers whereas frontend console
494 // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
495 // FIXME: Only pass runtime wrappers here.
496 let parameters = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:34497 for (let i = 0; i < rawParameters.length; ++i) {
Blink Reformat4c46d092018-04-07 15:32:37498 parameters[i] = this._parameterToRemoteObject(rawParameters[i]);
Tim van der Lippe1d6e57a2019-09-30 11:55:34499 }
Blink Reformat4c46d092018-04-07 15:32:37500
501 // There can be string log and string eval result. We distinguish between them based on message type.
502 const shouldFormatMessage =
Tim van der Lippe9b2f8712020-02-12 17:46:22503 SDK.RemoteObject.RemoteObject.type(
504 (/** @type {!Array.<!SDK.RemoteObject.RemoteObject>} **/ (parameters))[0]) === 'string' &&
505 (this._message.type !== SDK.ConsoleModel.MessageType.Result ||
506 this._message.level === SDK.ConsoleModel.MessageLevel.Error);
Blink Reformat4c46d092018-04-07 15:32:37507
508 // Multiple parameters with the first being a format string. Save unused substitutions.
509 if (shouldFormatMessage) {
510 const result = this._formatWithSubstitutionString(
511 /** @type {string} **/ (parameters[0].description), parameters.slice(1), formattedResult);
512 parameters = result.unusedSubstitutions;
Tim van der Lippe1d6e57a2019-09-30 11:55:34513 if (parameters.length) {
Blink Reformat4c46d092018-04-07 15:32:37514 formattedResult.createTextChild(' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34515 }
Blink Reformat4c46d092018-04-07 15:32:37516 }
517
518 // Single parameter, or unused substitutions from above.
519 for (let i = 0; i < parameters.length; ++i) {
520 // Inline strings when formatting.
Tim van der Lippe1d6e57a2019-09-30 11:55:34521 if (shouldFormatMessage && parameters[i].type === 'string') {
Erik Luo383f21d2018-11-07 23:16:37522 formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description));
Tim van der Lippe1d6e57a2019-09-30 11:55:34523 } else {
Blink Reformat4c46d092018-04-07 15:32:37524 formattedResult.appendChild(this._formatParameter(parameters[i], false, true));
Tim van der Lippe1d6e57a2019-09-30 11:55:34525 }
526 if (i < parameters.length - 1) {
Blink Reformat4c46d092018-04-07 15:32:37527 formattedResult.createTextChild(' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34528 }
Blink Reformat4c46d092018-04-07 15:32:37529 }
530 return formattedResult;
531 }
532
533 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22534 * @param {!SDK.RemoteObject.RemoteObject} output
Blink Reformat4c46d092018-04-07 15:32:37535 * @param {boolean=} forceObjectFormat
536 * @param {boolean=} includePreview
537 * @return {!Element}
538 */
539 _formatParameter(output, forceObjectFormat, includePreview) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34540 if (output.customPreview()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22541 return (new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output)).element;
Tim van der Lippe1d6e57a2019-09-30 11:55:34542 }
Blink Reformat4c46d092018-04-07 15:32:37543
544 const type = forceObjectFormat ? 'object' : (output.subtype || output.type);
545 let element;
546 switch (type) {
547 case 'error':
548 element = this._formatParameterAsError(output);
549 break;
550 case 'function':
551 element = this._formatParameterAsFunction(output, includePreview);
552 break;
553 case 'array':
554 case 'arraybuffer':
555 case 'blob':
556 case 'dataview':
557 case 'generator':
558 case 'iterator':
559 case 'map':
560 case 'object':
561 case 'promise':
562 case 'proxy':
563 case 'set':
564 case 'typedarray':
565 case 'weakmap':
566 case 'weakset':
567 element = this._formatParameterAsObject(output, includePreview);
568 break;
569 case 'node':
570 element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false);
571 break;
572 case 'string':
573 element = this._formatParameterAsString(output);
574 break;
575 case 'boolean':
576 case 'date':
577 case 'null':
578 case 'number':
579 case 'regexp':
580 case 'symbol':
581 case 'undefined':
582 case 'bigint':
583 element = this._formatParameterAsValue(output);
584 break;
585 default:
586 element = this._formatParameterAsValue(output);
587 console.error('Tried to format remote object of unknown type.');
588 }
589 element.classList.add('object-value-' + type);
590 element.classList.add('source-code');
591 return element;
592 }
593
594 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22595 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37596 * @return {!Element}
Tim van der Lippeeaacb722020-01-10 12:16:00597 * @suppress {accessControls}
Blink Reformat4c46d092018-04-07 15:32:37598 */
599 _formatParameterAsValue(obj) {
600 const result = createElement('span');
601 const description = obj.description || '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34602 if (description.length > Console.ConsoleViewMessage._MaxTokenizableStringLength) {
Tim van der Lippe9b2f8712020-02-12 17:46:22603 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Connor Moody1a5c0d32019-12-19 07:23:36604 createElement('span'), description, Console.ConsoleViewMessage._LongStringVisibleLength);
605 result.appendChild(propertyValue.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34606 } else {
Blink Reformat4c46d092018-04-07 15:32:37607 result.createTextChild(description);
Tim van der Lippe1d6e57a2019-09-30 11:55:34608 }
609 if (obj.objectId) {
Blink Reformat4c46d092018-04-07 15:32:37610 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34611 }
Blink Reformat4c46d092018-04-07 15:32:37612 return result;
613 }
614
615 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22616 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37617 * @param {boolean=} includePreview
618 * @return {!Element}
619 */
620 _formatParameterAsObject(obj, includePreview) {
621 const titleElement = createElementWithClass('span', 'console-object');
622 if (includePreview && obj.preview) {
623 titleElement.classList.add('console-object-preview');
624 this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
625 } else if (obj.type === 'function') {
626 const functionElement = titleElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22627 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
Blink Reformat4c46d092018-04-07 15:32:37628 titleElement.classList.add('object-value-function');
629 } else {
630 titleElement.createTextChild(obj.description || '');
631 }
632
Tim van der Lippe1d6e57a2019-09-30 11:55:34633 if (!obj.hasChildren || obj.customPreview()) {
Blink Reformat4c46d092018-04-07 15:32:37634 return titleElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34635 }
Blink Reformat4c46d092018-04-07 15:32:37636
637 const note = titleElement.createChild('span', 'object-state-note info-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22638 if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) {
Blink Reformat4c46d092018-04-07 15:32:37639 note.title = ls`This value will not be collected until console is cleared.`;
Tim van der Lippe1d6e57a2019-09-30 11:55:34640 } else {
Blink Reformat4c46d092018-04-07 15:32:37641 note.title = ls`Value below was evaluated just now.`;
Tim van der Lippe1d6e57a2019-09-30 11:55:34642 }
Blink Reformat4c46d092018-04-07 15:32:37643
Tim van der Lippe9b2f8712020-02-12 17:46:22644 const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier);
Blink Reformat4c46d092018-04-07 15:32:37645 section.element.classList.add('console-view-object-properties-section');
646 section.enableContextMenu();
Erik Luocc14b812018-11-03 01:33:09647 section.setShowSelectionOnKeyboardFocus(true, true);
Erik Luo383f21d2018-11-07 23:16:37648 this._selectableChildren.push(section);
Erik Luo840be6b2018-12-03 20:54:27649 section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
650 section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
651 section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
Blink Reformat4c46d092018-04-07 15:32:37652 return section.element;
653 }
654
655 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22656 * @param {!SDK.RemoteObject.RemoteObject} func
Blink Reformat4c46d092018-04-07 15:32:37657 * @param {boolean=} includePreview
658 * @return {!Element}
659 */
660 _formatParameterAsFunction(func, includePreview) {
661 const result = createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22662 SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37663 return result;
664
665 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22666 * @param {!SDK.RemoteObject.RemoteObject} targetFunction
Tim van der Lippeeaacb722020-01-10 12:16:00667 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37668 */
669 function formatTargetFunction(targetFunction) {
670 const functionElement = createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22671 const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(
Joey Arhard78a58f2018-12-05 01:59:45672 targetFunction, functionElement, true, includePreview);
Blink Reformat4c46d092018-04-07 15:32:37673 result.appendChild(functionElement);
674 if (targetFunction !== func) {
675 const note = result.createChild('span', 'object-info-state-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22676 note.title = Common.UIString.UIString('Function was resolved from bound function.');
Blink Reformat4c46d092018-04-07 15:32:37677 }
678 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false);
Joey Arhard78a58f2018-12-05 01:59:45679 promise.then(() => this._formattedParameterAsFunctionForTest());
Blink Reformat4c46d092018-04-07 15:32:37680 }
681 }
682
Joey Arhard78a58f2018-12-05 01:59:45683 _formattedParameterAsFunctionForTest() {
684 }
685
Blink Reformat4c46d092018-04-07 15:32:37686 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22687 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37688 * @param {!Event} event
689 */
690 _contextMenuEventFired(obj, event) {
Tim van der Lippe9b2f8712020-02-12 17:46:22691 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37692 contextMenu.appendApplicableItems(obj);
693 contextMenu.show();
694 }
695
696 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22697 * @param {?SDK.RemoteObject.RemoteObject} object
Blink Reformat4c46d092018-04-07 15:32:37698 * @param {!Array.<!Protocol.Runtime.PropertyPreview>} propertyPath
699 * @return {!Element}
700 */
701 _renderPropertyPreviewOrAccessor(object, propertyPath) {
702 const property = propertyPath.peekLast();
Tim van der Lippe1d6e57a2019-09-30 11:55:34703 if (property.type === 'accessor') {
Blink Reformat4c46d092018-04-07 15:32:37704 return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34705 }
Blink Reformat4c46d092018-04-07 15:32:37706 return this._previewFormatter.renderPropertyPreview(
707 property.type, /** @type {string} */ (property.subtype), property.value);
708 }
709
710 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22711 * @param {!SDK.RemoteObject.RemoteObject} remoteObject
Blink Reformat4c46d092018-04-07 15:32:37712 * @return {!Element}
713 */
714 _formatParameterAsNode(remoteObject) {
715 const result = createElement('span');
716
Tim van der Lippe9b2f8712020-02-12 17:46:22717 const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:34718 if (!domModel) {
Blink Reformat4c46d092018-04-07 15:32:37719 return result;
Tim van der Lippe1d6e57a2019-09-30 11:55:34720 }
Erik Luo54fdd912018-11-01 17:57:01721 domModel.pushObjectAsNodeToFrontend(remoteObject).then(async node => {
Blink Reformat4c46d092018-04-07 15:32:37722 if (!node) {
723 result.appendChild(this._formatParameterAsObject(remoteObject, false));
724 return;
725 }
Tim van der Lippe9b2f8712020-02-12 17:46:22726 const renderResult = await UI.UIUtils.Renderer.render(/** @type {!Object} */ (node));
Erik Luofc6a6302018-11-02 06:48:52727 if (renderResult) {
Erik Luo840be6b2018-12-03 20:54:27728 if (renderResult.tree) {
Erik Luo383f21d2018-11-07 23:16:37729 this._selectableChildren.push(renderResult.tree);
Erik Luo840be6b2018-12-03 20:54:27730 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
731 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
732 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
733 }
Erik Luofc6a6302018-11-02 06:48:52734 result.appendChild(renderResult.node);
735 } else {
736 result.appendChild(this._formatParameterAsObject(remoteObject, false));
737 }
Erik Luo54fdd912018-11-01 17:57:01738 this._formattedParameterAsNodeForTest();
Blink Reformat4c46d092018-04-07 15:32:37739 });
740
741 return result;
742 }
743
744 _formattedParameterAsNodeForTest() {
745 }
746
747 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22748 * @param {!SDK.RemoteObject.RemoteObject} output
Blink Reformat4c46d092018-04-07 15:32:37749 * @return {!Element}
750 */
751 _formatParameterAsString(output) {
752 const span = createElement('span');
Erik Luo383f21d2018-11-07 23:16:37753 span.appendChild(this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37754
755 const result = createElement('span');
756 result.createChild('span', 'object-value-string-quote').textContent = '"';
757 result.appendChild(span);
758 result.createChild('span', 'object-value-string-quote').textContent = '"';
759 return result;
760 }
761
762 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22763 * @param {!SDK.RemoteObject.RemoteObject} output
Blink Reformat4c46d092018-04-07 15:32:37764 * @return {!Element}
765 */
766 _formatParameterAsError(output) {
767 const result = createElement('span');
768 const errorSpan = this._tryFormatAsError(output.description || '');
Erik Luo383f21d2018-11-07 23:16:37769 result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37770 return result;
771 }
772
773 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22774 * @param {!SDK.RemoteObject.RemoteObject} output
Blink Reformat4c46d092018-04-07 15:32:37775 * @return {!Element}
776 */
777 _formatAsArrayEntry(output) {
778 return this._previewFormatter.renderPropertyPreview(output.type, output.subtype, output.description);
779 }
780
781 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22782 * @param {?SDK.RemoteObject.RemoteObject} object
Blink Reformat4c46d092018-04-07 15:32:37783 * @param {!Array.<string>} propertyPath
784 * @param {boolean} isArrayEntry
785 * @return {!Element}
786 */
787 _formatAsAccessorProperty(object, propertyPath, isArrayEntry) {
Tim van der Lippe9b2f8712020-02-12 17:46:22788 const rootElement =
789 ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
790 object, propertyPath, onInvokeGetterClick.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37791
792 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22793 * @param {!SDK.RemoteObject.CallFunctionResult} result
Tim van der Lippeeaacb722020-01-10 12:16:00794 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37795 */
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18796 function onInvokeGetterClick(result) {
797 const wasThrown = result.wasThrown;
798 const object = result.object;
Tim van der Lippe1d6e57a2019-09-30 11:55:34799 if (!object) {
Blink Reformat4c46d092018-04-07 15:32:37800 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34801 }
Blink Reformat4c46d092018-04-07 15:32:37802 rootElement.removeChildren();
803 if (wasThrown) {
804 const element = rootElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22805 element.textContent = Common.UIString.UIString('<exception>');
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18806 element.title = /** @type {string} */ (object.description);
Blink Reformat4c46d092018-04-07 15:32:37807 } else if (isArrayEntry) {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18808 rootElement.appendChild(this._formatAsArrayEntry(object));
Blink Reformat4c46d092018-04-07 15:32:37809 } else {
810 // Make a PropertyPreview from the RemoteObject similar to the backend logic.
811 const maxLength = 100;
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18812 const type = object.type;
813 const subtype = object.subtype;
Blink Reformat4c46d092018-04-07 15:32:37814 let description = '';
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18815 if (type !== 'function' && object.description) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34816 if (type === 'string' || subtype === 'regexp') {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18817 description = object.description.trimMiddle(maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34818 } else {
Tim van der Lippeffa78622019-09-16 12:07:12819 description = object.description.trimEndWithMaxLength(maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34820 }
Blink Reformat4c46d092018-04-07 15:32:37821 }
822 rootElement.appendChild(this._previewFormatter.renderPropertyPreview(type, subtype, description));
823 }
824 }
825
826 return rootElement;
827 }
828
829 /**
830 * @param {string} format
Tim van der Lippe9b2f8712020-02-12 17:46:22831 * @param {!Array.<!SDK.RemoteObject.RemoteObject>} parameters
Blink Reformat4c46d092018-04-07 15:32:37832 * @param {!Element} formattedResult
833 */
834 _formatWithSubstitutionString(format, parameters, formattedResult) {
835 const formatters = {};
836
837 /**
838 * @param {boolean} force
839 * @param {boolean} includePreview
Tim van der Lippe9b2f8712020-02-12 17:46:22840 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37841 * @return {!Element}
Tim van der Lippeeaacb722020-01-10 12:16:00842 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37843 */
844 function parameterFormatter(force, includePreview, obj) {
845 return this._formatParameter(obj, force, includePreview);
846 }
847
848 function stringFormatter(obj) {
849 return obj.description;
850 }
851
852 function floatFormatter(obj) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34853 if (typeof obj.value !== 'number') {
Blink Reformat4c46d092018-04-07 15:32:37854 return 'NaN';
Tim van der Lippe1d6e57a2019-09-30 11:55:34855 }
Blink Reformat4c46d092018-04-07 15:32:37856 return obj.value;
857 }
858
859 function integerFormatter(obj) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34860 if (obj.type === 'bigint') {
Alexey Kozyatinskiybeb38de2018-08-10 18:54:19861 return obj.description;
Tim van der Lippe1d6e57a2019-09-30 11:55:34862 }
863 if (typeof obj.value !== 'number') {
Blink Reformat4c46d092018-04-07 15:32:37864 return 'NaN';
Tim van der Lippe1d6e57a2019-09-30 11:55:34865 }
Blink Reformat4c46d092018-04-07 15:32:37866 return Math.floor(obj.value);
867 }
868
869 function bypassFormatter(obj) {
870 return (obj instanceof Node) ? obj : '';
871 }
872
873 let currentStyle = null;
874 function styleFormatter(obj) {
875 currentStyle = {};
876 const buffer = createElement('span');
877 buffer.setAttribute('style', obj.description);
878 for (let i = 0; i < buffer.style.length; i++) {
879 const property = buffer.style[i];
Tim van der Lippe1d6e57a2019-09-30 11:55:34880 if (isWhitelistedProperty(property)) {
Blink Reformat4c46d092018-04-07 15:32:37881 currentStyle[property] = buffer.style[property];
Tim van der Lippe1d6e57a2019-09-30 11:55:34882 }
Blink Reformat4c46d092018-04-07 15:32:37883 }
884 }
885
886 function isWhitelistedProperty(property) {
887 // Make sure that allowed properties do not interfere with link visibility.
888 const prefixes = [
889 'background', 'border', 'color', 'font', 'line', 'margin', 'padding', 'text', '-webkit-background',
890 '-webkit-border', '-webkit-font', '-webkit-margin', '-webkit-padding', '-webkit-text'
891 ];
892 for (let i = 0; i < prefixes.length; i++) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34893 if (property.startsWith(prefixes[i])) {
Blink Reformat4c46d092018-04-07 15:32:37894 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:34895 }
Blink Reformat4c46d092018-04-07 15:32:37896 }
897 return false;
898 }
899
900 // Firebug uses %o for formatting objects.
901 formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */);
902 formatters.s = stringFormatter;
903 formatters.f = floatFormatter;
904 // Firebug allows both %i and %d for formatting integers.
905 formatters.i = integerFormatter;
906 formatters.d = integerFormatter;
907
908 // Firebug uses %c for styling the message.
909 formatters.c = styleFormatter;
910
911 // Support %O to force object formatting, instead of the type-based %o formatting.
912 formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */);
913
914 formatters._ = bypassFormatter;
915
916 /**
917 * @param {!Element} a
918 * @param {*} b
Tim van der Lippeeaacb722020-01-10 12:16:00919 * @this {!ConsoleViewMessage}
Erik Luo17926392018-05-17 22:06:12920 * @return {!Element}
Blink Reformat4c46d092018-04-07 15:32:37921 */
922 function append(a, b) {
923 if (b instanceof Node) {
924 a.appendChild(b);
Erik Luo17926392018-05-17 22:06:12925 return a;
926 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34927 if (typeof b === 'undefined') {
Erik Luo17926392018-05-17 22:06:12928 return a;
Tim van der Lippe1d6e57a2019-09-30 11:55:34929 }
Erik Luo17926392018-05-17 22:06:12930 if (!currentStyle) {
Erik Luo383f21d2018-11-07 23:16:37931 a.appendChild(this._linkifyStringAsFragment(String(b)));
Erik Luo17926392018-05-17 22:06:12932 return a;
933 }
934 const lines = String(b).split('\n');
935 for (let i = 0; i < lines.length; i++) {
936 const line = lines[i];
Erik Luo383f21d2018-11-07 23:16:37937 const lineFragment = this._linkifyStringAsFragment(line);
Erik Luo17926392018-05-17 22:06:12938 const wrapper = createElement('span');
939 wrapper.style.setProperty('contain', 'paint');
940 wrapper.style.setProperty('display', 'inline-block');
941 wrapper.style.setProperty('max-width', '100%');
942 wrapper.appendChild(lineFragment);
943 applyCurrentStyle(wrapper);
944 for (const child of wrapper.children) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34945 if (child.classList.contains('devtools-link')) {
Erik Luo17926392018-05-17 22:06:12946 this._applyForcedVisibleStyle(child);
Tim van der Lippe1d6e57a2019-09-30 11:55:34947 }
Blink Reformat4c46d092018-04-07 15:32:37948 }
Erik Luo17926392018-05-17 22:06:12949 a.appendChild(wrapper);
Tim van der Lippe1d6e57a2019-09-30 11:55:34950 if (i < lines.length - 1) {
Erik Luo17926392018-05-17 22:06:12951 a.appendChild(createElement('br'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34952 }
Blink Reformat4c46d092018-04-07 15:32:37953 }
954 return a;
955 }
956
957 /**
958 * @param {!Element} element
959 */
960 function applyCurrentStyle(element) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34961 for (const key in currentStyle) {
Blink Reformat4c46d092018-04-07 15:32:37962 element.style[key] = currentStyle[key];
Tim van der Lippe1d6e57a2019-09-30 11:55:34963 }
Blink Reformat4c46d092018-04-07 15:32:37964 }
965
966 // String.format does treat formattedResult like a Builder, result is an object.
967 return String.format(format, parameters, formatters, formattedResult, append.bind(this));
968 }
969
970 /**
971 * @param {!Element} element
972 */
973 _applyForcedVisibleStyle(element) {
974 element.style.setProperty('-webkit-text-stroke', '0', 'important');
975 element.style.setProperty('text-decoration', 'underline', 'important');
976
Paul Lewis93d8e2c2020-01-24 16:34:55977 const themedColor =
Tim van der Lippe9b2f8712020-02-12 17:46:22978 self.UI.themeSupport.patchColorText('rgb(33%, 33%, 33%)', UI.UIUtils.ThemeSupport.ColorUsage.Foreground);
Blink Reformat4c46d092018-04-07 15:32:37979 element.style.setProperty('color', themedColor, 'important');
980
981 let backgroundColor = 'hsl(0, 0%, 100%)';
Tim van der Lippe9b2f8712020-02-12 17:46:22982 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Blink Reformat4c46d092018-04-07 15:32:37983 backgroundColor = 'hsl(0, 100%, 97%)';
Tim van der Lippe9b2f8712020-02-12 17:46:22984 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:37985 backgroundColor = 'hsl(50, 100%, 95%)';
Tim van der Lippe1d6e57a2019-09-30 11:55:34986 }
Blink Reformat4c46d092018-04-07 15:32:37987 const themedBackgroundColor =
Tim van der Lippe9b2f8712020-02-12 17:46:22988 self.UI.themeSupport.patchColorText(backgroundColor, UI.UIUtils.ThemeSupport.ColorUsage.Background);
Blink Reformat4c46d092018-04-07 15:32:37989 element.style.setProperty('background-color', themedBackgroundColor, 'important');
990 }
991
992 /**
993 * @return {boolean}
994 */
995 matchesFilterRegex(regexObject) {
996 regexObject.lastIndex = 0;
Erik Luo5976c8c2018-07-24 02:03:09997 const contentElement = this.contentElement();
998 const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : '';
999 return (anchorText && regexObject.test(anchorText.trim())) ||
1000 regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
Blink Reformat4c46d092018-04-07 15:32:371001 }
1002
1003 /**
1004 * @param {string} filter
1005 * @return {boolean}
1006 */
1007 matchesFilterText(filter) {
1008 const text = this.contentElement().deepTextContent();
1009 return text.toLowerCase().includes(filter.toLowerCase());
1010 }
1011
1012 updateTimestamp() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341013 if (!this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371014 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341015 }
Blink Reformat4c46d092018-04-07 15:32:371016
Paul Lewis4b64b3f2020-01-23 11:41:201017 if (self.Common.settings.moduleSetting('consoleTimestampsEnabled').get()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341018 if (!this._timestampElement) {
Blink Reformat4c46d092018-04-07 15:32:371019 this._timestampElement = createElementWithClass('span', 'console-timestamp');
Tim van der Lippe1d6e57a2019-09-30 11:55:341020 }
Tim van der Lippe9b2f8712020-02-12 17:46:221021 this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' ';
1022 this._timestampElement.title = UI.UIUtils.formatTimestamp(this._message.timestamp, true);
Blink Reformat4c46d092018-04-07 15:32:371023 this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild);
1024 } else if (this._timestampElement) {
1025 this._timestampElement.remove();
1026 delete this._timestampElement;
1027 }
Blink Reformat4c46d092018-04-07 15:32:371028 }
1029
1030 /**
1031 * @return {number}
1032 */
1033 nestingLevel() {
1034 return this._nestingLevel;
1035 }
1036
1037 /**
1038 * @param {boolean} inSimilarGroup
1039 * @param {boolean=} isLast
1040 */
1041 setInSimilarGroup(inSimilarGroup, isLast) {
1042 this._inSimilarGroup = inSimilarGroup;
1043 this._lastInSimilarGroup = inSimilarGroup && !!isLast;
1044 if (this._similarGroupMarker && !inSimilarGroup) {
1045 this._similarGroupMarker.remove();
1046 this._similarGroupMarker = null;
1047 } else if (this._element && !this._similarGroupMarker && inSimilarGroup) {
1048 this._similarGroupMarker = createElementWithClass('div', 'nesting-level-marker');
1049 this._element.insertBefore(this._similarGroupMarker, this._element.firstChild);
1050 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1051 }
1052 }
1053
1054 /**
1055 * @return {boolean}
1056 */
1057 isLastInSimilarGroup() {
1058 return this._inSimilarGroup && this._lastInSimilarGroup;
1059 }
1060
1061 resetCloseGroupDecorationCount() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341062 if (!this._closeGroupDecorationCount) {
Blink Reformat4c46d092018-04-07 15:32:371063 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341064 }
Blink Reformat4c46d092018-04-07 15:32:371065 this._closeGroupDecorationCount = 0;
1066 this._updateCloseGroupDecorations();
1067 }
1068
1069 incrementCloseGroupDecorationCount() {
1070 ++this._closeGroupDecorationCount;
1071 this._updateCloseGroupDecorations();
1072 }
1073
1074 _updateCloseGroupDecorations() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341075 if (!this._nestingLevelMarkers) {
Blink Reformat4c46d092018-04-07 15:32:371076 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341077 }
Blink Reformat4c46d092018-04-07 15:32:371078 for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) {
1079 const marker = this._nestingLevelMarkers[i];
1080 marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount);
1081 }
1082 }
1083
1084 /**
Erik Luo0b8282e2018-10-08 20:37:461085 * @return {number}
1086 */
1087 _focusedChildIndex() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341088 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461089 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341090 }
Erik Luo383f21d2018-11-07 23:16:371091 return this._selectableChildren.findIndex(child => child.element.hasFocus());
Erik Luo0b8282e2018-10-08 20:37:461092 }
1093
1094 /**
Erik Luo8ef5d0c2018-09-25 21:16:001095 * @param {!Event} event
1096 */
1097 _onKeyDown(event) {
Tim van der Lippe9b2f8712020-02-12 17:46:221098 if (UI.UIUtils.isEditing() || !this._element.hasFocus() || this._element.hasSelection()) {
Erik Luo8ef5d0c2018-09-25 21:16:001099 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341100 }
1101 if (this.maybeHandleOnKeyDown(event)) {
Erik Luo8ef5d0c2018-09-25 21:16:001102 event.consume(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:341103 }
Erik Luo8ef5d0c2018-09-25 21:16:001104 }
1105
1106 /**
1107 * @protected
1108 * @param {!Event} event
1109 */
1110 maybeHandleOnKeyDown(event) {
1111 // Handle trace expansion.
Erik Luo0b8282e2018-10-08 20:37:461112 const focusedChildIndex = this._focusedChildIndex();
1113 const isWrapperFocused = focusedChildIndex === -1;
1114 if (this._expandTrace && isWrapperFocused) {
Erik Luo8ef5d0c2018-09-25 21:16:001115 if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) {
1116 this._expandTrace(!this._traceExpanded);
1117 return true;
1118 }
1119 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341120 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461121 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:341122 }
Erik Luo0b8282e2018-10-08 20:37:461123
1124 if (event.key === 'ArrowLeft') {
1125 this._element.focus();
1126 return true;
1127 }
1128 if (event.key === 'ArrowRight') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341129 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461130 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341131 }
Erik Luo0b8282e2018-10-08 20:37:461132 }
1133 if (event.key === 'ArrowUp') {
Erik Luo182bece2018-11-29 03:15:221134 const firstVisibleChild = this._nearestVisibleChild(0);
1135 if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
Erik Luo0b8282e2018-10-08 20:37:461136 this._element.focus();
1137 return true;
Erik Luo182bece2018-11-29 03:15:221138 } else if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461139 return true;
1140 }
1141 }
1142 if (event.key === 'ArrowDown') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341143 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461144 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341145 }
1146 if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) {
Erik Luo0b8282e2018-10-08 20:37:461147 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341148 }
Erik Luo0b8282e2018-10-08 20:37:461149 }
Erik Luo8ef5d0c2018-09-25 21:16:001150 return false;
1151 }
1152
Erik Luo182bece2018-11-29 03:15:221153 /**
1154 * @param {number} fromIndex
1155 * @param {boolean=} backwards
1156 * @return {boolean}
1157 */
1158 _selectNearestVisibleChild(fromIndex, backwards) {
1159 const nearestChild = this._nearestVisibleChild(fromIndex, backwards);
1160 if (nearestChild) {
Erik Luo31c21f62018-12-13 03:39:391161 nearestChild.forceSelect();
Erik Luo182bece2018-11-29 03:15:221162 return true;
1163 }
1164 return false;
1165 }
1166
1167 /**
1168 * @param {number} fromIndex
1169 * @param {boolean=} backwards
Erik Luo31c21f62018-12-13 03:39:391170 * @return {?{element: !Element, forceSelect: function()}}
Erik Luo182bece2018-11-29 03:15:221171 */
1172 _nearestVisibleChild(fromIndex, backwards) {
1173 const childCount = this._selectableChildren.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:341174 if (fromIndex < 0 || fromIndex >= childCount) {
Erik Luo182bece2018-11-29 03:15:221175 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341176 }
Erik Luo182bece2018-11-29 03:15:221177 const direction = backwards ? -1 : 1;
1178 let index = fromIndex;
1179
1180 while (!this._selectableChildren[index].element.offsetParent) {
1181 index += direction;
Tim van der Lippe1d6e57a2019-09-30 11:55:341182 if (index < 0 || index >= childCount) {
Erik Luo182bece2018-11-29 03:15:221183 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341184 }
Erik Luo182bece2018-11-29 03:15:221185 }
1186 return this._selectableChildren[index];
1187 }
1188
Erik Luo0b8282e2018-10-08 20:37:461189 focusLastChildOrSelf() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341190 if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461191 this._element.focus();
Tim van der Lippe1d6e57a2019-09-30 11:55:341192 }
Erik Luo0b8282e2018-10-08 20:37:461193 }
1194
1195 /**
Blink Reformat4c46d092018-04-07 15:32:371196 * @return {!Element}
1197 */
1198 contentElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341199 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371200 return this._contentElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:341201 }
Blink Reformat4c46d092018-04-07 15:32:371202
1203 const contentElement = createElementWithClass('div', 'console-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341204 if (this._messageLevelIcon) {
Blink Reformat4c46d092018-04-07 15:32:371205 contentElement.appendChild(this._messageLevelIcon);
Tim van der Lippe1d6e57a2019-09-30 11:55:341206 }
Blink Reformat4c46d092018-04-07 15:32:371207 this._contentElement = contentElement;
1208
1209 let formattedMessage;
1210 const shouldIncludeTrace = !!this._message.stackTrace &&
Tim van der Lippe9b2f8712020-02-12 17:46:221211 (this._message.source === SDK.ConsoleModel.MessageSource.Network ||
1212 this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1213 this._message.level === SDK.ConsoleModel.MessageLevel.Error ||
1214 this._message.level === SDK.ConsoleModel.MessageLevel.Warning ||
1215 this._message.type === SDK.ConsoleModel.MessageType.Trace);
Tim van der Lippe1d6e57a2019-09-30 11:55:341216 if (this._message.runtimeModel() && shouldIncludeTrace) {
Blink Reformat4c46d092018-04-07 15:32:371217 formattedMessage = this._buildMessageWithStackTrace();
Tim van der Lippe9b2f8712020-02-12 17:46:221218 } else if (this._message.type === SDK.ConsoleModel.MessageType.Table) {
Blink Reformat4c46d092018-04-07 15:32:371219 formattedMessage = this._buildTableMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341220 } else {
Blink Reformat4c46d092018-04-07 15:32:371221 formattedMessage = this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341222 }
Blink Reformat4c46d092018-04-07 15:32:371223 contentElement.appendChild(formattedMessage);
1224
1225 this.updateTimestamp();
1226 return this._contentElement;
1227 }
1228
1229 /**
1230 * @return {!Element}
1231 */
1232 toMessageElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341233 if (this._element) {
Blink Reformat4c46d092018-04-07 15:32:371234 return this._element;
Tim van der Lippe1d6e57a2019-09-30 11:55:341235 }
Blink Reformat4c46d092018-04-07 15:32:371236
1237 this._element = createElement('div');
Pavel Feldmandb310912019-01-30 00:31:201238 this._element.tabIndex = -1;
1239 this._element.addEventListener('keydown', this._onKeyDown.bind(this));
Blink Reformat4c46d092018-04-07 15:32:371240 this.updateMessageElement();
1241 return this._element;
1242 }
1243
1244 updateMessageElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341245 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371246 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341247 }
Blink Reformat4c46d092018-04-07 15:32:371248
1249 this._element.className = 'console-message-wrapper';
1250 this._element.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:341251 if (this._message.isGroupStartMessage()) {
Blink Reformat4c46d092018-04-07 15:32:371252 this._element.classList.add('console-group-title');
Tim van der Lippe1d6e57a2019-09-30 11:55:341253 }
Tim van der Lippe9b2f8712020-02-12 17:46:221254 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:371255 this._element.classList.add('console-from-api');
Tim van der Lippe1d6e57a2019-09-30 11:55:341256 }
Blink Reformat4c46d092018-04-07 15:32:371257 if (this._inSimilarGroup) {
1258 this._similarGroupMarker = this._element.createChild('div', 'nesting-level-marker');
1259 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1260 }
1261
1262 this._nestingLevelMarkers = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341263 for (let i = 0; i < this._nestingLevel; ++i) {
Blink Reformat4c46d092018-04-07 15:32:371264 this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker'));
Tim van der Lippe1d6e57a2019-09-30 11:55:341265 }
Blink Reformat4c46d092018-04-07 15:32:371266 this._updateCloseGroupDecorations();
1267 this._element.message = this;
1268
1269 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221270 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371271 this._element.classList.add('console-verbose-level');
Blink Reformat4c46d092018-04-07 15:32:371272 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221273 case SDK.ConsoleModel.MessageLevel.Info:
Blink Reformat4c46d092018-04-07 15:32:371274 this._element.classList.add('console-info-level');
Tim van der Lippe9b2f8712020-02-12 17:46:221275 if (this._message.type === SDK.ConsoleModel.MessageType.System) {
Blink Reformat4c46d092018-04-07 15:32:371276 this._element.classList.add('console-system-type');
Tim van der Lippe1d6e57a2019-09-30 11:55:341277 }
Blink Reformat4c46d092018-04-07 15:32:371278 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221279 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371280 this._element.classList.add('console-warning-level');
Blink Reformat4c46d092018-04-07 15:32:371281 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221282 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371283 this._element.classList.add('console-error-level');
Blink Reformat4c46d092018-04-07 15:32:371284 break;
1285 }
Erik Luofd3e7d42018-09-25 02:12:351286 this._updateMessageLevelIcon();
Tim van der Lippe1d6e57a2019-09-30 11:55:341287 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371288 this._element.classList.add('console-warning-level');
Tim van der Lippe1d6e57a2019-09-30 11:55:341289 }
Blink Reformat4c46d092018-04-07 15:32:371290
1291 this._element.appendChild(this.contentElement());
Tim van der Lippe1d6e57a2019-09-30 11:55:341292 if (this._repeatCount > 1) {
Blink Reformat4c46d092018-04-07 15:32:371293 this._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341294 }
Blink Reformat4c46d092018-04-07 15:32:371295 }
1296
1297 /**
1298 * @return {boolean}
1299 */
1300 _shouldRenderAsWarning() {
Tim van der Lippe9b2f8712020-02-12 17:46:221301 return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose ||
1302 this._message.level === SDK.ConsoleModel.MessageLevel.Info) &&
1303 (this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1304 this._message.source === SDK.ConsoleModel.MessageSource.Deprecation ||
1305 this._message.source === SDK.ConsoleModel.MessageSource.Intervention ||
1306 this._message.source === SDK.ConsoleModel.MessageSource.Recommendation);
Blink Reformat4c46d092018-04-07 15:32:371307 }
1308
Erik Luofd3e7d42018-09-25 02:12:351309 _updateMessageLevelIcon() {
1310 let iconType = '';
1311 let accessibleName = '';
Tim van der Lippe9b2f8712020-02-12 17:46:221312 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351313 iconType = 'smallicon-warning';
1314 accessibleName = ls`Warning`;
Tim van der Lippe9b2f8712020-02-12 17:46:221315 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351316 iconType = 'smallicon-error';
1317 accessibleName = ls`Error`;
1318 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341319 if (!iconType && !this._messageLevelIcon) {
Blink Reformat4c46d092018-04-07 15:32:371320 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341321 }
Blink Reformat4c46d092018-04-07 15:32:371322 if (iconType && !this._messageLevelIcon) {
Tim van der Lippe9b2f8712020-02-12 17:46:221323 this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon');
Tim van der Lippe1d6e57a2019-09-30 11:55:341324 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371325 this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341326 }
Blink Reformat4c46d092018-04-07 15:32:371327 }
1328 this._messageLevelIcon.setIconType(iconType);
Erik Luofd3e7d42018-09-25 02:12:351329 UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371330 }
1331
1332 /**
1333 * @return {number}
1334 */
1335 repeatCount() {
1336 return this._repeatCount || 1;
1337 }
1338
1339 resetIncrementRepeatCount() {
1340 this._repeatCount = 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341341 if (!this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371342 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341343 }
Blink Reformat4c46d092018-04-07 15:32:371344
1345 this._repeatCountElement.remove();
Tim van der Lippe1d6e57a2019-09-30 11:55:341346 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371347 this._contentElement.classList.remove('repeated-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341348 }
Blink Reformat4c46d092018-04-07 15:32:371349 delete this._repeatCountElement;
1350 }
1351
1352 incrementRepeatCount() {
1353 this._repeatCount++;
1354 this._showRepeatCountElement();
1355 }
1356
1357 /**
1358 * @param {number} repeatCount
1359 */
1360 setRepeatCount(repeatCount) {
1361 this._repeatCount = repeatCount;
1362 this._showRepeatCountElement();
1363 }
1364
1365 _showRepeatCountElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341366 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371367 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341368 }
Blink Reformat4c46d092018-04-07 15:32:371369
1370 if (!this._repeatCountElement) {
Joel Einbinder7fbe24c2019-01-24 05:19:011371 this._repeatCountElement = createElementWithClass('span', 'console-message-repeat-count', 'dt-small-bubble');
Blink Reformat4c46d092018-04-07 15:32:371372 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221373 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371374 this._repeatCountElement.type = 'warning';
1375 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221376 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371377 this._repeatCountElement.type = 'error';
1378 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221379 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371380 this._repeatCountElement.type = 'verbose';
1381 break;
1382 default:
1383 this._repeatCountElement.type = 'info';
1384 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341385 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371386 this._repeatCountElement.type = 'warning';
Tim van der Lippe1d6e57a2019-09-30 11:55:341387 }
Blink Reformat4c46d092018-04-07 15:32:371388
1389 this._element.insertBefore(this._repeatCountElement, this._contentElement);
1390 this._contentElement.classList.add('repeated-message');
1391 }
1392 this._repeatCountElement.textContent = this._repeatCount;
Erik Luofd3e7d42018-09-25 02:12:351393 let accessibleName = ls`Repeat ${this._repeatCount}`;
Tim van der Lippe9b2f8712020-02-12 17:46:221394 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351395 accessibleName = ls`Warning ${accessibleName}`;
Tim van der Lippe9b2f8712020-02-12 17:46:221396 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351397 accessibleName = ls`Error ${accessibleName}`;
Tim van der Lippe1d6e57a2019-09-30 11:55:341398 }
Erik Luofd3e7d42018-09-25 02:12:351399 UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371400 }
1401
1402 get text() {
1403 return this._message.messageText;
1404 }
1405
1406 /**
1407 * @return {string}
1408 */
1409 toExportString() {
1410 const lines = [];
1411 const nodes = this.contentElement().childTextNodes();
Tim van der Lippe9b2f8712020-02-12 17:46:221412 const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join('');
Tim van der Lippe1d6e57a2019-09-30 11:55:341413 for (let i = 0; i < this.repeatCount(); ++i) {
Blink Reformat4c46d092018-04-07 15:32:371414 lines.push(messageContent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341415 }
Blink Reformat4c46d092018-04-07 15:32:371416 return lines.join('\n');
1417 }
1418
1419 /**
1420 * @param {?RegExp} regex
1421 */
1422 setSearchRegex(regex) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341423 if (this._searchHiglightNodeChanges && this._searchHiglightNodeChanges.length) {
Tim van der Lippe9b2f8712020-02-12 17:46:221424 UI.UIUtils.revertDomChanges(this._searchHiglightNodeChanges);
Tim van der Lippe1d6e57a2019-09-30 11:55:341425 }
Blink Reformat4c46d092018-04-07 15:32:371426 this._searchRegex = regex;
1427 this._searchHighlightNodes = [];
1428 this._searchHiglightNodeChanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341429 if (!this._searchRegex) {
Blink Reformat4c46d092018-04-07 15:32:371430 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341431 }
Blink Reformat4c46d092018-04-07 15:32:371432
1433 const text = this.contentElement().deepTextContent();
1434 let match;
1435 this._searchRegex.lastIndex = 0;
1436 const sourceRanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341437 while ((match = this._searchRegex.exec(text)) && match[0]) {
Tim van der Lippe9b2f8712020-02-12 17:46:221438 sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length));
Tim van der Lippe1d6e57a2019-09-30 11:55:341439 }
Blink Reformat4c46d092018-04-07 15:32:371440
1441 if (sourceRanges.length) {
1442 this._searchHighlightNodes =
Tim van der Lippe9b2f8712020-02-12 17:46:221443 UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHiglightNodeChanges);
Blink Reformat4c46d092018-04-07 15:32:371444 }
1445 }
1446
1447 /**
1448 * @return {?RegExp}
1449 */
1450 searchRegex() {
1451 return this._searchRegex;
1452 }
1453
1454 /**
1455 * @return {number}
1456 */
1457 searchCount() {
1458 return this._searchHighlightNodes.length;
1459 }
1460
1461 /**
1462 * @return {!Element}
1463 */
1464 searchHighlightNode(index) {
1465 return this._searchHighlightNodes[index];
1466 }
1467
1468 /**
1469 * @param {string} string
1470 * @return {?Element}
1471 */
1472 _tryFormatAsError(string) {
1473 /**
1474 * @param {string} prefix
1475 */
1476 function startsWith(prefix) {
1477 return string.startsWith(prefix);
1478 }
1479
1480 const errorPrefixes =
1481 ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError'];
Tim van der Lippe1d6e57a2019-09-30 11:55:341482 if (!this._message.runtimeModel() || !errorPrefixes.some(startsWith)) {
Blink Reformat4c46d092018-04-07 15:32:371483 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341484 }
Blink Reformat4c46d092018-04-07 15:32:371485 const debuggerModel = this._message.runtimeModel().debuggerModel();
1486 const baseURL = this._message.runtimeModel().target().inspectedURL();
1487
1488 const lines = string.split('\n');
1489 const links = [];
1490 let position = 0;
1491 for (let i = 0; i < lines.length; ++i) {
1492 position += i > 0 ? lines[i - 1].length + 1 : 0;
1493 const isCallFrameLine = /^\s*at\s/.test(lines[i]);
Tim van der Lippe1d6e57a2019-09-30 11:55:341494 if (!isCallFrameLine && links.length) {
Blink Reformat4c46d092018-04-07 15:32:371495 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341496 }
Blink Reformat4c46d092018-04-07 15:32:371497
Tim van der Lippe1d6e57a2019-09-30 11:55:341498 if (!isCallFrameLine) {
Blink Reformat4c46d092018-04-07 15:32:371499 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341500 }
Blink Reformat4c46d092018-04-07 15:32:371501
1502 let openBracketIndex = -1;
1503 let closeBracketIndex = -1;
Yang Guo39256bd2019-07-18 06:02:251504 const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g;
1505 const inBrackets = /\([^\)\(]+\)/g;
1506 let lastMatch = null;
1507 let currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341508 while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251509 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341510 }
Yang Guo39256bd2019-07-18 06:02:251511 if (!lastMatch) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341512 while ((currentMatch = inBrackets.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251513 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341514 }
Yang Guo39256bd2019-07-18 06:02:251515 }
1516 if (lastMatch) {
1517 openBracketIndex = lastMatch.index;
1518 closeBracketIndex = lastMatch.index + lastMatch[0].length - 1;
Blink Reformat4c46d092018-04-07 15:32:371519 }
1520 const hasOpenBracket = openBracketIndex !== -1;
1521 const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3;
1522 const right = hasOpenBracket ? closeBracketIndex : lines[i].length;
1523 const linkCandidate = lines[i].substring(left, right);
Tim van der Lippe9b2f8712020-02-12 17:46:221524 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate);
Tim van der Lippe1d6e57a2019-09-30 11:55:341525 if (!splitResult) {
Blink Reformat4c46d092018-04-07 15:32:371526 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341527 }
Blink Reformat4c46d092018-04-07 15:32:371528
Tim van der Lippe1d6e57a2019-09-30 11:55:341529 if (splitResult.url === '<anonymous>') {
Blink Reformat4c46d092018-04-07 15:32:371530 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341531 }
Blink Reformat4c46d092018-04-07 15:32:371532 let url = parseOrScriptMatch(splitResult.url);
Tim van der Lippe9b2f8712020-02-12 17:46:221533 if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) {
1534 url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url));
Tim van der Lippe1d6e57a2019-09-30 11:55:341535 }
1536 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371537 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341538 }
Blink Reformat4c46d092018-04-07 15:32:371539
1540 links.push({
1541 url: url,
1542 positionLeft: position + left,
1543 positionRight: position + right,
1544 lineNumber: splitResult.lineNumber,
1545 columnNumber: splitResult.columnNumber
1546 });
1547 }
1548
Tim van der Lippe1d6e57a2019-09-30 11:55:341549 if (!links.length) {
Blink Reformat4c46d092018-04-07 15:32:371550 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341551 }
Blink Reformat4c46d092018-04-07 15:32:371552
1553 const formattedResult = createElement('span');
1554 let start = 0;
1555 for (let i = 0; i < links.length; ++i) {
Erik Luo383f21d2018-11-07 23:16:371556 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft)));
Erik Luo182bece2018-11-29 03:15:221557 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
Jack Lynch236e1d22020-01-07 20:33:511558 debuggerModel.target(), null, links[i].url, links[i].lineNumber, {columnNumber: links[i].columnNumber});
Erik Luo182bece2018-11-29 03:15:221559 scriptLocationLink.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391560 this._selectableChildren.push({element: scriptLocationLink, forceSelect: () => scriptLocationLink.focus()});
Erik Luo182bece2018-11-29 03:15:221561 formattedResult.appendChild(scriptLocationLink);
Blink Reformat4c46d092018-04-07 15:32:371562 start = links[i].positionRight;
1563 }
1564
Tim van der Lippe1d6e57a2019-09-30 11:55:341565 if (start !== string.length) {
Erik Luo383f21d2018-11-07 23:16:371566 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start)));
Tim van der Lippe1d6e57a2019-09-30 11:55:341567 }
Blink Reformat4c46d092018-04-07 15:32:371568
1569 return formattedResult;
1570
1571 /**
1572 * @param {?string} url
1573 * @return {?string}
1574 */
1575 function parseOrScriptMatch(url) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341576 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371577 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341578 }
Tim van der Lippe9b2f8712020-02-12 17:46:221579 const parsedURL = Common.ParsedURL.ParsedURL.fromString(url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341580 if (parsedURL) {
Blink Reformat4c46d092018-04-07 15:32:371581 return parsedURL.url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341582 }
1583 if (debuggerModel.scriptsForSourceURL(url).length) {
Blink Reformat4c46d092018-04-07 15:32:371584 return url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341585 }
Blink Reformat4c46d092018-04-07 15:32:371586 return null;
1587 }
1588 }
1589
1590 /**
1591 * @param {string} string
1592 * @param {function(string,string,number=,number=):!Node} linkifier
1593 * @return {!DocumentFragment}
Tim van der Lippeeaacb722020-01-10 12:16:001594 * @suppress {accessControls}
Blink Reformat4c46d092018-04-07 15:32:371595 */
Erik Luofc2214f2018-11-21 19:54:581596 _linkifyWithCustomLinkifier(string, linkifier) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341597 if (string.length > Console.ConsoleViewMessage._MaxTokenizableStringLength) {
Tim van der Lippe9b2f8712020-02-12 17:46:221598 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Connor Moody1a5c0d32019-12-19 07:23:361599 createElement('span'), string, Console.ConsoleViewMessage._LongStringVisibleLength);
1600 const fragment = createDocumentFragment();
1601 fragment.appendChild(propertyValue.element);
1602 return fragment;
Tim van der Lippe1d6e57a2019-09-30 11:55:341603 }
Blink Reformat4c46d092018-04-07 15:32:371604 const container = createDocumentFragment();
Tim van der Lippeeaacb722020-01-10 12:16:001605 const tokens = ConsoleViewMessage._tokenizeMessageText(string);
Blink Reformat4c46d092018-04-07 15:32:371606 for (const token of tokens) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341607 if (!token.text) {
Erik Luofc2214f2018-11-21 19:54:581608 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341609 }
Blink Reformat4c46d092018-04-07 15:32:371610 switch (token.type) {
1611 case 'url': {
1612 const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text);
Tim van der Lippe9b2f8712020-02-12 17:46:221613 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL);
Blink Reformat4c46d092018-04-07 15:32:371614 let linkNode;
Tim van der Lippe1d6e57a2019-09-30 11:55:341615 if (splitResult) {
Blink Reformat4c46d092018-04-07 15:32:371616 linkNode = linkifier(token.text, splitResult.url, splitResult.lineNumber, splitResult.columnNumber);
Tim van der Lippe1d6e57a2019-09-30 11:55:341617 } else {
Blink Reformat4c46d092018-04-07 15:32:371618 linkNode = linkifier(token.text, token.value);
Tim van der Lippe1d6e57a2019-09-30 11:55:341619 }
Blink Reformat4c46d092018-04-07 15:32:371620 container.appendChild(linkNode);
1621 break;
1622 }
1623 default:
1624 container.appendChild(createTextNode(token.text));
1625 break;
1626 }
1627 }
1628 return container;
1629 }
1630
1631 /**
Blink Reformat4c46d092018-04-07 15:32:371632 * @param {string} string
1633 * @return {!DocumentFragment}
1634 */
Erik Luo383f21d2018-11-07 23:16:371635 _linkifyStringAsFragment(string) {
Erik Luofc2214f2018-11-21 19:54:581636 return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => {
Tim van der Lippe9b2f8712020-02-12 17:46:221637 const linkElement = Components.Linkifier.Linkifier.linkifyURL(url, {text, lineNumber, columnNumber});
Erik Luo383f21d2018-11-07 23:16:371638 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391639 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo383f21d2018-11-07 23:16:371640 return linkElement;
Blink Reformat4c46d092018-04-07 15:32:371641 });
1642 }
1643
1644 /**
1645 * @param {string} string
1646 * @return {!Array<{type: string, text: (string|undefined)}>}
Tim van der Lippeeaacb722020-01-10 12:16:001647 * @suppress {accessControls}
Blink Reformat4c46d092018-04-07 15:32:371648 */
1649 static _tokenizeMessageText(string) {
Tim van der Lippeeaacb722020-01-10 12:16:001650 if (!ConsoleViewMessage._tokenizerRegexes) {
Blink Reformat4c46d092018-04-07 15:32:371651 const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f';
1652 const linkStringRegex = new RegExp(
1653 '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes +
1654 '"\')}\\],:;.!?]',
1655 'u');
1656 const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/;
1657 const timeRegex = /took [\d]+ms/;
1658 const eventRegex = /'\w+' event/;
1659 const milestoneRegex = /\sM[6-7]\d/;
1660 const autofillRegex = /\(suggested: \"[\w-]+\"\)/;
1661 const handlers = new Map();
1662 handlers.set(linkStringRegex, 'url');
1663 handlers.set(pathLineRegex, 'url');
1664 handlers.set(timeRegex, 'time');
1665 handlers.set(eventRegex, 'event');
1666 handlers.set(milestoneRegex, 'milestone');
1667 handlers.set(autofillRegex, 'autofill');
Tim van der Lippeeaacb722020-01-10 12:16:001668 ConsoleViewMessage._tokenizerRegexes = Array.from(handlers.keys());
1669 ConsoleViewMessage._tokenizerTypes = Array.from(handlers.values());
Blink Reformat4c46d092018-04-07 15:32:371670 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341671 if (string.length > Console.ConsoleViewMessage._MaxTokenizableStringLength) {
Blink Reformat4c46d092018-04-07 15:32:371672 return [{text: string, type: undefined}];
Tim van der Lippe1d6e57a2019-09-30 11:55:341673 }
Tim van der Lippe9b2f8712020-02-12 17:46:221674 const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, ConsoleViewMessage._tokenizerRegexes);
Tim van der Lippeeaacb722020-01-10 12:16:001675 return results.map(result => ({text: result.value, type: ConsoleViewMessage._tokenizerTypes[result.regexIndex]}));
Blink Reformat4c46d092018-04-07 15:32:371676 }
1677
1678 /**
1679 * @return {string}
1680 */
1681 groupKey() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341682 if (!this._groupKey) {
Blink Reformat4c46d092018-04-07 15:32:371683 this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle();
Tim van der Lippe1d6e57a2019-09-30 11:55:341684 }
Blink Reformat4c46d092018-04-07 15:32:371685 return this._groupKey;
1686 }
1687
1688 /**
1689 * @return {string}
1690 */
1691 groupTitle() {
Tim van der Lippeeaacb722020-01-10 12:16:001692 const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText);
Blink Reformat4c46d092018-04-07 15:32:371693 const result = tokens.reduce((acc, token) => {
1694 let text = token.text;
Tim van der Lippe1d6e57a2019-09-30 11:55:341695 if (token.type === 'url') {
Tim van der Lippe9b2f8712020-02-12 17:46:221696 text = Common.UIString.UIString('<URL>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341697 } else if (token.type === 'time') {
Tim van der Lippe9b2f8712020-02-12 17:46:221698 text = Common.UIString.UIString('took <N>ms');
Tim van der Lippe1d6e57a2019-09-30 11:55:341699 } else if (token.type === 'event') {
Tim van der Lippe9b2f8712020-02-12 17:46:221700 text = Common.UIString.UIString('<some> event');
Tim van der Lippe1d6e57a2019-09-30 11:55:341701 } else if (token.type === 'milestone') {
Tim van der Lippe9b2f8712020-02-12 17:46:221702 text = Common.UIString.UIString(' M<XX>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341703 } else if (token.type === 'autofill') {
Tim van der Lippe9b2f8712020-02-12 17:46:221704 text = Common.UIString.UIString('<attribute>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341705 }
Blink Reformat4c46d092018-04-07 15:32:371706 return acc + text;
1707 }, '');
1708 return result.replace(/[%]o/g, '');
1709 }
Paul Lewisbf7aa3c2019-11-20 17:03:381710}
Blink Reformat4c46d092018-04-07 15:32:371711
1712/**
1713 * @unrestricted
1714 */
Paul Lewisbf7aa3c2019-11-20 17:03:381715export class ConsoleGroupViewMessage extends ConsoleViewMessage {
Blink Reformat4c46d092018-04-07 15:32:371716 /**
Tim van der Lippe9b2f8712020-02-12 17:46:221717 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
1718 * @param {!Components.Linkifier.Linkifier} linkifier
Blink Reformat4c46d092018-04-07 15:32:371719 * @param {number} nestingLevel
Erik Luo8ef5d0c2018-09-25 21:16:001720 * @param {function()} onToggle
Erik Luo840be6b2018-12-03 20:54:271721 * @param {function(!Common.Event)} onResize
Blink Reformat4c46d092018-04-07 15:32:371722 */
Tim van der Lippeb45d9a02019-11-05 17:24:411723 constructor(consoleMessage, linkifier, nestingLevel, onToggle, onResize) {
Blink Reformat4c46d092018-04-07 15:32:371724 console.assert(consoleMessage.isGroupStartMessage());
Tim van der Lippeb45d9a02019-11-05 17:24:411725 super(consoleMessage, linkifier, nestingLevel, onResize);
Tim van der Lippe9b2f8712020-02-12 17:46:221726 this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed;
1727 /** @type {?UI.Icon.Icon} */
Blink Reformat4c46d092018-04-07 15:32:371728 this._expandGroupIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:001729 this._onToggle = onToggle;
Blink Reformat4c46d092018-04-07 15:32:371730 }
1731
1732 /**
1733 * @param {boolean} collapsed
1734 */
Erik Luo8ef5d0c2018-09-25 21:16:001735 _setCollapsed(collapsed) {
Blink Reformat4c46d092018-04-07 15:32:371736 this._collapsed = collapsed;
Tim van der Lippe1d6e57a2019-09-30 11:55:341737 if (this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371738 this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down');
Tim van der Lippe1d6e57a2019-09-30 11:55:341739 }
Erik Luo8ef5d0c2018-09-25 21:16:001740 this._onToggle.call(null);
Blink Reformat4c46d092018-04-07 15:32:371741 }
1742
1743 /**
1744 * @return {boolean}
1745 */
1746 collapsed() {
1747 return this._collapsed;
1748 }
1749
1750 /**
1751 * @override
Erik Luo8ef5d0c2018-09-25 21:16:001752 * @param {!Event} event
1753 */
1754 maybeHandleOnKeyDown(event) {
Erik Luo0b8282e2018-10-08 20:37:461755 const focusedChildIndex = this._focusedChildIndex();
1756 if (focusedChildIndex === -1) {
1757 if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) {
1758 this._setCollapsed(!this._collapsed);
1759 return true;
1760 }
Erik Luo8ef5d0c2018-09-25 21:16:001761 }
1762 return super.maybeHandleOnKeyDown(event);
1763 }
1764
1765 /**
1766 * @override
Blink Reformat4c46d092018-04-07 15:32:371767 * @return {!Element}
1768 */
1769 toMessageElement() {
1770 if (!this._element) {
1771 super.toMessageElement();
Erik Luo8ef5d0c2018-09-25 21:16:001772 const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down';
Tim van der Lippe9b2f8712020-02-12 17:46:221773 this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon');
Erik Luob5bfff42018-09-20 02:52:391774 // Intercept focus to avoid highlight on click.
1775 this._contentElement.tabIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341776 if (this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371777 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341778 } else {
Blink Reformat4c46d092018-04-07 15:32:371779 this._element.insertBefore(this._expandGroupIcon, this._contentElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:341780 }
Erik Luo8ef5d0c2018-09-25 21:16:001781 this._element.addEventListener('click', () => this._setCollapsed(!this._collapsed));
Blink Reformat4c46d092018-04-07 15:32:371782 }
1783 return this._element;
1784 }
1785
1786 /**
1787 * @override
1788 */
1789 _showRepeatCountElement() {
1790 super._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341791 if (this._repeatCountElement && this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371792 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341793 }
Blink Reformat4c46d092018-04-07 15:32:371794 }
Paul Lewisbf7aa3c2019-11-20 17:03:381795}
Blink Reformat4c46d092018-04-07 15:32:371796
1797/**
1798 * @const
1799 * @type {number}
1800 */
Paul Lewisbf7aa3c2019-11-20 17:03:381801export const MaxLengthForLinks = 40;
Blink Reformat4c46d092018-04-07 15:32:371802
Paul Lewisbf7aa3c2019-11-20 17:03:381803export const _MaxTokenizableStringLength = 10000;
1804export const _LongStringVisibleLength = 5000;