blob: d52cca934c69274d913ac78834f3e9dc6e65cbb9 [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
Tim van der Lippe9b2f8712020-02-12 17:46:2231import * 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';
Tim van der Lippe93b57c32020-02-20 17:38:4435import * as Platform from '../platform/platform.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2236import * as SDK from '../sdk/sdk.js';
37import * as TextUtils from '../text_utils/text_utils.js';
Paul Lewisca569a52020-09-09 16:11:5138import * as ThemeSupport from '../theme_support/theme_support.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2239import * as UI from '../ui/ui.js';
40
Tim van der Lippeeaacb722020-01-10 12:16:0041import {ConsoleViewportElement} from './ConsoleViewport.js'; // eslint-disable-line no-unused-vars
42
Sigurd Schneiderca7b4ff2020-10-14 07:45:4743/** @type {!WeakMap<!Element, !ConsoleViewMessage>} */
44const elementToMessage = new WeakMap();
45
46/**
47 * @param {!Element} element
48 */
49export const getMessageForElement = element => {
50 return elementToMessage.get(element);
51};
52
Blink Reformat4c46d092018-04-07 15:32:3753/**
Tim van der Lippeeaacb722020-01-10 12:16:0054 * @implements {ConsoleViewportElement}
Blink Reformat4c46d092018-04-07 15:32:3755 */
Tim van der Lippeeaacb722020-01-10 12:16:0056export class ConsoleViewMessage {
Blink Reformat4c46d092018-04-07 15:32:3757 /**
Tim van der Lippe9b2f8712020-02-12 17:46:2258 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
59 * @param {!Components.Linkifier.Linkifier} linkifier
Blink Reformat4c46d092018-04-07 15:32:3760 * @param {number} nestingLevel
Sigurd Schneider45f32c32020-10-13 13:32:0561 * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize
Blink Reformat4c46d092018-04-07 15:32:3762 */
Tim van der Lippeb45d9a02019-11-05 17:24:4163 constructor(consoleMessage, linkifier, nestingLevel, onResize) {
Blink Reformat4c46d092018-04-07 15:32:3764 this._message = consoleMessage;
65 this._linkifier = linkifier;
Blink Reformat4c46d092018-04-07 15:32:3766 this._repeatCount = 1;
67 this._closeGroupDecorationCount = 0;
68 this._nestingLevel = nestingLevel;
Sigurd Schneider45f32c32020-10-13 13:32:0569 /** @type {!Array<{element: !HTMLElement, forceSelect: function():void}>} */
Erik Luo383f21d2018-11-07 23:16:3770 this._selectableChildren = [];
Erik Luo840be6b2018-12-03 20:54:2771 this._messageResized = onResize;
Sigurd Schneider53e98632020-10-26 15:29:5072 /** @type {?HTMLElement} */
73 this._element = null;
Blink Reformat4c46d092018-04-07 15:32:3774
Sigurd Schneider45f32c32020-10-13 13:32:0575 /** @type {?DataGrid.SortableDataGrid.SortableDataGrid<?>} */
Blink Reformat4c46d092018-04-07 15:32:3776 this._dataGrid = null;
Tim van der Lippe9b2f8712020-02-12 17:46:2277 this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter();
Blink Reformat4c46d092018-04-07 15:32:3778 this._searchRegex = null;
Tim van der Lippe9b2f8712020-02-12 17:46:2279 /** @type {?UI.Icon.Icon} */
Blink Reformat4c46d092018-04-07 15:32:3780 this._messageLevelIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:0081 this._traceExpanded = false;
Sigurd Schneider45f32c32020-10-13 13:32:0582 /** @type {?function(boolean):void} */
Erik Luo8ef5d0c2018-09-25 21:16:0083 this._expandTrace = null;
Sigurd Schneider53f33522020-10-08 15:00:4984 /** @type {?HTMLElement} */
John Emaubb2897a2019-10-04 17:37:3285 this._anchorElement = null;
Sigurd Schneider45f32c32020-10-13 13:32:0586 /** @type {?HTMLElement} */
87 this._contentElement = null;
88 /** @type {?Array<!HTMLElement>} */
89 this._nestingLevelMarkers = null;
90 /** @type {!Array<!Element>} */
91 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:5292 /** @type {!Array<!UI.UIUtils.HighlightChange>} */
93 this._searchHighlightNodeChanges = [];
Sigurd Schneider53e98632020-10-26 15:29:5094 this._isVisible = false;
95 this._cachedHeight = 0;
96 this._messagePrefix = '';
97 /** @type {?HTMLElement} */
98 this._timestampElement = null;
99 this._inSimilarGroup = false;
100 /** @type {?HTMLElement} */
101 this._similarGroupMarker = null;
102 this._lastInSimilarGroup = false;
103 this._groupKey = '';
104 /** @type {?UI.UIUtils.DevToolsSmallBubble} */
105 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:37106 }
107
108 /**
109 * @override
Sigurd Schneider53f33522020-10-08 15:00:49110 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37111 */
112 element() {
113 return this.toMessageElement();
114 }
115
116 /**
Blink Reformat4c46d092018-04-07 15:32:37117 * @override
118 */
119 wasShown() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34120 if (this._dataGrid) {
Blink Reformat4c46d092018-04-07 15:32:37121 this._dataGrid.updateWidths();
Tim van der Lippe1d6e57a2019-09-30 11:55:34122 }
Blink Reformat4c46d092018-04-07 15:32:37123 this._isVisible = true;
124 }
125
126 onResize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34127 if (!this._isVisible) {
Blink Reformat4c46d092018-04-07 15:32:37128 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34129 }
130 if (this._dataGrid) {
Blink Reformat4c46d092018-04-07 15:32:37131 this._dataGrid.onResize();
Tim van der Lippe1d6e57a2019-09-30 11:55:34132 }
Blink Reformat4c46d092018-04-07 15:32:37133 }
134
135 /**
136 * @override
137 */
138 willHide() {
139 this._isVisible = false;
Erik Luo4b002322018-07-30 21:23:31140 this._cachedHeight = this.element().offsetHeight;
Blink Reformat4c46d092018-04-07 15:32:37141 }
142
143 /**
144 * @return {number}
145 */
146 fastHeight() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34147 if (this._cachedHeight) {
Blink Reformat4c46d092018-04-07 15:32:37148 return this._cachedHeight;
Tim van der Lippe1d6e57a2019-09-30 11:55:34149 }
Blink Reformat4c46d092018-04-07 15:32:37150 // This value reflects the 18px min-height of .console-message, plus the
151 // 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
152 const defaultConsoleRowHeight = 19;
Tim van der Lippe9b2f8712020-02-12 17:46:22153 if (this._message.type === SDK.ConsoleModel.MessageType.Table) {
Sigurd Schneider45f32c32020-10-13 13:32:05154 const table = this._message.parameters && this._message.parameters[0];
155 if (table && typeof table !== 'string' && table.preview) {
Blink Reformat4c46d092018-04-07 15:32:37156 return defaultConsoleRowHeight * table.preview.properties.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:34157 }
Blink Reformat4c46d092018-04-07 15:32:37158 }
159 return defaultConsoleRowHeight;
160 }
161
162 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22163 * @return {!SDK.ConsoleModel.ConsoleMessage}
Blink Reformat4c46d092018-04-07 15:32:37164 */
165 consoleMessage() {
166 return this._message;
167 }
168
169 /**
Sigurd Schneider53f33522020-10-08 15:00:49170 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37171 */
172 _buildTableMessage() {
Sigurd Schneider53f33522020-10-08 15:00:49173 const formattedMessage = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09174 formattedMessage.classList.add('source-code');
Erik Luo5976c8c2018-07-24 02:03:09175 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34176 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09177 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34178 }
Blink Reformat4c46d092018-04-07 15:32:37179
Tim van der Lippe20749392020-05-27 14:33:51180 const table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null;
181 if (!table) {
182 return this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:34183 }
Tim van der Lippe20749392020-05-27 14:33:51184 const actualTable = this._parameterToRemoteObject(table);
185 if (!actualTable || !actualTable.preview) {
Erik Luo16e3e382018-11-09 02:56:01186 return this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:34187 }
Blink Reformat4c46d092018-04-07 15:32:37188
189 const rawValueColumnSymbol = Symbol('rawValueColumn');
Sigurd Schneider45f32c32020-10-13 13:32:05190 /** @type {!Array<string|symbol>} */
Blink Reformat4c46d092018-04-07 15:32:37191 const columnNames = [];
Tim van der Lippe20749392020-05-27 14:33:51192 const preview = actualTable.preview;
Blink Reformat4c46d092018-04-07 15:32:37193 const rows = [];
194 for (let i = 0; i < preview.properties.length; ++i) {
195 const rowProperty = preview.properties[i];
Sigurd Schneider45f32c32020-10-13 13:32:05196 /** @type {!Array<!Protocol.Runtime.PropertyPreview|!{name:(string|symbol), type: !Protocol.Runtime.PropertyPreviewType, value: (string|undefined)}>} */
Blink Reformat4c46d092018-04-07 15:32:37197 let rowSubProperties;
Tim van der Lippe1d6e57a2019-09-30 11:55:34198 if (rowProperty.valuePreview) {
Blink Reformat4c46d092018-04-07 15:32:37199 rowSubProperties = rowProperty.valuePreview.properties;
Tim van der Lippe1d6e57a2019-09-30 11:55:34200 } else if (rowProperty.value) {
Blink Reformat4c46d092018-04-07 15:32:37201 rowSubProperties = [{name: rawValueColumnSymbol, type: rowProperty.type, value: rowProperty.value}];
Tim van der Lippe1d6e57a2019-09-30 11:55:34202 } else {
Blink Reformat4c46d092018-04-07 15:32:37203 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34204 }
Blink Reformat4c46d092018-04-07 15:32:37205
Sigurd Schneider45f32c32020-10-13 13:32:05206 /** @type {!Map<string|symbol, !HTMLElement>} */
207 const rowValue = new Map();
Blink Reformat4c46d092018-04-07 15:32:37208 const maxColumnsToRender = 20;
209 for (let j = 0; j < rowSubProperties.length; ++j) {
210 const cellProperty = rowSubProperties[j];
211 let columnRendered = columnNames.indexOf(cellProperty.name) !== -1;
212 if (!columnRendered) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34213 if (columnNames.length === maxColumnsToRender) {
Blink Reformat4c46d092018-04-07 15:32:37214 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34215 }
Blink Reformat4c46d092018-04-07 15:32:37216 columnRendered = true;
217 columnNames.push(cellProperty.name);
218 }
219
220 if (columnRendered) {
Sigurd Schneider45f32c32020-10-13 13:32:05221 const cellElement =
222 this._renderPropertyPreviewOrAccessor(actualTable, cellProperty, [rowProperty, cellProperty]);
Blink Reformat4c46d092018-04-07 15:32:37223 cellElement.classList.add('console-message-nowrap-below');
Sigurd Schneider45f32c32020-10-13 13:32:05224 rowValue.set(cellProperty.name, cellElement);
Blink Reformat4c46d092018-04-07 15:32:37225 }
226 }
Sigurd Schneider45f32c32020-10-13 13:32:05227 rows.push({rowName: rowProperty.name, rowValue});
Blink Reformat4c46d092018-04-07 15:32:37228 }
229
230 const flatValues = [];
Sigurd Schneider45f32c32020-10-13 13:32:05231 for (const {rowName, rowValue} of rows) {
Blink Reformat4c46d092018-04-07 15:32:37232 flatValues.push(rowName);
Tim van der Lippe1d6e57a2019-09-30 11:55:34233 for (let j = 0; j < columnNames.length; ++j) {
Sigurd Schneider45f32c32020-10-13 13:32:05234 flatValues.push(rowValue.get(columnNames[j]));
Tim van der Lippe1d6e57a2019-09-30 11:55:34235 }
Blink Reformat4c46d092018-04-07 15:32:37236 }
Tim van der Lippe9b2f8712020-02-12 17:46:22237 columnNames.unshift(Common.UIString.UIString('(index)'));
238 const columnDisplayNames =
Sigurd Schneider45f32c32020-10-13 13:32:05239 columnNames.map(name => name === rawValueColumnSymbol ? Common.UIString.UIString('Value') : name.toString());
Blink Reformat4c46d092018-04-07 15:32:37240
241 if (flatValues.length) {
Tim van der Lippe9b2f8712020-02-12 17:46:22242 this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create(columnDisplayNames, flatValues, ls`Console`);
Sigurd Schneider45f32c32020-10-13 13:32:05243 if (this._dataGrid) {
244 this._dataGrid.setStriped(true);
245 this._dataGrid.setFocusable(false);
Blink Reformat4c46d092018-04-07 15:32:37246
Sigurd Schneider45f32c32020-10-13 13:32:05247 const formattedResult = document.createElement('span');
248 formattedResult.classList.add('console-message-text');
249 const tableElement = formattedResult.createChild('div', 'console-message-formatted-table');
250 const dataGridContainer = tableElement.createChild('span');
251 tableElement.appendChild(this._formatParameter(actualTable, true, false));
252 dataGridContainer.appendChild(this._dataGrid.element);
253 formattedMessage.appendChild(formattedResult);
254 this._dataGrid.renderInline();
255 }
Blink Reformat4c46d092018-04-07 15:32:37256 }
257 return formattedMessage;
258 }
259
260 /**
Sigurd Schneider53f33522020-10-08 15:00:49261 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37262 */
263 _buildMessage() {
264 let messageElement;
265 let messageText = this._message.messageText;
Tim van der Lippe9b2f8712020-02-12 17:46:22266 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:37267 switch (this._message.type) {
Tim van der Lippe9b2f8712020-02-12 17:46:22268 case SDK.ConsoleModel.MessageType.Trace:
Blink Reformat4c46d092018-04-07 15:32:37269 messageElement = this._format(this._message.parameters || ['console.trace']);
270 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22271 case SDK.ConsoleModel.MessageType.Clear:
Tim van der Lippef49e2322020-05-01 15:03:09272 messageElement = document.createElement('span');
273 messageElement.classList.add('console-info');
Paul Lewis2d7d65c2020-03-16 17:26:30274 if (Common.Settings.Settings.instance().moduleSetting('preserveConsoleLog').get()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22275 messageElement.textContent =
276 Common.UIString.UIString('console.clear() was prevented due to \'Preserve log\'');
Tim van der Lippe1d6e57a2019-09-30 11:55:34277 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22278 messageElement.textContent = Common.UIString.UIString('Console was cleared');
Tim van der Lippe1d6e57a2019-09-30 11:55:34279 }
Tim van der Lippe9c9fb122020-09-08 15:06:17280 messageElement.title = ls`Clear all messages with ${
281 UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction('console.clear')}`;
Blink Reformat4c46d092018-04-07 15:32:37282 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22283 case SDK.ConsoleModel.MessageType.Dir: {
Blink Reformat4c46d092018-04-07 15:32:37284 const obj = this._message.parameters ? this._message.parameters[0] : undefined;
285 const args = ['%O', obj];
286 messageElement = this._format(args);
287 break;
288 }
Tim van der Lippe9b2f8712020-02-12 17:46:22289 case SDK.ConsoleModel.MessageType.Profile:
290 case SDK.ConsoleModel.MessageType.ProfileEnd:
Blink Reformat4c46d092018-04-07 15:32:37291 messageElement = this._format([messageText]);
292 break;
Blink Reformat4c46d092018-04-07 15:32:37293 default: {
Sigurd Schneider45f32c32020-10-13 13:32:05294 if (this._message.type === SDK.ConsoleModel.MessageType.Assert) {
295 this._messagePrefix = ls`Assertion failed: `;
296 }
297 if (this._message.parameters && this._message.parameters.length === 1) {
298 const parameter = this._message.parameters[0];
299 if (typeof parameter !== 'string' && parameter.type === 'string') {
300 messageElement = this._tryFormatAsError(/** @type {string} */ (parameter.value));
301 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34302 }
Blink Reformat4c46d092018-04-07 15:32:37303 const args = this._message.parameters || [messageText];
304 messageElement = messageElement || this._format(args);
305 }
306 }
307 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22308 if (this._message.source === SDK.ConsoleModel.MessageSource.Network) {
Erik Luofc2214f2018-11-21 19:54:58309 messageElement = this._formatAsNetworkRequest() || this._format([messageText]);
310 } else {
Blink Reformat4c46d092018-04-07 15:32:37311 const messageInParameters =
312 this._message.parameters && messageText === /** @type {string} */ (this._message.parameters[0]);
Tim van der Lippe9b2f8712020-02-12 17:46:22313 if (this._message.source === SDK.ConsoleModel.MessageSource.Violation) {
314 messageText = Common.UIString.UIString('[Violation] %s', messageText);
315 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Intervention) {
316 messageText = Common.UIString.UIString('[Intervention] %s', messageText);
317 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Deprecation) {
318 messageText = Common.UIString.UIString('[Deprecation] %s', messageText);
Tim van der Lippe1d6e57a2019-09-30 11:55:34319 }
Blink Reformat4c46d092018-04-07 15:32:37320 const args = this._message.parameters || [messageText];
Tim van der Lippe1d6e57a2019-09-30 11:55:34321 if (messageInParameters) {
Blink Reformat4c46d092018-04-07 15:32:37322 args[0] = messageText;
Tim van der Lippe1d6e57a2019-09-30 11:55:34323 }
Blink Reformat4c46d092018-04-07 15:32:37324 messageElement = this._format(args);
325 }
326 }
327 messageElement.classList.add('console-message-text');
328
Sigurd Schneider53f33522020-10-08 15:00:49329 const formattedMessage = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09330 formattedMessage.classList.add('source-code');
Erik Luo5976c8c2018-07-24 02:03:09331 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34332 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09333 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34334 }
Blink Reformat4c46d092018-04-07 15:32:37335 formattedMessage.appendChild(messageElement);
336 return formattedMessage;
337 }
338
339 /**
Sigurd Schneider53f33522020-10-08 15:00:49340 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37341 */
Erik Luofc2214f2018-11-21 19:54:58342 _formatAsNetworkRequest() {
Tim van der Lippe9b2f8712020-02-12 17:46:22343 const request = SDK.NetworkLog.NetworkLog.requestForConsoleMessage(this._message);
Tim van der Lippe1d6e57a2019-09-30 11:55:34344 if (!request) {
Erik Luofc2214f2018-11-21 19:54:58345 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34346 }
Sigurd Schneider53f33522020-10-08 15:00:49347 const messageElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippe9b2f8712020-02-12 17:46:22348 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Sigurd Schneider23c52972020-10-13 09:31:14349 UI.UIUtils.createTextChild(messageElement, request.requestMethod + ' ');
Tim van der Lippe9b2f8712020-02-12 17:46:22350 const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url());
Erik Luo182bece2018-11-29 03:15:22351 // Focus is handled by the viewport.
352 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39353 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22354 messageElement.appendChild(linkElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34355 if (request.failed) {
Sigurd Schneider23c52972020-10-13 09:31:14356 UI.UIUtils.createTextChildren(messageElement, ' ', request.localizedFailDescription || '');
Tim van der Lippe1d6e57a2019-09-30 11:55:34357 }
358 if (request.statusCode !== 0) {
Sigurd Schneider23c52972020-10-13 09:31:14359 UI.UIUtils.createTextChildren(messageElement, ' ', String(request.statusCode));
Tim van der Lippe1d6e57a2019-09-30 11:55:34360 }
361 if (request.statusText) {
Sigurd Schneider23c52972020-10-13 09:31:14362 UI.UIUtils.createTextChildren(messageElement, ' (', request.statusText, ')');
Tim van der Lippe1d6e57a2019-09-30 11:55:34363 }
Erik Luofc2214f2018-11-21 19:54:58364 } else {
Erik Luoad5f3942019-03-26 20:53:44365 const messageText = this._message.messageText;
366 const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:05367 const linkElement = url === request.url() ?
368 Components.Linkifier.Linkifier.linkifyRevealable(
369 /** @type {!SDK.NetworkRequest.NetworkRequest} */ (request), url, request.url()) :
370 Components.Linkifier.Linkifier.linkifyURL(
371 url, /** @type {!Components.Linkifier.LinkifyURLOptions} */ ({text, lineNumber, columnNumber}));
Erik Luo182bece2018-11-29 03:15:22372 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39373 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22374 return linkElement;
375 });
Erik Luofc2214f2018-11-21 19:54:58376 messageElement.appendChild(fragment);
377 }
378 return messageElement;
379 }
380
381 /**
Sigurd Schneider53f33522020-10-08 15:00:49382 * @return {?HTMLElement}
Erik Luofc2214f2018-11-21 19:54:58383 */
Blink Reformat4c46d092018-04-07 15:32:37384 _buildMessageAnchor() {
Sigurd Schneider45f32c32020-10-13 13:32:05385 /**
386 * @param {!SDK.ConsoleModel.ConsoleMessage} message
387 * @return {?HTMLElement}
388 */
389 const linkify = message => {
390 if (message.scriptId) {
391 return this._linkifyScriptId(message.scriptId, message.url || '', message.line, message.column);
392 }
393 if (message.stackTrace && message.stackTrace.callFrames.length) {
394 return this._linkifyStackTraceTopFrame(message.stackTrace);
395 }
396 if (message.url && message.url !== 'undefined') {
397 return this._linkifyLocation(message.url, message.line, message.column);
398 }
399 return null;
400 };
401 const anchorElement = linkify(this._message);
Blink Reformat4c46d092018-04-07 15:32:37402 // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
403 if (anchorElement) {
John Emauf7e30fb2019-10-04 19:12:32404 anchorElement.tabIndex = -1;
405 this._selectableChildren.push({
406 element: anchorElement,
407 forceSelect: () => anchorElement.focus(),
408 });
Sigurd Schneider53f33522020-10-08 15:00:49409 const anchorWrapperElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09410 anchorWrapperElement.classList.add('console-message-anchor');
Blink Reformat4c46d092018-04-07 15:32:37411 anchorWrapperElement.appendChild(anchorElement);
Sigurd Schneider23c52972020-10-13 09:31:14412 UI.UIUtils.createTextChild(anchorWrapperElement, ' ');
Blink Reformat4c46d092018-04-07 15:32:37413 return anchorWrapperElement;
414 }
415 return null;
416 }
417
418 /**
Sigurd Schneider45f32c32020-10-13 13:32:05419 * @param {!SDK.RuntimeModel.RuntimeModel} runtimeModel
Sigurd Schneider53f33522020-10-08 15:00:49420 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37421 */
Sigurd Schneider45f32c32020-10-13 13:32:05422 _buildMessageWithStackTrace(runtimeModel) {
Sigurd Schneider53f33522020-10-08 15:00:49423 const toggleElement = /** @type {!HTMLElement} */ (document.createElement('div'));
Tim van der Lippef49e2322020-05-01 15:03:09424 toggleElement.classList.add('console-message-stack-trace-toggle');
Blink Reformat4c46d092018-04-07 15:32:37425 const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
426
427 const messageElement = this._buildMessage();
Tim van der Lippe9b2f8712020-02-12 17:46:22428 const icon = UI.Icon.Icon.create('smallicon-triangle-right', 'console-message-expand-icon');
Blink Reformat4c46d092018-04-07 15:32:37429 const clickableElement = contentElement.createChild('div');
430 clickableElement.appendChild(icon);
Erik Luob5bfff42018-09-20 02:52:39431 // Intercept focus to avoid highlight on click.
432 clickableElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37433 clickableElement.appendChild(messageElement);
434 const stackTraceElement = contentElement.createChild('div');
435 const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
Sigurd Schneider45f32c32020-10-13 13:32:05436 runtimeModel.target(), this._linkifier,
437 {stackTrace: this._message.stackTrace, contentUpdated: undefined, tabStops: undefined});
Erik Luo182bece2018-11-29 03:15:22438 stackTraceElement.appendChild(stackTracePreview.element);
439 for (const linkElement of stackTracePreview.links) {
Erik Luo31c21f62018-12-13 03:39:39440 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22441 }
Blink Reformat4c46d092018-04-07 15:32:37442 stackTraceElement.classList.add('hidden');
Brandon Goddard04a5a762019-12-10 16:45:53443 UI.ARIAUtils.markAsTreeitem(this.element());
444 UI.ARIAUtils.setExpanded(this.element(), false);
Erik Luo8ef5d0c2018-09-25 21:16:00445 this._expandTrace = expand => {
Blink Reformat4c46d092018-04-07 15:32:37446 icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right');
447 stackTraceElement.classList.toggle('hidden', !expand);
Brandon Goddard04a5a762019-12-10 16:45:53448 UI.ARIAUtils.setExpanded(this.element(), expand);
Erik Luo8ef5d0c2018-09-25 21:16:00449 this._traceExpanded = expand;
450 };
Blink Reformat4c46d092018-04-07 15:32:37451
452 /**
Tim van der Lippeeaacb722020-01-10 12:16:00453 * @this {!ConsoleViewMessage}
Sigurd Schneider45f32c32020-10-13 13:32:05454 * @param {!Event} event
Blink Reformat4c46d092018-04-07 15:32:37455 */
Sigurd Schneider45f32c32020-10-13 13:32:05456 const toggleStackTrace = event => {
Tim van der Lippe9b2f8712020-02-12 17:46:22457 if (UI.UIUtils.isEditing() || contentElement.hasSelection()) {
Blink Reformat4c46d092018-04-07 15:32:37458 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34459 }
Sigurd Schneider45f32c32020-10-13 13:32:05460 this._expandTrace && this._expandTrace(stackTraceElement.classList.contains('hidden'));
Blink Reformat4c46d092018-04-07 15:32:37461 event.consume();
Sigurd Schneider45f32c32020-10-13 13:32:05462 };
Blink Reformat4c46d092018-04-07 15:32:37463
Sigurd Schneider45f32c32020-10-13 13:32:05464 clickableElement.addEventListener('click', toggleStackTrace, false);
Tim van der Lippe9b2f8712020-02-12 17:46:22465 if (this._message.type === SDK.ConsoleModel.MessageType.Trace) {
Erik Luo8ef5d0c2018-09-25 21:16:00466 this._expandTrace(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34467 }
Blink Reformat4c46d092018-04-07 15:32:37468
Sigurd Schneider45f32c32020-10-13 13:32:05469 // @ts-ignore
Erik Luo8ef5d0c2018-09-25 21:16:00470 toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true);
Blink Reformat4c46d092018-04-07 15:32:37471 return toggleElement;
472 }
473
474 /**
475 * @param {string} url
476 * @param {number} lineNumber
477 * @param {number} columnNumber
Sigurd Schneider53f33522020-10-08 15:00:49478 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37479 */
480 _linkifyLocation(url, lineNumber, columnNumber) {
Sigurd Schneider45f32c32020-10-13 13:32:05481 const runtimeModel = this._message.runtimeModel();
482 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37483 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34484 }
Blink Reformat4c46d092018-04-07 15:32:37485 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05486 runtimeModel.target(), /* scriptId */ null, url, lineNumber,
487 {columnNumber, className: undefined, tabStop: undefined});
Blink Reformat4c46d092018-04-07 15:32:37488 }
489
490 /**
491 * @param {!Protocol.Runtime.StackTrace} stackTrace
Sigurd Schneider53f33522020-10-08 15:00:49492 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37493 */
494 _linkifyStackTraceTopFrame(stackTrace) {
Sigurd Schneider45f32c32020-10-13 13:32:05495 const runtimeModel = this._message.runtimeModel();
496 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37497 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34498 }
Sigurd Schneider45f32c32020-10-13 13:32:05499 return this._linkifier.linkifyStackTraceTopFrame(runtimeModel.target(), stackTrace);
Blink Reformat4c46d092018-04-07 15:32:37500 }
501
502 /**
503 * @param {string} scriptId
504 * @param {string} url
505 * @param {number} lineNumber
506 * @param {number} columnNumber
Sigurd Schneider53f33522020-10-08 15:00:49507 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37508 */
509 _linkifyScriptId(scriptId, url, lineNumber, columnNumber) {
Sigurd Schneider45f32c32020-10-13 13:32:05510 const runtimeModel = this._message.runtimeModel();
511 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37512 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34513 }
Blink Reformat4c46d092018-04-07 15:32:37514 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05515 runtimeModel.target(), scriptId, url, lineNumber, {columnNumber, className: undefined, tabStop: undefined});
Blink Reformat4c46d092018-04-07 15:32:37516 }
517
518 /**
Sigurd Schneider45f32c32020-10-13 13:32:05519 * @param {!SDK.RemoteObject.RemoteObject|!Protocol.Runtime.RemoteObject|string|undefined} parameter
Tim van der Lippe9b2f8712020-02-12 17:46:22520 * @return {!SDK.RemoteObject.RemoteObject}
Blink Reformat4c46d092018-04-07 15:32:37521 */
522 _parameterToRemoteObject(parameter) {
Tim van der Lippe9b2f8712020-02-12 17:46:22523 if (parameter instanceof SDK.RemoteObject.RemoteObject) {
Blink Reformat4c46d092018-04-07 15:32:37524 return parameter;
Tim van der Lippe1d6e57a2019-09-30 11:55:34525 }
Blink Reformat4c46d092018-04-07 15:32:37526 const runtimeModel = this._message.runtimeModel();
Tim van der Lippe1d6e57a2019-09-30 11:55:34527 if (!runtimeModel) {
Tim van der Lippe9b2f8712020-02-12 17:46:22528 return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter);
Tim van der Lippe1d6e57a2019-09-30 11:55:34529 }
530 if (typeof parameter === 'object') {
Blink Reformat4c46d092018-04-07 15:32:37531 return runtimeModel.createRemoteObject(parameter);
Tim van der Lippe1d6e57a2019-09-30 11:55:34532 }
Blink Reformat4c46d092018-04-07 15:32:37533 return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
534 }
535
536 /**
Sigurd Schneider45f32c32020-10-13 13:32:05537 * @param {!Array.<!Protocol.Runtime.RemoteObject | !SDK.RemoteObject.RemoteObject | string | undefined>} rawParameters
Sigurd Schneider53f33522020-10-08 15:00:49538 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37539 */
540 _format(rawParameters) {
541 // This node is used like a Builder. Values are continually appended onto it.
Sigurd Schneider53f33522020-10-08 15:00:49542 const formattedResult = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34543 if (this._messagePrefix) {
Pavel Feldman9f0f0a32018-12-18 02:09:13544 formattedResult.createChild('span').textContent = this._messagePrefix;
Tim van der Lippe1d6e57a2019-09-30 11:55:34545 }
546 if (!rawParameters.length) {
Blink Reformat4c46d092018-04-07 15:32:37547 return formattedResult;
Tim van der Lippe1d6e57a2019-09-30 11:55:34548 }
Blink Reformat4c46d092018-04-07 15:32:37549
550 // Formatting code below assumes that parameters are all wrappers whereas frontend console
551 // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
552 // FIXME: Only pass runtime wrappers here.
Sigurd Schneider45f32c32020-10-13 13:32:05553 let parameters = rawParameters.map(this._parameterToRemoteObject.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37554
555 // There can be string log and string eval result. We distinguish between them based on message type.
556 const shouldFormatMessage =
Tim van der Lippe9b2f8712020-02-12 17:46:22557 SDK.RemoteObject.RemoteObject.type(
558 (/** @type {!Array.<!SDK.RemoteObject.RemoteObject>} **/ (parameters))[0]) === 'string' &&
559 (this._message.type !== SDK.ConsoleModel.MessageType.Result ||
560 this._message.level === SDK.ConsoleModel.MessageLevel.Error);
Blink Reformat4c46d092018-04-07 15:32:37561
562 // Multiple parameters with the first being a format string. Save unused substitutions.
563 if (shouldFormatMessage) {
564 const result = this._formatWithSubstitutionString(
565 /** @type {string} **/ (parameters[0].description), parameters.slice(1), formattedResult);
Sigurd Schneider45f32c32020-10-13 13:32:05566 parameters = Array.from(result.unusedSubstitutions || []);
Tim van der Lippe1d6e57a2019-09-30 11:55:34567 if (parameters.length) {
Sigurd Schneider23c52972020-10-13 09:31:14568 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34569 }
Blink Reformat4c46d092018-04-07 15:32:37570 }
571
572 // Single parameter, or unused substitutions from above.
573 for (let i = 0; i < parameters.length; ++i) {
574 // Inline strings when formatting.
Tim van der Lippe1d6e57a2019-09-30 11:55:34575 if (shouldFormatMessage && parameters[i].type === 'string') {
Sigurd Schneider45f32c32020-10-13 13:32:05576 formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description || ''));
Tim van der Lippe1d6e57a2019-09-30 11:55:34577 } else {
Blink Reformat4c46d092018-04-07 15:32:37578 formattedResult.appendChild(this._formatParameter(parameters[i], false, true));
Tim van der Lippe1d6e57a2019-09-30 11:55:34579 }
580 if (i < parameters.length - 1) {
Sigurd Schneider23c52972020-10-13 09:31:14581 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34582 }
Blink Reformat4c46d092018-04-07 15:32:37583 }
584 return formattedResult;
585 }
586
587 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22588 * @param {!SDK.RemoteObject.RemoteObject} output
Blink Reformat4c46d092018-04-07 15:32:37589 * @param {boolean=} forceObjectFormat
590 * @param {boolean=} includePreview
Sigurd Schneider53f33522020-10-08 15:00:49591 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37592 */
593 _formatParameter(output, forceObjectFormat, includePreview) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34594 if (output.customPreview()) {
Sigurd Schneider53f33522020-10-08 15:00:49595 return /** @type {!HTMLElement} */ ((new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output)).element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34596 }
Blink Reformat4c46d092018-04-07 15:32:37597
598 const type = forceObjectFormat ? 'object' : (output.subtype || output.type);
599 let element;
600 switch (type) {
601 case 'error':
602 element = this._formatParameterAsError(output);
603 break;
604 case 'function':
605 element = this._formatParameterAsFunction(output, includePreview);
606 break;
607 case 'array':
608 case 'arraybuffer':
609 case 'blob':
610 case 'dataview':
611 case 'generator':
612 case 'iterator':
613 case 'map':
614 case 'object':
615 case 'promise':
616 case 'proxy':
617 case 'set':
618 case 'typedarray':
619 case 'weakmap':
620 case 'weakset':
621 element = this._formatParameterAsObject(output, includePreview);
622 break;
623 case 'node':
624 element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false);
625 break;
626 case 'string':
627 element = this._formatParameterAsString(output);
628 break;
629 case 'boolean':
630 case 'date':
631 case 'null':
632 case 'number':
633 case 'regexp':
634 case 'symbol':
635 case 'undefined':
636 case 'bigint':
637 element = this._formatParameterAsValue(output);
638 break;
639 default:
640 element = this._formatParameterAsValue(output);
641 console.error('Tried to format remote object of unknown type.');
642 }
643 element.classList.add('object-value-' + type);
644 element.classList.add('source-code');
645 return element;
646 }
647
648 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22649 * @param {!SDK.RemoteObject.RemoteObject} obj
Sigurd Schneider53f33522020-10-08 15:00:49650 * @return {!HTMLElement}
Tim van der Lippeeaacb722020-01-10 12:16:00651 * @suppress {accessControls}
Blink Reformat4c46d092018-04-07 15:32:37652 */
653 _formatParameterAsValue(obj) {
Sigurd Schneider53f33522020-10-08 15:00:49654 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37655 const description = obj.description || '';
Sigurd Schneider8f4ac862020-10-13 13:30:11656 if (description.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22657 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:11658 document.createElement('span'), description, getLongStringVisibleLength());
Connor Moody1a5c0d32019-12-19 07:23:36659 result.appendChild(propertyValue.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34660 } else {
Sigurd Schneider23c52972020-10-13 09:31:14661 UI.UIUtils.createTextChild(result, description);
Tim van der Lippe1d6e57a2019-09-30 11:55:34662 }
663 if (obj.objectId) {
Blink Reformat4c46d092018-04-07 15:32:37664 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34665 }
Blink Reformat4c46d092018-04-07 15:32:37666 return result;
667 }
668
669 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22670 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37671 * @param {boolean=} includePreview
Sigurd Schneider53f33522020-10-08 15:00:49672 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37673 */
674 _formatParameterAsObject(obj, includePreview) {
Sigurd Schneider53f33522020-10-08 15:00:49675 const titleElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09676 titleElement.classList.add('console-object');
Blink Reformat4c46d092018-04-07 15:32:37677 if (includePreview && obj.preview) {
678 titleElement.classList.add('console-object-preview');
679 this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
680 } else if (obj.type === 'function') {
681 const functionElement = titleElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22682 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
Blink Reformat4c46d092018-04-07 15:32:37683 titleElement.classList.add('object-value-function');
684 } else {
Sigurd Schneider23c52972020-10-13 09:31:14685 UI.UIUtils.createTextChild(titleElement, obj.description || '');
Blink Reformat4c46d092018-04-07 15:32:37686 }
687
Tim van der Lippe1d6e57a2019-09-30 11:55:34688 if (!obj.hasChildren || obj.customPreview()) {
Blink Reformat4c46d092018-04-07 15:32:37689 return titleElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34690 }
Blink Reformat4c46d092018-04-07 15:32:37691
692 const note = titleElement.createChild('span', 'object-state-note info-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22693 if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) {
Blink Reformat4c46d092018-04-07 15:32:37694 note.title = ls`This value will not be collected until console is cleared.`;
Tim van der Lippe1d6e57a2019-09-30 11:55:34695 } else {
Blink Reformat4c46d092018-04-07 15:32:37696 note.title = ls`Value below was evaluated just now.`;
Tim van der Lippe1d6e57a2019-09-30 11:55:34697 }
Blink Reformat4c46d092018-04-07 15:32:37698
Tim van der Lippe9b2f8712020-02-12 17:46:22699 const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier);
Blink Reformat4c46d092018-04-07 15:32:37700 section.element.classList.add('console-view-object-properties-section');
701 section.enableContextMenu();
Erik Luocc14b812018-11-03 01:33:09702 section.setShowSelectionOnKeyboardFocus(true, true);
Erik Luo383f21d2018-11-07 23:16:37703 this._selectableChildren.push(section);
Erik Luo840be6b2018-12-03 20:54:27704 section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
705 section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
706 section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
Blink Reformat4c46d092018-04-07 15:32:37707 return section.element;
708 }
709
710 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22711 * @param {!SDK.RemoteObject.RemoteObject} func
Blink Reformat4c46d092018-04-07 15:32:37712 * @param {boolean=} includePreview
Sigurd Schneider53f33522020-10-08 15:00:49713 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37714 */
715 _formatParameterAsFunction(func, includePreview) {
Sigurd Schneider53f33522020-10-08 15:00:49716 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippe9b2f8712020-02-12 17:46:22717 SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37718 return result;
719
720 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22721 * @param {!SDK.RemoteObject.RemoteObject} targetFunction
Tim van der Lippeeaacb722020-01-10 12:16:00722 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37723 */
724 function formatTargetFunction(targetFunction) {
Sigurd Schneider53f33522020-10-08 15:00:49725 const functionElement = document.createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22726 const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(
Joey Arhard78a58f2018-12-05 01:59:45727 targetFunction, functionElement, true, includePreview);
Blink Reformat4c46d092018-04-07 15:32:37728 result.appendChild(functionElement);
729 if (targetFunction !== func) {
730 const note = result.createChild('span', 'object-info-state-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22731 note.title = Common.UIString.UIString('Function was resolved from bound function.');
Blink Reformat4c46d092018-04-07 15:32:37732 }
733 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false);
Joey Arhard78a58f2018-12-05 01:59:45734 promise.then(() => this._formattedParameterAsFunctionForTest());
Blink Reformat4c46d092018-04-07 15:32:37735 }
736 }
737
Joey Arhard78a58f2018-12-05 01:59:45738 _formattedParameterAsFunctionForTest() {
739 }
740
Blink Reformat4c46d092018-04-07 15:32:37741 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22742 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37743 * @param {!Event} event
744 */
745 _contextMenuEventFired(obj, event) {
Tim van der Lippe9b2f8712020-02-12 17:46:22746 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37747 contextMenu.appendApplicableItems(obj);
748 contextMenu.show();
749 }
750
751 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22752 * @param {?SDK.RemoteObject.RemoteObject} object
Sigurd Schneider45f32c32020-10-13 13:32:05753 * @param {!Protocol.Runtime.PropertyPreview|!{name:(string|symbol), type: !Protocol.Runtime.PropertyPreviewType, value: (string|undefined)}} property
754 * @param {!Array.<!{name:(string|symbol)}>} propertyPath
Sigurd Schneider53f33522020-10-08 15:00:49755 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37756 */
Sigurd Schneider45f32c32020-10-13 13:32:05757 _renderPropertyPreviewOrAccessor(object, property, propertyPath) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34758 if (property.type === 'accessor') {
Sigurd Schneider45f32c32020-10-13 13:32:05759 return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34760 }
Blink Reformat4c46d092018-04-07 15:32:37761 return this._previewFormatter.renderPropertyPreview(
Sigurd Schneider45f32c32020-10-13 13:32:05762 property.type, 'subtype' in property ? property.subtype : undefined, property.value);
Blink Reformat4c46d092018-04-07 15:32:37763 }
764
765 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22766 * @param {!SDK.RemoteObject.RemoteObject} remoteObject
Sigurd Schneider53f33522020-10-08 15:00:49767 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37768 */
769 _formatParameterAsNode(remoteObject) {
Sigurd Schneider53f33522020-10-08 15:00:49770 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37771
Tim van der Lippe9b2f8712020-02-12 17:46:22772 const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:34773 if (!domModel) {
Blink Reformat4c46d092018-04-07 15:32:37774 return result;
Tim van der Lippe1d6e57a2019-09-30 11:55:34775 }
Erik Luo54fdd912018-11-01 17:57:01776 domModel.pushObjectAsNodeToFrontend(remoteObject).then(async node => {
Blink Reformat4c46d092018-04-07 15:32:37777 if (!node) {
778 result.appendChild(this._formatParameterAsObject(remoteObject, false));
779 return;
780 }
Tim van der Lippe9b2f8712020-02-12 17:46:22781 const renderResult = await UI.UIUtils.Renderer.render(/** @type {!Object} */ (node));
Erik Luofc6a6302018-11-02 06:48:52782 if (renderResult) {
Erik Luo840be6b2018-12-03 20:54:27783 if (renderResult.tree) {
Erik Luo383f21d2018-11-07 23:16:37784 this._selectableChildren.push(renderResult.tree);
Erik Luo840be6b2018-12-03 20:54:27785 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
786 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
787 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
788 }
Erik Luofc6a6302018-11-02 06:48:52789 result.appendChild(renderResult.node);
790 } else {
791 result.appendChild(this._formatParameterAsObject(remoteObject, false));
792 }
Erik Luo54fdd912018-11-01 17:57:01793 this._formattedParameterAsNodeForTest();
Blink Reformat4c46d092018-04-07 15:32:37794 });
795
796 return result;
797 }
798
799 _formattedParameterAsNodeForTest() {
800 }
801
802 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22803 * @param {!SDK.RemoteObject.RemoteObject} output
Sigurd Schneider53f33522020-10-08 15:00:49804 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37805 */
806 _formatParameterAsString(output) {
Sigurd Schneider53f33522020-10-08 15:00:49807 const span = /** @type {!HTMLElement} */ (document.createElement('span'));
Erik Luo383f21d2018-11-07 23:16:37808 span.appendChild(this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37809
Sigurd Schneider53f33522020-10-08 15:00:49810 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37811 result.createChild('span', 'object-value-string-quote').textContent = '"';
812 result.appendChild(span);
813 result.createChild('span', 'object-value-string-quote').textContent = '"';
814 return result;
815 }
816
817 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22818 * @param {!SDK.RemoteObject.RemoteObject} output
Sigurd Schneider53f33522020-10-08 15:00:49819 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37820 */
821 _formatParameterAsError(output) {
Sigurd Schneider53f33522020-10-08 15:00:49822 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37823 const errorSpan = this._tryFormatAsError(output.description || '');
Erik Luo383f21d2018-11-07 23:16:37824 result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37825 return result;
826 }
827
828 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22829 * @param {!SDK.RemoteObject.RemoteObject} output
Sigurd Schneider53f33522020-10-08 15:00:49830 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37831 */
832 _formatAsArrayEntry(output) {
833 return this._previewFormatter.renderPropertyPreview(output.type, output.subtype, output.description);
834 }
835
836 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22837 * @param {?SDK.RemoteObject.RemoteObject} object
Blink Reformat4c46d092018-04-07 15:32:37838 * @param {!Array.<string>} propertyPath
839 * @param {boolean} isArrayEntry
Sigurd Schneider53f33522020-10-08 15:00:49840 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37841 */
842 _formatAsAccessorProperty(object, propertyPath, isArrayEntry) {
Tim van der Lippe9b2f8712020-02-12 17:46:22843 const rootElement =
844 ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
845 object, propertyPath, onInvokeGetterClick.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37846
847 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22848 * @param {!SDK.RemoteObject.CallFunctionResult} result
Tim van der Lippeeaacb722020-01-10 12:16:00849 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37850 */
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18851 function onInvokeGetterClick(result) {
852 const wasThrown = result.wasThrown;
853 const object = result.object;
Tim van der Lippe1d6e57a2019-09-30 11:55:34854 if (!object) {
Blink Reformat4c46d092018-04-07 15:32:37855 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34856 }
Blink Reformat4c46d092018-04-07 15:32:37857 rootElement.removeChildren();
858 if (wasThrown) {
859 const element = rootElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22860 element.textContent = Common.UIString.UIString('<exception>');
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18861 element.title = /** @type {string} */ (object.description);
Blink Reformat4c46d092018-04-07 15:32:37862 } else if (isArrayEntry) {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18863 rootElement.appendChild(this._formatAsArrayEntry(object));
Blink Reformat4c46d092018-04-07 15:32:37864 } else {
865 // Make a PropertyPreview from the RemoteObject similar to the backend logic.
866 const maxLength = 100;
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18867 const type = object.type;
868 const subtype = object.subtype;
Blink Reformat4c46d092018-04-07 15:32:37869 let description = '';
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18870 if (type !== 'function' && object.description) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34871 if (type === 'string' || subtype === 'regexp') {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18872 description = object.description.trimMiddle(maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34873 } else {
Tim van der Lippeffa78622019-09-16 12:07:12874 description = object.description.trimEndWithMaxLength(maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34875 }
Blink Reformat4c46d092018-04-07 15:32:37876 }
877 rootElement.appendChild(this._previewFormatter.renderPropertyPreview(type, subtype, description));
878 }
879 }
880
881 return rootElement;
882 }
883
884 /**
885 * @param {string} format
Tim van der Lippe9b2f8712020-02-12 17:46:22886 * @param {!Array.<!SDK.RemoteObject.RemoteObject>} parameters
Sigurd Schneider53f33522020-10-08 15:00:49887 * @param {!HTMLElement} formattedResult
Sigurd Schneider45f32c32020-10-13 13:32:05888 * @return {!{formattedResult:!Element, unusedSubstitutions: ?ArrayLike<!SDK.RemoteObject.RemoteObject>}}
Blink Reformat4c46d092018-04-07 15:32:37889 */
890 _formatWithSubstitutionString(format, parameters, formattedResult) {
Blink Reformat4c46d092018-04-07 15:32:37891 /**
892 * @param {boolean} force
893 * @param {boolean} includePreview
Sigurd Schneider45f32c32020-10-13 13:32:05894 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
895 * @return {!HTMLElement|string|undefined}
Tim van der Lippeeaacb722020-01-10 12:16:00896 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37897 */
898 function parameterFormatter(force, includePreview, obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05899 if (obj instanceof SDK.RemoteObject.RemoteObject) {
900 return this._formatParameter(obj, force, includePreview);
901 }
902 return stringFormatter(obj);
Blink Reformat4c46d092018-04-07 15:32:37903 }
904
Sigurd Schneider45f32c32020-10-13 13:32:05905 /**
906 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
907 */
Blink Reformat4c46d092018-04-07 15:32:37908 function stringFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05909 if (obj === undefined) {
910 return undefined;
911 }
912 if (typeof obj === 'string') {
913 return obj;
914 }
Blink Reformat4c46d092018-04-07 15:32:37915 return obj.description;
916 }
917
Sigurd Schneider45f32c32020-10-13 13:32:05918 /**
919 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
920 */
Blink Reformat4c46d092018-04-07 15:32:37921 function floatFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05922 if (obj instanceof SDK.RemoteObject.RemoteObject) {
923 if (typeof obj.value !== 'number') {
924 return 'NaN';
925 }
926 return obj.value;
Tim van der Lippe1d6e57a2019-09-30 11:55:34927 }
Sigurd Schneider45f32c32020-10-13 13:32:05928 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37929 }
930
Sigurd Schneider45f32c32020-10-13 13:32:05931 /**
932 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
933 */
Blink Reformat4c46d092018-04-07 15:32:37934 function integerFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05935 if (obj instanceof SDK.RemoteObject.RemoteObject) {
936 if (obj.type === 'bigint') {
937 return obj.description;
938 }
939 if (typeof obj.value !== 'number') {
940 return 'NaN';
941 }
942 return Math.floor(obj.value);
Tim van der Lippe1d6e57a2019-09-30 11:55:34943 }
Sigurd Schneider45f32c32020-10-13 13:32:05944 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37945 }
946
Sigurd Schneider45f32c32020-10-13 13:32:05947 /**
948 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
949 */
Blink Reformat4c46d092018-04-07 15:32:37950 function bypassFormatter(obj) {
951 return (obj instanceof Node) ? obj : '';
952 }
953
Sigurd Schneider45f32c32020-10-13 13:32:05954 /** @type {?Map<string, !{value:string, priority:string}>} */
Blink Reformat4c46d092018-04-07 15:32:37955 let currentStyle = null;
Sigurd Schneider45f32c32020-10-13 13:32:05956 /**
957 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
958 */
Blink Reformat4c46d092018-04-07 15:32:37959 function styleFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05960 currentStyle = new Map();
Sigurd Schneider53f33522020-10-08 15:00:49961 const buffer = document.createElement('span');
Sigurd Schneider45f32c32020-10-13 13:32:05962 if (obj === undefined) {
963 return;
964 }
965 if (typeof obj === 'string' || !obj.description) {
966 return;
967 }
Blink Reformat4c46d092018-04-07 15:32:37968 buffer.setAttribute('style', obj.description);
Sigurd Schneider45f32c32020-10-13 13:32:05969 for (const property of buffer.style) {
Mathias Bynens5165a7a2020-06-10 05:51:43970 if (isAllowedProperty(property)) {
Sigurd Schneider45f32c32020-10-13 13:32:05971 const info = {
972 value: buffer.style.getPropertyValue(property),
973 priority: buffer.style.getPropertyPriority(property)
974 };
975 currentStyle.set(property, info);
Tim van der Lippe1d6e57a2019-09-30 11:55:34976 }
Blink Reformat4c46d092018-04-07 15:32:37977 }
978 }
979
Sigurd Schneider45f32c32020-10-13 13:32:05980 /**
981 * @param {string} property
982 */
Mathias Bynens5165a7a2020-06-10 05:51:43983 function isAllowedProperty(property) {
Blink Reformat4c46d092018-04-07 15:32:37984 // Make sure that allowed properties do not interfere with link visibility.
985 const prefixes = [
986 'background', 'border', 'color', 'font', 'line', 'margin', 'padding', 'text', '-webkit-background',
987 '-webkit-border', '-webkit-font', '-webkit-margin', '-webkit-padding', '-webkit-text'
988 ];
Sigurd Schneider45f32c32020-10-13 13:32:05989 for (const prefix of prefixes) {
990 if (property.startsWith(prefix)) {
Blink Reformat4c46d092018-04-07 15:32:37991 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:34992 }
Blink Reformat4c46d092018-04-07 15:32:37993 }
994 return false;
995 }
996
Sigurd Schneider45f32c32020-10-13 13:32:05997 /** @type {!Object.<string, function((string|!{description: string}|undefined|!SDK.RemoteObject.RemoteObject), !Platform.StringUtilities.FORMATTER_TOKEN):*>} */
998 const formatters = {};
Blink Reformat4c46d092018-04-07 15:32:37999 // Firebug uses %o for formatting objects.
1000 formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */);
1001 formatters.s = stringFormatter;
1002 formatters.f = floatFormatter;
1003 // Firebug allows both %i and %d for formatting integers.
1004 formatters.i = integerFormatter;
1005 formatters.d = integerFormatter;
1006
1007 // Firebug uses %c for styling the message.
1008 formatters.c = styleFormatter;
1009
1010 // Support %O to force object formatting, instead of the type-based %o formatting.
1011 formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */);
1012
1013 formatters._ = bypassFormatter;
1014
1015 /**
Sigurd Schneider53f33522020-10-08 15:00:491016 * @param {!HTMLElement} a
Blink Reformat4c46d092018-04-07 15:32:371017 * @param {*} b
Tim van der Lippeeaacb722020-01-10 12:16:001018 * @this {!ConsoleViewMessage}
Sigurd Schneider53f33522020-10-08 15:00:491019 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371020 */
1021 function append(a, b) {
1022 if (b instanceof Node) {
1023 a.appendChild(b);
Erik Luo17926392018-05-17 22:06:121024 return a;
1025 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341026 if (typeof b === 'undefined') {
Erik Luo17926392018-05-17 22:06:121027 return a;
Tim van der Lippe1d6e57a2019-09-30 11:55:341028 }
Erik Luo17926392018-05-17 22:06:121029 if (!currentStyle) {
Erik Luo383f21d2018-11-07 23:16:371030 a.appendChild(this._linkifyStringAsFragment(String(b)));
Erik Luo17926392018-05-17 22:06:121031 return a;
1032 }
1033 const lines = String(b).split('\n');
1034 for (let i = 0; i < lines.length; i++) {
1035 const line = lines[i];
Erik Luo383f21d2018-11-07 23:16:371036 const lineFragment = this._linkifyStringAsFragment(line);
Sigurd Schneider53f33522020-10-08 15:00:491037 const wrapper = /** @type {!HTMLElement} */ (document.createElement('span'));
Erik Luo17926392018-05-17 22:06:121038 wrapper.style.setProperty('contain', 'paint');
1039 wrapper.style.setProperty('display', 'inline-block');
1040 wrapper.style.setProperty('max-width', '100%');
1041 wrapper.appendChild(lineFragment);
1042 applyCurrentStyle(wrapper);
1043 for (const child of wrapper.children) {
Sigurd Schneider53f33522020-10-08 15:00:491044 if (child.classList.contains('devtools-link') && child instanceof HTMLElement) {
Erik Luo17926392018-05-17 22:06:121045 this._applyForcedVisibleStyle(child);
Tim van der Lippe1d6e57a2019-09-30 11:55:341046 }
Blink Reformat4c46d092018-04-07 15:32:371047 }
Erik Luo17926392018-05-17 22:06:121048 a.appendChild(wrapper);
Tim van der Lippe1d6e57a2019-09-30 11:55:341049 if (i < lines.length - 1) {
Sigurd Schneider53f33522020-10-08 15:00:491050 a.appendChild(document.createElement('br'));
Tim van der Lippe1d6e57a2019-09-30 11:55:341051 }
Blink Reformat4c46d092018-04-07 15:32:371052 }
1053 return a;
1054 }
1055
1056 /**
Sigurd Schneider53f33522020-10-08 15:00:491057 * @param {!HTMLElement} element
Blink Reformat4c46d092018-04-07 15:32:371058 */
1059 function applyCurrentStyle(element) {
Sigurd Schneider45f32c32020-10-13 13:32:051060 if (!currentStyle) {
1061 return;
1062 }
1063 for (const [property, {value, priority}] of currentStyle.entries()) {
1064 element.style.setProperty(/** @type {string} */ (property), value, priority);
Tim van der Lippe1d6e57a2019-09-30 11:55:341065 }
Blink Reformat4c46d092018-04-07 15:32:371066 }
1067
Tim van der Lippe93b57c32020-02-20 17:38:441068 // Platform.StringUtilities.format does treat formattedResult like a Builder, result is an object.
1069 return Platform.StringUtilities.format(format, parameters, formatters, formattedResult, append.bind(this));
Blink Reformat4c46d092018-04-07 15:32:371070 }
1071
1072 /**
Sigurd Schneider53f33522020-10-08 15:00:491073 * @param {!HTMLElement} element
Blink Reformat4c46d092018-04-07 15:32:371074 */
1075 _applyForcedVisibleStyle(element) {
1076 element.style.setProperty('-webkit-text-stroke', '0', 'important');
1077 element.style.setProperty('text-decoration', 'underline', 'important');
1078
Paul Lewisca569a52020-09-09 16:11:511079 const themedColor = ThemeSupport.ThemeSupport.instance().patchColorText(
1080 'rgb(33%, 33%, 33%)', ThemeSupport.ThemeSupport.ColorUsage.Foreground);
Blink Reformat4c46d092018-04-07 15:32:371081 element.style.setProperty('color', themedColor, 'important');
1082
1083 let backgroundColor = 'hsl(0, 0%, 100%)';
Tim van der Lippe9b2f8712020-02-12 17:46:221084 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Blink Reformat4c46d092018-04-07 15:32:371085 backgroundColor = 'hsl(0, 100%, 97%)';
Tim van der Lippe9b2f8712020-02-12 17:46:221086 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371087 backgroundColor = 'hsl(50, 100%, 95%)';
Tim van der Lippe1d6e57a2019-09-30 11:55:341088 }
Paul Lewisca569a52020-09-09 16:11:511089 const themedBackgroundColor = ThemeSupport.ThemeSupport.instance().patchColorText(
1090 backgroundColor, ThemeSupport.ThemeSupport.ColorUsage.Background);
Blink Reformat4c46d092018-04-07 15:32:371091 element.style.setProperty('background-color', themedBackgroundColor, 'important');
1092 }
1093
1094 /**
Sigurd Schneider45f32c32020-10-13 13:32:051095 * @param {!RegExp} regexObject
Blink Reformat4c46d092018-04-07 15:32:371096 * @return {boolean}
1097 */
1098 matchesFilterRegex(regexObject) {
1099 regexObject.lastIndex = 0;
Erik Luo5976c8c2018-07-24 02:03:091100 const contentElement = this.contentElement();
1101 const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : '';
Sigurd Schneider45f32c32020-10-13 13:32:051102 return (!!anchorText && regexObject.test(anchorText.trim())) ||
Erik Luo5976c8c2018-07-24 02:03:091103 regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
Blink Reformat4c46d092018-04-07 15:32:371104 }
1105
1106 /**
1107 * @param {string} filter
1108 * @return {boolean}
1109 */
1110 matchesFilterText(filter) {
1111 const text = this.contentElement().deepTextContent();
1112 return text.toLowerCase().includes(filter.toLowerCase());
1113 }
1114
1115 updateTimestamp() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341116 if (!this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371117 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341118 }
Blink Reformat4c46d092018-04-07 15:32:371119
Paul Lewis2d7d65c2020-03-16 17:26:301120 if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341121 if (!this._timestampElement) {
Sigurd Schneider53e98632020-10-26 15:29:501122 this._timestampElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:091123 this._timestampElement.classList.add('console-timestamp');
Tim van der Lippe1d6e57a2019-09-30 11:55:341124 }
Tim van der Lippe9b2f8712020-02-12 17:46:221125 this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' ';
1126 this._timestampElement.title = UI.UIUtils.formatTimestamp(this._message.timestamp, true);
Blink Reformat4c46d092018-04-07 15:32:371127 this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild);
1128 } else if (this._timestampElement) {
1129 this._timestampElement.remove();
Sigurd Schneider53e98632020-10-26 15:29:501130 this._timestampElement = null;
Blink Reformat4c46d092018-04-07 15:32:371131 }
Blink Reformat4c46d092018-04-07 15:32:371132 }
1133
1134 /**
1135 * @return {number}
1136 */
1137 nestingLevel() {
1138 return this._nestingLevel;
1139 }
1140
1141 /**
1142 * @param {boolean} inSimilarGroup
1143 * @param {boolean=} isLast
1144 */
1145 setInSimilarGroup(inSimilarGroup, isLast) {
1146 this._inSimilarGroup = inSimilarGroup;
1147 this._lastInSimilarGroup = inSimilarGroup && !!isLast;
1148 if (this._similarGroupMarker && !inSimilarGroup) {
1149 this._similarGroupMarker.remove();
1150 this._similarGroupMarker = null;
1151 } else if (this._element && !this._similarGroupMarker && inSimilarGroup) {
Sigurd Schneider53e98632020-10-26 15:29:501152 this._similarGroupMarker = /** @type {!HTMLElement} */ (document.createElement('div'));
Tim van der Lippef49e2322020-05-01 15:03:091153 this._similarGroupMarker.classList.add('nesting-level-marker');
Blink Reformat4c46d092018-04-07 15:32:371154 this._element.insertBefore(this._similarGroupMarker, this._element.firstChild);
1155 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1156 }
1157 }
1158
1159 /**
1160 * @return {boolean}
1161 */
1162 isLastInSimilarGroup() {
Sigurd Schneider45f32c32020-10-13 13:32:051163 return !!this._inSimilarGroup && !!this._lastInSimilarGroup;
Blink Reformat4c46d092018-04-07 15:32:371164 }
1165
1166 resetCloseGroupDecorationCount() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341167 if (!this._closeGroupDecorationCount) {
Blink Reformat4c46d092018-04-07 15:32:371168 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341169 }
Blink Reformat4c46d092018-04-07 15:32:371170 this._closeGroupDecorationCount = 0;
1171 this._updateCloseGroupDecorations();
1172 }
1173
1174 incrementCloseGroupDecorationCount() {
1175 ++this._closeGroupDecorationCount;
1176 this._updateCloseGroupDecorations();
1177 }
1178
1179 _updateCloseGroupDecorations() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341180 if (!this._nestingLevelMarkers) {
Blink Reformat4c46d092018-04-07 15:32:371181 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341182 }
Blink Reformat4c46d092018-04-07 15:32:371183 for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) {
1184 const marker = this._nestingLevelMarkers[i];
1185 marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount);
1186 }
1187 }
1188
1189 /**
Erik Luo0b8282e2018-10-08 20:37:461190 * @return {number}
1191 */
1192 _focusedChildIndex() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341193 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461194 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341195 }
Erik Luo383f21d2018-11-07 23:16:371196 return this._selectableChildren.findIndex(child => child.element.hasFocus());
Erik Luo0b8282e2018-10-08 20:37:461197 }
1198
1199 /**
Sigurd Schneider45f32c32020-10-13 13:32:051200 * @param {!KeyboardEvent} event
Erik Luo8ef5d0c2018-09-25 21:16:001201 */
1202 _onKeyDown(event) {
Sigurd Schneider45f32c32020-10-13 13:32:051203 if (UI.UIUtils.isEditing() || !this._element || !this._element.hasFocus() || this._element.hasSelection()) {
Erik Luo8ef5d0c2018-09-25 21:16:001204 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341205 }
1206 if (this.maybeHandleOnKeyDown(event)) {
Erik Luo8ef5d0c2018-09-25 21:16:001207 event.consume(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:341208 }
Erik Luo8ef5d0c2018-09-25 21:16:001209 }
1210
1211 /**
1212 * @protected
Sigurd Schneider45f32c32020-10-13 13:32:051213 * @param {!KeyboardEvent} event
Erik Luo8ef5d0c2018-09-25 21:16:001214 */
1215 maybeHandleOnKeyDown(event) {
1216 // Handle trace expansion.
Erik Luo0b8282e2018-10-08 20:37:461217 const focusedChildIndex = this._focusedChildIndex();
1218 const isWrapperFocused = focusedChildIndex === -1;
1219 if (this._expandTrace && isWrapperFocused) {
Erik Luo8ef5d0c2018-09-25 21:16:001220 if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) {
1221 this._expandTrace(!this._traceExpanded);
1222 return true;
1223 }
1224 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341225 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461226 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:341227 }
Erik Luo0b8282e2018-10-08 20:37:461228
1229 if (event.key === 'ArrowLeft') {
Sigurd Schneider45f32c32020-10-13 13:32:051230 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461231 return true;
1232 }
1233 if (event.key === 'ArrowRight') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341234 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461235 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341236 }
Erik Luo0b8282e2018-10-08 20:37:461237 }
1238 if (event.key === 'ArrowUp') {
Erik Luo182bece2018-11-29 03:15:221239 const firstVisibleChild = this._nearestVisibleChild(0);
1240 if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
Sigurd Schneider45f32c32020-10-13 13:32:051241 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461242 return true;
Mathias Bynensf06e8c02020-02-28 13:58:281243 }
1244 if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461245 return true;
1246 }
1247 }
1248 if (event.key === 'ArrowDown') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341249 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461250 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341251 }
1252 if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) {
Erik Luo0b8282e2018-10-08 20:37:461253 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341254 }
Erik Luo0b8282e2018-10-08 20:37:461255 }
Erik Luo8ef5d0c2018-09-25 21:16:001256 return false;
1257 }
1258
Erik Luo182bece2018-11-29 03:15:221259 /**
1260 * @param {number} fromIndex
1261 * @param {boolean=} backwards
1262 * @return {boolean}
1263 */
1264 _selectNearestVisibleChild(fromIndex, backwards) {
1265 const nearestChild = this._nearestVisibleChild(fromIndex, backwards);
1266 if (nearestChild) {
Erik Luo31c21f62018-12-13 03:39:391267 nearestChild.forceSelect();
Erik Luo182bece2018-11-29 03:15:221268 return true;
1269 }
1270 return false;
1271 }
1272
1273 /**
1274 * @param {number} fromIndex
1275 * @param {boolean=} backwards
Erik Luo31c21f62018-12-13 03:39:391276 * @return {?{element: !Element, forceSelect: function()}}
Erik Luo182bece2018-11-29 03:15:221277 */
1278 _nearestVisibleChild(fromIndex, backwards) {
1279 const childCount = this._selectableChildren.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:341280 if (fromIndex < 0 || fromIndex >= childCount) {
Erik Luo182bece2018-11-29 03:15:221281 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341282 }
Erik Luo182bece2018-11-29 03:15:221283 const direction = backwards ? -1 : 1;
1284 let index = fromIndex;
1285
1286 while (!this._selectableChildren[index].element.offsetParent) {
1287 index += direction;
Tim van der Lippe1d6e57a2019-09-30 11:55:341288 if (index < 0 || index >= childCount) {
Erik Luo182bece2018-11-29 03:15:221289 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341290 }
Erik Luo182bece2018-11-29 03:15:221291 }
1292 return this._selectableChildren[index];
1293 }
1294
Sigurd Schneider53f33522020-10-08 15:00:491295 /**
1296 * @override
1297 */
Erik Luo0b8282e2018-10-08 20:37:461298 focusLastChildOrSelf() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341299 if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461300 this._element.focus();
Tim van der Lippe1d6e57a2019-09-30 11:55:341301 }
Erik Luo0b8282e2018-10-08 20:37:461302 }
1303
1304 /**
Sigurd Schneiderb2953b22020-10-09 09:30:151305 * @protected
1306 * @param {!HTMLElement} element
1307 */
1308 setContentElement(element) {
1309 console.assert(!this._contentElement, 'Cannot set content element twice');
1310 this._contentElement = element;
1311 }
1312
Sigurd Schneiderb2953b22020-10-09 09:30:151313 /**
1314 * @protected
1315 * @return {?HTMLElement}
1316 */
1317 getContentElement() {
1318 return this._contentElement;
1319 }
1320
1321 /**
Sigurd Schneider53f33522020-10-08 15:00:491322 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371323 */
1324 contentElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341325 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371326 return this._contentElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:341327 }
Blink Reformat4c46d092018-04-07 15:32:371328
Sigurd Schneider53f33522020-10-08 15:00:491329 const contentElement = /** @type {!HTMLElement} */ (document.createElement('div'));
Tim van der Lippef49e2322020-05-01 15:03:091330 contentElement.classList.add('console-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341331 if (this._messageLevelIcon) {
Blink Reformat4c46d092018-04-07 15:32:371332 contentElement.appendChild(this._messageLevelIcon);
Tim van der Lippe1d6e57a2019-09-30 11:55:341333 }
Blink Reformat4c46d092018-04-07 15:32:371334 this._contentElement = contentElement;
1335
Sigurd Schneider45f32c32020-10-13 13:32:051336 const runtimeModel = this._message.runtimeModel();
Blink Reformat4c46d092018-04-07 15:32:371337 let formattedMessage;
1338 const shouldIncludeTrace = !!this._message.stackTrace &&
Tim van der Lippe9b2f8712020-02-12 17:46:221339 (this._message.source === SDK.ConsoleModel.MessageSource.Network ||
1340 this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1341 this._message.level === SDK.ConsoleModel.MessageLevel.Error ||
1342 this._message.level === SDK.ConsoleModel.MessageLevel.Warning ||
1343 this._message.type === SDK.ConsoleModel.MessageType.Trace);
Sigurd Schneider45f32c32020-10-13 13:32:051344 if (runtimeModel && shouldIncludeTrace) {
1345 formattedMessage = this._buildMessageWithStackTrace(runtimeModel);
Tim van der Lippe9b2f8712020-02-12 17:46:221346 } else if (this._message.type === SDK.ConsoleModel.MessageType.Table) {
Blink Reformat4c46d092018-04-07 15:32:371347 formattedMessage = this._buildTableMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341348 } else {
Blink Reformat4c46d092018-04-07 15:32:371349 formattedMessage = this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341350 }
Blink Reformat4c46d092018-04-07 15:32:371351 contentElement.appendChild(formattedMessage);
1352
1353 this.updateTimestamp();
1354 return this._contentElement;
1355 }
1356
1357 /**
Sigurd Schneider53f33522020-10-08 15:00:491358 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371359 */
1360 toMessageElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341361 if (this._element) {
Blink Reformat4c46d092018-04-07 15:32:371362 return this._element;
Tim van der Lippe1d6e57a2019-09-30 11:55:341363 }
Blink Reformat4c46d092018-04-07 15:32:371364
Sigurd Schneider53f33522020-10-08 15:00:491365 this._element = /** @type {!HTMLElement} */ (document.createElement('div'));
Pavel Feldmandb310912019-01-30 00:31:201366 this._element.tabIndex = -1;
Sigurd Schneider45f32c32020-10-13 13:32:051367 this._element.addEventListener('keydown', /** @type {!EventListener} */ (this._onKeyDown.bind(this)));
Blink Reformat4c46d092018-04-07 15:32:371368 this.updateMessageElement();
1369 return this._element;
1370 }
1371
1372 updateMessageElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341373 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371374 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341375 }
Blink Reformat4c46d092018-04-07 15:32:371376
1377 this._element.className = 'console-message-wrapper';
1378 this._element.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:341379 if (this._message.isGroupStartMessage()) {
Blink Reformat4c46d092018-04-07 15:32:371380 this._element.classList.add('console-group-title');
Tim van der Lippe1d6e57a2019-09-30 11:55:341381 }
Tim van der Lippe9b2f8712020-02-12 17:46:221382 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:371383 this._element.classList.add('console-from-api');
Tim van der Lippe1d6e57a2019-09-30 11:55:341384 }
Blink Reformat4c46d092018-04-07 15:32:371385 if (this._inSimilarGroup) {
Sigurd Schneider53e98632020-10-26 15:29:501386 this._similarGroupMarker = /** @type {!HTMLElement} */ (this._element.createChild('div', 'nesting-level-marker'));
Blink Reformat4c46d092018-04-07 15:32:371387 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1388 }
1389
1390 this._nestingLevelMarkers = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341391 for (let i = 0; i < this._nestingLevel; ++i) {
Blink Reformat4c46d092018-04-07 15:32:371392 this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker'));
Tim van der Lippe1d6e57a2019-09-30 11:55:341393 }
Blink Reformat4c46d092018-04-07 15:32:371394 this._updateCloseGroupDecorations();
Sigurd Schneiderca7b4ff2020-10-14 07:45:471395 elementToMessage.set(this._element, this);
Blink Reformat4c46d092018-04-07 15:32:371396
1397 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221398 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371399 this._element.classList.add('console-verbose-level');
Blink Reformat4c46d092018-04-07 15:32:371400 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221401 case SDK.ConsoleModel.MessageLevel.Info:
Blink Reformat4c46d092018-04-07 15:32:371402 this._element.classList.add('console-info-level');
Tim van der Lippe9b2f8712020-02-12 17:46:221403 if (this._message.type === SDK.ConsoleModel.MessageType.System) {
Blink Reformat4c46d092018-04-07 15:32:371404 this._element.classList.add('console-system-type');
Tim van der Lippe1d6e57a2019-09-30 11:55:341405 }
Blink Reformat4c46d092018-04-07 15:32:371406 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221407 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371408 this._element.classList.add('console-warning-level');
Blink Reformat4c46d092018-04-07 15:32:371409 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221410 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371411 this._element.classList.add('console-error-level');
Blink Reformat4c46d092018-04-07 15:32:371412 break;
1413 }
Erik Luofd3e7d42018-09-25 02:12:351414 this._updateMessageLevelIcon();
Tim van der Lippe1d6e57a2019-09-30 11:55:341415 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371416 this._element.classList.add('console-warning-level');
Tim van der Lippe1d6e57a2019-09-30 11:55:341417 }
Blink Reformat4c46d092018-04-07 15:32:371418
1419 this._element.appendChild(this.contentElement());
Tim van der Lippe1d6e57a2019-09-30 11:55:341420 if (this._repeatCount > 1) {
Blink Reformat4c46d092018-04-07 15:32:371421 this._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341422 }
Blink Reformat4c46d092018-04-07 15:32:371423 }
1424
1425 /**
1426 * @return {boolean}
1427 */
1428 _shouldRenderAsWarning() {
Tim van der Lippe9b2f8712020-02-12 17:46:221429 return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose ||
1430 this._message.level === SDK.ConsoleModel.MessageLevel.Info) &&
1431 (this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1432 this._message.source === SDK.ConsoleModel.MessageSource.Deprecation ||
1433 this._message.source === SDK.ConsoleModel.MessageSource.Intervention ||
1434 this._message.source === SDK.ConsoleModel.MessageSource.Recommendation);
Blink Reformat4c46d092018-04-07 15:32:371435 }
1436
Erik Luofd3e7d42018-09-25 02:12:351437 _updateMessageLevelIcon() {
1438 let iconType = '';
1439 let accessibleName = '';
Tim van der Lippe9b2f8712020-02-12 17:46:221440 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351441 iconType = 'smallicon-warning';
1442 accessibleName = ls`Warning`;
Tim van der Lippe9b2f8712020-02-12 17:46:221443 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351444 iconType = 'smallicon-error';
1445 accessibleName = ls`Error`;
1446 }
Sigurd Schneider45f32c32020-10-13 13:32:051447 if (!this._messageLevelIcon) {
1448 if (!iconType) {
1449 return;
1450 }
Tim van der Lippe9b2f8712020-02-12 17:46:221451 this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon');
Tim van der Lippe1d6e57a2019-09-30 11:55:341452 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371453 this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341454 }
Blink Reformat4c46d092018-04-07 15:32:371455 }
1456 this._messageLevelIcon.setIconType(iconType);
Erik Luofd3e7d42018-09-25 02:12:351457 UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371458 }
1459
1460 /**
1461 * @return {number}
1462 */
1463 repeatCount() {
1464 return this._repeatCount || 1;
1465 }
1466
1467 resetIncrementRepeatCount() {
1468 this._repeatCount = 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341469 if (!this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371470 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341471 }
Blink Reformat4c46d092018-04-07 15:32:371472
1473 this._repeatCountElement.remove();
Tim van der Lippe1d6e57a2019-09-30 11:55:341474 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371475 this._contentElement.classList.remove('repeated-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341476 }
Sigurd Schneider53e98632020-10-26 15:29:501477 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:371478 }
1479
1480 incrementRepeatCount() {
1481 this._repeatCount++;
1482 this._showRepeatCountElement();
1483 }
1484
1485 /**
1486 * @param {number} repeatCount
1487 */
1488 setRepeatCount(repeatCount) {
1489 this._repeatCount = repeatCount;
1490 this._showRepeatCountElement();
1491 }
1492
Tim van der Lippeee954d42020-05-04 10:35:571493 /**
1494 * @suppress {checkTypes}
1495 */
Blink Reformat4c46d092018-04-07 15:32:371496 _showRepeatCountElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341497 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371498 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341499 }
Blink Reformat4c46d092018-04-07 15:32:371500
1501 if (!this._repeatCountElement) {
Sigurd Schneider45f32c32020-10-13 13:32:051502 this._repeatCountElement =
1503 /** @type {!UI.UIUtils.DevToolsSmallBubble} */ (document.createElement('span', {is: 'dt-small-bubble'}));
Tim van der Lippeee954d42020-05-04 10:35:571504 this._repeatCountElement.classList.add('console-message-repeat-count');
Blink Reformat4c46d092018-04-07 15:32:371505 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221506 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371507 this._repeatCountElement.type = 'warning';
1508 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221509 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371510 this._repeatCountElement.type = 'error';
1511 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221512 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371513 this._repeatCountElement.type = 'verbose';
1514 break;
1515 default:
1516 this._repeatCountElement.type = 'info';
1517 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341518 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371519 this._repeatCountElement.type = 'warning';
Tim van der Lippe1d6e57a2019-09-30 11:55:341520 }
Blink Reformat4c46d092018-04-07 15:32:371521
1522 this._element.insertBefore(this._repeatCountElement, this._contentElement);
Sigurd Schneider45f32c32020-10-13 13:32:051523 this.contentElement().classList.add('repeated-message');
Blink Reformat4c46d092018-04-07 15:32:371524 }
Sigurd Schneider45f32c32020-10-13 13:32:051525 this._repeatCountElement.textContent = `${this._repeatCount}`;
Erik Luofd3e7d42018-09-25 02:12:351526 let accessibleName = ls`Repeat ${this._repeatCount}`;
Tim van der Lippe9b2f8712020-02-12 17:46:221527 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351528 accessibleName = ls`Warning ${accessibleName}`;
Tim van der Lippe9b2f8712020-02-12 17:46:221529 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351530 accessibleName = ls`Error ${accessibleName}`;
Tim van der Lippe1d6e57a2019-09-30 11:55:341531 }
Erik Luofd3e7d42018-09-25 02:12:351532 UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371533 }
1534
1535 get text() {
1536 return this._message.messageText;
1537 }
1538
1539 /**
1540 * @return {string}
1541 */
1542 toExportString() {
1543 const lines = [];
1544 const nodes = this.contentElement().childTextNodes();
Tim van der Lippe9b2f8712020-02-12 17:46:221545 const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join('');
Tim van der Lippe1d6e57a2019-09-30 11:55:341546 for (let i = 0; i < this.repeatCount(); ++i) {
Blink Reformat4c46d092018-04-07 15:32:371547 lines.push(messageContent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341548 }
Blink Reformat4c46d092018-04-07 15:32:371549 return lines.join('\n');
1550 }
1551
1552 /**
1553 * @param {?RegExp} regex
1554 */
1555 setSearchRegex(regex) {
Sigurd Schneidere8e75cf2020-10-13 08:17:521556 if (this._searchHighlightNodeChanges && this._searchHighlightNodeChanges.length) {
1557 UI.UIUtils.revertDomChanges(this._searchHighlightNodeChanges);
Tim van der Lippe1d6e57a2019-09-30 11:55:341558 }
Blink Reformat4c46d092018-04-07 15:32:371559 this._searchRegex = regex;
1560 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:521561 this._searchHighlightNodeChanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341562 if (!this._searchRegex) {
Blink Reformat4c46d092018-04-07 15:32:371563 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341564 }
Blink Reformat4c46d092018-04-07 15:32:371565
1566 const text = this.contentElement().deepTextContent();
1567 let match;
1568 this._searchRegex.lastIndex = 0;
1569 const sourceRanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341570 while ((match = this._searchRegex.exec(text)) && match[0]) {
Tim van der Lippe9b2f8712020-02-12 17:46:221571 sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length));
Tim van der Lippe1d6e57a2019-09-30 11:55:341572 }
Blink Reformat4c46d092018-04-07 15:32:371573
1574 if (sourceRanges.length) {
1575 this._searchHighlightNodes =
Sigurd Schneidere8e75cf2020-10-13 08:17:521576 UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHighlightNodeChanges);
Blink Reformat4c46d092018-04-07 15:32:371577 }
1578 }
1579
1580 /**
1581 * @return {?RegExp}
1582 */
1583 searchRegex() {
1584 return this._searchRegex;
1585 }
1586
1587 /**
1588 * @return {number}
1589 */
1590 searchCount() {
1591 return this._searchHighlightNodes.length;
1592 }
1593
1594 /**
Sigurd Schneider45f32c32020-10-13 13:32:051595 * @param {number} index
1596 * @return {!Element}
Blink Reformat4c46d092018-04-07 15:32:371597 */
1598 searchHighlightNode(index) {
1599 return this._searchHighlightNodes[index];
1600 }
1601
1602 /**
1603 * @param {string} string
Sigurd Schneider53f33522020-10-08 15:00:491604 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371605 */
1606 _tryFormatAsError(string) {
1607 /**
1608 * @param {string} prefix
1609 */
1610 function startsWith(prefix) {
1611 return string.startsWith(prefix);
1612 }
1613
Sigurd Schneider45f32c32020-10-13 13:32:051614 const runtimeModel = this._message.runtimeModel();
Blink Reformat4c46d092018-04-07 15:32:371615 const errorPrefixes =
1616 ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError'];
Sigurd Schneider45f32c32020-10-13 13:32:051617 if (!runtimeModel || !errorPrefixes.some(startsWith)) {
Blink Reformat4c46d092018-04-07 15:32:371618 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341619 }
Sigurd Schneider45f32c32020-10-13 13:32:051620 const debuggerModel = runtimeModel.debuggerModel();
1621 const baseURL = runtimeModel.target().inspectedURL();
Blink Reformat4c46d092018-04-07 15:32:371622
1623 const lines = string.split('\n');
1624 const links = [];
1625 let position = 0;
1626 for (let i = 0; i < lines.length; ++i) {
1627 position += i > 0 ? lines[i - 1].length + 1 : 0;
1628 const isCallFrameLine = /^\s*at\s/.test(lines[i]);
Tim van der Lippe1d6e57a2019-09-30 11:55:341629 if (!isCallFrameLine && links.length) {
Blink Reformat4c46d092018-04-07 15:32:371630 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341631 }
Blink Reformat4c46d092018-04-07 15:32:371632
Tim van der Lippe1d6e57a2019-09-30 11:55:341633 if (!isCallFrameLine) {
Blink Reformat4c46d092018-04-07 15:32:371634 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341635 }
Blink Reformat4c46d092018-04-07 15:32:371636
1637 let openBracketIndex = -1;
1638 let closeBracketIndex = -1;
Yang Guo39256bd2019-07-18 06:02:251639 const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g;
1640 const inBrackets = /\([^\)\(]+\)/g;
1641 let lastMatch = null;
1642 let currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341643 while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251644 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341645 }
Yang Guo39256bd2019-07-18 06:02:251646 if (!lastMatch) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341647 while ((currentMatch = inBrackets.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251648 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341649 }
Yang Guo39256bd2019-07-18 06:02:251650 }
1651 if (lastMatch) {
1652 openBracketIndex = lastMatch.index;
1653 closeBracketIndex = lastMatch.index + lastMatch[0].length - 1;
Blink Reformat4c46d092018-04-07 15:32:371654 }
1655 const hasOpenBracket = openBracketIndex !== -1;
1656 const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3;
1657 const right = hasOpenBracket ? closeBracketIndex : lines[i].length;
1658 const linkCandidate = lines[i].substring(left, right);
Tim van der Lippe9b2f8712020-02-12 17:46:221659 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate);
Tim van der Lippe1d6e57a2019-09-30 11:55:341660 if (!splitResult) {
Blink Reformat4c46d092018-04-07 15:32:371661 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341662 }
Blink Reformat4c46d092018-04-07 15:32:371663
Tim van der Lippe1d6e57a2019-09-30 11:55:341664 if (splitResult.url === '<anonymous>') {
Blink Reformat4c46d092018-04-07 15:32:371665 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341666 }
Blink Reformat4c46d092018-04-07 15:32:371667 let url = parseOrScriptMatch(splitResult.url);
Tim van der Lippe9b2f8712020-02-12 17:46:221668 if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) {
1669 url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url));
Tim van der Lippe1d6e57a2019-09-30 11:55:341670 }
1671 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371672 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341673 }
Blink Reformat4c46d092018-04-07 15:32:371674
1675 links.push({
1676 url: url,
1677 positionLeft: position + left,
1678 positionRight: position + right,
1679 lineNumber: splitResult.lineNumber,
1680 columnNumber: splitResult.columnNumber
1681 });
1682 }
1683
Tim van der Lippe1d6e57a2019-09-30 11:55:341684 if (!links.length) {
Blink Reformat4c46d092018-04-07 15:32:371685 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341686 }
Blink Reformat4c46d092018-04-07 15:32:371687
Sigurd Schneider53f33522020-10-08 15:00:491688 const formattedResult = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:371689 let start = 0;
1690 for (let i = 0; i < links.length; ++i) {
Erik Luo383f21d2018-11-07 23:16:371691 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft)));
Erik Luo182bece2018-11-29 03:15:221692 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:051693 debuggerModel.target(), null, links[i].url, links[i].lineNumber,
1694 {columnNumber: links[i].columnNumber, className: undefined, tabStop: undefined});
Erik Luo182bece2018-11-29 03:15:221695 scriptLocationLink.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391696 this._selectableChildren.push({element: scriptLocationLink, forceSelect: () => scriptLocationLink.focus()});
Erik Luo182bece2018-11-29 03:15:221697 formattedResult.appendChild(scriptLocationLink);
Blink Reformat4c46d092018-04-07 15:32:371698 start = links[i].positionRight;
1699 }
1700
Tim van der Lippe1d6e57a2019-09-30 11:55:341701 if (start !== string.length) {
Erik Luo383f21d2018-11-07 23:16:371702 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start)));
Tim van der Lippe1d6e57a2019-09-30 11:55:341703 }
Blink Reformat4c46d092018-04-07 15:32:371704
1705 return formattedResult;
1706
1707 /**
1708 * @param {?string} url
1709 * @return {?string}
1710 */
1711 function parseOrScriptMatch(url) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341712 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371713 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341714 }
Tim van der Lippe9b2f8712020-02-12 17:46:221715 const parsedURL = Common.ParsedURL.ParsedURL.fromString(url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341716 if (parsedURL) {
Blink Reformat4c46d092018-04-07 15:32:371717 return parsedURL.url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341718 }
1719 if (debuggerModel.scriptsForSourceURL(url).length) {
Blink Reformat4c46d092018-04-07 15:32:371720 return url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341721 }
Blink Reformat4c46d092018-04-07 15:32:371722 return null;
1723 }
1724 }
1725
1726 /**
1727 * @param {string} string
1728 * @param {function(string,string,number=,number=):!Node} linkifier
1729 * @return {!DocumentFragment}
Tim van der Lippeeaacb722020-01-10 12:16:001730 * @suppress {accessControls}
Blink Reformat4c46d092018-04-07 15:32:371731 */
Erik Luofc2214f2018-11-21 19:54:581732 _linkifyWithCustomLinkifier(string, linkifier) {
Sigurd Schneider8f4ac862020-10-13 13:30:111733 if (string.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:221734 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:111735 document.createElement('span'), string, getLongStringVisibleLength());
Sigurd Schneider45f32c32020-10-13 13:32:051736 const fragment = document.createDocumentFragment();
Connor Moody1a5c0d32019-12-19 07:23:361737 fragment.appendChild(propertyValue.element);
1738 return fragment;
Tim van der Lippe1d6e57a2019-09-30 11:55:341739 }
Sigurd Schneider45f32c32020-10-13 13:32:051740 const container = document.createDocumentFragment();
Tim van der Lippeeaacb722020-01-10 12:16:001741 const tokens = ConsoleViewMessage._tokenizeMessageText(string);
Blink Reformat4c46d092018-04-07 15:32:371742 for (const token of tokens) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341743 if (!token.text) {
Erik Luofc2214f2018-11-21 19:54:581744 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341745 }
Blink Reformat4c46d092018-04-07 15:32:371746 switch (token.type) {
1747 case 'url': {
1748 const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text);
Tim van der Lippe9b2f8712020-02-12 17:46:221749 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL);
Kim-Anh Tran9e49a452020-02-17 09:46:101750 const sourceURL = Common.ParsedURL.ParsedURL.removeWasmFunctionInfoFromURL(splitResult.url);
Blink Reformat4c46d092018-04-07 15:32:371751 let linkNode;
Tim van der Lippe1d6e57a2019-09-30 11:55:341752 if (splitResult) {
Kim-Anh Tran9e49a452020-02-17 09:46:101753 linkNode = linkifier(token.text, sourceURL, splitResult.lineNumber, splitResult.columnNumber);
Tim van der Lippe1d6e57a2019-09-30 11:55:341754 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051755 linkNode = linkifier(token.text, '');
Tim van der Lippe1d6e57a2019-09-30 11:55:341756 }
Blink Reformat4c46d092018-04-07 15:32:371757 container.appendChild(linkNode);
1758 break;
1759 }
1760 default:
Sigurd Schneider45f32c32020-10-13 13:32:051761 container.appendChild(document.createTextNode(token.text));
Blink Reformat4c46d092018-04-07 15:32:371762 break;
1763 }
1764 }
1765 return container;
1766 }
1767
1768 /**
Blink Reformat4c46d092018-04-07 15:32:371769 * @param {string} string
1770 * @return {!DocumentFragment}
1771 */
Erik Luo383f21d2018-11-07 23:16:371772 _linkifyStringAsFragment(string) {
Erik Luofc2214f2018-11-21 19:54:581773 return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:051774 const options = {text, lineNumber, columnNumber};
1775 const linkElement = Components.Linkifier.Linkifier.linkifyURL(
1776 url, /** @type {!Components.Linkifier.LinkifyURLOptions} */ (options));
Erik Luo383f21d2018-11-07 23:16:371777 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391778 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo383f21d2018-11-07 23:16:371779 return linkElement;
Blink Reformat4c46d092018-04-07 15:32:371780 });
1781 }
1782
1783 /**
1784 * @param {string} string
Sigurd Schneider30ac3dd2020-10-13 09:06:391785 * @return {!Array<{type: (string|undefined), text: string}>}
Tim van der Lippeeaacb722020-01-10 12:16:001786 * @suppress {accessControls}
Blink Reformat4c46d092018-04-07 15:32:371787 */
1788 static _tokenizeMessageText(string) {
Sigurd Schneider30ac3dd2020-10-13 09:06:391789 const {tokenizerRegexes, tokenizerTypes} = getOrCreateTokenizers();
Sigurd Schneider8f4ac862020-10-13 13:30:111790 if (string.length > getMaxTokenizableStringLength()) {
Blink Reformat4c46d092018-04-07 15:32:371791 return [{text: string, type: undefined}];
Tim van der Lippe1d6e57a2019-09-30 11:55:341792 }
Sigurd Schneider30ac3dd2020-10-13 09:06:391793 const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, tokenizerRegexes);
1794 return results.map(result => ({text: result.value, type: tokenizerTypes[result.regexIndex]}));
Blink Reformat4c46d092018-04-07 15:32:371795 }
1796
1797 /**
1798 * @return {string}
1799 */
1800 groupKey() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341801 if (!this._groupKey) {
Blink Reformat4c46d092018-04-07 15:32:371802 this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle();
Tim van der Lippe1d6e57a2019-09-30 11:55:341803 }
Blink Reformat4c46d092018-04-07 15:32:371804 return this._groupKey;
1805 }
1806
1807 /**
1808 * @return {string}
1809 */
1810 groupTitle() {
Tim van der Lippeeaacb722020-01-10 12:16:001811 const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText);
Blink Reformat4c46d092018-04-07 15:32:371812 const result = tokens.reduce((acc, token) => {
1813 let text = token.text;
Tim van der Lippe1d6e57a2019-09-30 11:55:341814 if (token.type === 'url') {
Tim van der Lippe9b2f8712020-02-12 17:46:221815 text = Common.UIString.UIString('<URL>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341816 } else if (token.type === 'time') {
Tim van der Lippe9b2f8712020-02-12 17:46:221817 text = Common.UIString.UIString('took <N>ms');
Tim van der Lippe1d6e57a2019-09-30 11:55:341818 } else if (token.type === 'event') {
Tim van der Lippe9b2f8712020-02-12 17:46:221819 text = Common.UIString.UIString('<some> event');
Tim van der Lippe1d6e57a2019-09-30 11:55:341820 } else if (token.type === 'milestone') {
Tim van der Lippe9b2f8712020-02-12 17:46:221821 text = Common.UIString.UIString(' M<XX>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341822 } else if (token.type === 'autofill') {
Tim van der Lippe9b2f8712020-02-12 17:46:221823 text = Common.UIString.UIString('<attribute>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341824 }
Blink Reformat4c46d092018-04-07 15:32:371825 return acc + text;
1826 }, '');
1827 return result.replace(/[%]o/g, '');
1828 }
Paul Lewisbf7aa3c2019-11-20 17:03:381829}
Blink Reformat4c46d092018-04-07 15:32:371830
Sigurd Schneider30ac3dd2020-10-13 09:06:391831/** @type {?Array<!RegExp>} */
1832let tokenizerRegexes = null;
1833/** @type {?Array<string>} */
1834let tokenizerTypes = null;
1835
1836/**
1837 * @return {!{tokenizerRegexes:!Array<!RegExp>, tokenizerTypes:Array<string>}}
1838 */
1839function getOrCreateTokenizers() {
1840 if (!tokenizerRegexes || !tokenizerTypes) {
1841 const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f';
1842 const linkStringRegex = new RegExp(
1843 '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes +
1844 '"\')}\\],:;.!?]',
1845 'u');
1846 const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/;
1847 const timeRegex = /took [\d]+ms/;
1848 const eventRegex = /'\w+' event/;
1849 const milestoneRegex = /\sM[6-7]\d/;
1850 const autofillRegex = /\(suggested: \"[\w-]+\"\)/;
1851 /** @type {!Map<!RegExp, string>} */
1852 const handlers = new Map();
1853 handlers.set(linkStringRegex, 'url');
1854 handlers.set(pathLineRegex, 'url');
1855 handlers.set(timeRegex, 'time');
1856 handlers.set(eventRegex, 'event');
1857 handlers.set(milestoneRegex, 'milestone');
1858 handlers.set(autofillRegex, 'autofill');
1859 tokenizerRegexes = Array.from(handlers.keys());
1860 tokenizerTypes = Array.from(handlers.values());
1861 return {tokenizerRegexes, tokenizerTypes};
1862 }
1863 return {tokenizerRegexes, tokenizerTypes};
1864}
1865
Paul Lewisbf7aa3c2019-11-20 17:03:381866export class ConsoleGroupViewMessage extends ConsoleViewMessage {
Blink Reformat4c46d092018-04-07 15:32:371867 /**
Tim van der Lippe9b2f8712020-02-12 17:46:221868 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
1869 * @param {!Components.Linkifier.Linkifier} linkifier
Blink Reformat4c46d092018-04-07 15:32:371870 * @param {number} nestingLevel
Sigurd Schneider45f32c32020-10-13 13:32:051871 * @param {function(): void} onToggle
1872 * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize
Blink Reformat4c46d092018-04-07 15:32:371873 */
Tim van der Lippeb45d9a02019-11-05 17:24:411874 constructor(consoleMessage, linkifier, nestingLevel, onToggle, onResize) {
Blink Reformat4c46d092018-04-07 15:32:371875 console.assert(consoleMessage.isGroupStartMessage());
Tim van der Lippeb45d9a02019-11-05 17:24:411876 super(consoleMessage, linkifier, nestingLevel, onResize);
Tim van der Lippe9b2f8712020-02-12 17:46:221877 this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed;
1878 /** @type {?UI.Icon.Icon} */
Blink Reformat4c46d092018-04-07 15:32:371879 this._expandGroupIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:001880 this._onToggle = onToggle;
Blink Reformat4c46d092018-04-07 15:32:371881 }
1882
1883 /**
1884 * @param {boolean} collapsed
1885 */
Erik Luo8ef5d0c2018-09-25 21:16:001886 _setCollapsed(collapsed) {
Blink Reformat4c46d092018-04-07 15:32:371887 this._collapsed = collapsed;
Tim van der Lippe1d6e57a2019-09-30 11:55:341888 if (this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371889 this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down');
Tim van der Lippe1d6e57a2019-09-30 11:55:341890 }
Erik Luo8ef5d0c2018-09-25 21:16:001891 this._onToggle.call(null);
Blink Reformat4c46d092018-04-07 15:32:371892 }
1893
1894 /**
1895 * @return {boolean}
1896 */
1897 collapsed() {
1898 return this._collapsed;
1899 }
1900
1901 /**
1902 * @override
Sigurd Schneider45f32c32020-10-13 13:32:051903 * @param {!KeyboardEvent} event
Erik Luo8ef5d0c2018-09-25 21:16:001904 */
1905 maybeHandleOnKeyDown(event) {
Erik Luo0b8282e2018-10-08 20:37:461906 const focusedChildIndex = this._focusedChildIndex();
1907 if (focusedChildIndex === -1) {
1908 if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) {
1909 this._setCollapsed(!this._collapsed);
1910 return true;
1911 }
Erik Luo8ef5d0c2018-09-25 21:16:001912 }
1913 return super.maybeHandleOnKeyDown(event);
1914 }
1915
1916 /**
1917 * @override
Sigurd Schneider53f33522020-10-08 15:00:491918 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371919 */
1920 toMessageElement() {
Sigurd Schneider45f32c32020-10-13 13:32:051921 /** @type {?HTMLElement} */
1922 let element = this._element || null;
1923 if (!element) {
1924 element = super.toMessageElement();
Erik Luo8ef5d0c2018-09-25 21:16:001925 const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down';
Tim van der Lippe9b2f8712020-02-12 17:46:221926 this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon');
Erik Luob5bfff42018-09-20 02:52:391927 // Intercept focus to avoid highlight on click.
Sigurd Schneider45f32c32020-10-13 13:32:051928 this.contentElement().tabIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341929 if (this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371930 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341931 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051932 element.insertBefore(this._expandGroupIcon, this._contentElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:341933 }
Sigurd Schneider45f32c32020-10-13 13:32:051934 element.addEventListener('click', () => this._setCollapsed(!this._collapsed));
Blink Reformat4c46d092018-04-07 15:32:371935 }
Sigurd Schneider45f32c32020-10-13 13:32:051936 return element;
Blink Reformat4c46d092018-04-07 15:32:371937 }
1938
1939 /**
1940 * @override
1941 */
1942 _showRepeatCountElement() {
1943 super._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341944 if (this._repeatCountElement && this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371945 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341946 }
Blink Reformat4c46d092018-04-07 15:32:371947 }
Paul Lewisbf7aa3c2019-11-20 17:03:381948}
Blink Reformat4c46d092018-04-07 15:32:371949
Sigurd Schneiderca7b4ff2020-10-14 07:45:471950export class ConsoleCommand extends ConsoleViewMessage {
1951 /**
1952 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
1953 * @param {!Components.Linkifier.Linkifier} linkifier
1954 * @param {number} nestingLevel
1955 * @param {function(!Common.EventTarget.EventTargetEvent):void} onResize
1956 */
1957 constructor(consoleMessage, linkifier, nestingLevel, onResize) {
1958 super(consoleMessage, linkifier, nestingLevel, onResize);
1959 /** @type {?HTMLElement} */
1960 this._formattedCommand = null;
1961 }
1962
1963 /**
1964 * @override
1965 * @return {!HTMLElement}
1966 */
1967 contentElement() {
1968 const contentElement = this.getContentElement();
1969 if (contentElement) {
1970 return contentElement;
1971 }
1972 const newContentElement = /** @type {!HTMLElement} */ (document.createElement('div'));
1973 this.setContentElement(newContentElement);
1974 newContentElement.classList.add('console-user-command');
1975 const icon = UI.Icon.Icon.create('smallicon-user-command', 'command-result-icon');
1976 newContentElement.appendChild(icon);
1977
1978 elementToMessage.set(newContentElement, this);
1979
1980 this._formattedCommand = /** @type {!HTMLElement} */ (document.createElement('span'));
1981 this._formattedCommand.classList.add('source-code');
1982 this._formattedCommand.textContent = Platform.StringUtilities.replaceControlCharacters(this.text);
1983 newContentElement.appendChild(this._formattedCommand);
1984
1985 if (this._formattedCommand.textContent.length < MaxLengthToIgnoreHighlighter) {
1986 const javascriptSyntaxHighlighter = new UI.SyntaxHighlighter.SyntaxHighlighter('text/javascript', true);
1987 javascriptSyntaxHighlighter.syntaxHighlightNode(this._formattedCommand).then(this._updateSearch.bind(this));
1988 } else {
1989 this._updateSearch();
1990 }
1991
1992 this.updateTimestamp();
1993 return newContentElement;
1994 }
1995
1996 _updateSearch() {
1997 this.setSearchRegex(this.searchRegex());
1998 }
1999}
2000
2001export class ConsoleCommandResult extends ConsoleViewMessage {
2002 /**
2003 * @override
2004 * @return {!HTMLElement}
2005 */
2006 contentElement() {
2007 const element = super.contentElement();
2008 if (!element.classList.contains('console-user-command-result')) {
2009 element.classList.add('console-user-command-result');
2010 if (this.consoleMessage().level === SDK.ConsoleModel.MessageLevel.Info) {
2011 const icon = UI.Icon.Icon.create('smallicon-command-result', 'command-result-icon');
2012 element.insertBefore(icon, element.firstChild);
2013 }
2014 }
2015 return element;
2016 }
2017}
2018
2019/**
2020 * The maximum length before strings are considered too long for syntax highlighting.
2021 * @const
2022 * @type {number}
2023 */
2024const MaxLengthToIgnoreHighlighter = 10000;
2025
Blink Reformat4c46d092018-04-07 15:32:372026/**
2027 * @const
2028 * @type {number}
2029 */
Paul Lewisbf7aa3c2019-11-20 17:03:382030export const MaxLengthForLinks = 40;
Blink Reformat4c46d092018-04-07 15:32:372031
Sigurd Schneider8f4ac862020-10-13 13:30:112032let _MaxTokenizableStringLength = 10000;
2033let _LongStringVisibleLength = 5000;
2034
2035export const getMaxTokenizableStringLength = () => {
2036 return _MaxTokenizableStringLength;
2037};
2038
2039/**
2040 * @param {number} length
2041 */
2042export const setMaxTokenizableStringLength = length => {
2043 _MaxTokenizableStringLength = length;
2044};
2045
2046export const getLongStringVisibleLength = () => {
2047 return _LongStringVisibleLength;
2048};
2049
2050/**
2051 * @param {number} length
2052 */
2053export const setLongStringVisibleLength = length => {
2054 _LongStringVisibleLength = length;
2055};