blob: 6c579300582df1232a6d4bb2a5b94185c388dec7 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Joseph Pecoraro
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
Tim van der Lippe9b2f8712020-02-12 17:46:2230
31import * as Common from '../common/common.js';
32import * as Components from '../components/components.js';
33import * as DataGrid from '../data_grid/data_grid.js';
34import * as ObjectUI from '../object_ui/object_ui.js';
Tim van der Lippe93b57c32020-02-20 17:38:4435import * as Platform from '../platform/platform.js';
Tim van der Lippe05149e972021-01-15 11:40:4536import {ls} from '../platform/platform.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2237import * as SDK from '../sdk/sdk.js';
38import * as TextUtils from '../text_utils/text_utils.js';
Paul Lewisca569a52020-09-09 16:11:5139import * as ThemeSupport from '../theme_support/theme_support.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2240import * as UI from '../ui/ui.js';
41
Tim van der Lippeeaacb722020-01-10 12:16:0042import {ConsoleViewportElement} from './ConsoleViewport.js'; // eslint-disable-line no-unused-vars
43
Sigurd Schneiderca7b4ff2020-10-14 07:45:4744/** @type {!WeakMap<!Element, !ConsoleViewMessage>} */
45const elementToMessage = new WeakMap();
46
47/**
48 * @param {!Element} element
49 */
50export const getMessageForElement = element => {
51 return elementToMessage.get(element);
52};
53
Sigurd Schneider8bfb4212020-10-27 10:27:3754// This value reflects the 18px min-height of .console-message, plus the
55// 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
56const defaultConsoleRowHeight = 19;
57
58/**
59 * @param {?SDK.RuntimeModel.RuntimeModel} runtimeModel
60 */
61const parameterToRemoteObject = runtimeModel =>
62 /**
63 * @param {!SDK.RemoteObject.RemoteObject|!Protocol.Runtime.RemoteObject|string|undefined} parameter
64 * @return {!SDK.RemoteObject.RemoteObject}
65 */
66 parameter => {
67 if (parameter instanceof SDK.RemoteObject.RemoteObject) {
68 return parameter;
69 }
70 if (!runtimeModel) {
71 return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter);
72 }
73 if (typeof parameter === 'object') {
74 return runtimeModel.createRemoteObject(parameter);
75 }
76 return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
77 };
78
Blink Reformat4c46d092018-04-07 15:32:3779/**
Tim van der Lippeeaacb722020-01-10 12:16:0080 * @implements {ConsoleViewportElement}
Blink Reformat4c46d092018-04-07 15:32:3781 */
Tim van der Lippeeaacb722020-01-10 12:16:0082export class ConsoleViewMessage {
Blink Reformat4c46d092018-04-07 15:32:3783 /**
Tim van der Lippe9b2f8712020-02-12 17:46:2284 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
85 * @param {!Components.Linkifier.Linkifier} linkifier
Blink Reformat4c46d092018-04-07 15:32:3786 * @param {number} nestingLevel
Sigurd Schneider45f32c32020-10-13 13:32:0587 * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize
Blink Reformat4c46d092018-04-07 15:32:3788 */
Tim van der Lippeb45d9a02019-11-05 17:24:4189 constructor(consoleMessage, linkifier, nestingLevel, onResize) {
Blink Reformat4c46d092018-04-07 15:32:3790 this._message = consoleMessage;
91 this._linkifier = linkifier;
Blink Reformat4c46d092018-04-07 15:32:3792 this._repeatCount = 1;
93 this._closeGroupDecorationCount = 0;
94 this._nestingLevel = nestingLevel;
Sigurd Schneider45f32c32020-10-13 13:32:0595 /** @type {!Array<{element: !HTMLElement, forceSelect: function():void}>} */
Erik Luo383f21d2018-11-07 23:16:3796 this._selectableChildren = [];
Erik Luo840be6b2018-12-03 20:54:2797 this._messageResized = onResize;
Sigurd Schneider53e98632020-10-26 15:29:5098 /** @type {?HTMLElement} */
99 this._element = null;
Blink Reformat4c46d092018-04-07 15:32:37100
Tim van der Lippe9b2f8712020-02-12 17:46:22101 this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter();
Blink Reformat4c46d092018-04-07 15:32:37102 this._searchRegex = null;
Tim van der Lippe9b2f8712020-02-12 17:46:22103 /** @type {?UI.Icon.Icon} */
Blink Reformat4c46d092018-04-07 15:32:37104 this._messageLevelIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:00105 this._traceExpanded = false;
Sigurd Schneider45f32c32020-10-13 13:32:05106 /** @type {?function(boolean):void} */
Erik Luo8ef5d0c2018-09-25 21:16:00107 this._expandTrace = null;
Sigurd Schneider53f33522020-10-08 15:00:49108 /** @type {?HTMLElement} */
John Emaubb2897a2019-10-04 17:37:32109 this._anchorElement = null;
Sigurd Schneider45f32c32020-10-13 13:32:05110 /** @type {?HTMLElement} */
111 this._contentElement = null;
112 /** @type {?Array<!HTMLElement>} */
113 this._nestingLevelMarkers = null;
114 /** @type {!Array<!Element>} */
115 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:52116 /** @type {!Array<!UI.UIUtils.HighlightChange>} */
117 this._searchHighlightNodeChanges = [];
Sigurd Schneider53e98632020-10-26 15:29:50118 this._isVisible = false;
119 this._cachedHeight = 0;
120 this._messagePrefix = '';
121 /** @type {?HTMLElement} */
122 this._timestampElement = null;
123 this._inSimilarGroup = false;
124 /** @type {?HTMLElement} */
125 this._similarGroupMarker = null;
126 this._lastInSimilarGroup = false;
127 this._groupKey = '';
128 /** @type {?UI.UIUtils.DevToolsSmallBubble} */
129 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:37130 }
131
132 /**
133 * @override
Sigurd Schneider53f33522020-10-08 15:00:49134 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37135 */
136 element() {
137 return this.toMessageElement();
138 }
139
140 /**
Blink Reformat4c46d092018-04-07 15:32:37141 * @override
142 */
143 wasShown() {
Blink Reformat4c46d092018-04-07 15:32:37144 this._isVisible = true;
145 }
146
147 onResize() {
Blink Reformat4c46d092018-04-07 15:32:37148 }
149
150 /**
151 * @override
152 */
153 willHide() {
154 this._isVisible = false;
Erik Luo4b002322018-07-30 21:23:31155 this._cachedHeight = this.element().offsetHeight;
Blink Reformat4c46d092018-04-07 15:32:37156 }
157
Sigurd Schneider8bfb4212020-10-27 10:27:37158 isVisible() {
159 return this._isVisible;
160 }
161
Blink Reformat4c46d092018-04-07 15:32:37162 /**
163 * @return {number}
164 */
165 fastHeight() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34166 if (this._cachedHeight) {
Blink Reformat4c46d092018-04-07 15:32:37167 return this._cachedHeight;
Tim van der Lippe1d6e57a2019-09-30 11:55:34168 }
Sigurd Schneider8bfb4212020-10-27 10:27:37169 return this.approximateFastHeight();
170 }
171
172 approximateFastHeight() {
Blink Reformat4c46d092018-04-07 15:32:37173 return defaultConsoleRowHeight;
174 }
175
176 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22177 * @return {!SDK.ConsoleModel.ConsoleMessage}
Blink Reformat4c46d092018-04-07 15:32:37178 */
179 consoleMessage() {
180 return this._message;
181 }
182
183 /**
Sigurd Schneider53f33522020-10-08 15:00:49184 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37185 */
Blink Reformat4c46d092018-04-07 15:32:37186 _buildMessage() {
187 let messageElement;
188 let messageText = this._message.messageText;
Tim van der Lippe9b2f8712020-02-12 17:46:22189 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:37190 switch (this._message.type) {
Tim van der Lippe9b2f8712020-02-12 17:46:22191 case SDK.ConsoleModel.MessageType.Trace:
Blink Reformat4c46d092018-04-07 15:32:37192 messageElement = this._format(this._message.parameters || ['console.trace']);
193 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22194 case SDK.ConsoleModel.MessageType.Clear:
Tim van der Lippef49e2322020-05-01 15:03:09195 messageElement = document.createElement('span');
196 messageElement.classList.add('console-info');
Paul Lewis2d7d65c2020-03-16 17:26:30197 if (Common.Settings.Settings.instance().moduleSetting('preserveConsoleLog').get()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22198 messageElement.textContent =
199 Common.UIString.UIString('console.clear() was prevented due to \'Preserve log\'');
Tim van der Lippe1d6e57a2019-09-30 11:55:34200 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22201 messageElement.textContent = Common.UIString.UIString('Console was cleared');
Tim van der Lippe1d6e57a2019-09-30 11:55:34202 }
Tim van der Lippe420e5e32020-11-23 16:58:46203 UI.Tooltip.Tooltip.install(
204 messageElement,
205 ls`Clear all messages with ${
206 UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction('console.clear')}`);
Blink Reformat4c46d092018-04-07 15:32:37207 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22208 case SDK.ConsoleModel.MessageType.Dir: {
Blink Reformat4c46d092018-04-07 15:32:37209 const obj = this._message.parameters ? this._message.parameters[0] : undefined;
210 const args = ['%O', obj];
211 messageElement = this._format(args);
212 break;
213 }
Tim van der Lippe9b2f8712020-02-12 17:46:22214 case SDK.ConsoleModel.MessageType.Profile:
215 case SDK.ConsoleModel.MessageType.ProfileEnd:
Blink Reformat4c46d092018-04-07 15:32:37216 messageElement = this._format([messageText]);
217 break;
218 default: {
Sigurd Schneider45f32c32020-10-13 13:32:05219 if (this._message.type === SDK.ConsoleModel.MessageType.Assert) {
220 this._messagePrefix = ls`Assertion failed: `;
221 }
222 if (this._message.parameters && this._message.parameters.length === 1) {
223 const parameter = this._message.parameters[0];
224 if (typeof parameter !== 'string' && parameter.type === 'string') {
225 messageElement = this._tryFormatAsError(/** @type {string} */ (parameter.value));
226 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34227 }
Blink Reformat4c46d092018-04-07 15:32:37228 const args = this._message.parameters || [messageText];
229 messageElement = messageElement || this._format(args);
230 }
231 }
232 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22233 if (this._message.source === SDK.ConsoleModel.MessageSource.Network) {
Erik Luofc2214f2018-11-21 19:54:58234 messageElement = this._formatAsNetworkRequest() || this._format([messageText]);
235 } else {
Blink Reformat4c46d092018-04-07 15:32:37236 const messageInParameters =
237 this._message.parameters && messageText === /** @type {string} */ (this._message.parameters[0]);
Tim van der Lippe9b2f8712020-02-12 17:46:22238 if (this._message.source === SDK.ConsoleModel.MessageSource.Violation) {
239 messageText = Common.UIString.UIString('[Violation] %s', messageText);
240 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Intervention) {
241 messageText = Common.UIString.UIString('[Intervention] %s', messageText);
242 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Deprecation) {
243 messageText = Common.UIString.UIString('[Deprecation] %s', messageText);
Tim van der Lippe1d6e57a2019-09-30 11:55:34244 }
Blink Reformat4c46d092018-04-07 15:32:37245 const args = this._message.parameters || [messageText];
Tim van der Lippe1d6e57a2019-09-30 11:55:34246 if (messageInParameters) {
Blink Reformat4c46d092018-04-07 15:32:37247 args[0] = messageText;
Tim van der Lippe1d6e57a2019-09-30 11:55:34248 }
Blink Reformat4c46d092018-04-07 15:32:37249 messageElement = this._format(args);
250 }
251 }
252 messageElement.classList.add('console-message-text');
253
Sigurd Schneider53f33522020-10-08 15:00:49254 const formattedMessage = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09255 formattedMessage.classList.add('source-code');
Erik Luo5976c8c2018-07-24 02:03:09256 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34257 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09258 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34259 }
Blink Reformat4c46d092018-04-07 15:32:37260 formattedMessage.appendChild(messageElement);
261 return formattedMessage;
262 }
263
264 /**
Sigurd Schneider53f33522020-10-08 15:00:49265 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37266 */
Erik Luofc2214f2018-11-21 19:54:58267 _formatAsNetworkRequest() {
Tim van der Lippe9b2f8712020-02-12 17:46:22268 const request = SDK.NetworkLog.NetworkLog.requestForConsoleMessage(this._message);
Tim van der Lippe1d6e57a2019-09-30 11:55:34269 if (!request) {
Erik Luofc2214f2018-11-21 19:54:58270 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34271 }
Sigurd Schneider53f33522020-10-08 15:00:49272 const messageElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippe9b2f8712020-02-12 17:46:22273 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Sigurd Schneider23c52972020-10-13 09:31:14274 UI.UIUtils.createTextChild(messageElement, request.requestMethod + ' ');
Tim van der Lippe9b2f8712020-02-12 17:46:22275 const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url());
Erik Luo182bece2018-11-29 03:15:22276 // Focus is handled by the viewport.
277 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39278 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22279 messageElement.appendChild(linkElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34280 if (request.failed) {
Sigurd Schneider23c52972020-10-13 09:31:14281 UI.UIUtils.createTextChildren(messageElement, ' ', request.localizedFailDescription || '');
Tim van der Lippe1d6e57a2019-09-30 11:55:34282 }
283 if (request.statusCode !== 0) {
Sigurd Schneider23c52972020-10-13 09:31:14284 UI.UIUtils.createTextChildren(messageElement, ' ', String(request.statusCode));
Tim van der Lippe1d6e57a2019-09-30 11:55:34285 }
286 if (request.statusText) {
Sigurd Schneider23c52972020-10-13 09:31:14287 UI.UIUtils.createTextChildren(messageElement, ' (', request.statusText, ')');
Tim van der Lippe1d6e57a2019-09-30 11:55:34288 }
Erik Luofc2214f2018-11-21 19:54:58289 } else {
Erik Luoad5f3942019-03-26 20:53:44290 const messageText = this._message.messageText;
291 const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:05292 const linkElement = url === request.url() ?
293 Components.Linkifier.Linkifier.linkifyRevealable(
294 /** @type {!SDK.NetworkRequest.NetworkRequest} */ (request), url, request.url()) :
295 Components.Linkifier.Linkifier.linkifyURL(
296 url, /** @type {!Components.Linkifier.LinkifyURLOptions} */ ({text, lineNumber, columnNumber}));
Erik Luo182bece2018-11-29 03:15:22297 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:39298 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22299 return linkElement;
300 });
Erik Luofc2214f2018-11-21 19:54:58301 messageElement.appendChild(fragment);
302 }
303 return messageElement;
304 }
305
306 /**
Sigurd Schneider53f33522020-10-08 15:00:49307 * @return {?HTMLElement}
Erik Luofc2214f2018-11-21 19:54:58308 */
Blink Reformat4c46d092018-04-07 15:32:37309 _buildMessageAnchor() {
Sigurd Schneider45f32c32020-10-13 13:32:05310 /**
311 * @param {!SDK.ConsoleModel.ConsoleMessage} message
312 * @return {?HTMLElement}
313 */
314 const linkify = message => {
315 if (message.scriptId) {
316 return this._linkifyScriptId(message.scriptId, message.url || '', message.line, message.column);
317 }
318 if (message.stackTrace && message.stackTrace.callFrames.length) {
319 return this._linkifyStackTraceTopFrame(message.stackTrace);
320 }
321 if (message.url && message.url !== 'undefined') {
322 return this._linkifyLocation(message.url, message.line, message.column);
323 }
324 return null;
325 };
326 const anchorElement = linkify(this._message);
Blink Reformat4c46d092018-04-07 15:32:37327 // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
328 if (anchorElement) {
John Emauf7e30fb2019-10-04 19:12:32329 anchorElement.tabIndex = -1;
330 this._selectableChildren.push({
331 element: anchorElement,
332 forceSelect: () => anchorElement.focus(),
333 });
Sigurd Schneider53f33522020-10-08 15:00:49334 const anchorWrapperElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09335 anchorWrapperElement.classList.add('console-message-anchor');
Blink Reformat4c46d092018-04-07 15:32:37336 anchorWrapperElement.appendChild(anchorElement);
Sigurd Schneider23c52972020-10-13 09:31:14337 UI.UIUtils.createTextChild(anchorWrapperElement, ' ');
Blink Reformat4c46d092018-04-07 15:32:37338 return anchorWrapperElement;
339 }
340 return null;
341 }
342
343 /**
Sigurd Schneider45f32c32020-10-13 13:32:05344 * @param {!SDK.RuntimeModel.RuntimeModel} runtimeModel
Sigurd Schneider53f33522020-10-08 15:00:49345 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37346 */
Sigurd Schneider45f32c32020-10-13 13:32:05347 _buildMessageWithStackTrace(runtimeModel) {
Sigurd Schneider53f33522020-10-08 15:00:49348 const toggleElement = /** @type {!HTMLElement} */ (document.createElement('div'));
Tim van der Lippef49e2322020-05-01 15:03:09349 toggleElement.classList.add('console-message-stack-trace-toggle');
Blink Reformat4c46d092018-04-07 15:32:37350 const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
351
352 const messageElement = this._buildMessage();
Tim van der Lippe9b2f8712020-02-12 17:46:22353 const icon = UI.Icon.Icon.create('smallicon-triangle-right', 'console-message-expand-icon');
Blink Reformat4c46d092018-04-07 15:32:37354 const clickableElement = contentElement.createChild('div');
355 clickableElement.appendChild(icon);
Erik Luob5bfff42018-09-20 02:52:39356 // Intercept focus to avoid highlight on click.
357 clickableElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37358 clickableElement.appendChild(messageElement);
359 const stackTraceElement = contentElement.createChild('div');
360 const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
Sigurd Schneider45f32c32020-10-13 13:32:05361 runtimeModel.target(), this._linkifier,
362 {stackTrace: this._message.stackTrace, contentUpdated: undefined, tabStops: undefined});
Erik Luo182bece2018-11-29 03:15:22363 stackTraceElement.appendChild(stackTracePreview.element);
364 for (const linkElement of stackTracePreview.links) {
Erik Luo31c21f62018-12-13 03:39:39365 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22366 }
Blink Reformat4c46d092018-04-07 15:32:37367 stackTraceElement.classList.add('hidden');
Brandon Goddard04a5a762019-12-10 16:45:53368 UI.ARIAUtils.markAsTreeitem(this.element());
369 UI.ARIAUtils.setExpanded(this.element(), false);
Erik Luo8ef5d0c2018-09-25 21:16:00370 this._expandTrace = expand => {
Blink Reformat4c46d092018-04-07 15:32:37371 icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right');
372 stackTraceElement.classList.toggle('hidden', !expand);
Brandon Goddard04a5a762019-12-10 16:45:53373 UI.ARIAUtils.setExpanded(this.element(), expand);
Erik Luo8ef5d0c2018-09-25 21:16:00374 this._traceExpanded = expand;
375 };
Blink Reformat4c46d092018-04-07 15:32:37376
377 /**
Tim van der Lippeeaacb722020-01-10 12:16:00378 * @this {!ConsoleViewMessage}
Sigurd Schneider45f32c32020-10-13 13:32:05379 * @param {!Event} event
Blink Reformat4c46d092018-04-07 15:32:37380 */
Sigurd Schneider45f32c32020-10-13 13:32:05381 const toggleStackTrace = event => {
Tim van der Lippe9b2f8712020-02-12 17:46:22382 if (UI.UIUtils.isEditing() || contentElement.hasSelection()) {
Blink Reformat4c46d092018-04-07 15:32:37383 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34384 }
Sigurd Schneider45f32c32020-10-13 13:32:05385 this._expandTrace && this._expandTrace(stackTraceElement.classList.contains('hidden'));
Blink Reformat4c46d092018-04-07 15:32:37386 event.consume();
Sigurd Schneider45f32c32020-10-13 13:32:05387 };
Blink Reformat4c46d092018-04-07 15:32:37388
Sigurd Schneider45f32c32020-10-13 13:32:05389 clickableElement.addEventListener('click', toggleStackTrace, false);
Tim van der Lippe9b2f8712020-02-12 17:46:22390 if (this._message.type === SDK.ConsoleModel.MessageType.Trace) {
Erik Luo8ef5d0c2018-09-25 21:16:00391 this._expandTrace(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34392 }
Blink Reformat4c46d092018-04-07 15:32:37393
Sigurd Schneider45f32c32020-10-13 13:32:05394 // @ts-ignore
Erik Luo8ef5d0c2018-09-25 21:16:00395 toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true);
Blink Reformat4c46d092018-04-07 15:32:37396 return toggleElement;
397 }
398
399 /**
400 * @param {string} url
401 * @param {number} lineNumber
402 * @param {number} columnNumber
Sigurd Schneider53f33522020-10-08 15:00:49403 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37404 */
405 _linkifyLocation(url, lineNumber, columnNumber) {
Sigurd Schneider45f32c32020-10-13 13:32:05406 const runtimeModel = this._message.runtimeModel();
407 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37408 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34409 }
Blink Reformat4c46d092018-04-07 15:32:37410 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05411 runtimeModel.target(), /* scriptId */ null, url, lineNumber,
412 {columnNumber, className: undefined, tabStop: undefined});
Blink Reformat4c46d092018-04-07 15:32:37413 }
414
415 /**
416 * @param {!Protocol.Runtime.StackTrace} stackTrace
Sigurd Schneider53f33522020-10-08 15:00:49417 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37418 */
419 _linkifyStackTraceTopFrame(stackTrace) {
Sigurd Schneider45f32c32020-10-13 13:32:05420 const runtimeModel = this._message.runtimeModel();
421 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37422 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34423 }
Sigurd Schneider45f32c32020-10-13 13:32:05424 return this._linkifier.linkifyStackTraceTopFrame(runtimeModel.target(), stackTrace);
Blink Reformat4c46d092018-04-07 15:32:37425 }
426
427 /**
428 * @param {string} scriptId
429 * @param {string} url
430 * @param {number} lineNumber
431 * @param {number} columnNumber
Sigurd Schneider53f33522020-10-08 15:00:49432 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37433 */
434 _linkifyScriptId(scriptId, url, lineNumber, columnNumber) {
Sigurd Schneider45f32c32020-10-13 13:32:05435 const runtimeModel = this._message.runtimeModel();
436 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37437 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34438 }
Blink Reformat4c46d092018-04-07 15:32:37439 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05440 runtimeModel.target(), scriptId, url, lineNumber, {columnNumber, className: undefined, tabStop: undefined});
Blink Reformat4c46d092018-04-07 15:32:37441 }
442
443 /**
Sigurd Schneider45f32c32020-10-13 13:32:05444 * @param {!Array.<!Protocol.Runtime.RemoteObject | !SDK.RemoteObject.RemoteObject | string | undefined>} rawParameters
Sigurd Schneider53f33522020-10-08 15:00:49445 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37446 */
447 _format(rawParameters) {
448 // This node is used like a Builder. Values are continually appended onto it.
Sigurd Schneider53f33522020-10-08 15:00:49449 const formattedResult = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34450 if (this._messagePrefix) {
Pavel Feldman9f0f0a32018-12-18 02:09:13451 formattedResult.createChild('span').textContent = this._messagePrefix;
Tim van der Lippe1d6e57a2019-09-30 11:55:34452 }
453 if (!rawParameters.length) {
Blink Reformat4c46d092018-04-07 15:32:37454 return formattedResult;
Tim van der Lippe1d6e57a2019-09-30 11:55:34455 }
Blink Reformat4c46d092018-04-07 15:32:37456
457 // Formatting code below assumes that parameters are all wrappers whereas frontend console
458 // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
459 // FIXME: Only pass runtime wrappers here.
Sigurd Schneider8bfb4212020-10-27 10:27:37460 let parameters = rawParameters.map(parameterToRemoteObject(this._message.runtimeModel()));
Blink Reformat4c46d092018-04-07 15:32:37461
462 // There can be string log and string eval result. We distinguish between them based on message type.
463 const shouldFormatMessage =
Tim van der Lippe9b2f8712020-02-12 17:46:22464 SDK.RemoteObject.RemoteObject.type(
465 (/** @type {!Array.<!SDK.RemoteObject.RemoteObject>} **/ (parameters))[0]) === 'string' &&
466 (this._message.type !== SDK.ConsoleModel.MessageType.Result ||
467 this._message.level === SDK.ConsoleModel.MessageLevel.Error);
Blink Reformat4c46d092018-04-07 15:32:37468
469 // Multiple parameters with the first being a format string. Save unused substitutions.
470 if (shouldFormatMessage) {
471 const result = this._formatWithSubstitutionString(
472 /** @type {string} **/ (parameters[0].description), parameters.slice(1), formattedResult);
Sigurd Schneider45f32c32020-10-13 13:32:05473 parameters = Array.from(result.unusedSubstitutions || []);
Tim van der Lippe1d6e57a2019-09-30 11:55:34474 if (parameters.length) {
Sigurd Schneider23c52972020-10-13 09:31:14475 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34476 }
Blink Reformat4c46d092018-04-07 15:32:37477 }
478
479 // Single parameter, or unused substitutions from above.
480 for (let i = 0; i < parameters.length; ++i) {
481 // Inline strings when formatting.
Tim van der Lippe1d6e57a2019-09-30 11:55:34482 if (shouldFormatMessage && parameters[i].type === 'string') {
Sigurd Schneider45f32c32020-10-13 13:32:05483 formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description || ''));
Tim van der Lippe1d6e57a2019-09-30 11:55:34484 } else {
Blink Reformat4c46d092018-04-07 15:32:37485 formattedResult.appendChild(this._formatParameter(parameters[i], false, true));
Tim van der Lippe1d6e57a2019-09-30 11:55:34486 }
487 if (i < parameters.length - 1) {
Sigurd Schneider23c52972020-10-13 09:31:14488 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34489 }
Blink Reformat4c46d092018-04-07 15:32:37490 }
491 return formattedResult;
492 }
493
494 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22495 * @param {!SDK.RemoteObject.RemoteObject} output
Blink Reformat4c46d092018-04-07 15:32:37496 * @param {boolean=} forceObjectFormat
497 * @param {boolean=} includePreview
Sigurd Schneider53f33522020-10-08 15:00:49498 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37499 */
500 _formatParameter(output, forceObjectFormat, includePreview) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34501 if (output.customPreview()) {
Sigurd Schneider53f33522020-10-08 15:00:49502 return /** @type {!HTMLElement} */ ((new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output)).element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34503 }
Blink Reformat4c46d092018-04-07 15:32:37504
Alfonso Castaño20d22cd2020-10-28 16:23:58505 const outputType = forceObjectFormat ? 'object' : (output.subtype || output.type);
Blink Reformat4c46d092018-04-07 15:32:37506 let element;
Alfonso Castaño20d22cd2020-10-28 16:23:58507 switch (outputType) {
Blink Reformat4c46d092018-04-07 15:32:37508 case 'error':
509 element = this._formatParameterAsError(output);
510 break;
511 case 'function':
512 element = this._formatParameterAsFunction(output, includePreview);
513 break;
514 case 'array':
515 case 'arraybuffer':
516 case 'blob':
517 case 'dataview':
518 case 'generator':
519 case 'iterator':
520 case 'map':
521 case 'object':
522 case 'promise':
523 case 'proxy':
524 case 'set':
525 case 'typedarray':
526 case 'weakmap':
527 case 'weakset':
Benedikt Meurerdead3152020-12-07 08:43:40528 case 'webassemblymemory':
Blink Reformat4c46d092018-04-07 15:32:37529 element = this._formatParameterAsObject(output, includePreview);
530 break;
531 case 'node':
532 element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false);
533 break;
Alfonso Castaño20d22cd2020-10-28 16:23:58534 case 'trustedtype':
535 element = this._formatParameterAsObject(output, false);
536 break;
Blink Reformat4c46d092018-04-07 15:32:37537 case 'string':
538 element = this._formatParameterAsString(output);
539 break;
540 case 'boolean':
541 case 'date':
542 case 'null':
543 case 'number':
544 case 'regexp':
545 case 'symbol':
546 case 'undefined':
547 case 'bigint':
548 element = this._formatParameterAsValue(output);
549 break;
550 default:
551 element = this._formatParameterAsValue(output);
Alfonso Castaño20d22cd2020-10-28 16:23:58552 console.error(`Tried to format remote object of unknown type ${outputType}.`);
Blink Reformat4c46d092018-04-07 15:32:37553 }
Alfonso Castaño20d22cd2020-10-28 16:23:58554 element.classList.add(`object-value-${outputType}`);
Blink Reformat4c46d092018-04-07 15:32:37555 element.classList.add('source-code');
556 return element;
557 }
558
559 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22560 * @param {!SDK.RemoteObject.RemoteObject} obj
Sigurd Schneider53f33522020-10-08 15:00:49561 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37562 */
563 _formatParameterAsValue(obj) {
Sigurd Schneider53f33522020-10-08 15:00:49564 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37565 const description = obj.description || '';
Sigurd Schneider8f4ac862020-10-13 13:30:11566 if (description.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22567 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:11568 document.createElement('span'), description, getLongStringVisibleLength());
Connor Moody1a5c0d32019-12-19 07:23:36569 result.appendChild(propertyValue.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34570 } else {
Sigurd Schneider23c52972020-10-13 09:31:14571 UI.UIUtils.createTextChild(result, description);
Tim van der Lippe1d6e57a2019-09-30 11:55:34572 }
573 if (obj.objectId) {
Blink Reformat4c46d092018-04-07 15:32:37574 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34575 }
Blink Reformat4c46d092018-04-07 15:32:37576 return result;
577 }
578
579 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22580 * @param {!SDK.RemoteObject.RemoteObject} obj
Alfonso Castaño20d22cd2020-10-28 16:23:58581 * @return {!HTMLElement}
Alfonso Castaño20d22cd2020-10-28 16:23:58582 */
583 _formatParameterAsTrustedType(obj) {
584 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
585 const trustedContentSpan = document.createElement('span');
586 trustedContentSpan.appendChild(this._formatParameterAsString(obj));
587 trustedContentSpan.classList.add('object-value-string');
Alfonso Castañodfe8ca32020-10-29 13:03:09588 UI.UIUtils.createTextChild(result, `${obj.className} `);
Alfonso Castaño20d22cd2020-10-28 16:23:58589 result.appendChild(trustedContentSpan);
Alfonso Castaño20d22cd2020-10-28 16:23:58590 return result;
591 }
592
593 /**
594 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37595 * @param {boolean=} includePreview
Sigurd Schneider53f33522020-10-08 15:00:49596 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37597 */
598 _formatParameterAsObject(obj, includePreview) {
Sigurd Schneider53f33522020-10-08 15:00:49599 const titleElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:09600 titleElement.classList.add('console-object');
Blink Reformat4c46d092018-04-07 15:32:37601 if (includePreview && obj.preview) {
602 titleElement.classList.add('console-object-preview');
603 this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
604 } else if (obj.type === 'function') {
605 const functionElement = titleElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22606 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
Blink Reformat4c46d092018-04-07 15:32:37607 titleElement.classList.add('object-value-function');
Alfonso Castaño20d22cd2020-10-28 16:23:58608 } else if (obj.subtype === 'trustedtype') {
609 titleElement.appendChild(this._formatParameterAsTrustedType(obj));
Blink Reformat4c46d092018-04-07 15:32:37610 } else {
Sigurd Schneider23c52972020-10-13 09:31:14611 UI.UIUtils.createTextChild(titleElement, obj.description || '');
Blink Reformat4c46d092018-04-07 15:32:37612 }
613
Tim van der Lippe1d6e57a2019-09-30 11:55:34614 if (!obj.hasChildren || obj.customPreview()) {
Blink Reformat4c46d092018-04-07 15:32:37615 return titleElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34616 }
Blink Reformat4c46d092018-04-07 15:32:37617
618 const note = titleElement.createChild('span', 'object-state-note info-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22619 if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) {
Tim van der Lippe70842f32020-11-23 16:56:57620 UI.Tooltip.Tooltip.install(note, ls`This value will not be collected until console is cleared.`);
Tim van der Lippe1d6e57a2019-09-30 11:55:34621 } else {
Tim van der Lippe70842f32020-11-23 16:56:57622 UI.Tooltip.Tooltip.install(
623 note, ls`This value was evaluated upon first expanding. It may have changed since then.`);
Tim van der Lippe1d6e57a2019-09-30 11:55:34624 }
Blink Reformat4c46d092018-04-07 15:32:37625
Tim van der Lippe9b2f8712020-02-12 17:46:22626 const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier);
Blink Reformat4c46d092018-04-07 15:32:37627 section.element.classList.add('console-view-object-properties-section');
628 section.enableContextMenu();
Erik Luocc14b812018-11-03 01:33:09629 section.setShowSelectionOnKeyboardFocus(true, true);
Erik Luo383f21d2018-11-07 23:16:37630 this._selectableChildren.push(section);
Erik Luo840be6b2018-12-03 20:54:27631 section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
632 section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
633 section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
Blink Reformat4c46d092018-04-07 15:32:37634 return section.element;
635 }
636
637 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22638 * @param {!SDK.RemoteObject.RemoteObject} func
Blink Reformat4c46d092018-04-07 15:32:37639 * @param {boolean=} includePreview
Sigurd Schneider53f33522020-10-08 15:00:49640 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37641 */
642 _formatParameterAsFunction(func, includePreview) {
Sigurd Schneider53f33522020-10-08 15:00:49643 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippe9b2f8712020-02-12 17:46:22644 SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37645 return result;
646
647 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22648 * @param {!SDK.RemoteObject.RemoteObject} targetFunction
Tim van der Lippeeaacb722020-01-10 12:16:00649 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37650 */
651 function formatTargetFunction(targetFunction) {
Sigurd Schneider53f33522020-10-08 15:00:49652 const functionElement = document.createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22653 const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(
Joey Arhard78a58f2018-12-05 01:59:45654 targetFunction, functionElement, true, includePreview);
Blink Reformat4c46d092018-04-07 15:32:37655 result.appendChild(functionElement);
656 if (targetFunction !== func) {
657 const note = result.createChild('span', 'object-info-state-note');
Tim van der Lippe70842f32020-11-23 16:56:57658 UI.Tooltip.Tooltip.install(note, Common.UIString.UIString('Function was resolved from bound function.'));
Blink Reformat4c46d092018-04-07 15:32:37659 }
660 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false);
Joey Arhard78a58f2018-12-05 01:59:45661 promise.then(() => this._formattedParameterAsFunctionForTest());
Blink Reformat4c46d092018-04-07 15:32:37662 }
663 }
664
Joey Arhard78a58f2018-12-05 01:59:45665 _formattedParameterAsFunctionForTest() {
666 }
667
Blink Reformat4c46d092018-04-07 15:32:37668 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22669 * @param {!SDK.RemoteObject.RemoteObject} obj
Blink Reformat4c46d092018-04-07 15:32:37670 * @param {!Event} event
671 */
672 _contextMenuEventFired(obj, event) {
Tim van der Lippe9b2f8712020-02-12 17:46:22673 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37674 contextMenu.appendApplicableItems(obj);
675 contextMenu.show();
676 }
677
678 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22679 * @param {?SDK.RemoteObject.RemoteObject} object
Sigurd Schneider45f32c32020-10-13 13:32:05680 * @param {!Protocol.Runtime.PropertyPreview|!{name:(string|symbol), type: !Protocol.Runtime.PropertyPreviewType, value: (string|undefined)}} property
681 * @param {!Array.<!{name:(string|symbol)}>} propertyPath
Sigurd Schneider53f33522020-10-08 15:00:49682 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37683 */
Sigurd Schneider45f32c32020-10-13 13:32:05684 _renderPropertyPreviewOrAccessor(object, property, propertyPath) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34685 if (property.type === 'accessor') {
Sigurd Schneider45f32c32020-10-13 13:32:05686 return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34687 }
Blink Reformat4c46d092018-04-07 15:32:37688 return this._previewFormatter.renderPropertyPreview(
Alfonso Castaño20d22cd2020-10-28 16:23:58689 property.type, 'subtype' in property ? property.subtype : undefined, null, property.value);
Blink Reformat4c46d092018-04-07 15:32:37690 }
691
692 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22693 * @param {!SDK.RemoteObject.RemoteObject} remoteObject
Sigurd Schneider53f33522020-10-08 15:00:49694 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37695 */
696 _formatParameterAsNode(remoteObject) {
Sigurd Schneider53f33522020-10-08 15:00:49697 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37698
Tim van der Lippe9b2f8712020-02-12 17:46:22699 const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:34700 if (!domModel) {
Blink Reformat4c46d092018-04-07 15:32:37701 return result;
Tim van der Lippe1d6e57a2019-09-30 11:55:34702 }
Erik Luo54fdd912018-11-01 17:57:01703 domModel.pushObjectAsNodeToFrontend(remoteObject).then(async node => {
Blink Reformat4c46d092018-04-07 15:32:37704 if (!node) {
705 result.appendChild(this._formatParameterAsObject(remoteObject, false));
706 return;
707 }
Tim van der Lippe9b2f8712020-02-12 17:46:22708 const renderResult = await UI.UIUtils.Renderer.render(/** @type {!Object} */ (node));
Erik Luofc6a6302018-11-02 06:48:52709 if (renderResult) {
Erik Luo840be6b2018-12-03 20:54:27710 if (renderResult.tree) {
Erik Luo383f21d2018-11-07 23:16:37711 this._selectableChildren.push(renderResult.tree);
Erik Luo840be6b2018-12-03 20:54:27712 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
713 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
714 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
715 }
Erik Luofc6a6302018-11-02 06:48:52716 result.appendChild(renderResult.node);
717 } else {
718 result.appendChild(this._formatParameterAsObject(remoteObject, false));
719 }
Erik Luo54fdd912018-11-01 17:57:01720 this._formattedParameterAsNodeForTest();
Blink Reformat4c46d092018-04-07 15:32:37721 });
722
723 return result;
724 }
725
726 _formattedParameterAsNodeForTest() {
727 }
728
729 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22730 * @param {!SDK.RemoteObject.RemoteObject} output
Sigurd Schneider53f33522020-10-08 15:00:49731 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37732 */
733 _formatParameterAsString(output) {
Sigurd Schneider53f33522020-10-08 15:00:49734 const span = /** @type {!HTMLElement} */ (document.createElement('span'));
Erik Luo383f21d2018-11-07 23:16:37735 span.appendChild(this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37736
Sigurd Schneider53f33522020-10-08 15:00:49737 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37738 result.createChild('span', 'object-value-string-quote').textContent = '"';
739 result.appendChild(span);
740 result.createChild('span', 'object-value-string-quote').textContent = '"';
741 return result;
742 }
743
744 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22745 * @param {!SDK.RemoteObject.RemoteObject} output
Sigurd Schneider53f33522020-10-08 15:00:49746 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37747 */
748 _formatParameterAsError(output) {
Sigurd Schneider53f33522020-10-08 15:00:49749 const result = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:37750 const errorSpan = this._tryFormatAsError(output.description || '');
Erik Luo383f21d2018-11-07 23:16:37751 result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37752 return result;
753 }
754
755 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22756 * @param {!SDK.RemoteObject.RemoteObject} output
Sigurd Schneider53f33522020-10-08 15:00:49757 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37758 */
759 _formatAsArrayEntry(output) {
Alfonso Castaño20d22cd2020-10-28 16:23:58760 return this._previewFormatter.renderPropertyPreview(
761 output.type, output.subtype, output.className, output.description);
Blink Reformat4c46d092018-04-07 15:32:37762 }
763
764 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22765 * @param {?SDK.RemoteObject.RemoteObject} object
Blink Reformat4c46d092018-04-07 15:32:37766 * @param {!Array.<string>} propertyPath
767 * @param {boolean} isArrayEntry
Sigurd Schneider53f33522020-10-08 15:00:49768 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37769 */
770 _formatAsAccessorProperty(object, propertyPath, isArrayEntry) {
Tim van der Lippe9b2f8712020-02-12 17:46:22771 const rootElement =
772 ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
773 object, propertyPath, onInvokeGetterClick.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37774
775 /**
Tim van der Lippe9b2f8712020-02-12 17:46:22776 * @param {!SDK.RemoteObject.CallFunctionResult} result
Tim van der Lippeeaacb722020-01-10 12:16:00777 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37778 */
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18779 function onInvokeGetterClick(result) {
780 const wasThrown = result.wasThrown;
781 const object = result.object;
Tim van der Lippe1d6e57a2019-09-30 11:55:34782 if (!object) {
Blink Reformat4c46d092018-04-07 15:32:37783 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34784 }
Blink Reformat4c46d092018-04-07 15:32:37785 rootElement.removeChildren();
786 if (wasThrown) {
787 const element = rootElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22788 element.textContent = Common.UIString.UIString('<exception>');
Tim van der Lippe70842f32020-11-23 16:56:57789 UI.Tooltip.Tooltip.install(element, /** @type {string} */ (object.description));
Blink Reformat4c46d092018-04-07 15:32:37790 } else if (isArrayEntry) {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18791 rootElement.appendChild(this._formatAsArrayEntry(object));
Blink Reformat4c46d092018-04-07 15:32:37792 } else {
793 // Make a PropertyPreview from the RemoteObject similar to the backend logic.
794 const maxLength = 100;
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18795 const type = object.type;
796 const subtype = object.subtype;
Blink Reformat4c46d092018-04-07 15:32:37797 let description = '';
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18798 if (type !== 'function' && object.description) {
Alfonso Castaño20d22cd2020-10-28 16:23:58799 if (type === 'string' || subtype === 'regexp' || subtype === 'trustedtype') {
Tim van der Lippe213266c2021-01-18 15:48:31800 description = Platform.StringUtilities.trimMiddle(object.description, maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34801 } else {
Tim van der Lippe3cc3af32021-01-19 14:25:26802 description = Platform.StringUtilities.trimEndWithMaxLength(object.description, maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34803 }
Blink Reformat4c46d092018-04-07 15:32:37804 }
Alfonso Castaño20d22cd2020-10-28 16:23:58805 rootElement.appendChild(
806 this._previewFormatter.renderPropertyPreview(type, subtype, object.className, description));
Blink Reformat4c46d092018-04-07 15:32:37807 }
808 }
809
810 return rootElement;
811 }
812
813 /**
814 * @param {string} format
Tim van der Lippe9b2f8712020-02-12 17:46:22815 * @param {!Array.<!SDK.RemoteObject.RemoteObject>} parameters
Sigurd Schneider53f33522020-10-08 15:00:49816 * @param {!HTMLElement} formattedResult
Sigurd Schneider45f32c32020-10-13 13:32:05817 * @return {!{formattedResult:!Element, unusedSubstitutions: ?ArrayLike<!SDK.RemoteObject.RemoteObject>}}
Blink Reformat4c46d092018-04-07 15:32:37818 */
819 _formatWithSubstitutionString(format, parameters, formattedResult) {
Blink Reformat4c46d092018-04-07 15:32:37820 /**
821 * @param {boolean} force
822 * @param {boolean} includePreview
Sigurd Schneider45f32c32020-10-13 13:32:05823 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
824 * @return {!HTMLElement|string|undefined}
Tim van der Lippeeaacb722020-01-10 12:16:00825 * @this {ConsoleViewMessage}
Blink Reformat4c46d092018-04-07 15:32:37826 */
827 function parameterFormatter(force, includePreview, obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05828 if (obj instanceof SDK.RemoteObject.RemoteObject) {
829 return this._formatParameter(obj, force, includePreview);
830 }
831 return stringFormatter(obj);
Blink Reformat4c46d092018-04-07 15:32:37832 }
833
Sigurd Schneider45f32c32020-10-13 13:32:05834 /**
835 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
836 */
Blink Reformat4c46d092018-04-07 15:32:37837 function stringFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05838 if (obj === undefined) {
839 return undefined;
840 }
841 if (typeof obj === 'string') {
842 return obj;
843 }
Blink Reformat4c46d092018-04-07 15:32:37844 return obj.description;
845 }
846
Sigurd Schneider45f32c32020-10-13 13:32:05847 /**
848 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
849 */
Blink Reformat4c46d092018-04-07 15:32:37850 function floatFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05851 if (obj instanceof SDK.RemoteObject.RemoteObject) {
852 if (typeof obj.value !== 'number') {
853 return 'NaN';
854 }
855 return obj.value;
Tim van der Lippe1d6e57a2019-09-30 11:55:34856 }
Sigurd Schneider45f32c32020-10-13 13:32:05857 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37858 }
859
Sigurd Schneider45f32c32020-10-13 13:32:05860 /**
861 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
862 */
Blink Reformat4c46d092018-04-07 15:32:37863 function integerFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05864 if (obj instanceof SDK.RemoteObject.RemoteObject) {
865 if (obj.type === 'bigint') {
866 return obj.description;
867 }
868 if (typeof obj.value !== 'number') {
869 return 'NaN';
870 }
871 return Math.floor(obj.value);
Tim van der Lippe1d6e57a2019-09-30 11:55:34872 }
Sigurd Schneider45f32c32020-10-13 13:32:05873 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37874 }
875
Sigurd Schneider45f32c32020-10-13 13:32:05876 /**
877 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
878 */
Blink Reformat4c46d092018-04-07 15:32:37879 function bypassFormatter(obj) {
880 return (obj instanceof Node) ? obj : '';
881 }
882
Sigurd Schneider45f32c32020-10-13 13:32:05883 /** @type {?Map<string, !{value:string, priority:string}>} */
Blink Reformat4c46d092018-04-07 15:32:37884 let currentStyle = null;
Sigurd Schneider45f32c32020-10-13 13:32:05885 /**
886 * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj
887 */
Blink Reformat4c46d092018-04-07 15:32:37888 function styleFormatter(obj) {
Sigurd Schneider45f32c32020-10-13 13:32:05889 currentStyle = new Map();
Sigurd Schneider53f33522020-10-08 15:00:49890 const buffer = document.createElement('span');
Sigurd Schneider45f32c32020-10-13 13:32:05891 if (obj === undefined) {
892 return;
893 }
894 if (typeof obj === 'string' || !obj.description) {
895 return;
896 }
Blink Reformat4c46d092018-04-07 15:32:37897 buffer.setAttribute('style', obj.description);
Sigurd Schneider45f32c32020-10-13 13:32:05898 for (const property of buffer.style) {
Mathias Bynens5165a7a2020-06-10 05:51:43899 if (isAllowedProperty(property)) {
Sigurd Schneider45f32c32020-10-13 13:32:05900 const info = {
901 value: buffer.style.getPropertyValue(property),
902 priority: buffer.style.getPropertyPriority(property)
903 };
904 currentStyle.set(property, info);
Tim van der Lippe1d6e57a2019-09-30 11:55:34905 }
Blink Reformat4c46d092018-04-07 15:32:37906 }
907 }
908
Sigurd Schneider45f32c32020-10-13 13:32:05909 /**
910 * @param {string} property
911 */
Mathias Bynens5165a7a2020-06-10 05:51:43912 function isAllowedProperty(property) {
Blink Reformat4c46d092018-04-07 15:32:37913 // Make sure that allowed properties do not interfere with link visibility.
914 const prefixes = [
915 'background', 'border', 'color', 'font', 'line', 'margin', 'padding', 'text', '-webkit-background',
916 '-webkit-border', '-webkit-font', '-webkit-margin', '-webkit-padding', '-webkit-text'
917 ];
Sigurd Schneider45f32c32020-10-13 13:32:05918 for (const prefix of prefixes) {
919 if (property.startsWith(prefix)) {
Blink Reformat4c46d092018-04-07 15:32:37920 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:34921 }
Blink Reformat4c46d092018-04-07 15:32:37922 }
923 return false;
924 }
925
Jack Franklind37a14b2020-12-11 10:13:58926 /** @type {!Object.<string, function((string|!{description: string}|undefined|!SDK.RemoteObject.RemoteObject), !Platform.StringUtilities.FormatterToken):*>} */
Sigurd Schneider45f32c32020-10-13 13:32:05927 const formatters = {};
Blink Reformat4c46d092018-04-07 15:32:37928 // Firebug uses %o for formatting objects.
929 formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */);
930 formatters.s = stringFormatter;
931 formatters.f = floatFormatter;
932 // Firebug allows both %i and %d for formatting integers.
933 formatters.i = integerFormatter;
934 formatters.d = integerFormatter;
935
936 // Firebug uses %c for styling the message.
937 formatters.c = styleFormatter;
938
939 // Support %O to force object formatting, instead of the type-based %o formatting.
940 formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */);
941
942 formatters._ = bypassFormatter;
943
944 /**
Sigurd Schneider53f33522020-10-08 15:00:49945 * @param {!HTMLElement} a
Blink Reformat4c46d092018-04-07 15:32:37946 * @param {*} b
Tim van der Lippeeaacb722020-01-10 12:16:00947 * @this {!ConsoleViewMessage}
Sigurd Schneider53f33522020-10-08 15:00:49948 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:37949 */
950 function append(a, b) {
951 if (b instanceof Node) {
952 a.appendChild(b);
Erik Luo17926392018-05-17 22:06:12953 return a;
954 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34955 if (typeof b === 'undefined') {
Erik Luo17926392018-05-17 22:06:12956 return a;
Tim van der Lippe1d6e57a2019-09-30 11:55:34957 }
Erik Luo17926392018-05-17 22:06:12958 if (!currentStyle) {
Erik Luo383f21d2018-11-07 23:16:37959 a.appendChild(this._linkifyStringAsFragment(String(b)));
Erik Luo17926392018-05-17 22:06:12960 return a;
961 }
962 const lines = String(b).split('\n');
963 for (let i = 0; i < lines.length; i++) {
964 const line = lines[i];
Erik Luo383f21d2018-11-07 23:16:37965 const lineFragment = this._linkifyStringAsFragment(line);
Sigurd Schneider53f33522020-10-08 15:00:49966 const wrapper = /** @type {!HTMLElement} */ (document.createElement('span'));
Erik Luo17926392018-05-17 22:06:12967 wrapper.style.setProperty('contain', 'paint');
968 wrapper.style.setProperty('display', 'inline-block');
969 wrapper.style.setProperty('max-width', '100%');
970 wrapper.appendChild(lineFragment);
971 applyCurrentStyle(wrapper);
972 for (const child of wrapper.children) {
Sigurd Schneider53f33522020-10-08 15:00:49973 if (child.classList.contains('devtools-link') && child instanceof HTMLElement) {
Erik Luo17926392018-05-17 22:06:12974 this._applyForcedVisibleStyle(child);
Tim van der Lippe1d6e57a2019-09-30 11:55:34975 }
Blink Reformat4c46d092018-04-07 15:32:37976 }
Erik Luo17926392018-05-17 22:06:12977 a.appendChild(wrapper);
Tim van der Lippe1d6e57a2019-09-30 11:55:34978 if (i < lines.length - 1) {
Sigurd Schneider53f33522020-10-08 15:00:49979 a.appendChild(document.createElement('br'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34980 }
Blink Reformat4c46d092018-04-07 15:32:37981 }
982 return a;
983 }
984
985 /**
Sigurd Schneider53f33522020-10-08 15:00:49986 * @param {!HTMLElement} element
Blink Reformat4c46d092018-04-07 15:32:37987 */
988 function applyCurrentStyle(element) {
Sigurd Schneider45f32c32020-10-13 13:32:05989 if (!currentStyle) {
990 return;
991 }
992 for (const [property, {value, priority}] of currentStyle.entries()) {
993 element.style.setProperty(/** @type {string} */ (property), value, priority);
Tim van der Lippe1d6e57a2019-09-30 11:55:34994 }
Blink Reformat4c46d092018-04-07 15:32:37995 }
996
Tim van der Lippe93b57c32020-02-20 17:38:44997 // Platform.StringUtilities.format does treat formattedResult like a Builder, result is an object.
998 return Platform.StringUtilities.format(format, parameters, formatters, formattedResult, append.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37999 }
1000
1001 /**
Sigurd Schneider53f33522020-10-08 15:00:491002 * @param {!HTMLElement} element
Blink Reformat4c46d092018-04-07 15:32:371003 */
1004 _applyForcedVisibleStyle(element) {
1005 element.style.setProperty('-webkit-text-stroke', '0', 'important');
1006 element.style.setProperty('text-decoration', 'underline', 'important');
1007
Paul Lewisca569a52020-09-09 16:11:511008 const themedColor = ThemeSupport.ThemeSupport.instance().patchColorText(
1009 'rgb(33%, 33%, 33%)', ThemeSupport.ThemeSupport.ColorUsage.Foreground);
Blink Reformat4c46d092018-04-07 15:32:371010 element.style.setProperty('color', themedColor, 'important');
1011
1012 let backgroundColor = 'hsl(0, 0%, 100%)';
Tim van der Lippe9b2f8712020-02-12 17:46:221013 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Blink Reformat4c46d092018-04-07 15:32:371014 backgroundColor = 'hsl(0, 100%, 97%)';
Tim van der Lippe9b2f8712020-02-12 17:46:221015 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371016 backgroundColor = 'hsl(50, 100%, 95%)';
Tim van der Lippe1d6e57a2019-09-30 11:55:341017 }
Paul Lewisca569a52020-09-09 16:11:511018 const themedBackgroundColor = ThemeSupport.ThemeSupport.instance().patchColorText(
1019 backgroundColor, ThemeSupport.ThemeSupport.ColorUsage.Background);
Blink Reformat4c46d092018-04-07 15:32:371020 element.style.setProperty('background-color', themedBackgroundColor, 'important');
1021 }
1022
1023 /**
Sigurd Schneider45f32c32020-10-13 13:32:051024 * @param {!RegExp} regexObject
Blink Reformat4c46d092018-04-07 15:32:371025 * @return {boolean}
1026 */
1027 matchesFilterRegex(regexObject) {
1028 regexObject.lastIndex = 0;
Erik Luo5976c8c2018-07-24 02:03:091029 const contentElement = this.contentElement();
1030 const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : '';
Tim van der Lipped7cfd142021-01-07 12:17:241031 return (Boolean(anchorText) && regexObject.test(anchorText.trim())) ||
Erik Luo5976c8c2018-07-24 02:03:091032 regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
Blink Reformat4c46d092018-04-07 15:32:371033 }
1034
1035 /**
1036 * @param {string} filter
1037 * @return {boolean}
1038 */
1039 matchesFilterText(filter) {
1040 const text = this.contentElement().deepTextContent();
1041 return text.toLowerCase().includes(filter.toLowerCase());
1042 }
1043
1044 updateTimestamp() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341045 if (!this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371046 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341047 }
Blink Reformat4c46d092018-04-07 15:32:371048
Paul Lewis2d7d65c2020-03-16 17:26:301049 if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341050 if (!this._timestampElement) {
Sigurd Schneider53e98632020-10-26 15:29:501051 this._timestampElement = /** @type {!HTMLElement} */ (document.createElement('span'));
Tim van der Lippef49e2322020-05-01 15:03:091052 this._timestampElement.classList.add('console-timestamp');
Tim van der Lippe1d6e57a2019-09-30 11:55:341053 }
Tim van der Lippe9b2f8712020-02-12 17:46:221054 this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' ';
Tim van der Lippe70842f32020-11-23 16:56:571055 UI.Tooltip.Tooltip.install(this._timestampElement, UI.UIUtils.formatTimestamp(this._message.timestamp, true));
Blink Reformat4c46d092018-04-07 15:32:371056 this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild);
1057 } else if (this._timestampElement) {
1058 this._timestampElement.remove();
Sigurd Schneider53e98632020-10-26 15:29:501059 this._timestampElement = null;
Blink Reformat4c46d092018-04-07 15:32:371060 }
Blink Reformat4c46d092018-04-07 15:32:371061 }
1062
1063 /**
1064 * @return {number}
1065 */
1066 nestingLevel() {
1067 return this._nestingLevel;
1068 }
1069
1070 /**
1071 * @param {boolean} inSimilarGroup
1072 * @param {boolean=} isLast
1073 */
1074 setInSimilarGroup(inSimilarGroup, isLast) {
1075 this._inSimilarGroup = inSimilarGroup;
Tim van der Lipped7cfd142021-01-07 12:17:241076 this._lastInSimilarGroup = inSimilarGroup && Boolean(isLast);
Blink Reformat4c46d092018-04-07 15:32:371077 if (this._similarGroupMarker && !inSimilarGroup) {
1078 this._similarGroupMarker.remove();
1079 this._similarGroupMarker = null;
1080 } else if (this._element && !this._similarGroupMarker && inSimilarGroup) {
Sigurd Schneider53e98632020-10-26 15:29:501081 this._similarGroupMarker = /** @type {!HTMLElement} */ (document.createElement('div'));
Tim van der Lippef49e2322020-05-01 15:03:091082 this._similarGroupMarker.classList.add('nesting-level-marker');
Blink Reformat4c46d092018-04-07 15:32:371083 this._element.insertBefore(this._similarGroupMarker, this._element.firstChild);
1084 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1085 }
1086 }
1087
1088 /**
1089 * @return {boolean}
1090 */
1091 isLastInSimilarGroup() {
Tim van der Lipped7cfd142021-01-07 12:17:241092 return Boolean(this._inSimilarGroup) && Boolean(this._lastInSimilarGroup);
Blink Reformat4c46d092018-04-07 15:32:371093 }
1094
1095 resetCloseGroupDecorationCount() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341096 if (!this._closeGroupDecorationCount) {
Blink Reformat4c46d092018-04-07 15:32:371097 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341098 }
Blink Reformat4c46d092018-04-07 15:32:371099 this._closeGroupDecorationCount = 0;
1100 this._updateCloseGroupDecorations();
1101 }
1102
1103 incrementCloseGroupDecorationCount() {
1104 ++this._closeGroupDecorationCount;
1105 this._updateCloseGroupDecorations();
1106 }
1107
1108 _updateCloseGroupDecorations() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341109 if (!this._nestingLevelMarkers) {
Blink Reformat4c46d092018-04-07 15:32:371110 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341111 }
Blink Reformat4c46d092018-04-07 15:32:371112 for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) {
1113 const marker = this._nestingLevelMarkers[i];
1114 marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount);
1115 }
1116 }
1117
1118 /**
Erik Luo0b8282e2018-10-08 20:37:461119 * @return {number}
1120 */
1121 _focusedChildIndex() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341122 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461123 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341124 }
Erik Luo383f21d2018-11-07 23:16:371125 return this._selectableChildren.findIndex(child => child.element.hasFocus());
Erik Luo0b8282e2018-10-08 20:37:461126 }
1127
1128 /**
Sigurd Schneider45f32c32020-10-13 13:32:051129 * @param {!KeyboardEvent} event
Erik Luo8ef5d0c2018-09-25 21:16:001130 */
1131 _onKeyDown(event) {
Sigurd Schneider45f32c32020-10-13 13:32:051132 if (UI.UIUtils.isEditing() || !this._element || !this._element.hasFocus() || this._element.hasSelection()) {
Erik Luo8ef5d0c2018-09-25 21:16:001133 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341134 }
1135 if (this.maybeHandleOnKeyDown(event)) {
Erik Luo8ef5d0c2018-09-25 21:16:001136 event.consume(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:341137 }
Erik Luo8ef5d0c2018-09-25 21:16:001138 }
1139
1140 /**
1141 * @protected
Sigurd Schneider45f32c32020-10-13 13:32:051142 * @param {!KeyboardEvent} event
Erik Luo8ef5d0c2018-09-25 21:16:001143 */
1144 maybeHandleOnKeyDown(event) {
1145 // Handle trace expansion.
Erik Luo0b8282e2018-10-08 20:37:461146 const focusedChildIndex = this._focusedChildIndex();
1147 const isWrapperFocused = focusedChildIndex === -1;
1148 if (this._expandTrace && isWrapperFocused) {
Erik Luo8ef5d0c2018-09-25 21:16:001149 if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) {
1150 this._expandTrace(!this._traceExpanded);
1151 return true;
1152 }
1153 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341154 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461155 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:341156 }
Erik Luo0b8282e2018-10-08 20:37:461157
1158 if (event.key === 'ArrowLeft') {
Sigurd Schneider45f32c32020-10-13 13:32:051159 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461160 return true;
1161 }
1162 if (event.key === 'ArrowRight') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341163 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461164 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341165 }
Erik Luo0b8282e2018-10-08 20:37:461166 }
1167 if (event.key === 'ArrowUp') {
Erik Luo182bece2018-11-29 03:15:221168 const firstVisibleChild = this._nearestVisibleChild(0);
1169 if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
Sigurd Schneider45f32c32020-10-13 13:32:051170 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461171 return true;
Mathias Bynensf06e8c02020-02-28 13:58:281172 }
1173 if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461174 return true;
1175 }
1176 }
1177 if (event.key === 'ArrowDown') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341178 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461179 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341180 }
1181 if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) {
Erik Luo0b8282e2018-10-08 20:37:461182 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341183 }
Erik Luo0b8282e2018-10-08 20:37:461184 }
Erik Luo8ef5d0c2018-09-25 21:16:001185 return false;
1186 }
1187
Erik Luo182bece2018-11-29 03:15:221188 /**
1189 * @param {number} fromIndex
1190 * @param {boolean=} backwards
1191 * @return {boolean}
1192 */
1193 _selectNearestVisibleChild(fromIndex, backwards) {
1194 const nearestChild = this._nearestVisibleChild(fromIndex, backwards);
1195 if (nearestChild) {
Erik Luo31c21f62018-12-13 03:39:391196 nearestChild.forceSelect();
Erik Luo182bece2018-11-29 03:15:221197 return true;
1198 }
1199 return false;
1200 }
1201
1202 /**
1203 * @param {number} fromIndex
1204 * @param {boolean=} backwards
Erik Luo31c21f62018-12-13 03:39:391205 * @return {?{element: !Element, forceSelect: function()}}
Erik Luo182bece2018-11-29 03:15:221206 */
1207 _nearestVisibleChild(fromIndex, backwards) {
1208 const childCount = this._selectableChildren.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:341209 if (fromIndex < 0 || fromIndex >= childCount) {
Erik Luo182bece2018-11-29 03:15:221210 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341211 }
Erik Luo182bece2018-11-29 03:15:221212 const direction = backwards ? -1 : 1;
1213 let index = fromIndex;
1214
1215 while (!this._selectableChildren[index].element.offsetParent) {
1216 index += direction;
Tim van der Lippe1d6e57a2019-09-30 11:55:341217 if (index < 0 || index >= childCount) {
Erik Luo182bece2018-11-29 03:15:221218 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341219 }
Erik Luo182bece2018-11-29 03:15:221220 }
1221 return this._selectableChildren[index];
1222 }
1223
Sigurd Schneider53f33522020-10-08 15:00:491224 /**
1225 * @override
1226 */
Erik Luo0b8282e2018-10-08 20:37:461227 focusLastChildOrSelf() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341228 if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461229 this._element.focus();
Tim van der Lippe1d6e57a2019-09-30 11:55:341230 }
Erik Luo0b8282e2018-10-08 20:37:461231 }
1232
1233 /**
Sigurd Schneiderb2953b22020-10-09 09:30:151234 * @protected
1235 * @param {!HTMLElement} element
1236 */
1237 setContentElement(element) {
1238 console.assert(!this._contentElement, 'Cannot set content element twice');
1239 this._contentElement = element;
1240 }
1241
Sigurd Schneiderb2953b22020-10-09 09:30:151242 /**
1243 * @protected
1244 * @return {?HTMLElement}
1245 */
1246 getContentElement() {
1247 return this._contentElement;
1248 }
1249
1250 /**
Sigurd Schneider53f33522020-10-08 15:00:491251 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371252 */
1253 contentElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341254 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371255 return this._contentElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:341256 }
Blink Reformat4c46d092018-04-07 15:32:371257
Sigurd Schneider53f33522020-10-08 15:00:491258 const contentElement = /** @type {!HTMLElement} */ (document.createElement('div'));
Tim van der Lippef49e2322020-05-01 15:03:091259 contentElement.classList.add('console-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341260 if (this._messageLevelIcon) {
Blink Reformat4c46d092018-04-07 15:32:371261 contentElement.appendChild(this._messageLevelIcon);
Tim van der Lippe1d6e57a2019-09-30 11:55:341262 }
Blink Reformat4c46d092018-04-07 15:32:371263 this._contentElement = contentElement;
1264
Sigurd Schneider45f32c32020-10-13 13:32:051265 const runtimeModel = this._message.runtimeModel();
Blink Reformat4c46d092018-04-07 15:32:371266 let formattedMessage;
Tim van der Lipped7cfd142021-01-07 12:17:241267 const shouldIncludeTrace = Boolean(this._message.stackTrace) &&
Tim van der Lippe9b2f8712020-02-12 17:46:221268 (this._message.source === SDK.ConsoleModel.MessageSource.Network ||
1269 this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1270 this._message.level === SDK.ConsoleModel.MessageLevel.Error ||
1271 this._message.level === SDK.ConsoleModel.MessageLevel.Warning ||
1272 this._message.type === SDK.ConsoleModel.MessageType.Trace);
Sigurd Schneider45f32c32020-10-13 13:32:051273 if (runtimeModel && shouldIncludeTrace) {
1274 formattedMessage = this._buildMessageWithStackTrace(runtimeModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:341275 } else {
Blink Reformat4c46d092018-04-07 15:32:371276 formattedMessage = this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341277 }
Blink Reformat4c46d092018-04-07 15:32:371278 contentElement.appendChild(formattedMessage);
1279
1280 this.updateTimestamp();
1281 return this._contentElement;
1282 }
1283
1284 /**
Sigurd Schneider53f33522020-10-08 15:00:491285 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371286 */
1287 toMessageElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341288 if (this._element) {
Blink Reformat4c46d092018-04-07 15:32:371289 return this._element;
Tim van der Lippe1d6e57a2019-09-30 11:55:341290 }
Blink Reformat4c46d092018-04-07 15:32:371291
Sigurd Schneider53f33522020-10-08 15:00:491292 this._element = /** @type {!HTMLElement} */ (document.createElement('div'));
Pavel Feldmandb310912019-01-30 00:31:201293 this._element.tabIndex = -1;
Sigurd Schneider45f32c32020-10-13 13:32:051294 this._element.addEventListener('keydown', /** @type {!EventListener} */ (this._onKeyDown.bind(this)));
Blink Reformat4c46d092018-04-07 15:32:371295 this.updateMessageElement();
1296 return this._element;
1297 }
1298
1299 updateMessageElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341300 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371301 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341302 }
Blink Reformat4c46d092018-04-07 15:32:371303
1304 this._element.className = 'console-message-wrapper';
1305 this._element.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:341306 if (this._message.isGroupStartMessage()) {
Blink Reformat4c46d092018-04-07 15:32:371307 this._element.classList.add('console-group-title');
Tim van der Lippe1d6e57a2019-09-30 11:55:341308 }
Tim van der Lippe9b2f8712020-02-12 17:46:221309 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:371310 this._element.classList.add('console-from-api');
Tim van der Lippe1d6e57a2019-09-30 11:55:341311 }
Blink Reformat4c46d092018-04-07 15:32:371312 if (this._inSimilarGroup) {
Sigurd Schneider53e98632020-10-26 15:29:501313 this._similarGroupMarker = /** @type {!HTMLElement} */ (this._element.createChild('div', 'nesting-level-marker'));
Blink Reformat4c46d092018-04-07 15:32:371314 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1315 }
1316
1317 this._nestingLevelMarkers = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341318 for (let i = 0; i < this._nestingLevel; ++i) {
Blink Reformat4c46d092018-04-07 15:32:371319 this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker'));
Tim van der Lippe1d6e57a2019-09-30 11:55:341320 }
Blink Reformat4c46d092018-04-07 15:32:371321 this._updateCloseGroupDecorations();
Sigurd Schneiderca7b4ff2020-10-14 07:45:471322 elementToMessage.set(this._element, this);
Blink Reformat4c46d092018-04-07 15:32:371323
1324 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221325 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371326 this._element.classList.add('console-verbose-level');
Blink Reformat4c46d092018-04-07 15:32:371327 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221328 case SDK.ConsoleModel.MessageLevel.Info:
Blink Reformat4c46d092018-04-07 15:32:371329 this._element.classList.add('console-info-level');
Tim van der Lippe9b2f8712020-02-12 17:46:221330 if (this._message.type === SDK.ConsoleModel.MessageType.System) {
Blink Reformat4c46d092018-04-07 15:32:371331 this._element.classList.add('console-system-type');
Tim van der Lippe1d6e57a2019-09-30 11:55:341332 }
Blink Reformat4c46d092018-04-07 15:32:371333 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221334 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371335 this._element.classList.add('console-warning-level');
Blink Reformat4c46d092018-04-07 15:32:371336 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221337 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371338 this._element.classList.add('console-error-level');
Blink Reformat4c46d092018-04-07 15:32:371339 break;
1340 }
Erik Luofd3e7d42018-09-25 02:12:351341 this._updateMessageLevelIcon();
Tim van der Lippe1d6e57a2019-09-30 11:55:341342 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371343 this._element.classList.add('console-warning-level');
Tim van der Lippe1d6e57a2019-09-30 11:55:341344 }
Blink Reformat4c46d092018-04-07 15:32:371345
1346 this._element.appendChild(this.contentElement());
Tim van der Lippe1d6e57a2019-09-30 11:55:341347 if (this._repeatCount > 1) {
Blink Reformat4c46d092018-04-07 15:32:371348 this._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341349 }
Blink Reformat4c46d092018-04-07 15:32:371350 }
1351
1352 /**
1353 * @return {boolean}
1354 */
1355 _shouldRenderAsWarning() {
Tim van der Lippe9b2f8712020-02-12 17:46:221356 return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose ||
1357 this._message.level === SDK.ConsoleModel.MessageLevel.Info) &&
1358 (this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1359 this._message.source === SDK.ConsoleModel.MessageSource.Deprecation ||
1360 this._message.source === SDK.ConsoleModel.MessageSource.Intervention ||
1361 this._message.source === SDK.ConsoleModel.MessageSource.Recommendation);
Blink Reformat4c46d092018-04-07 15:32:371362 }
1363
Erik Luofd3e7d42018-09-25 02:12:351364 _updateMessageLevelIcon() {
1365 let iconType = '';
1366 let accessibleName = '';
Tim van der Lippe9b2f8712020-02-12 17:46:221367 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351368 iconType = 'smallicon-warning';
1369 accessibleName = ls`Warning`;
Tim van der Lippe9b2f8712020-02-12 17:46:221370 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351371 iconType = 'smallicon-error';
1372 accessibleName = ls`Error`;
1373 }
Sigurd Schneider45f32c32020-10-13 13:32:051374 if (!this._messageLevelIcon) {
1375 if (!iconType) {
1376 return;
1377 }
Tim van der Lippe9b2f8712020-02-12 17:46:221378 this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon');
Tim van der Lippe1d6e57a2019-09-30 11:55:341379 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371380 this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341381 }
Blink Reformat4c46d092018-04-07 15:32:371382 }
1383 this._messageLevelIcon.setIconType(iconType);
Erik Luofd3e7d42018-09-25 02:12:351384 UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371385 }
1386
1387 /**
1388 * @return {number}
1389 */
1390 repeatCount() {
1391 return this._repeatCount || 1;
1392 }
1393
1394 resetIncrementRepeatCount() {
1395 this._repeatCount = 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341396 if (!this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371397 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341398 }
Blink Reformat4c46d092018-04-07 15:32:371399
1400 this._repeatCountElement.remove();
Tim van der Lippe1d6e57a2019-09-30 11:55:341401 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371402 this._contentElement.classList.remove('repeated-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341403 }
Sigurd Schneider53e98632020-10-26 15:29:501404 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:371405 }
1406
1407 incrementRepeatCount() {
1408 this._repeatCount++;
1409 this._showRepeatCountElement();
1410 }
1411
1412 /**
1413 * @param {number} repeatCount
1414 */
1415 setRepeatCount(repeatCount) {
1416 this._repeatCount = repeatCount;
1417 this._showRepeatCountElement();
1418 }
Blink Reformat4c46d092018-04-07 15:32:371419 _showRepeatCountElement() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341420 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371421 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341422 }
Blink Reformat4c46d092018-04-07 15:32:371423
1424 if (!this._repeatCountElement) {
Sigurd Schneider45f32c32020-10-13 13:32:051425 this._repeatCountElement =
1426 /** @type {!UI.UIUtils.DevToolsSmallBubble} */ (document.createElement('span', {is: 'dt-small-bubble'}));
Tim van der Lippeee954d42020-05-04 10:35:571427 this._repeatCountElement.classList.add('console-message-repeat-count');
Blink Reformat4c46d092018-04-07 15:32:371428 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221429 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371430 this._repeatCountElement.type = 'warning';
1431 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221432 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371433 this._repeatCountElement.type = 'error';
1434 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221435 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371436 this._repeatCountElement.type = 'verbose';
1437 break;
1438 default:
1439 this._repeatCountElement.type = 'info';
1440 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341441 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371442 this._repeatCountElement.type = 'warning';
Tim van der Lippe1d6e57a2019-09-30 11:55:341443 }
Blink Reformat4c46d092018-04-07 15:32:371444
1445 this._element.insertBefore(this._repeatCountElement, this._contentElement);
Sigurd Schneider45f32c32020-10-13 13:32:051446 this.contentElement().classList.add('repeated-message');
Blink Reformat4c46d092018-04-07 15:32:371447 }
Sigurd Schneider45f32c32020-10-13 13:32:051448 this._repeatCountElement.textContent = `${this._repeatCount}`;
Erik Luofd3e7d42018-09-25 02:12:351449 let accessibleName = ls`Repeat ${this._repeatCount}`;
Tim van der Lippe9b2f8712020-02-12 17:46:221450 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351451 accessibleName = ls`Warning ${accessibleName}`;
Tim van der Lippe9b2f8712020-02-12 17:46:221452 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351453 accessibleName = ls`Error ${accessibleName}`;
Tim van der Lippe1d6e57a2019-09-30 11:55:341454 }
Erik Luofd3e7d42018-09-25 02:12:351455 UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371456 }
1457
1458 get text() {
1459 return this._message.messageText;
1460 }
1461
1462 /**
1463 * @return {string}
1464 */
1465 toExportString() {
1466 const lines = [];
1467 const nodes = this.contentElement().childTextNodes();
Tim van der Lippe9b2f8712020-02-12 17:46:221468 const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join('');
Tim van der Lippe1d6e57a2019-09-30 11:55:341469 for (let i = 0; i < this.repeatCount(); ++i) {
Blink Reformat4c46d092018-04-07 15:32:371470 lines.push(messageContent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341471 }
Blink Reformat4c46d092018-04-07 15:32:371472 return lines.join('\n');
1473 }
1474
1475 /**
1476 * @param {?RegExp} regex
1477 */
1478 setSearchRegex(regex) {
Sigurd Schneidere8e75cf2020-10-13 08:17:521479 if (this._searchHighlightNodeChanges && this._searchHighlightNodeChanges.length) {
1480 UI.UIUtils.revertDomChanges(this._searchHighlightNodeChanges);
Tim van der Lippe1d6e57a2019-09-30 11:55:341481 }
Blink Reformat4c46d092018-04-07 15:32:371482 this._searchRegex = regex;
1483 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:521484 this._searchHighlightNodeChanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341485 if (!this._searchRegex) {
Blink Reformat4c46d092018-04-07 15:32:371486 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341487 }
Blink Reformat4c46d092018-04-07 15:32:371488
1489 const text = this.contentElement().deepTextContent();
1490 let match;
1491 this._searchRegex.lastIndex = 0;
1492 const sourceRanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341493 while ((match = this._searchRegex.exec(text)) && match[0]) {
Tim van der Lippe9b2f8712020-02-12 17:46:221494 sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length));
Tim van der Lippe1d6e57a2019-09-30 11:55:341495 }
Blink Reformat4c46d092018-04-07 15:32:371496
1497 if (sourceRanges.length) {
1498 this._searchHighlightNodes =
Sigurd Schneidere8e75cf2020-10-13 08:17:521499 UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHighlightNodeChanges);
Blink Reformat4c46d092018-04-07 15:32:371500 }
1501 }
1502
1503 /**
1504 * @return {?RegExp}
1505 */
1506 searchRegex() {
1507 return this._searchRegex;
1508 }
1509
1510 /**
1511 * @return {number}
1512 */
1513 searchCount() {
1514 return this._searchHighlightNodes.length;
1515 }
1516
1517 /**
Sigurd Schneider45f32c32020-10-13 13:32:051518 * @param {number} index
1519 * @return {!Element}
Blink Reformat4c46d092018-04-07 15:32:371520 */
1521 searchHighlightNode(index) {
1522 return this._searchHighlightNodes[index];
1523 }
1524
1525 /**
1526 * @param {string} string
Sigurd Schneider53f33522020-10-08 15:00:491527 * @return {?HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371528 */
1529 _tryFormatAsError(string) {
1530 /**
1531 * @param {string} prefix
1532 */
1533 function startsWith(prefix) {
1534 return string.startsWith(prefix);
1535 }
1536
Sigurd Schneider45f32c32020-10-13 13:32:051537 const runtimeModel = this._message.runtimeModel();
Blink Reformat4c46d092018-04-07 15:32:371538 const errorPrefixes =
1539 ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError'];
Sigurd Schneider45f32c32020-10-13 13:32:051540 if (!runtimeModel || !errorPrefixes.some(startsWith)) {
Blink Reformat4c46d092018-04-07 15:32:371541 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341542 }
Sigurd Schneider45f32c32020-10-13 13:32:051543 const debuggerModel = runtimeModel.debuggerModel();
1544 const baseURL = runtimeModel.target().inspectedURL();
Blink Reformat4c46d092018-04-07 15:32:371545
1546 const lines = string.split('\n');
1547 const links = [];
1548 let position = 0;
1549 for (let i = 0; i < lines.length; ++i) {
1550 position += i > 0 ? lines[i - 1].length + 1 : 0;
1551 const isCallFrameLine = /^\s*at\s/.test(lines[i]);
Tim van der Lippe1d6e57a2019-09-30 11:55:341552 if (!isCallFrameLine && links.length) {
Blink Reformat4c46d092018-04-07 15:32:371553 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341554 }
Blink Reformat4c46d092018-04-07 15:32:371555
Tim van der Lippe1d6e57a2019-09-30 11:55:341556 if (!isCallFrameLine) {
Blink Reformat4c46d092018-04-07 15:32:371557 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341558 }
Blink Reformat4c46d092018-04-07 15:32:371559
1560 let openBracketIndex = -1;
1561 let closeBracketIndex = -1;
Yang Guo39256bd2019-07-18 06:02:251562 const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g;
1563 const inBrackets = /\([^\)\(]+\)/g;
1564 let lastMatch = null;
1565 let currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341566 while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251567 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341568 }
Yang Guo39256bd2019-07-18 06:02:251569 if (!lastMatch) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341570 while ((currentMatch = inBrackets.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251571 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341572 }
Yang Guo39256bd2019-07-18 06:02:251573 }
1574 if (lastMatch) {
1575 openBracketIndex = lastMatch.index;
1576 closeBracketIndex = lastMatch.index + lastMatch[0].length - 1;
Blink Reformat4c46d092018-04-07 15:32:371577 }
1578 const hasOpenBracket = openBracketIndex !== -1;
1579 const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3;
1580 const right = hasOpenBracket ? closeBracketIndex : lines[i].length;
1581 const linkCandidate = lines[i].substring(left, right);
Tim van der Lippe9b2f8712020-02-12 17:46:221582 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate);
Tim van der Lippe1d6e57a2019-09-30 11:55:341583 if (!splitResult) {
Blink Reformat4c46d092018-04-07 15:32:371584 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341585 }
Blink Reformat4c46d092018-04-07 15:32:371586
Tim van der Lippe1d6e57a2019-09-30 11:55:341587 if (splitResult.url === '<anonymous>') {
Blink Reformat4c46d092018-04-07 15:32:371588 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341589 }
Blink Reformat4c46d092018-04-07 15:32:371590 let url = parseOrScriptMatch(splitResult.url);
Tim van der Lippe9b2f8712020-02-12 17:46:221591 if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) {
1592 url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url));
Tim van der Lippe1d6e57a2019-09-30 11:55:341593 }
1594 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371595 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341596 }
Blink Reformat4c46d092018-04-07 15:32:371597
1598 links.push({
1599 url: url,
1600 positionLeft: position + left,
1601 positionRight: position + right,
1602 lineNumber: splitResult.lineNumber,
1603 columnNumber: splitResult.columnNumber
1604 });
1605 }
1606
Tim van der Lippe1d6e57a2019-09-30 11:55:341607 if (!links.length) {
Blink Reformat4c46d092018-04-07 15:32:371608 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341609 }
Blink Reformat4c46d092018-04-07 15:32:371610
Sigurd Schneider53f33522020-10-08 15:00:491611 const formattedResult = /** @type {!HTMLElement} */ (document.createElement('span'));
Blink Reformat4c46d092018-04-07 15:32:371612 let start = 0;
1613 for (let i = 0; i < links.length; ++i) {
Erik Luo383f21d2018-11-07 23:16:371614 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft)));
Erik Luo182bece2018-11-29 03:15:221615 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:051616 debuggerModel.target(), null, links[i].url, links[i].lineNumber,
1617 {columnNumber: links[i].columnNumber, className: undefined, tabStop: undefined});
Erik Luo182bece2018-11-29 03:15:221618 scriptLocationLink.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391619 this._selectableChildren.push({element: scriptLocationLink, forceSelect: () => scriptLocationLink.focus()});
Erik Luo182bece2018-11-29 03:15:221620 formattedResult.appendChild(scriptLocationLink);
Blink Reformat4c46d092018-04-07 15:32:371621 start = links[i].positionRight;
1622 }
1623
Tim van der Lippe1d6e57a2019-09-30 11:55:341624 if (start !== string.length) {
Erik Luo383f21d2018-11-07 23:16:371625 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start)));
Tim van der Lippe1d6e57a2019-09-30 11:55:341626 }
Blink Reformat4c46d092018-04-07 15:32:371627
1628 return formattedResult;
1629
1630 /**
1631 * @param {?string} url
1632 * @return {?string}
1633 */
1634 function parseOrScriptMatch(url) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341635 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371636 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341637 }
Tim van der Lippe9b2f8712020-02-12 17:46:221638 const parsedURL = Common.ParsedURL.ParsedURL.fromString(url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341639 if (parsedURL) {
Blink Reformat4c46d092018-04-07 15:32:371640 return parsedURL.url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341641 }
1642 if (debuggerModel.scriptsForSourceURL(url).length) {
Blink Reformat4c46d092018-04-07 15:32:371643 return url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341644 }
Blink Reformat4c46d092018-04-07 15:32:371645 return null;
1646 }
1647 }
1648
1649 /**
1650 * @param {string} string
1651 * @param {function(string,string,number=,number=):!Node} linkifier
1652 * @return {!DocumentFragment}
1653 */
Erik Luofc2214f2018-11-21 19:54:581654 _linkifyWithCustomLinkifier(string, linkifier) {
Sigurd Schneider8f4ac862020-10-13 13:30:111655 if (string.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:221656 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:111657 document.createElement('span'), string, getLongStringVisibleLength());
Sigurd Schneider45f32c32020-10-13 13:32:051658 const fragment = document.createDocumentFragment();
Connor Moody1a5c0d32019-12-19 07:23:361659 fragment.appendChild(propertyValue.element);
1660 return fragment;
Tim van der Lippe1d6e57a2019-09-30 11:55:341661 }
Sigurd Schneider45f32c32020-10-13 13:32:051662 const container = document.createDocumentFragment();
Tim van der Lippeeaacb722020-01-10 12:16:001663 const tokens = ConsoleViewMessage._tokenizeMessageText(string);
Blink Reformat4c46d092018-04-07 15:32:371664 for (const token of tokens) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341665 if (!token.text) {
Erik Luofc2214f2018-11-21 19:54:581666 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341667 }
Blink Reformat4c46d092018-04-07 15:32:371668 switch (token.type) {
1669 case 'url': {
1670 const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text);
Tim van der Lippe9b2f8712020-02-12 17:46:221671 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL);
Kim-Anh Tran9e49a452020-02-17 09:46:101672 const sourceURL = Common.ParsedURL.ParsedURL.removeWasmFunctionInfoFromURL(splitResult.url);
Blink Reformat4c46d092018-04-07 15:32:371673 let linkNode;
Tim van der Lippe1d6e57a2019-09-30 11:55:341674 if (splitResult) {
Kim-Anh Tran9e49a452020-02-17 09:46:101675 linkNode = linkifier(token.text, sourceURL, splitResult.lineNumber, splitResult.columnNumber);
Tim van der Lippe1d6e57a2019-09-30 11:55:341676 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051677 linkNode = linkifier(token.text, '');
Tim van der Lippe1d6e57a2019-09-30 11:55:341678 }
Blink Reformat4c46d092018-04-07 15:32:371679 container.appendChild(linkNode);
1680 break;
1681 }
1682 default:
Sigurd Schneider45f32c32020-10-13 13:32:051683 container.appendChild(document.createTextNode(token.text));
Blink Reformat4c46d092018-04-07 15:32:371684 break;
1685 }
1686 }
1687 return container;
1688 }
1689
1690 /**
Blink Reformat4c46d092018-04-07 15:32:371691 * @param {string} string
1692 * @return {!DocumentFragment}
1693 */
Erik Luo383f21d2018-11-07 23:16:371694 _linkifyStringAsFragment(string) {
Erik Luofc2214f2018-11-21 19:54:581695 return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:051696 const options = {text, lineNumber, columnNumber};
1697 const linkElement = Components.Linkifier.Linkifier.linkifyURL(
1698 url, /** @type {!Components.Linkifier.LinkifyURLOptions} */ (options));
Erik Luo383f21d2018-11-07 23:16:371699 linkElement.tabIndex = -1;
Erik Luo31c21f62018-12-13 03:39:391700 this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()});
Erik Luo383f21d2018-11-07 23:16:371701 return linkElement;
Blink Reformat4c46d092018-04-07 15:32:371702 });
1703 }
1704
1705 /**
1706 * @param {string} string
Sigurd Schneider30ac3dd2020-10-13 09:06:391707 * @return {!Array<{type: (string|undefined), text: string}>}
Blink Reformat4c46d092018-04-07 15:32:371708 */
1709 static _tokenizeMessageText(string) {
Sigurd Schneider30ac3dd2020-10-13 09:06:391710 const {tokenizerRegexes, tokenizerTypes} = getOrCreateTokenizers();
Sigurd Schneider8f4ac862020-10-13 13:30:111711 if (string.length > getMaxTokenizableStringLength()) {
Blink Reformat4c46d092018-04-07 15:32:371712 return [{text: string, type: undefined}];
Tim van der Lippe1d6e57a2019-09-30 11:55:341713 }
Sigurd Schneider30ac3dd2020-10-13 09:06:391714 const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, tokenizerRegexes);
1715 return results.map(result => ({text: result.value, type: tokenizerTypes[result.regexIndex]}));
Blink Reformat4c46d092018-04-07 15:32:371716 }
1717
1718 /**
1719 * @return {string}
1720 */
1721 groupKey() {
Tim van der Lippe1d6e57a2019-09-30 11:55:341722 if (!this._groupKey) {
Blink Reformat4c46d092018-04-07 15:32:371723 this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle();
Tim van der Lippe1d6e57a2019-09-30 11:55:341724 }
Blink Reformat4c46d092018-04-07 15:32:371725 return this._groupKey;
1726 }
1727
1728 /**
1729 * @return {string}
1730 */
1731 groupTitle() {
Tim van der Lippeeaacb722020-01-10 12:16:001732 const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText);
Blink Reformat4c46d092018-04-07 15:32:371733 const result = tokens.reduce((acc, token) => {
1734 let text = token.text;
Tim van der Lippe1d6e57a2019-09-30 11:55:341735 if (token.type === 'url') {
Tim van der Lippe9b2f8712020-02-12 17:46:221736 text = Common.UIString.UIString('<URL>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341737 } else if (token.type === 'time') {
Tim van der Lippe9b2f8712020-02-12 17:46:221738 text = Common.UIString.UIString('took <N>ms');
Tim van der Lippe1d6e57a2019-09-30 11:55:341739 } else if (token.type === 'event') {
Tim van der Lippe9b2f8712020-02-12 17:46:221740 text = Common.UIString.UIString('<some> event');
Tim van der Lippe1d6e57a2019-09-30 11:55:341741 } else if (token.type === 'milestone') {
Tim van der Lippe9b2f8712020-02-12 17:46:221742 text = Common.UIString.UIString(' M<XX>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341743 } else if (token.type === 'autofill') {
Tim van der Lippe9b2f8712020-02-12 17:46:221744 text = Common.UIString.UIString('<attribute>');
Tim van der Lippe1d6e57a2019-09-30 11:55:341745 }
Blink Reformat4c46d092018-04-07 15:32:371746 return acc + text;
1747 }, '');
1748 return result.replace(/[%]o/g, '');
1749 }
Paul Lewisbf7aa3c2019-11-20 17:03:381750}
Blink Reformat4c46d092018-04-07 15:32:371751
Sigurd Schneider30ac3dd2020-10-13 09:06:391752/** @type {?Array<!RegExp>} */
1753let tokenizerRegexes = null;
1754/** @type {?Array<string>} */
1755let tokenizerTypes = null;
1756
1757/**
1758 * @return {!{tokenizerRegexes:!Array<!RegExp>, tokenizerTypes:Array<string>}}
1759 */
1760function getOrCreateTokenizers() {
1761 if (!tokenizerRegexes || !tokenizerTypes) {
1762 const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f';
1763 const linkStringRegex = new RegExp(
1764 '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes +
1765 '"\')}\\],:;.!?]',
1766 'u');
1767 const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/;
1768 const timeRegex = /took [\d]+ms/;
1769 const eventRegex = /'\w+' event/;
1770 const milestoneRegex = /\sM[6-7]\d/;
1771 const autofillRegex = /\(suggested: \"[\w-]+\"\)/;
1772 /** @type {!Map<!RegExp, string>} */
1773 const handlers = new Map();
1774 handlers.set(linkStringRegex, 'url');
1775 handlers.set(pathLineRegex, 'url');
1776 handlers.set(timeRegex, 'time');
1777 handlers.set(eventRegex, 'event');
1778 handlers.set(milestoneRegex, 'milestone');
1779 handlers.set(autofillRegex, 'autofill');
1780 tokenizerRegexes = Array.from(handlers.keys());
1781 tokenizerTypes = Array.from(handlers.values());
1782 return {tokenizerRegexes, tokenizerTypes};
1783 }
1784 return {tokenizerRegexes, tokenizerTypes};
1785}
1786
Paul Lewisbf7aa3c2019-11-20 17:03:381787export class ConsoleGroupViewMessage extends ConsoleViewMessage {
Blink Reformat4c46d092018-04-07 15:32:371788 /**
Tim van der Lippe9b2f8712020-02-12 17:46:221789 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
1790 * @param {!Components.Linkifier.Linkifier} linkifier
Blink Reformat4c46d092018-04-07 15:32:371791 * @param {number} nestingLevel
Sigurd Schneider45f32c32020-10-13 13:32:051792 * @param {function(): void} onToggle
1793 * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize
Blink Reformat4c46d092018-04-07 15:32:371794 */
Tim van der Lippeb45d9a02019-11-05 17:24:411795 constructor(consoleMessage, linkifier, nestingLevel, onToggle, onResize) {
Blink Reformat4c46d092018-04-07 15:32:371796 console.assert(consoleMessage.isGroupStartMessage());
Tim van der Lippeb45d9a02019-11-05 17:24:411797 super(consoleMessage, linkifier, nestingLevel, onResize);
Tim van der Lippe9b2f8712020-02-12 17:46:221798 this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed;
1799 /** @type {?UI.Icon.Icon} */
Blink Reformat4c46d092018-04-07 15:32:371800 this._expandGroupIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:001801 this._onToggle = onToggle;
Blink Reformat4c46d092018-04-07 15:32:371802 }
1803
1804 /**
1805 * @param {boolean} collapsed
1806 */
Erik Luo8ef5d0c2018-09-25 21:16:001807 _setCollapsed(collapsed) {
Blink Reformat4c46d092018-04-07 15:32:371808 this._collapsed = collapsed;
Tim van der Lippe1d6e57a2019-09-30 11:55:341809 if (this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371810 this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down');
Tim van der Lippe1d6e57a2019-09-30 11:55:341811 }
Erik Luo8ef5d0c2018-09-25 21:16:001812 this._onToggle.call(null);
Blink Reformat4c46d092018-04-07 15:32:371813 }
1814
1815 /**
1816 * @return {boolean}
1817 */
1818 collapsed() {
1819 return this._collapsed;
1820 }
1821
1822 /**
1823 * @override
Sigurd Schneider45f32c32020-10-13 13:32:051824 * @param {!KeyboardEvent} event
Erik Luo8ef5d0c2018-09-25 21:16:001825 */
1826 maybeHandleOnKeyDown(event) {
Erik Luo0b8282e2018-10-08 20:37:461827 const focusedChildIndex = this._focusedChildIndex();
1828 if (focusedChildIndex === -1) {
1829 if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) {
1830 this._setCollapsed(!this._collapsed);
1831 return true;
1832 }
Erik Luo8ef5d0c2018-09-25 21:16:001833 }
1834 return super.maybeHandleOnKeyDown(event);
1835 }
1836
1837 /**
1838 * @override
Sigurd Schneider53f33522020-10-08 15:00:491839 * @return {!HTMLElement}
Blink Reformat4c46d092018-04-07 15:32:371840 */
1841 toMessageElement() {
Sigurd Schneider45f32c32020-10-13 13:32:051842 /** @type {?HTMLElement} */
1843 let element = this._element || null;
1844 if (!element) {
1845 element = super.toMessageElement();
Erik Luo8ef5d0c2018-09-25 21:16:001846 const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down';
Tim van der Lippe9b2f8712020-02-12 17:46:221847 this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon');
Erik Luob5bfff42018-09-20 02:52:391848 // Intercept focus to avoid highlight on click.
Sigurd Schneider45f32c32020-10-13 13:32:051849 this.contentElement().tabIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341850 if (this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371851 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341852 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051853 element.insertBefore(this._expandGroupIcon, this._contentElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:341854 }
Sigurd Schneider45f32c32020-10-13 13:32:051855 element.addEventListener('click', () => this._setCollapsed(!this._collapsed));
Blink Reformat4c46d092018-04-07 15:32:371856 }
Sigurd Schneider45f32c32020-10-13 13:32:051857 return element;
Blink Reformat4c46d092018-04-07 15:32:371858 }
1859
1860 /**
1861 * @override
1862 */
1863 _showRepeatCountElement() {
1864 super._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341865 if (this._repeatCountElement && this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371866 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341867 }
Blink Reformat4c46d092018-04-07 15:32:371868 }
Paul Lewisbf7aa3c2019-11-20 17:03:381869}
Blink Reformat4c46d092018-04-07 15:32:371870
Sigurd Schneiderca7b4ff2020-10-14 07:45:471871export class ConsoleCommand extends ConsoleViewMessage {
1872 /**
1873 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
1874 * @param {!Components.Linkifier.Linkifier} linkifier
1875 * @param {number} nestingLevel
1876 * @param {function(!Common.EventTarget.EventTargetEvent):void} onResize
1877 */
1878 constructor(consoleMessage, linkifier, nestingLevel, onResize) {
1879 super(consoleMessage, linkifier, nestingLevel, onResize);
1880 /** @type {?HTMLElement} */
1881 this._formattedCommand = null;
1882 }
1883
1884 /**
1885 * @override
1886 * @return {!HTMLElement}
1887 */
1888 contentElement() {
1889 const contentElement = this.getContentElement();
1890 if (contentElement) {
1891 return contentElement;
1892 }
1893 const newContentElement = /** @type {!HTMLElement} */ (document.createElement('div'));
1894 this.setContentElement(newContentElement);
1895 newContentElement.classList.add('console-user-command');
1896 const icon = UI.Icon.Icon.create('smallicon-user-command', 'command-result-icon');
1897 newContentElement.appendChild(icon);
1898
1899 elementToMessage.set(newContentElement, this);
1900
1901 this._formattedCommand = /** @type {!HTMLElement} */ (document.createElement('span'));
1902 this._formattedCommand.classList.add('source-code');
1903 this._formattedCommand.textContent = Platform.StringUtilities.replaceControlCharacters(this.text);
1904 newContentElement.appendChild(this._formattedCommand);
1905
1906 if (this._formattedCommand.textContent.length < MaxLengthToIgnoreHighlighter) {
1907 const javascriptSyntaxHighlighter = new UI.SyntaxHighlighter.SyntaxHighlighter('text/javascript', true);
1908 javascriptSyntaxHighlighter.syntaxHighlightNode(this._formattedCommand).then(this._updateSearch.bind(this));
1909 } else {
1910 this._updateSearch();
1911 }
1912
1913 this.updateTimestamp();
1914 return newContentElement;
1915 }
1916
1917 _updateSearch() {
1918 this.setSearchRegex(this.searchRegex());
1919 }
1920}
1921
1922export class ConsoleCommandResult extends ConsoleViewMessage {
1923 /**
1924 * @override
1925 * @return {!HTMLElement}
1926 */
1927 contentElement() {
1928 const element = super.contentElement();
1929 if (!element.classList.contains('console-user-command-result')) {
1930 element.classList.add('console-user-command-result');
1931 if (this.consoleMessage().level === SDK.ConsoleModel.MessageLevel.Info) {
1932 const icon = UI.Icon.Icon.create('smallicon-command-result', 'command-result-icon');
1933 element.insertBefore(icon, element.firstChild);
1934 }
1935 }
1936 return element;
1937 }
1938}
1939
Sigurd Schneider8bfb4212020-10-27 10:27:371940export class ConsoleTableMessageView extends ConsoleViewMessage {
1941 /**
1942 * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage
1943 * @param {!Components.Linkifier.Linkifier} linkifier
1944 * @param {number} nestingLevel
1945 * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize
1946 */
1947 constructor(consoleMessage, linkifier, nestingLevel, onResize) {
1948 super(consoleMessage, linkifier, nestingLevel, onResize);
1949 console.assert(consoleMessage.type === SDK.ConsoleModel.MessageType.Table);
1950 /** @type {?DataGrid.SortableDataGrid.SortableDataGrid<?>} */
1951 this._dataGrid = null;
1952 }
1953
1954 /**
1955 * @override
1956 */
1957 wasShown() {
1958 if (this._dataGrid) {
1959 this._dataGrid.updateWidths();
1960 }
1961 super.wasShown();
1962 }
1963
1964 /**
1965 * @override
1966 */
1967 onResize() {
1968 if (!this.isVisible()) {
1969 return;
1970 }
1971 if (this._dataGrid) {
1972 this._dataGrid.onResize();
1973 }
1974 }
1975
1976 /**
1977 * @override
1978 * @return {!HTMLElement}
1979 */
1980 contentElement() {
1981 const contentElement = this.getContentElement();
1982 if (contentElement) {
1983 return contentElement;
1984 }
1985
1986 const newContentElement = /** @type {!HTMLElement} */ (document.createElement('div'));
1987 newContentElement.classList.add('console-message');
1988 if (this._messageLevelIcon) {
1989 newContentElement.appendChild(this._messageLevelIcon);
1990 }
1991 this.setContentElement(newContentElement);
1992
1993 newContentElement.appendChild(this._buildTableMessage());
1994 this.updateTimestamp();
1995 return newContentElement;
1996 }
1997
1998 /**
1999 * @return {!HTMLElement}
2000 */
2001 _buildTableMessage() {
2002 const formattedMessage = /** @type {!HTMLElement} */ (document.createElement('span'));
2003 formattedMessage.classList.add('source-code');
2004 this._anchorElement = this._buildMessageAnchor();
2005 if (this._anchorElement) {
2006 formattedMessage.appendChild(this._anchorElement);
2007 }
2008
2009 const table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null;
2010 if (!table) {
2011 return this._buildMessage();
2012 }
2013 const actualTable = parameterToRemoteObject(this._message.runtimeModel())(table);
2014 if (!actualTable || !actualTable.preview) {
2015 return this._buildMessage();
2016 }
2017
2018 const rawValueColumnSymbol = Symbol('rawValueColumn');
2019 /** @type {!Array<string|symbol>} */
2020 const columnNames = [];
2021 const preview = actualTable.preview;
2022 const rows = [];
2023 for (let i = 0; i < preview.properties.length; ++i) {
2024 const rowProperty = preview.properties[i];
2025 /** @type {!Array<!Protocol.Runtime.PropertyPreview|!{name:(string|symbol), type: !Protocol.Runtime.PropertyPreviewType, value: (string|undefined)}>} */
2026 let rowSubProperties;
Sigurd Schneiderb393a432020-11-06 12:08:212027 if (rowProperty.valuePreview && rowProperty.valuePreview.properties.length) {
Sigurd Schneider8bfb4212020-10-27 10:27:372028 rowSubProperties = rowProperty.valuePreview.properties;
2029 } else if (rowProperty.value) {
2030 rowSubProperties = [{name: rawValueColumnSymbol, type: rowProperty.type, value: rowProperty.value}];
2031 } else {
2032 continue;
2033 }
2034
2035 /** @type {!Map<string|symbol, !HTMLElement>} */
2036 const rowValue = new Map();
2037 const maxColumnsToRender = 20;
2038 for (let j = 0; j < rowSubProperties.length; ++j) {
2039 const cellProperty = rowSubProperties[j];
2040 let columnRendered = columnNames.indexOf(cellProperty.name) !== -1;
2041 if (!columnRendered) {
2042 if (columnNames.length === maxColumnsToRender) {
2043 continue;
2044 }
2045 columnRendered = true;
2046 columnNames.push(cellProperty.name);
2047 }
2048
2049 if (columnRendered) {
2050 const cellElement =
2051 this._renderPropertyPreviewOrAccessor(actualTable, cellProperty, [rowProperty, cellProperty]);
2052 cellElement.classList.add('console-message-nowrap-below');
2053 rowValue.set(cellProperty.name, cellElement);
2054 }
2055 }
2056 rows.push({rowName: rowProperty.name, rowValue});
2057 }
2058
2059 const flatValues = [];
2060 for (const {rowName, rowValue} of rows) {
2061 flatValues.push(rowName);
2062 for (let j = 0; j < columnNames.length; ++j) {
2063 flatValues.push(rowValue.get(columnNames[j]));
2064 }
2065 }
2066 columnNames.unshift(Common.UIString.UIString('(index)'));
2067 const columnDisplayNames =
2068 columnNames.map(name => name === rawValueColumnSymbol ? Common.UIString.UIString('Value') : name.toString());
2069
2070 if (flatValues.length) {
2071 this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create(columnDisplayNames, flatValues, ls`Console`);
2072 if (this._dataGrid) {
2073 this._dataGrid.setStriped(true);
2074 this._dataGrid.setFocusable(false);
2075
2076 const formattedResult = document.createElement('span');
2077 formattedResult.classList.add('console-message-text');
2078 const tableElement = formattedResult.createChild('div', 'console-message-formatted-table');
2079 const dataGridContainer = tableElement.createChild('span');
2080 tableElement.appendChild(this._formatParameter(actualTable, true, false));
2081 dataGridContainer.appendChild(this._dataGrid.element);
2082 formattedMessage.appendChild(formattedResult);
2083 this._dataGrid.renderInline();
2084 }
2085 }
2086 return formattedMessage;
2087 }
2088
2089 /**
2090 * @override
2091 * @return {number}
2092 */
2093 approximateFastHeight() {
2094 const table = this._message.parameters && this._message.parameters[0];
2095 if (table && typeof table !== 'string' && table.preview) {
2096 return defaultConsoleRowHeight * table.preview.properties.length;
2097 }
2098 return defaultConsoleRowHeight;
2099 }
2100}
2101
Sigurd Schneiderca7b4ff2020-10-14 07:45:472102/**
2103 * The maximum length before strings are considered too long for syntax highlighting.
2104 * @const
2105 * @type {number}
2106 */
2107const MaxLengthToIgnoreHighlighter = 10000;
2108
Blink Reformat4c46d092018-04-07 15:32:372109/**
2110 * @const
2111 * @type {number}
2112 */
Paul Lewisbf7aa3c2019-11-20 17:03:382113export const MaxLengthForLinks = 40;
Blink Reformat4c46d092018-04-07 15:32:372114
Sigurd Schneider8f4ac862020-10-13 13:30:112115let _MaxTokenizableStringLength = 10000;
2116let _LongStringVisibleLength = 5000;
2117
2118export const getMaxTokenizableStringLength = () => {
2119 return _MaxTokenizableStringLength;
2120};
2121
2122/**
2123 * @param {number} length
2124 */
2125export const setMaxTokenizableStringLength = length => {
2126 _MaxTokenizableStringLength = length;
2127};
2128
2129export const getLongStringVisibleLength = () => {
2130 return _LongStringVisibleLength;
2131};
2132
2133/**
2134 * @param {number} length
2135 */
2136export const setLongStringVisibleLength = length => {
2137 _LongStringVisibleLength = length;
2138};