blob: 2700d8cace21ec821382539cf97b05bc6e0c5372 [file] [log] [blame]
Jan Scheffler19cd7ec2021-02-12 14:16:301// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Blink Reformat4c46d092018-04-07 15:32:375/*
6 * Copyright (C) 2011 Google Inc. All rights reserved.
7 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
8 * Copyright (C) 2009 Joseph Pecoraro
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
Tim van der Lippe9b2f8712020-02-12 17:46:2234
Jan Scheffler19cd7ec2021-02-12 14:16:3035/* eslint-disable rulesdir/no_underscored_properties */
36
Tim van der Lippe9b2f8712020-02-12 17:46:2237import * as Common from '../common/common.js';
38import * as Components from '../components/components.js';
39import * as DataGrid from '../data_grid/data_grid.js';
Christy Chen9c6d8982021-02-08 02:28:3140import * as i18n from '../i18n/i18n.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2241import * as ObjectUI from '../object_ui/object_ui.js';
Tim van der Lippe93b57c32020-02-20 17:38:4442import * as Platform from '../platform/platform.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2243import * as SDK from '../sdk/sdk.js';
Andres Olivaresf59e6de2021-02-18 13:10:2544import * as TextEditor from '../text_editor/text_editor.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2245import * as TextUtils from '../text_utils/text_utils.js';
Paul Lewisca569a52020-09-09 16:11:5146import * as ThemeSupport from '../theme_support/theme_support.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2247import * as UI from '../ui/ui.js';
48
Tim van der Lippebfbb58f2021-02-25 17:34:1949import type {ConsoleViewportElement} from './ConsoleViewport.js';
Tim van der Lippeeaacb722020-01-10 12:16:0050
Simon Zündfbfd1072021-03-01 07:38:5351const UIStrings = {
Christy Chen9c6d8982021-02-08 02:28:3152 /**
Peter Marshallf295a4b2021-02-26 09:48:0353 * @description Message element text content in Console View Message of the Console panel. Shown
54 * when the user tried to run console.clear() but the 'Preserve log' option is enabled, which stops
55 * the log from being cleared.
Christy Chen9c6d8982021-02-08 02:28:3156 */
57 consoleclearWasPreventedDueTo: '`console.clear()` was prevented due to \'Preserve log\'',
58 /**
Peter Marshallb8bd00f2021-02-24 08:25:1859 * @description Text shown in the Console panel after the user has cleared the console, which
60 * removes all messages from the console so that it is empty.
Christy Chen9c6d8982021-02-08 02:28:3161 */
62 consoleWasCleared: 'Console was cleared',
63 /**
64 *@description Message element title in Console View Message of the Console panel
65 *@example {Ctrl+L} PH1
66 */
67 clearAllMessagesWithS: 'Clear all messages with {PH1}',
68 /**
69 *@description Message prefix in Console View Message of the Console panel
70 */
71 assertionFailed: 'Assertion failed: ',
72 /**
73 *@description Message text in Console View Message of the Console panel
74 *@example {console.log(1)} PH1
75 */
Peter Marshall40dd7d92021-02-19 11:07:3776 violationS: '`[Violation]` {PH1}',
Christy Chen9c6d8982021-02-08 02:28:3177 /**
78 *@description Message text in Console View Message of the Console panel
79 *@example {console.log(1)} PH1
80 */
Peter Marshall40dd7d92021-02-19 11:07:3781 interventionS: '`[Intervention]` {PH1}',
Christy Chen9c6d8982021-02-08 02:28:3182 /**
83 *@description Message text in Console View Message of the Console panel
84 *@example {console.log(1)} PH1
85 */
Peter Marshall40dd7d92021-02-19 11:07:3786 deprecationS: '`[Deprecation]` {PH1}',
Christy Chen9c6d8982021-02-08 02:28:3187 /**
88 *@description Note title in Console View Message of the Console panel
89 */
90 thisValueWillNotBeCollectedUntil: 'This value will not be collected until console is cleared.',
91 /**
92 *@description Note title in Console View Message of the Console panel
93 */
94 thisValueWasEvaluatedUponFirst: 'This value was evaluated upon first expanding. It may have changed since then.',
95 /**
96 *@description Note title in Console View Message of the Console panel
97 */
98 functionWasResolvedFromBound: 'Function was resolved from bound function.',
99 /**
Peter Marshallf625dc82021-03-02 08:10:57100 * @description Shown in the Console panel when an exception is thrown when trying to access a
101 * property on an object. Should be translated.
Christy Chen9c6d8982021-02-08 02:28:31102 */
103 exception: '<exception>',
104 /**
105 *@description Text to indicate an item is a warning
106 */
107 warning: 'Warning',
108 /**
109 *@description Text for errors
110 */
111 error: 'Error',
112 /**
Peter Marshall2bdcc642021-03-03 10:02:09113 * @description Announced by the screen reader to indicate how many times a particular message in
114 * the console was repeated.
Christy Chen9c6d8982021-02-08 02:28:31115 */
Peter Marshall2bdcc642021-03-03 10:02:09116 repeatS: '{n, plural, =1 {Repeated # time} other {Repeated # times}}',
Christy Chen9c6d8982021-02-08 02:28:31117 /**
Peter Marshall2bdcc642021-03-03 10:02:09118 * @description Announced by the screen reader to indicate how many times a particular warning
119 * message in the console was repeated.
Christy Chen9c6d8982021-02-08 02:28:31120 */
Peter Marshall2bdcc642021-03-03 10:02:09121 warningS: '{n, plural, =1 {Warning, Repeated # time} other {Warning, Repeated # times}}',
Christy Chen9c6d8982021-02-08 02:28:31122 /**
Peter Marshall2bdcc642021-03-03 10:02:09123 * @description Announced by the screen reader to indicate how many times a particular error
124 * message in the console was repeated.
Christy Chen9c6d8982021-02-08 02:28:31125 */
Peter Marshall2bdcc642021-03-03 10:02:09126 errorS: '{n, plural, =1 {Error, Repeated # time} other {Error, Repeated # times}}',
Christy Chen9c6d8982021-02-08 02:28:31127 /**
Tim van der Lippee4b96c62021-02-17 12:43:16128 *@description Text appended to grouped console messages that are related to URL requests
Christy Chen9c6d8982021-02-08 02:28:31129 */
130 url: '<URL>',
131 /**
Tim van der Lippee4b96c62021-02-17 12:43:16132 *@description Text appended to grouped console messages about tasks that took longer than N ms
Christy Chen9c6d8982021-02-08 02:28:31133 */
134 tookNms: 'took <N>ms',
135 /**
Tim van der Lippee4b96c62021-02-17 12:43:16136 *@description Text appended to grouped console messages about tasks that are related to some DOM event
Christy Chen9c6d8982021-02-08 02:28:31137 */
138 someEvent: '<some> event',
139 /**
Tim van der Lippee4b96c62021-02-17 12:43:16140 *@description Text appended to grouped console messages about tasks that are related to a particular milestone
Christy Chen9c6d8982021-02-08 02:28:31141 */
142 Mxx: ' M<XX>',
143 /**
Tim van der Lippee4b96c62021-02-17 12:43:16144 *@description Text appended to grouped console messages about tasks that are related to autofill completions
Christy Chen9c6d8982021-02-08 02:28:31145 */
146 attribute: '<attribute>',
147 /**
148 *@description Text for the index of something
149 */
150 index: '(index)',
151 /**
152 *@description Text for the value of something
153 */
154 value: 'Value',
155 /**
156 *@description Title of the Console tool
157 */
158 console: 'Console',
159};
Jan Scheffler19cd7ec2021-02-12 14:16:30160const str_ = i18n.i18n.registerUIStrings('console/ConsoleViewMessage.ts', UIStrings);
Christy Chen9c6d8982021-02-08 02:28:31161const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Jan Scheffler19cd7ec2021-02-12 14:16:30162const elementToMessage = new WeakMap<Element, ConsoleViewMessage>();
Sigurd Schneiderca7b4ff2020-10-14 07:45:47163
Jan Scheffler19cd7ec2021-02-12 14:16:30164export const getMessageForElement = (element: Element): ConsoleViewMessage|undefined => {
Sigurd Schneiderca7b4ff2020-10-14 07:45:47165 return elementToMessage.get(element);
166};
167
Sigurd Schneider8bfb4212020-10-27 10:27:37168// This value reflects the 18px min-height of .console-message, plus the
169// 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
170const defaultConsoleRowHeight = 19;
171
Jan Scheffler19cd7ec2021-02-12 14:16:30172const parameterToRemoteObject = (runtimeModel: SDK.RuntimeModel.RuntimeModel|null): (
173 parameter?: SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|string) => SDK.RemoteObject.RemoteObject =>
174 (parameter?: string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject): SDK.RemoteObject.RemoteObject => {
Sigurd Schneider8bfb4212020-10-27 10:27:37175 if (parameter instanceof SDK.RemoteObject.RemoteObject) {
176 return parameter;
177 }
178 if (!runtimeModel) {
179 return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter);
180 }
181 if (typeof parameter === 'object') {
182 return runtimeModel.createRemoteObject(parameter);
183 }
184 return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
185 };
186
Jan Scheffler19cd7ec2021-02-12 14:16:30187export class ConsoleViewMessage implements ConsoleViewportElement {
188 _message: SDK.ConsoleModel.ConsoleMessage;
189 _linkifier: Components.Linkifier.Linkifier;
190 _repeatCount: number;
191 _closeGroupDecorationCount: number;
192 _nestingLevel: number;
193 _selectableChildren: {
194 element: HTMLElement,
195 forceSelect: () => void,
196 }[];
197 _messageResized: (arg0: Common.EventTarget.EventTargetEvent) => void;
198 _element: HTMLElement|null;
199 _previewFormatter: ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter;
200 _searchRegex: RegExp|null;
201 _messageLevelIcon: UI.Icon.Icon|null;
202 _traceExpanded: boolean;
203 _expandTrace: ((arg0: boolean) => void)|null;
204 _anchorElement: HTMLElement|null;
205 _contentElement: HTMLElement|null;
206 _nestingLevelMarkers: HTMLElement[]|null;
207 _searchHighlightNodes: Element[];
208 _searchHighlightNodeChanges: UI.UIUtils.HighlightChange[];
209 _isVisible: boolean;
210 _cachedHeight: number;
211 _messagePrefix: string;
212 _timestampElement: HTMLElement|null;
213 _inSimilarGroup: boolean;
214 _similarGroupMarker: HTMLElement|null;
215 _lastInSimilarGroup: boolean;
216 _groupKey: string;
217 _repeatCountElement: UI.UIUtils.DevToolsSmallBubble|null;
218
219 constructor(
220 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
221 onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Blink Reformat4c46d092018-04-07 15:32:37222 this._message = consoleMessage;
223 this._linkifier = linkifier;
Blink Reformat4c46d092018-04-07 15:32:37224 this._repeatCount = 1;
225 this._closeGroupDecorationCount = 0;
226 this._nestingLevel = nestingLevel;
Erik Luo383f21d2018-11-07 23:16:37227 this._selectableChildren = [];
Erik Luo840be6b2018-12-03 20:54:27228 this._messageResized = onResize;
Sigurd Schneider53e98632020-10-26 15:29:50229 this._element = null;
Blink Reformat4c46d092018-04-07 15:32:37230
Tim van der Lippe9b2f8712020-02-12 17:46:22231 this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter();
Blink Reformat4c46d092018-04-07 15:32:37232 this._searchRegex = null;
Blink Reformat4c46d092018-04-07 15:32:37233 this._messageLevelIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:00234 this._traceExpanded = false;
Erik Luo8ef5d0c2018-09-25 21:16:00235 this._expandTrace = null;
John Emaubb2897a2019-10-04 17:37:32236 this._anchorElement = null;
Sigurd Schneider45f32c32020-10-13 13:32:05237 this._contentElement = null;
Sigurd Schneider45f32c32020-10-13 13:32:05238 this._nestingLevelMarkers = null;
Sigurd Schneider45f32c32020-10-13 13:32:05239 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:52240 this._searchHighlightNodeChanges = [];
Sigurd Schneider53e98632020-10-26 15:29:50241 this._isVisible = false;
242 this._cachedHeight = 0;
243 this._messagePrefix = '';
Sigurd Schneider53e98632020-10-26 15:29:50244 this._timestampElement = null;
245 this._inSimilarGroup = false;
Sigurd Schneider53e98632020-10-26 15:29:50246 this._similarGroupMarker = null;
247 this._lastInSimilarGroup = false;
248 this._groupKey = '';
Sigurd Schneider53e98632020-10-26 15:29:50249 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:37250 }
251
Jan Scheffler19cd7ec2021-02-12 14:16:30252 element(): HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37253 return this.toMessageElement();
254 }
255
Jan Scheffler19cd7ec2021-02-12 14:16:30256 wasShown(): void {
Blink Reformat4c46d092018-04-07 15:32:37257 this._isVisible = true;
258 }
259
Jan Scheffler19cd7ec2021-02-12 14:16:30260 onResize(): void {
Blink Reformat4c46d092018-04-07 15:32:37261 }
262
Jan Scheffler19cd7ec2021-02-12 14:16:30263 willHide(): void {
Blink Reformat4c46d092018-04-07 15:32:37264 this._isVisible = false;
Erik Luo4b002322018-07-30 21:23:31265 this._cachedHeight = this.element().offsetHeight;
Blink Reformat4c46d092018-04-07 15:32:37266 }
267
Jan Scheffler19cd7ec2021-02-12 14:16:30268 isVisible(): boolean {
Sigurd Schneider8bfb4212020-10-27 10:27:37269 return this._isVisible;
270 }
271
Jan Scheffler19cd7ec2021-02-12 14:16:30272 fastHeight(): number {
Tim van der Lippe1d6e57a2019-09-30 11:55:34273 if (this._cachedHeight) {
Blink Reformat4c46d092018-04-07 15:32:37274 return this._cachedHeight;
Tim van der Lippe1d6e57a2019-09-30 11:55:34275 }
Sigurd Schneider8bfb4212020-10-27 10:27:37276 return this.approximateFastHeight();
277 }
278
Jan Scheffler19cd7ec2021-02-12 14:16:30279 approximateFastHeight(): number {
Blink Reformat4c46d092018-04-07 15:32:37280 return defaultConsoleRowHeight;
281 }
282
Jan Scheffler19cd7ec2021-02-12 14:16:30283 consoleMessage(): SDK.ConsoleModel.ConsoleMessage {
Blink Reformat4c46d092018-04-07 15:32:37284 return this._message;
285 }
286
Jan Scheffler19cd7ec2021-02-12 14:16:30287 _buildMessage(): HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37288 let messageElement;
Jan Scheffler19cd7ec2021-02-12 14:16:30289 let messageText: Common.UIString.LocalizedString|string = this._message.messageText;
Tim van der Lippe9b2f8712020-02-12 17:46:22290 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:37291 switch (this._message.type) {
Tim van der Lippe9b2f8712020-02-12 17:46:22292 case SDK.ConsoleModel.MessageType.Trace:
Blink Reformat4c46d092018-04-07 15:32:37293 messageElement = this._format(this._message.parameters || ['console.trace']);
294 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22295 case SDK.ConsoleModel.MessageType.Clear:
Tim van der Lippef49e2322020-05-01 15:03:09296 messageElement = document.createElement('span');
297 messageElement.classList.add('console-info');
Paul Lewis2d7d65c2020-03-16 17:26:30298 if (Common.Settings.Settings.instance().moduleSetting('preserveConsoleLog').get()) {
Christy Chen9c6d8982021-02-08 02:28:31299 messageElement.textContent = i18nString(UIStrings.consoleclearWasPreventedDueTo);
Tim van der Lippe1d6e57a2019-09-30 11:55:34300 } else {
Christy Chen9c6d8982021-02-08 02:28:31301 messageElement.textContent = i18nString(UIStrings.consoleWasCleared);
Tim van der Lippe1d6e57a2019-09-30 11:55:34302 }
Tim van der Lippe420e5e32020-11-23 16:58:46303 UI.Tooltip.Tooltip.install(
Christy Chen9c6d8982021-02-08 02:28:31304 messageElement, i18nString(UIStrings.clearAllMessagesWithS, {
Jan Scheffler19cd7ec2021-02-12 14:16:30305 PH1: UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction('console.clear'),
Christy Chen9c6d8982021-02-08 02:28:31306 }));
Blink Reformat4c46d092018-04-07 15:32:37307 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22308 case SDK.ConsoleModel.MessageType.Dir: {
Blink Reformat4c46d092018-04-07 15:32:37309 const obj = this._message.parameters ? this._message.parameters[0] : undefined;
310 const args = ['%O', obj];
311 messageElement = this._format(args);
312 break;
313 }
Tim van der Lippe9b2f8712020-02-12 17:46:22314 case SDK.ConsoleModel.MessageType.Profile:
315 case SDK.ConsoleModel.MessageType.ProfileEnd:
Blink Reformat4c46d092018-04-07 15:32:37316 messageElement = this._format([messageText]);
317 break;
318 default: {
Sigurd Schneider45f32c32020-10-13 13:32:05319 if (this._message.type === SDK.ConsoleModel.MessageType.Assert) {
Christy Chen9c6d8982021-02-08 02:28:31320 this._messagePrefix = i18nString(UIStrings.assertionFailed);
Sigurd Schneider45f32c32020-10-13 13:32:05321 }
322 if (this._message.parameters && this._message.parameters.length === 1) {
323 const parameter = this._message.parameters[0];
324 if (typeof parameter !== 'string' && parameter.type === 'string') {
Jan Scheffler19cd7ec2021-02-12 14:16:30325 messageElement = this._tryFormatAsError((parameter.value as string));
Sigurd Schneider45f32c32020-10-13 13:32:05326 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34327 }
Blink Reformat4c46d092018-04-07 15:32:37328 const args = this._message.parameters || [messageText];
329 messageElement = messageElement || this._format(args);
330 }
331 }
332 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22333 if (this._message.source === SDK.ConsoleModel.MessageSource.Network) {
Erik Luofc2214f2018-11-21 19:54:58334 messageElement = this._formatAsNetworkRequest() || this._format([messageText]);
335 } else {
Jan Scheffler19cd7ec2021-02-12 14:16:30336 const messageInParameters = this._message.parameters && messageText === (this._message.parameters[0] as string);
Peter Marshall40dd7d92021-02-19 11:07:37337 // These terms are locked because the console message will not be translated anyway.
Tim van der Lippe9b2f8712020-02-12 17:46:22338 if (this._message.source === SDK.ConsoleModel.MessageSource.Violation) {
Christy Chen9c6d8982021-02-08 02:28:31339 messageText = i18nString(UIStrings.violationS, {PH1: messageText});
Tim van der Lippe9b2f8712020-02-12 17:46:22340 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Intervention) {
Christy Chen9c6d8982021-02-08 02:28:31341 messageText = i18nString(UIStrings.interventionS, {PH1: messageText});
Tim van der Lippe9b2f8712020-02-12 17:46:22342 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Deprecation) {
Christy Chen9c6d8982021-02-08 02:28:31343 messageText = i18nString(UIStrings.deprecationS, {PH1: messageText});
Tim van der Lippe1d6e57a2019-09-30 11:55:34344 }
Blink Reformat4c46d092018-04-07 15:32:37345 const args = this._message.parameters || [messageText];
Tim van der Lippe1d6e57a2019-09-30 11:55:34346 if (messageInParameters) {
Blink Reformat4c46d092018-04-07 15:32:37347 args[0] = messageText;
Tim van der Lippe1d6e57a2019-09-30 11:55:34348 }
Blink Reformat4c46d092018-04-07 15:32:37349 messageElement = this._format(args);
350 }
351 }
352 messageElement.classList.add('console-message-text');
353
Jan Scheffler19cd7ec2021-02-12 14:16:30354 const formattedMessage = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09355 formattedMessage.classList.add('source-code');
Erik Luo5976c8c2018-07-24 02:03:09356 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34357 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09358 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34359 }
Blink Reformat4c46d092018-04-07 15:32:37360 formattedMessage.appendChild(messageElement);
361 return formattedMessage;
362 }
363
Jan Scheffler19cd7ec2021-02-12 14:16:30364 _formatAsNetworkRequest(): HTMLElement|null {
Tim van der Lippe9b2f8712020-02-12 17:46:22365 const request = SDK.NetworkLog.NetworkLog.requestForConsoleMessage(this._message);
Tim van der Lippe1d6e57a2019-09-30 11:55:34366 if (!request) {
Erik Luofc2214f2018-11-21 19:54:58367 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34368 }
Jan Scheffler19cd7ec2021-02-12 14:16:30369 const messageElement = (document.createElement('span') as HTMLElement);
Tim van der Lippe9b2f8712020-02-12 17:46:22370 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Sigurd Schneider23c52972020-10-13 09:31:14371 UI.UIUtils.createTextChild(messageElement, request.requestMethod + ' ');
Tim van der Lippe9b2f8712020-02-12 17:46:22372 const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url());
Erik Luo182bece2018-11-29 03:15:22373 // Focus is handled by the viewport.
374 linkElement.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:30375 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22376 messageElement.appendChild(linkElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34377 if (request.failed) {
Sigurd Schneider23c52972020-10-13 09:31:14378 UI.UIUtils.createTextChildren(messageElement, ' ', request.localizedFailDescription || '');
Tim van der Lippe1d6e57a2019-09-30 11:55:34379 }
380 if (request.statusCode !== 0) {
Sigurd Schneider23c52972020-10-13 09:31:14381 UI.UIUtils.createTextChildren(messageElement, ' ', String(request.statusCode));
Tim van der Lippe1d6e57a2019-09-30 11:55:34382 }
383 if (request.statusText) {
Sigurd Schneider23c52972020-10-13 09:31:14384 UI.UIUtils.createTextChildren(messageElement, ' (', request.statusText, ')');
Tim van der Lippe1d6e57a2019-09-30 11:55:34385 }
Erik Luofc2214f2018-11-21 19:54:58386 } else {
Erik Luoad5f3942019-03-26 20:53:44387 const messageText = this._message.messageText;
388 const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:05389 const linkElement = url === request.url() ?
390 Components.Linkifier.Linkifier.linkifyRevealable(
Jan Scheffler19cd7ec2021-02-12 14:16:30391 (request as SDK.NetworkRequest.NetworkRequest), url, request.url()) :
Sigurd Schneider45f32c32020-10-13 13:32:05392 Components.Linkifier.Linkifier.linkifyURL(
Jan Scheffler19cd7ec2021-02-12 14:16:30393 url, ({text, lineNumber, columnNumber} as Components.Linkifier.LinkifyURLOptions));
Erik Luo182bece2018-11-29 03:15:22394 linkElement.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:30395 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22396 return linkElement;
397 });
Erik Luofc2214f2018-11-21 19:54:58398 messageElement.appendChild(fragment);
399 }
400 return messageElement;
401 }
402
Jan Scheffler19cd7ec2021-02-12 14:16:30403 _buildMessageAnchor(): HTMLElement|null {
404 const linkify = (message: SDK.ConsoleModel.ConsoleMessage): HTMLElement|null => {
Sigurd Schneider45f32c32020-10-13 13:32:05405 if (message.scriptId) {
406 return this._linkifyScriptId(message.scriptId, message.url || '', message.line, message.column);
407 }
408 if (message.stackTrace && message.stackTrace.callFrames.length) {
409 return this._linkifyStackTraceTopFrame(message.stackTrace);
410 }
411 if (message.url && message.url !== 'undefined') {
412 return this._linkifyLocation(message.url, message.line, message.column);
413 }
414 return null;
415 };
416 const anchorElement = linkify(this._message);
Blink Reformat4c46d092018-04-07 15:32:37417 // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
418 if (anchorElement) {
John Emauf7e30fb2019-10-04 19:12:32419 anchorElement.tabIndex = -1;
420 this._selectableChildren.push({
421 element: anchorElement,
Jan Scheffler19cd7ec2021-02-12 14:16:30422 forceSelect: (): void => anchorElement.focus(),
John Emauf7e30fb2019-10-04 19:12:32423 });
Jan Scheffler19cd7ec2021-02-12 14:16:30424 const anchorWrapperElement = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09425 anchorWrapperElement.classList.add('console-message-anchor');
Blink Reformat4c46d092018-04-07 15:32:37426 anchorWrapperElement.appendChild(anchorElement);
Sigurd Schneider23c52972020-10-13 09:31:14427 UI.UIUtils.createTextChild(anchorWrapperElement, ' ');
Blink Reformat4c46d092018-04-07 15:32:37428 return anchorWrapperElement;
429 }
430 return null;
431 }
432
Jan Scheffler19cd7ec2021-02-12 14:16:30433 _buildMessageWithStackTrace(runtimeModel: SDK.RuntimeModel.RuntimeModel): HTMLElement {
434 const toggleElement = (document.createElement('div') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09435 toggleElement.classList.add('console-message-stack-trace-toggle');
Blink Reformat4c46d092018-04-07 15:32:37436 const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
437
438 const messageElement = this._buildMessage();
Tim van der Lippe9b2f8712020-02-12 17:46:22439 const icon = UI.Icon.Icon.create('smallicon-triangle-right', 'console-message-expand-icon');
Blink Reformat4c46d092018-04-07 15:32:37440 const clickableElement = contentElement.createChild('div');
441 clickableElement.appendChild(icon);
Erik Luob5bfff42018-09-20 02:52:39442 // Intercept focus to avoid highlight on click.
443 clickableElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37444 clickableElement.appendChild(messageElement);
445 const stackTraceElement = contentElement.createChild('div');
446 const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
Wolfgang Beyer08261c72021-03-09 09:21:34447 runtimeModel.target(), this._linkifier, {stackTrace: this._message.stackTrace, tabStops: undefined});
Erik Luo182bece2018-11-29 03:15:22448 stackTraceElement.appendChild(stackTracePreview.element);
449 for (const linkElement of stackTracePreview.links) {
Jan Scheffler19cd7ec2021-02-12 14:16:30450 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22451 }
Blink Reformat4c46d092018-04-07 15:32:37452 stackTraceElement.classList.add('hidden');
Brandon Goddard04a5a762019-12-10 16:45:53453 UI.ARIAUtils.markAsTreeitem(this.element());
454 UI.ARIAUtils.setExpanded(this.element(), false);
Jan Scheffler19cd7ec2021-02-12 14:16:30455 this._expandTrace = (expand: boolean): void => {
Blink Reformat4c46d092018-04-07 15:32:37456 icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right');
457 stackTraceElement.classList.toggle('hidden', !expand);
Brandon Goddard04a5a762019-12-10 16:45:53458 UI.ARIAUtils.setExpanded(this.element(), expand);
Erik Luo8ef5d0c2018-09-25 21:16:00459 this._traceExpanded = expand;
460 };
Blink Reformat4c46d092018-04-07 15:32:37461
Jan Scheffler19cd7ec2021-02-12 14:16:30462 const toggleStackTrace = (event: Event): void => {
Tim van der Lippe9b2f8712020-02-12 17:46:22463 if (UI.UIUtils.isEditing() || contentElement.hasSelection()) {
Blink Reformat4c46d092018-04-07 15:32:37464 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34465 }
Sigurd Schneider45f32c32020-10-13 13:32:05466 this._expandTrace && this._expandTrace(stackTraceElement.classList.contains('hidden'));
Blink Reformat4c46d092018-04-07 15:32:37467 event.consume();
Sigurd Schneider45f32c32020-10-13 13:32:05468 };
Blink Reformat4c46d092018-04-07 15:32:37469
Sigurd Schneider45f32c32020-10-13 13:32:05470 clickableElement.addEventListener('click', toggleStackTrace, false);
Tim van der Lippe9b2f8712020-02-12 17:46:22471 if (this._message.type === SDK.ConsoleModel.MessageType.Trace) {
Erik Luo8ef5d0c2018-09-25 21:16:00472 this._expandTrace(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34473 }
Blink Reformat4c46d092018-04-07 15:32:37474
Sigurd Schneider45f32c32020-10-13 13:32:05475 // @ts-ignore
Erik Luo8ef5d0c2018-09-25 21:16:00476 toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true);
Blink Reformat4c46d092018-04-07 15:32:37477 return toggleElement;
478 }
479
Jan Scheffler19cd7ec2021-02-12 14:16:30480 _linkifyLocation(url: string, lineNumber: number, columnNumber: number): HTMLElement|null {
Sigurd Schneider45f32c32020-10-13 13:32:05481 const runtimeModel = this._message.runtimeModel();
482 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37483 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34484 }
Blink Reformat4c46d092018-04-07 15:32:37485 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05486 runtimeModel.target(), /* scriptId */ null, url, lineNumber,
487 {columnNumber, className: undefined, tabStop: undefined});
Blink Reformat4c46d092018-04-07 15:32:37488 }
489
Jan Scheffler19cd7ec2021-02-12 14:16:30490 _linkifyStackTraceTopFrame(stackTrace: Protocol.Runtime.StackTrace): HTMLElement|null {
Sigurd Schneider45f32c32020-10-13 13:32:05491 const runtimeModel = this._message.runtimeModel();
492 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37493 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34494 }
Sigurd Schneider45f32c32020-10-13 13:32:05495 return this._linkifier.linkifyStackTraceTopFrame(runtimeModel.target(), stackTrace);
Blink Reformat4c46d092018-04-07 15:32:37496 }
497
Jan Scheffler19cd7ec2021-02-12 14:16:30498 _linkifyScriptId(scriptId: string, url: string, lineNumber: number, columnNumber: number): HTMLElement|null {
Sigurd Schneider45f32c32020-10-13 13:32:05499 const runtimeModel = this._message.runtimeModel();
500 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37501 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34502 }
Blink Reformat4c46d092018-04-07 15:32:37503 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05504 runtimeModel.target(), scriptId, url, lineNumber, {columnNumber, className: undefined, tabStop: undefined});
Blink Reformat4c46d092018-04-07 15:32:37505 }
506
Jan Scheffler19cd7ec2021-02-12 14:16:30507 _format(rawParameters: (string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|undefined)[]):
508 HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37509 // This node is used like a Builder. Values are continually appended onto it.
Jan Scheffler19cd7ec2021-02-12 14:16:30510 const formattedResult = (document.createElement('span') as HTMLElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34511 if (this._messagePrefix) {
Pavel Feldman9f0f0a32018-12-18 02:09:13512 formattedResult.createChild('span').textContent = this._messagePrefix;
Tim van der Lippe1d6e57a2019-09-30 11:55:34513 }
514 if (!rawParameters.length) {
Blink Reformat4c46d092018-04-07 15:32:37515 return formattedResult;
Tim van der Lippe1d6e57a2019-09-30 11:55:34516 }
Blink Reformat4c46d092018-04-07 15:32:37517
518 // Formatting code below assumes that parameters are all wrappers whereas frontend console
519 // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
520 // FIXME: Only pass runtime wrappers here.
Sigurd Schneider8bfb4212020-10-27 10:27:37521 let parameters = rawParameters.map(parameterToRemoteObject(this._message.runtimeModel()));
Blink Reformat4c46d092018-04-07 15:32:37522
523 // There can be string log and string eval result. We distinguish between them based on message type.
524 const shouldFormatMessage =
Jan Scheffler19cd7ec2021-02-12 14:16:30525 SDK.RemoteObject.RemoteObject.type((parameters as SDK.RemoteObject.RemoteObject[])[0]) === 'string' &&
Tim van der Lippe9b2f8712020-02-12 17:46:22526 (this._message.type !== SDK.ConsoleModel.MessageType.Result ||
527 this._message.level === SDK.ConsoleModel.MessageLevel.Error);
Blink Reformat4c46d092018-04-07 15:32:37528
529 // Multiple parameters with the first being a format string. Save unused substitutions.
530 if (shouldFormatMessage) {
531 const result = this._formatWithSubstitutionString(
Jan Scheffler19cd7ec2021-02-12 14:16:30532 (parameters[0].description as string), parameters.slice(1), formattedResult);
Sigurd Schneider45f32c32020-10-13 13:32:05533 parameters = Array.from(result.unusedSubstitutions || []);
Tim van der Lippe1d6e57a2019-09-30 11:55:34534 if (parameters.length) {
Sigurd Schneider23c52972020-10-13 09:31:14535 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34536 }
Blink Reformat4c46d092018-04-07 15:32:37537 }
538
539 // Single parameter, or unused substitutions from above.
540 for (let i = 0; i < parameters.length; ++i) {
541 // Inline strings when formatting.
Tim van der Lippe1d6e57a2019-09-30 11:55:34542 if (shouldFormatMessage && parameters[i].type === 'string') {
Sigurd Schneider45f32c32020-10-13 13:32:05543 formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description || ''));
Tim van der Lippe1d6e57a2019-09-30 11:55:34544 } else {
Blink Reformat4c46d092018-04-07 15:32:37545 formattedResult.appendChild(this._formatParameter(parameters[i], false, true));
Tim van der Lippe1d6e57a2019-09-30 11:55:34546 }
547 if (i < parameters.length - 1) {
Sigurd Schneider23c52972020-10-13 09:31:14548 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34549 }
Blink Reformat4c46d092018-04-07 15:32:37550 }
551 return formattedResult;
552 }
553
Jan Scheffler19cd7ec2021-02-12 14:16:30554 _formatParameter(output: SDK.RemoteObject.RemoteObject, forceObjectFormat?: boolean, includePreview?: boolean):
555 HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:34556 if (output.customPreview()) {
Jan Scheffler19cd7ec2021-02-12 14:16:30557 return new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output).element as HTMLElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34558 }
Blink Reformat4c46d092018-04-07 15:32:37559
Alfonso Castaño20d22cd2020-10-28 16:23:58560 const outputType = forceObjectFormat ? 'object' : (output.subtype || output.type);
Blink Reformat4c46d092018-04-07 15:32:37561 let element;
Alfonso Castaño20d22cd2020-10-28 16:23:58562 switch (outputType) {
Blink Reformat4c46d092018-04-07 15:32:37563 case 'error':
564 element = this._formatParameterAsError(output);
565 break;
566 case 'function':
567 element = this._formatParameterAsFunction(output, includePreview);
568 break;
569 case 'array':
570 case 'arraybuffer':
571 case 'blob':
572 case 'dataview':
573 case 'generator':
574 case 'iterator':
575 case 'map':
576 case 'object':
577 case 'promise':
578 case 'proxy':
579 case 'set':
580 case 'typedarray':
581 case 'weakmap':
582 case 'weakset':
Benedikt Meurerdead3152020-12-07 08:43:40583 case 'webassemblymemory':
Blink Reformat4c46d092018-04-07 15:32:37584 element = this._formatParameterAsObject(output, includePreview);
585 break;
586 case 'node':
587 element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false);
588 break;
Alfonso Castaño20d22cd2020-10-28 16:23:58589 case 'trustedtype':
590 element = this._formatParameterAsObject(output, false);
591 break;
Blink Reformat4c46d092018-04-07 15:32:37592 case 'string':
593 element = this._formatParameterAsString(output);
594 break;
595 case 'boolean':
596 case 'date':
597 case 'null':
598 case 'number':
599 case 'regexp':
600 case 'symbol':
601 case 'undefined':
602 case 'bigint':
603 element = this._formatParameterAsValue(output);
604 break;
605 default:
606 element = this._formatParameterAsValue(output);
Alfonso Castaño20d22cd2020-10-28 16:23:58607 console.error(`Tried to format remote object of unknown type ${outputType}.`);
Blink Reformat4c46d092018-04-07 15:32:37608 }
Alfonso Castaño20d22cd2020-10-28 16:23:58609 element.classList.add(`object-value-${outputType}`);
Blink Reformat4c46d092018-04-07 15:32:37610 element.classList.add('source-code');
611 return element;
612 }
613
Jan Scheffler19cd7ec2021-02-12 14:16:30614 _formatParameterAsValue(obj: SDK.RemoteObject.RemoteObject): HTMLElement {
615 const result = (document.createElement('span') as HTMLElement);
Blink Reformat4c46d092018-04-07 15:32:37616 const description = obj.description || '';
Sigurd Schneider8f4ac862020-10-13 13:30:11617 if (description.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22618 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:11619 document.createElement('span'), description, getLongStringVisibleLength());
Connor Moody1a5c0d32019-12-19 07:23:36620 result.appendChild(propertyValue.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34621 } else {
Sigurd Schneider23c52972020-10-13 09:31:14622 UI.UIUtils.createTextChild(result, description);
Tim van der Lippe1d6e57a2019-09-30 11:55:34623 }
624 if (obj.objectId) {
Blink Reformat4c46d092018-04-07 15:32:37625 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34626 }
Blink Reformat4c46d092018-04-07 15:32:37627 return result;
628 }
629
Jan Scheffler19cd7ec2021-02-12 14:16:30630 _formatParameterAsTrustedType(obj: SDK.RemoteObject.RemoteObject): HTMLElement {
631 const result = (document.createElement('span') as HTMLElement);
Alfonso Castaño20d22cd2020-10-28 16:23:58632 const trustedContentSpan = document.createElement('span');
633 trustedContentSpan.appendChild(this._formatParameterAsString(obj));
634 trustedContentSpan.classList.add('object-value-string');
Alfonso Castañodfe8ca32020-10-29 13:03:09635 UI.UIUtils.createTextChild(result, `${obj.className} `);
Alfonso Castaño20d22cd2020-10-28 16:23:58636 result.appendChild(trustedContentSpan);
Alfonso Castaño20d22cd2020-10-28 16:23:58637 return result;
638 }
639
Jan Scheffler19cd7ec2021-02-12 14:16:30640 _formatParameterAsObject(obj: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement {
641 const titleElement = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09642 titleElement.classList.add('console-object');
Blink Reformat4c46d092018-04-07 15:32:37643 if (includePreview && obj.preview) {
644 titleElement.classList.add('console-object-preview');
645 this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
646 } else if (obj.type === 'function') {
647 const functionElement = titleElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22648 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
Blink Reformat4c46d092018-04-07 15:32:37649 titleElement.classList.add('object-value-function');
Alfonso Castaño20d22cd2020-10-28 16:23:58650 } else if (obj.subtype === 'trustedtype') {
651 titleElement.appendChild(this._formatParameterAsTrustedType(obj));
Blink Reformat4c46d092018-04-07 15:32:37652 } else {
Sigurd Schneider23c52972020-10-13 09:31:14653 UI.UIUtils.createTextChild(titleElement, obj.description || '');
Blink Reformat4c46d092018-04-07 15:32:37654 }
655
Tim van der Lippe1d6e57a2019-09-30 11:55:34656 if (!obj.hasChildren || obj.customPreview()) {
Blink Reformat4c46d092018-04-07 15:32:37657 return titleElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34658 }
Blink Reformat4c46d092018-04-07 15:32:37659
660 const note = titleElement.createChild('span', 'object-state-note info-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22661 if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) {
Christy Chen9c6d8982021-02-08 02:28:31662 UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWillNotBeCollectedUntil));
Tim van der Lippe1d6e57a2019-09-30 11:55:34663 } else {
Christy Chen9c6d8982021-02-08 02:28:31664 UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWasEvaluatedUponFirst));
Tim van der Lippe1d6e57a2019-09-30 11:55:34665 }
Blink Reformat4c46d092018-04-07 15:32:37666
Tim van der Lippe9b2f8712020-02-12 17:46:22667 const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier);
Blink Reformat4c46d092018-04-07 15:32:37668 section.element.classList.add('console-view-object-properties-section');
669 section.enableContextMenu();
Erik Luocc14b812018-11-03 01:33:09670 section.setShowSelectionOnKeyboardFocus(true, true);
Erik Luo383f21d2018-11-07 23:16:37671 this._selectableChildren.push(section);
Erik Luo840be6b2018-12-03 20:54:27672 section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
673 section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
674 section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
Blink Reformat4c46d092018-04-07 15:32:37675 return section.element;
676 }
677
Jan Scheffler19cd7ec2021-02-12 14:16:30678 _formatParameterAsFunction(func: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement {
679 const result = (document.createElement('span') as HTMLElement);
Tim van der Lippe9b2f8712020-02-12 17:46:22680 SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37681 return result;
682
Jan Scheffler19cd7ec2021-02-12 14:16:30683 function formatTargetFunction(this: ConsoleViewMessage, targetFunction: SDK.RemoteObject.RemoteObject): void {
Sigurd Schneider53f33522020-10-08 15:00:49684 const functionElement = document.createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22685 const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(
Joey Arhard78a58f2018-12-05 01:59:45686 targetFunction, functionElement, true, includePreview);
Blink Reformat4c46d092018-04-07 15:32:37687 result.appendChild(functionElement);
688 if (targetFunction !== func) {
689 const note = result.createChild('span', 'object-info-state-note');
Christy Chen9c6d8982021-02-08 02:28:31690 UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.functionWasResolvedFromBound));
Blink Reformat4c46d092018-04-07 15:32:37691 }
692 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false);
Joey Arhard78a58f2018-12-05 01:59:45693 promise.then(() => this._formattedParameterAsFunctionForTest());
Blink Reformat4c46d092018-04-07 15:32:37694 }
695 }
696
Jan Scheffler19cd7ec2021-02-12 14:16:30697 _formattedParameterAsFunctionForTest(): void {
Joey Arhard78a58f2018-12-05 01:59:45698 }
699
Jan Scheffler19cd7ec2021-02-12 14:16:30700 _contextMenuEventFired(obj: SDK.RemoteObject.RemoteObject, event: Event): void {
Tim van der Lippe9b2f8712020-02-12 17:46:22701 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37702 contextMenu.appendApplicableItems(obj);
703 contextMenu.show();
704 }
705
Jan Scheffler19cd7ec2021-02-12 14:16:30706 _renderPropertyPreviewOrAccessor(
707 object: SDK.RemoteObject.RemoteObject|null, property: Protocol.Runtime.PropertyPreview, propertyPath: {
708 name: (string|symbol),
709 }[]): HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:34710 if (property.type === 'accessor') {
Sigurd Schneider45f32c32020-10-13 13:32:05711 return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34712 }
Blink Reformat4c46d092018-04-07 15:32:37713 return this._previewFormatter.renderPropertyPreview(
Alfonso Castaño20d22cd2020-10-28 16:23:58714 property.type, 'subtype' in property ? property.subtype : undefined, null, property.value);
Blink Reformat4c46d092018-04-07 15:32:37715 }
716
Jan Scheffler19cd7ec2021-02-12 14:16:30717 _formatParameterAsNode(remoteObject: SDK.RemoteObject.RemoteObject): HTMLElement {
718 const result = document.createElement('span');
Blink Reformat4c46d092018-04-07 15:32:37719
Tim van der Lippe9b2f8712020-02-12 17:46:22720 const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:34721 if (!domModel) {
Blink Reformat4c46d092018-04-07 15:32:37722 return result;
Tim van der Lippe1d6e57a2019-09-30 11:55:34723 }
Jan Scheffler19cd7ec2021-02-12 14:16:30724 domModel.pushObjectAsNodeToFrontend(remoteObject).then(async (node: SDK.DOMModel.DOMNode|null) => {
Blink Reformat4c46d092018-04-07 15:32:37725 if (!node) {
726 result.appendChild(this._formatParameterAsObject(remoteObject, false));
727 return;
728 }
Jan Scheffler19cd7ec2021-02-12 14:16:30729 const renderResult = await UI.UIUtils.Renderer.render((node as Object));
Erik Luofc6a6302018-11-02 06:48:52730 if (renderResult) {
Erik Luo840be6b2018-12-03 20:54:27731 if (renderResult.tree) {
Erik Luo383f21d2018-11-07 23:16:37732 this._selectableChildren.push(renderResult.tree);
Erik Luo840be6b2018-12-03 20:54:27733 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
734 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
735 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
736 }
Erik Luofc6a6302018-11-02 06:48:52737 result.appendChild(renderResult.node);
738 } else {
739 result.appendChild(this._formatParameterAsObject(remoteObject, false));
740 }
Erik Luo54fdd912018-11-01 17:57:01741 this._formattedParameterAsNodeForTest();
Blink Reformat4c46d092018-04-07 15:32:37742 });
743
744 return result;
745 }
746
Jan Scheffler19cd7ec2021-02-12 14:16:30747 _formattedParameterAsNodeForTest(): void {
Blink Reformat4c46d092018-04-07 15:32:37748 }
749
Jan Scheffler19cd7ec2021-02-12 14:16:30750 _formatParameterAsString(output: SDK.RemoteObject.RemoteObject): HTMLElement {
Benedikt Meurer62b49da2021-02-18 09:15:55751 // Properly escape double quotes here, so users don't get surprised
752 // when they copy strings from the console (https://crbug.com/1178530).
753 const description = output.description ?? '';
754 const text = JSON.stringify(description);
Jan Scheffler19cd7ec2021-02-12 14:16:30755 const result = (document.createElement('span') as HTMLElement);
Benedikt Meurer62b49da2021-02-18 09:15:55756 result.appendChild(this._linkifyStringAsFragment(text));
Blink Reformat4c46d092018-04-07 15:32:37757 return result;
758 }
759
Jan Scheffler19cd7ec2021-02-12 14:16:30760 _formatParameterAsError(output: SDK.RemoteObject.RemoteObject): HTMLElement {
761 const result = (document.createElement('span') as HTMLElement);
Blink Reformat4c46d092018-04-07 15:32:37762 const errorSpan = this._tryFormatAsError(output.description || '');
Erik Luo383f21d2018-11-07 23:16:37763 result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37764 return result;
765 }
766
Jan Scheffler19cd7ec2021-02-12 14:16:30767 _formatAsArrayEntry(output: SDK.RemoteObject.RemoteObject): HTMLElement {
Alfonso Castaño20d22cd2020-10-28 16:23:58768 return this._previewFormatter.renderPropertyPreview(
769 output.type, output.subtype, output.className, output.description);
Blink Reformat4c46d092018-04-07 15:32:37770 }
771
Jan Scheffler19cd7ec2021-02-12 14:16:30772 _formatAsAccessorProperty(object: SDK.RemoteObject.RemoteObject|null, propertyPath: string[], isArrayEntry: boolean):
773 HTMLElement {
Tim van der Lippe9b2f8712020-02-12 17:46:22774 const rootElement =
775 ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
776 object, propertyPath, onInvokeGetterClick.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37777
Jan Scheffler19cd7ec2021-02-12 14:16:30778 function onInvokeGetterClick(this: ConsoleViewMessage, result: SDK.RemoteObject.CallFunctionResult): void {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18779 const wasThrown = result.wasThrown;
780 const object = result.object;
Tim van der Lippe1d6e57a2019-09-30 11:55:34781 if (!object) {
Blink Reformat4c46d092018-04-07 15:32:37782 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34783 }
Blink Reformat4c46d092018-04-07 15:32:37784 rootElement.removeChildren();
785 if (wasThrown) {
786 const element = rootElement.createChild('span');
Christy Chen9c6d8982021-02-08 02:28:31787 element.textContent = i18nString(UIStrings.exception);
Jan Scheffler19cd7ec2021-02-12 14:16:30788 UI.Tooltip.Tooltip.install(element, (object.description as string));
Blink Reformat4c46d092018-04-07 15:32:37789 } else if (isArrayEntry) {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18790 rootElement.appendChild(this._formatAsArrayEntry(object));
Blink Reformat4c46d092018-04-07 15:32:37791 } else {
792 // Make a PropertyPreview from the RemoteObject similar to the backend logic.
793 const maxLength = 100;
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18794 const type = object.type;
795 const subtype = object.subtype;
Blink Reformat4c46d092018-04-07 15:32:37796 let description = '';
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18797 if (type !== 'function' && object.description) {
Alfonso Castaño20d22cd2020-10-28 16:23:58798 if (type === 'string' || subtype === 'regexp' || subtype === 'trustedtype') {
Tim van der Lippe213266c2021-01-18 15:48:31799 description = Platform.StringUtilities.trimMiddle(object.description, maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34800 } else {
Tim van der Lippe3cc3af32021-01-19 14:25:26801 description = Platform.StringUtilities.trimEndWithMaxLength(object.description, maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34802 }
Blink Reformat4c46d092018-04-07 15:32:37803 }
Alfonso Castaño20d22cd2020-10-28 16:23:58804 rootElement.appendChild(
805 this._previewFormatter.renderPropertyPreview(type, subtype, object.className, description));
Blink Reformat4c46d092018-04-07 15:32:37806 }
807 }
808
809 return rootElement;
810 }
811
Jan Scheffler19cd7ec2021-02-12 14:16:30812 _formatWithSubstitutionString(
813 format: string, parameters: SDK.RemoteObject.RemoteObject[], formattedResult: HTMLElement): {
814 formattedResult: Element,
815 unusedSubstitutions: ArrayLike<SDK.RemoteObject.RemoteObject>|null,
816 } {
817 function parameterFormatter(
818 this: ConsoleViewMessage, force: boolean, includePreview: boolean,
819 obj?: string|SDK.RemoteObject.RemoteObject): string|HTMLElement|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05820 if (obj instanceof SDK.RemoteObject.RemoteObject) {
821 return this._formatParameter(obj, force, includePreview);
822 }
823 return stringFormatter(obj);
Blink Reformat4c46d092018-04-07 15:32:37824 }
825
Jan Scheffler19cd7ec2021-02-12 14:16:30826 function stringFormatter(obj?: string|SDK.RemoteObject.RemoteObject): string|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05827 if (obj === undefined) {
828 return undefined;
829 }
830 if (typeof obj === 'string') {
831 return obj;
832 }
Blink Reformat4c46d092018-04-07 15:32:37833 return obj.description;
834 }
835
Jan Scheffler19cd7ec2021-02-12 14:16:30836 function floatFormatter(obj?: string|SDK.RemoteObject.RemoteObject): number|string|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05837 if (obj instanceof SDK.RemoteObject.RemoteObject) {
838 if (typeof obj.value !== 'number') {
839 return 'NaN';
840 }
841 return obj.value;
Tim van der Lippe1d6e57a2019-09-30 11:55:34842 }
Sigurd Schneider45f32c32020-10-13 13:32:05843 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37844 }
845
Jan Scheffler19cd7ec2021-02-12 14:16:30846 function integerFormatter(obj?: string|SDK.RemoteObject.RemoteObject): string|number|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05847 if (obj instanceof SDK.RemoteObject.RemoteObject) {
848 if (obj.type === 'bigint') {
849 return obj.description;
850 }
851 if (typeof obj.value !== 'number') {
852 return 'NaN';
853 }
854 return Math.floor(obj.value);
Tim van der Lippe1d6e57a2019-09-30 11:55:34855 }
Sigurd Schneider45f32c32020-10-13 13:32:05856 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37857 }
858
Jan Scheffler19cd7ec2021-02-12 14:16:30859 function bypassFormatter(obj?: string|SDK.RemoteObject.RemoteObject): Node|string {
Blink Reformat4c46d092018-04-07 15:32:37860 return (obj instanceof Node) ? obj : '';
861 }
862
Jan Scheffler19cd7ec2021-02-12 14:16:30863 let currentStyle: Map<string, {value: string, priority: string}>|null = null;
864 function styleFormatter(obj?: string|SDK.RemoteObject.RemoteObject): void {
Sigurd Schneider45f32c32020-10-13 13:32:05865 currentStyle = new Map();
Sigurd Schneider53f33522020-10-08 15:00:49866 const buffer = document.createElement('span');
Sigurd Schneider45f32c32020-10-13 13:32:05867 if (obj === undefined) {
868 return;
869 }
870 if (typeof obj === 'string' || !obj.description) {
871 return;
872 }
Blink Reformat4c46d092018-04-07 15:32:37873 buffer.setAttribute('style', obj.description);
Sigurd Schneider45f32c32020-10-13 13:32:05874 for (const property of buffer.style) {
Mathias Bynens5165a7a2020-06-10 05:51:43875 if (isAllowedProperty(property)) {
Sigurd Schneider45f32c32020-10-13 13:32:05876 const info = {
877 value: buffer.style.getPropertyValue(property),
Jan Scheffler19cd7ec2021-02-12 14:16:30878 priority: buffer.style.getPropertyPriority(property),
Sigurd Schneider45f32c32020-10-13 13:32:05879 };
880 currentStyle.set(property, info);
Tim van der Lippe1d6e57a2019-09-30 11:55:34881 }
Blink Reformat4c46d092018-04-07 15:32:37882 }
883 }
884
Jan Scheffler19cd7ec2021-02-12 14:16:30885 function isAllowedProperty(property: string): boolean {
Blink Reformat4c46d092018-04-07 15:32:37886 // Make sure that allowed properties do not interfere with link visibility.
887 const prefixes = [
Jan Scheffler19cd7ec2021-02-12 14:16:30888 'background',
889 'border',
890 'color',
891 'font',
892 'line',
893 'margin',
894 'padding',
895 'text',
896 '-webkit-background',
897 '-webkit-border',
898 '-webkit-font',
899 '-webkit-margin',
900 '-webkit-padding',
901 '-webkit-text',
Blink Reformat4c46d092018-04-07 15:32:37902 ];
Sigurd Schneider45f32c32020-10-13 13:32:05903 for (const prefix of prefixes) {
904 if (property.startsWith(prefix)) {
Blink Reformat4c46d092018-04-07 15:32:37905 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:34906 }
Blink Reformat4c46d092018-04-07 15:32:37907 }
908 return false;
909 }
910
Jan Scheffler19cd7ec2021-02-12 14:16:30911 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration)
912 // eslint-disable-next-line @typescript-eslint/no-explicit-any
913 const formatters: Record<string, Platform.StringUtilities.FormatterFunction<any>> = {};
Blink Reformat4c46d092018-04-07 15:32:37914 // Firebug uses %o for formatting objects.
915 formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */);
916 formatters.s = stringFormatter;
917 formatters.f = floatFormatter;
918 // Firebug allows both %i and %d for formatting integers.
919 formatters.i = integerFormatter;
920 formatters.d = integerFormatter;
921
922 // Firebug uses %c for styling the message.
923 formatters.c = styleFormatter;
924
925 // Support %O to force object formatting, instead of the type-based %o formatting.
926 formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */);
927
928 formatters._ = bypassFormatter;
929
Jan Scheffler19cd7ec2021-02-12 14:16:30930 function append(this: ConsoleViewMessage, a: HTMLElement, b?: string|Node): HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37931 if (b instanceof Node) {
932 a.appendChild(b);
Erik Luo17926392018-05-17 22:06:12933 return a;
934 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34935 if (typeof b === 'undefined') {
Erik Luo17926392018-05-17 22:06:12936 return a;
Tim van der Lippe1d6e57a2019-09-30 11:55:34937 }
Erik Luo17926392018-05-17 22:06:12938 if (!currentStyle) {
Erik Luo383f21d2018-11-07 23:16:37939 a.appendChild(this._linkifyStringAsFragment(String(b)));
Erik Luo17926392018-05-17 22:06:12940 return a;
941 }
942 const lines = String(b).split('\n');
943 for (let i = 0; i < lines.length; i++) {
944 const line = lines[i];
Erik Luo383f21d2018-11-07 23:16:37945 const lineFragment = this._linkifyStringAsFragment(line);
Jan Scheffler19cd7ec2021-02-12 14:16:30946 const wrapper = (document.createElement('span') as HTMLElement);
Erik Luo17926392018-05-17 22:06:12947 wrapper.style.setProperty('contain', 'paint');
948 wrapper.style.setProperty('display', 'inline-block');
949 wrapper.style.setProperty('max-width', '100%');
950 wrapper.appendChild(lineFragment);
951 applyCurrentStyle(wrapper);
952 for (const child of wrapper.children) {
Sigurd Schneider53f33522020-10-08 15:00:49953 if (child.classList.contains('devtools-link') && child instanceof HTMLElement) {
Erik Luo17926392018-05-17 22:06:12954 this._applyForcedVisibleStyle(child);
Tim van der Lippe1d6e57a2019-09-30 11:55:34955 }
Blink Reformat4c46d092018-04-07 15:32:37956 }
Erik Luo17926392018-05-17 22:06:12957 a.appendChild(wrapper);
Tim van der Lippe1d6e57a2019-09-30 11:55:34958 if (i < lines.length - 1) {
Sigurd Schneider53f33522020-10-08 15:00:49959 a.appendChild(document.createElement('br'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34960 }
Blink Reformat4c46d092018-04-07 15:32:37961 }
962 return a;
963 }
964
Jan Scheffler19cd7ec2021-02-12 14:16:30965 function applyCurrentStyle(element: HTMLElement): void {
Sigurd Schneider45f32c32020-10-13 13:32:05966 if (!currentStyle) {
967 return;
968 }
969 for (const [property, {value, priority}] of currentStyle.entries()) {
Jan Scheffler19cd7ec2021-02-12 14:16:30970 element.style.setProperty((property as string), value, priority);
Tim van der Lippe1d6e57a2019-09-30 11:55:34971 }
Blink Reformat4c46d092018-04-07 15:32:37972 }
973
Tim van der Lippe93b57c32020-02-20 17:38:44974 // Platform.StringUtilities.format does treat formattedResult like a Builder, result is an object.
975 return Platform.StringUtilities.format(format, parameters, formatters, formattedResult, append.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37976 }
977
Jan Scheffler19cd7ec2021-02-12 14:16:30978 _applyForcedVisibleStyle(element: HTMLElement): void {
Blink Reformat4c46d092018-04-07 15:32:37979 element.style.setProperty('-webkit-text-stroke', '0', 'important');
980 element.style.setProperty('text-decoration', 'underline', 'important');
981
Paul Lewisca569a52020-09-09 16:11:51982 const themedColor = ThemeSupport.ThemeSupport.instance().patchColorText(
983 'rgb(33%, 33%, 33%)', ThemeSupport.ThemeSupport.ColorUsage.Foreground);
Blink Reformat4c46d092018-04-07 15:32:37984 element.style.setProperty('color', themedColor, 'important');
985
986 let backgroundColor = 'hsl(0, 0%, 100%)';
Tim van der Lippe9b2f8712020-02-12 17:46:22987 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Blink Reformat4c46d092018-04-07 15:32:37988 backgroundColor = 'hsl(0, 100%, 97%)';
Tim van der Lippe9b2f8712020-02-12 17:46:22989 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:37990 backgroundColor = 'hsl(50, 100%, 95%)';
Tim van der Lippe1d6e57a2019-09-30 11:55:34991 }
Paul Lewisca569a52020-09-09 16:11:51992 const themedBackgroundColor = ThemeSupport.ThemeSupport.instance().patchColorText(
993 backgroundColor, ThemeSupport.ThemeSupport.ColorUsage.Background);
Blink Reformat4c46d092018-04-07 15:32:37994 element.style.setProperty('background-color', themedBackgroundColor, 'important');
995 }
996
Jan Scheffler19cd7ec2021-02-12 14:16:30997 matchesFilterRegex(regexObject: RegExp): boolean {
Blink Reformat4c46d092018-04-07 15:32:37998 regexObject.lastIndex = 0;
Erik Luo5976c8c2018-07-24 02:03:09999 const contentElement = this.contentElement();
1000 const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : '';
Tim van der Lipped7cfd142021-01-07 12:17:241001 return (Boolean(anchorText) && regexObject.test(anchorText.trim())) ||
Erik Luo5976c8c2018-07-24 02:03:091002 regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
Blink Reformat4c46d092018-04-07 15:32:371003 }
1004
Jan Scheffler19cd7ec2021-02-12 14:16:301005 matchesFilterText(filter: string): boolean {
Blink Reformat4c46d092018-04-07 15:32:371006 const text = this.contentElement().deepTextContent();
1007 return text.toLowerCase().includes(filter.toLowerCase());
1008 }
1009
Jan Scheffler19cd7ec2021-02-12 14:16:301010 updateTimestamp(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341011 if (!this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371012 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341013 }
Blink Reformat4c46d092018-04-07 15:32:371014
Paul Lewis2d7d65c2020-03-16 17:26:301015 if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341016 if (!this._timestampElement) {
Jan Scheffler19cd7ec2021-02-12 14:16:301017 this._timestampElement = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:091018 this._timestampElement.classList.add('console-timestamp');
Tim van der Lippe1d6e57a2019-09-30 11:55:341019 }
Tim van der Lippe9b2f8712020-02-12 17:46:221020 this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' ';
Tim van der Lippe70842f32020-11-23 16:56:571021 UI.Tooltip.Tooltip.install(this._timestampElement, UI.UIUtils.formatTimestamp(this._message.timestamp, true));
Blink Reformat4c46d092018-04-07 15:32:371022 this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild);
1023 } else if (this._timestampElement) {
1024 this._timestampElement.remove();
Sigurd Schneider53e98632020-10-26 15:29:501025 this._timestampElement = null;
Blink Reformat4c46d092018-04-07 15:32:371026 }
Blink Reformat4c46d092018-04-07 15:32:371027 }
1028
Jan Scheffler19cd7ec2021-02-12 14:16:301029 nestingLevel(): number {
Blink Reformat4c46d092018-04-07 15:32:371030 return this._nestingLevel;
1031 }
1032
Jan Scheffler19cd7ec2021-02-12 14:16:301033 setInSimilarGroup(inSimilarGroup: boolean, isLast?: boolean): void {
Blink Reformat4c46d092018-04-07 15:32:371034 this._inSimilarGroup = inSimilarGroup;
Tim van der Lipped7cfd142021-01-07 12:17:241035 this._lastInSimilarGroup = inSimilarGroup && Boolean(isLast);
Blink Reformat4c46d092018-04-07 15:32:371036 if (this._similarGroupMarker && !inSimilarGroup) {
1037 this._similarGroupMarker.remove();
1038 this._similarGroupMarker = null;
1039 } else if (this._element && !this._similarGroupMarker && inSimilarGroup) {
Jan Scheffler19cd7ec2021-02-12 14:16:301040 this._similarGroupMarker = (document.createElement('div') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:091041 this._similarGroupMarker.classList.add('nesting-level-marker');
Blink Reformat4c46d092018-04-07 15:32:371042 this._element.insertBefore(this._similarGroupMarker, this._element.firstChild);
1043 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1044 }
1045 }
1046
Jan Scheffler19cd7ec2021-02-12 14:16:301047 isLastInSimilarGroup(): boolean {
Tim van der Lipped7cfd142021-01-07 12:17:241048 return Boolean(this._inSimilarGroup) && Boolean(this._lastInSimilarGroup);
Blink Reformat4c46d092018-04-07 15:32:371049 }
1050
Jan Scheffler19cd7ec2021-02-12 14:16:301051 resetCloseGroupDecorationCount(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341052 if (!this._closeGroupDecorationCount) {
Blink Reformat4c46d092018-04-07 15:32:371053 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341054 }
Blink Reformat4c46d092018-04-07 15:32:371055 this._closeGroupDecorationCount = 0;
1056 this._updateCloseGroupDecorations();
1057 }
1058
Jan Scheffler19cd7ec2021-02-12 14:16:301059 incrementCloseGroupDecorationCount(): void {
Blink Reformat4c46d092018-04-07 15:32:371060 ++this._closeGroupDecorationCount;
1061 this._updateCloseGroupDecorations();
1062 }
1063
Jan Scheffler19cd7ec2021-02-12 14:16:301064 _updateCloseGroupDecorations(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341065 if (!this._nestingLevelMarkers) {
Blink Reformat4c46d092018-04-07 15:32:371066 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341067 }
Blink Reformat4c46d092018-04-07 15:32:371068 for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) {
1069 const marker = this._nestingLevelMarkers[i];
1070 marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount);
1071 }
1072 }
1073
Jan Scheffler19cd7ec2021-02-12 14:16:301074 _focusedChildIndex(): number {
Tim van der Lippe1d6e57a2019-09-30 11:55:341075 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461076 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341077 }
Erik Luo383f21d2018-11-07 23:16:371078 return this._selectableChildren.findIndex(child => child.element.hasFocus());
Erik Luo0b8282e2018-10-08 20:37:461079 }
1080
Jan Scheffler19cd7ec2021-02-12 14:16:301081 _onKeyDown(event: KeyboardEvent): void {
Sigurd Schneider45f32c32020-10-13 13:32:051082 if (UI.UIUtils.isEditing() || !this._element || !this._element.hasFocus() || this._element.hasSelection()) {
Erik Luo8ef5d0c2018-09-25 21:16:001083 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341084 }
1085 if (this.maybeHandleOnKeyDown(event)) {
Erik Luo8ef5d0c2018-09-25 21:16:001086 event.consume(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:341087 }
Erik Luo8ef5d0c2018-09-25 21:16:001088 }
1089
Jan Scheffler19cd7ec2021-02-12 14:16:301090 maybeHandleOnKeyDown(event: KeyboardEvent): boolean {
Erik Luo8ef5d0c2018-09-25 21:16:001091 // Handle trace expansion.
Erik Luo0b8282e2018-10-08 20:37:461092 const focusedChildIndex = this._focusedChildIndex();
1093 const isWrapperFocused = focusedChildIndex === -1;
1094 if (this._expandTrace && isWrapperFocused) {
Erik Luo8ef5d0c2018-09-25 21:16:001095 if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) {
1096 this._expandTrace(!this._traceExpanded);
1097 return true;
1098 }
1099 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341100 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461101 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:341102 }
Erik Luo0b8282e2018-10-08 20:37:461103
1104 if (event.key === 'ArrowLeft') {
Sigurd Schneider45f32c32020-10-13 13:32:051105 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461106 return true;
1107 }
1108 if (event.key === 'ArrowRight') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341109 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461110 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341111 }
Erik Luo0b8282e2018-10-08 20:37:461112 }
1113 if (event.key === 'ArrowUp') {
Erik Luo182bece2018-11-29 03:15:221114 const firstVisibleChild = this._nearestVisibleChild(0);
1115 if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
Sigurd Schneider45f32c32020-10-13 13:32:051116 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461117 return true;
Mathias Bynensf06e8c02020-02-28 13:58:281118 }
1119 if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461120 return true;
1121 }
1122 }
1123 if (event.key === 'ArrowDown') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341124 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461125 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341126 }
1127 if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) {
Erik Luo0b8282e2018-10-08 20:37:461128 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341129 }
Erik Luo0b8282e2018-10-08 20:37:461130 }
Erik Luo8ef5d0c2018-09-25 21:16:001131 return false;
1132 }
1133
Jan Scheffler19cd7ec2021-02-12 14:16:301134 _selectNearestVisibleChild(fromIndex: number, backwards?: boolean): boolean {
Erik Luo182bece2018-11-29 03:15:221135 const nearestChild = this._nearestVisibleChild(fromIndex, backwards);
1136 if (nearestChild) {
Erik Luo31c21f62018-12-13 03:39:391137 nearestChild.forceSelect();
Erik Luo182bece2018-11-29 03:15:221138 return true;
1139 }
1140 return false;
1141 }
1142
Jan Scheffler19cd7ec2021-02-12 14:16:301143 _nearestVisibleChild(fromIndex: number, backwards?: boolean): {
1144 element: Element,
1145 forceSelect: () => void,
1146 }|null {
Erik Luo182bece2018-11-29 03:15:221147 const childCount = this._selectableChildren.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:341148 if (fromIndex < 0 || fromIndex >= childCount) {
Erik Luo182bece2018-11-29 03:15:221149 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341150 }
Erik Luo182bece2018-11-29 03:15:221151 const direction = backwards ? -1 : 1;
1152 let index = fromIndex;
1153
1154 while (!this._selectableChildren[index].element.offsetParent) {
1155 index += direction;
Tim van der Lippe1d6e57a2019-09-30 11:55:341156 if (index < 0 || index >= childCount) {
Erik Luo182bece2018-11-29 03:15:221157 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341158 }
Erik Luo182bece2018-11-29 03:15:221159 }
1160 return this._selectableChildren[index];
1161 }
1162
Jan Scheffler19cd7ec2021-02-12 14:16:301163 focusLastChildOrSelf(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341164 if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461165 this._element.focus();
Tim van der Lippe1d6e57a2019-09-30 11:55:341166 }
Erik Luo0b8282e2018-10-08 20:37:461167 }
1168
Jan Scheffler19cd7ec2021-02-12 14:16:301169 setContentElement(element: HTMLElement): void {
Sigurd Schneiderb2953b22020-10-09 09:30:151170 console.assert(!this._contentElement, 'Cannot set content element twice');
1171 this._contentElement = element;
1172 }
1173
Jan Scheffler19cd7ec2021-02-12 14:16:301174 getContentElement(): HTMLElement|null {
Sigurd Schneiderb2953b22020-10-09 09:30:151175 return this._contentElement;
1176 }
1177
Jan Scheffler19cd7ec2021-02-12 14:16:301178 contentElement(): HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:341179 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371180 return this._contentElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:341181 }
Blink Reformat4c46d092018-04-07 15:32:371182
Jan Scheffler19cd7ec2021-02-12 14:16:301183 const contentElement = (document.createElement('div') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:091184 contentElement.classList.add('console-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341185 if (this._messageLevelIcon) {
Blink Reformat4c46d092018-04-07 15:32:371186 contentElement.appendChild(this._messageLevelIcon);
Tim van der Lippe1d6e57a2019-09-30 11:55:341187 }
Blink Reformat4c46d092018-04-07 15:32:371188 this._contentElement = contentElement;
1189
Sigurd Schneider45f32c32020-10-13 13:32:051190 const runtimeModel = this._message.runtimeModel();
Blink Reformat4c46d092018-04-07 15:32:371191 let formattedMessage;
Tim van der Lipped7cfd142021-01-07 12:17:241192 const shouldIncludeTrace = Boolean(this._message.stackTrace) &&
Tim van der Lippe9b2f8712020-02-12 17:46:221193 (this._message.source === SDK.ConsoleModel.MessageSource.Network ||
1194 this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1195 this._message.level === SDK.ConsoleModel.MessageLevel.Error ||
1196 this._message.level === SDK.ConsoleModel.MessageLevel.Warning ||
1197 this._message.type === SDK.ConsoleModel.MessageType.Trace);
Sigurd Schneider45f32c32020-10-13 13:32:051198 if (runtimeModel && shouldIncludeTrace) {
1199 formattedMessage = this._buildMessageWithStackTrace(runtimeModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:341200 } else {
Blink Reformat4c46d092018-04-07 15:32:371201 formattedMessage = this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341202 }
Blink Reformat4c46d092018-04-07 15:32:371203 contentElement.appendChild(formattedMessage);
1204
1205 this.updateTimestamp();
1206 return this._contentElement;
1207 }
1208
Jan Scheffler19cd7ec2021-02-12 14:16:301209 toMessageElement(): HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:341210 if (this._element) {
Blink Reformat4c46d092018-04-07 15:32:371211 return this._element;
Tim van der Lippe1d6e57a2019-09-30 11:55:341212 }
Jan Scheffler19cd7ec2021-02-12 14:16:301213 this._element = (document.createElement('div') as HTMLElement);
Pavel Feldmandb310912019-01-30 00:31:201214 this._element.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:301215 this._element.addEventListener('keydown', (this._onKeyDown.bind(this) as EventListener));
Blink Reformat4c46d092018-04-07 15:32:371216 this.updateMessageElement();
1217 return this._element;
1218 }
1219
Jan Scheffler19cd7ec2021-02-12 14:16:301220 updateMessageElement(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341221 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371222 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341223 }
Blink Reformat4c46d092018-04-07 15:32:371224
1225 this._element.className = 'console-message-wrapper';
1226 this._element.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:341227 if (this._message.isGroupStartMessage()) {
Blink Reformat4c46d092018-04-07 15:32:371228 this._element.classList.add('console-group-title');
Tim van der Lippe1d6e57a2019-09-30 11:55:341229 }
Tim van der Lippe9b2f8712020-02-12 17:46:221230 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:371231 this._element.classList.add('console-from-api');
Tim van der Lippe1d6e57a2019-09-30 11:55:341232 }
Blink Reformat4c46d092018-04-07 15:32:371233 if (this._inSimilarGroup) {
Jan Scheffler19cd7ec2021-02-12 14:16:301234 this._similarGroupMarker = (this._element.createChild('div', 'nesting-level-marker') as HTMLElement);
Blink Reformat4c46d092018-04-07 15:32:371235 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1236 }
1237
1238 this._nestingLevelMarkers = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341239 for (let i = 0; i < this._nestingLevel; ++i) {
Blink Reformat4c46d092018-04-07 15:32:371240 this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker'));
Tim van der Lippe1d6e57a2019-09-30 11:55:341241 }
Blink Reformat4c46d092018-04-07 15:32:371242 this._updateCloseGroupDecorations();
Sigurd Schneiderca7b4ff2020-10-14 07:45:471243 elementToMessage.set(this._element, this);
Blink Reformat4c46d092018-04-07 15:32:371244
1245 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221246 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371247 this._element.classList.add('console-verbose-level');
Blink Reformat4c46d092018-04-07 15:32:371248 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221249 case SDK.ConsoleModel.MessageLevel.Info:
Blink Reformat4c46d092018-04-07 15:32:371250 this._element.classList.add('console-info-level');
Tim van der Lippe9b2f8712020-02-12 17:46:221251 if (this._message.type === SDK.ConsoleModel.MessageType.System) {
Blink Reformat4c46d092018-04-07 15:32:371252 this._element.classList.add('console-system-type');
Tim van der Lippe1d6e57a2019-09-30 11:55:341253 }
Blink Reformat4c46d092018-04-07 15:32:371254 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221255 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371256 this._element.classList.add('console-warning-level');
Blink Reformat4c46d092018-04-07 15:32:371257 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221258 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371259 this._element.classList.add('console-error-level');
Blink Reformat4c46d092018-04-07 15:32:371260 break;
1261 }
Erik Luofd3e7d42018-09-25 02:12:351262 this._updateMessageLevelIcon();
Tim van der Lippe1d6e57a2019-09-30 11:55:341263 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371264 this._element.classList.add('console-warning-level');
Tim van der Lippe1d6e57a2019-09-30 11:55:341265 }
Blink Reformat4c46d092018-04-07 15:32:371266
1267 this._element.appendChild(this.contentElement());
Tim van der Lippe1d6e57a2019-09-30 11:55:341268 if (this._repeatCount > 1) {
Blink Reformat4c46d092018-04-07 15:32:371269 this._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341270 }
Blink Reformat4c46d092018-04-07 15:32:371271 }
1272
Jan Scheffler19cd7ec2021-02-12 14:16:301273 _shouldRenderAsWarning(): boolean {
Tim van der Lippe9b2f8712020-02-12 17:46:221274 return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose ||
1275 this._message.level === SDK.ConsoleModel.MessageLevel.Info) &&
1276 (this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1277 this._message.source === SDK.ConsoleModel.MessageSource.Deprecation ||
1278 this._message.source === SDK.ConsoleModel.MessageSource.Intervention ||
1279 this._message.source === SDK.ConsoleModel.MessageSource.Recommendation);
Blink Reformat4c46d092018-04-07 15:32:371280 }
1281
Jan Scheffler19cd7ec2021-02-12 14:16:301282 _updateMessageLevelIcon(): void {
Erik Luofd3e7d42018-09-25 02:12:351283 let iconType = '';
1284 let accessibleName = '';
Tim van der Lippe9b2f8712020-02-12 17:46:221285 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351286 iconType = 'smallicon-warning';
Christy Chen9c6d8982021-02-08 02:28:311287 accessibleName = i18nString(UIStrings.warning);
Tim van der Lippe9b2f8712020-02-12 17:46:221288 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351289 iconType = 'smallicon-error';
Christy Chen9c6d8982021-02-08 02:28:311290 accessibleName = i18nString(UIStrings.error);
Erik Luofd3e7d42018-09-25 02:12:351291 }
Sigurd Schneider45f32c32020-10-13 13:32:051292 if (!this._messageLevelIcon) {
1293 if (!iconType) {
1294 return;
1295 }
Tim van der Lippe9b2f8712020-02-12 17:46:221296 this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon');
Tim van der Lippe1d6e57a2019-09-30 11:55:341297 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371298 this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341299 }
Blink Reformat4c46d092018-04-07 15:32:371300 }
1301 this._messageLevelIcon.setIconType(iconType);
Erik Luofd3e7d42018-09-25 02:12:351302 UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371303 }
1304
Jan Scheffler19cd7ec2021-02-12 14:16:301305 repeatCount(): number {
Blink Reformat4c46d092018-04-07 15:32:371306 return this._repeatCount || 1;
1307 }
1308
Jan Scheffler19cd7ec2021-02-12 14:16:301309 resetIncrementRepeatCount(): void {
Blink Reformat4c46d092018-04-07 15:32:371310 this._repeatCount = 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341311 if (!this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371312 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341313 }
Blink Reformat4c46d092018-04-07 15:32:371314
1315 this._repeatCountElement.remove();
Tim van der Lippe1d6e57a2019-09-30 11:55:341316 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371317 this._contentElement.classList.remove('repeated-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341318 }
Sigurd Schneider53e98632020-10-26 15:29:501319 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:371320 }
1321
Jan Scheffler19cd7ec2021-02-12 14:16:301322 incrementRepeatCount(): void {
Blink Reformat4c46d092018-04-07 15:32:371323 this._repeatCount++;
1324 this._showRepeatCountElement();
1325 }
1326
Jan Scheffler19cd7ec2021-02-12 14:16:301327 setRepeatCount(repeatCount: number): void {
Blink Reformat4c46d092018-04-07 15:32:371328 this._repeatCount = repeatCount;
1329 this._showRepeatCountElement();
1330 }
Jan Scheffler19cd7ec2021-02-12 14:16:301331 _showRepeatCountElement(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341332 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371333 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341334 }
Blink Reformat4c46d092018-04-07 15:32:371335
1336 if (!this._repeatCountElement) {
Sigurd Schneider45f32c32020-10-13 13:32:051337 this._repeatCountElement =
Jan Scheffler19cd7ec2021-02-12 14:16:301338 (document.createElement('span', {is: 'dt-small-bubble'}) as UI.UIUtils.DevToolsSmallBubble);
Tim van der Lippeee954d42020-05-04 10:35:571339 this._repeatCountElement.classList.add('console-message-repeat-count');
Blink Reformat4c46d092018-04-07 15:32:371340 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221341 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371342 this._repeatCountElement.type = 'warning';
1343 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221344 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371345 this._repeatCountElement.type = 'error';
1346 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221347 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371348 this._repeatCountElement.type = 'verbose';
1349 break;
1350 default:
1351 this._repeatCountElement.type = 'info';
1352 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341353 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371354 this._repeatCountElement.type = 'warning';
Tim van der Lippe1d6e57a2019-09-30 11:55:341355 }
Blink Reformat4c46d092018-04-07 15:32:371356
1357 this._element.insertBefore(this._repeatCountElement, this._contentElement);
Sigurd Schneider45f32c32020-10-13 13:32:051358 this.contentElement().classList.add('repeated-message');
Blink Reformat4c46d092018-04-07 15:32:371359 }
Sigurd Schneider45f32c32020-10-13 13:32:051360 this._repeatCountElement.textContent = `${this._repeatCount}`;
Peter Marshall2bdcc642021-03-03 10:02:091361
1362 let accessibleName;
Tim van der Lippe9b2f8712020-02-12 17:46:221363 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Peter Marshall2bdcc642021-03-03 10:02:091364 accessibleName = i18nString(UIStrings.warningS, {n: this._repeatCount});
Tim van der Lippe9b2f8712020-02-12 17:46:221365 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Peter Marshall2bdcc642021-03-03 10:02:091366 accessibleName = i18nString(UIStrings.errorS, {n: this._repeatCount});
1367 } else {
1368 accessibleName = i18nString(UIStrings.repeatS, {n: this._repeatCount});
Tim van der Lippe1d6e57a2019-09-30 11:55:341369 }
Erik Luofd3e7d42018-09-25 02:12:351370 UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371371 }
1372
Jan Scheffler19cd7ec2021-02-12 14:16:301373 get text(): string {
Blink Reformat4c46d092018-04-07 15:32:371374 return this._message.messageText;
1375 }
1376
Jan Scheffler19cd7ec2021-02-12 14:16:301377 toExportString(): string {
Blink Reformat4c46d092018-04-07 15:32:371378 const lines = [];
1379 const nodes = this.contentElement().childTextNodes();
Tim van der Lippe9b2f8712020-02-12 17:46:221380 const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join('');
Tim van der Lippe1d6e57a2019-09-30 11:55:341381 for (let i = 0; i < this.repeatCount(); ++i) {
Blink Reformat4c46d092018-04-07 15:32:371382 lines.push(messageContent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341383 }
Blink Reformat4c46d092018-04-07 15:32:371384 return lines.join('\n');
1385 }
1386
Jan Scheffler19cd7ec2021-02-12 14:16:301387 setSearchRegex(regex: RegExp|null): void {
Sigurd Schneidere8e75cf2020-10-13 08:17:521388 if (this._searchHighlightNodeChanges && this._searchHighlightNodeChanges.length) {
1389 UI.UIUtils.revertDomChanges(this._searchHighlightNodeChanges);
Tim van der Lippe1d6e57a2019-09-30 11:55:341390 }
Blink Reformat4c46d092018-04-07 15:32:371391 this._searchRegex = regex;
1392 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:521393 this._searchHighlightNodeChanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341394 if (!this._searchRegex) {
Blink Reformat4c46d092018-04-07 15:32:371395 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341396 }
Blink Reformat4c46d092018-04-07 15:32:371397
1398 const text = this.contentElement().deepTextContent();
1399 let match;
1400 this._searchRegex.lastIndex = 0;
1401 const sourceRanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341402 while ((match = this._searchRegex.exec(text)) && match[0]) {
Tim van der Lippe9b2f8712020-02-12 17:46:221403 sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length));
Tim van der Lippe1d6e57a2019-09-30 11:55:341404 }
Blink Reformat4c46d092018-04-07 15:32:371405
1406 if (sourceRanges.length) {
1407 this._searchHighlightNodes =
Sigurd Schneidere8e75cf2020-10-13 08:17:521408 UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHighlightNodeChanges);
Blink Reformat4c46d092018-04-07 15:32:371409 }
1410 }
1411
Jan Scheffler19cd7ec2021-02-12 14:16:301412 searchRegex(): RegExp|null {
Blink Reformat4c46d092018-04-07 15:32:371413 return this._searchRegex;
1414 }
1415
Jan Scheffler19cd7ec2021-02-12 14:16:301416 searchCount(): number {
Blink Reformat4c46d092018-04-07 15:32:371417 return this._searchHighlightNodes.length;
1418 }
1419
Jan Scheffler19cd7ec2021-02-12 14:16:301420 searchHighlightNode(index: number): Element {
Blink Reformat4c46d092018-04-07 15:32:371421 return this._searchHighlightNodes[index];
1422 }
1423
Jan Scheffler19cd7ec2021-02-12 14:16:301424 _tryFormatAsError(string: string): HTMLElement|null {
1425 function startsWith(prefix: string): boolean {
Blink Reformat4c46d092018-04-07 15:32:371426 return string.startsWith(prefix);
1427 }
1428
Sigurd Schneider45f32c32020-10-13 13:32:051429 const runtimeModel = this._message.runtimeModel();
Sigurd Schneider8d0fe542021-03-17 12:28:291430 // TODO: Consider removing these in favor of a simpler regex.
Blink Reformat4c46d092018-04-07 15:32:371431 const errorPrefixes =
1432 ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError'];
Sigurd Schneider8d0fe542021-03-17 12:28:291433 if (!runtimeModel || !errorPrefixes.some(startsWith) && !/^[\w.]+Error\b/.test(string)) {
Blink Reformat4c46d092018-04-07 15:32:371434 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341435 }
Sigurd Schneider45f32c32020-10-13 13:32:051436 const debuggerModel = runtimeModel.debuggerModel();
1437 const baseURL = runtimeModel.target().inspectedURL();
Blink Reformat4c46d092018-04-07 15:32:371438
1439 const lines = string.split('\n');
1440 const links = [];
1441 let position = 0;
1442 for (let i = 0; i < lines.length; ++i) {
1443 position += i > 0 ? lines[i - 1].length + 1 : 0;
1444 const isCallFrameLine = /^\s*at\s/.test(lines[i]);
Tim van der Lippe1d6e57a2019-09-30 11:55:341445 if (!isCallFrameLine && links.length) {
Blink Reformat4c46d092018-04-07 15:32:371446 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341447 }
Blink Reformat4c46d092018-04-07 15:32:371448
Tim van der Lippe1d6e57a2019-09-30 11:55:341449 if (!isCallFrameLine) {
Blink Reformat4c46d092018-04-07 15:32:371450 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341451 }
Blink Reformat4c46d092018-04-07 15:32:371452
1453 let openBracketIndex = -1;
1454 let closeBracketIndex = -1;
Yang Guo39256bd2019-07-18 06:02:251455 const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g;
1456 const inBrackets = /\([^\)\(]+\)/g;
Jan Scheffler19cd7ec2021-02-12 14:16:301457 let lastMatch: RegExpExecArray|null = null;
Yang Guo39256bd2019-07-18 06:02:251458 let currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341459 while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251460 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341461 }
Yang Guo39256bd2019-07-18 06:02:251462 if (!lastMatch) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341463 while ((currentMatch = inBrackets.exec(lines[i]))) {
Yang Guo39256bd2019-07-18 06:02:251464 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341465 }
Yang Guo39256bd2019-07-18 06:02:251466 }
1467 if (lastMatch) {
1468 openBracketIndex = lastMatch.index;
1469 closeBracketIndex = lastMatch.index + lastMatch[0].length - 1;
Blink Reformat4c46d092018-04-07 15:32:371470 }
1471 const hasOpenBracket = openBracketIndex !== -1;
1472 const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3;
1473 const right = hasOpenBracket ? closeBracketIndex : lines[i].length;
1474 const linkCandidate = lines[i].substring(left, right);
Tim van der Lippe9b2f8712020-02-12 17:46:221475 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate);
Tim van der Lippe1d6e57a2019-09-30 11:55:341476 if (!splitResult) {
Blink Reformat4c46d092018-04-07 15:32:371477 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341478 }
Blink Reformat4c46d092018-04-07 15:32:371479
Tim van der Lippe1d6e57a2019-09-30 11:55:341480 if (splitResult.url === '<anonymous>') {
Blink Reformat4c46d092018-04-07 15:32:371481 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341482 }
Blink Reformat4c46d092018-04-07 15:32:371483 let url = parseOrScriptMatch(splitResult.url);
Tim van der Lippe9b2f8712020-02-12 17:46:221484 if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) {
1485 url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url));
Tim van der Lippe1d6e57a2019-09-30 11:55:341486 }
1487 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371488 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341489 }
Blink Reformat4c46d092018-04-07 15:32:371490
1491 links.push({
1492 url: url,
1493 positionLeft: position + left,
1494 positionRight: position + right,
1495 lineNumber: splitResult.lineNumber,
Jan Scheffler19cd7ec2021-02-12 14:16:301496 columnNumber: splitResult.columnNumber,
Blink Reformat4c46d092018-04-07 15:32:371497 });
1498 }
1499
Tim van der Lippe1d6e57a2019-09-30 11:55:341500 if (!links.length) {
Blink Reformat4c46d092018-04-07 15:32:371501 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341502 }
Blink Reformat4c46d092018-04-07 15:32:371503
Jan Scheffler19cd7ec2021-02-12 14:16:301504 const formattedResult = document.createElement('span');
Blink Reformat4c46d092018-04-07 15:32:371505 let start = 0;
1506 for (let i = 0; i < links.length; ++i) {
Erik Luo383f21d2018-11-07 23:16:371507 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft)));
Erik Luo182bece2018-11-29 03:15:221508 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:051509 debuggerModel.target(), null, links[i].url, links[i].lineNumber,
1510 {columnNumber: links[i].columnNumber, className: undefined, tabStop: undefined});
Erik Luo182bece2018-11-29 03:15:221511 scriptLocationLink.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:301512 this._selectableChildren.push({element: scriptLocationLink, forceSelect: (): void => scriptLocationLink.focus()});
Erik Luo182bece2018-11-29 03:15:221513 formattedResult.appendChild(scriptLocationLink);
Blink Reformat4c46d092018-04-07 15:32:371514 start = links[i].positionRight;
1515 }
1516
Tim van der Lippe1d6e57a2019-09-30 11:55:341517 if (start !== string.length) {
Erik Luo383f21d2018-11-07 23:16:371518 formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start)));
Tim van der Lippe1d6e57a2019-09-30 11:55:341519 }
Blink Reformat4c46d092018-04-07 15:32:371520
1521 return formattedResult;
1522
Jan Scheffler19cd7ec2021-02-12 14:16:301523 function parseOrScriptMatch(url: string|null): string|null {
Tim van der Lippe1d6e57a2019-09-30 11:55:341524 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371525 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341526 }
Tim van der Lippe9b2f8712020-02-12 17:46:221527 const parsedURL = Common.ParsedURL.ParsedURL.fromString(url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341528 if (parsedURL) {
Blink Reformat4c46d092018-04-07 15:32:371529 return parsedURL.url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341530 }
1531 if (debuggerModel.scriptsForSourceURL(url).length) {
Blink Reformat4c46d092018-04-07 15:32:371532 return url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341533 }
Blink Reformat4c46d092018-04-07 15:32:371534 return null;
1535 }
1536 }
1537
Jan Scheffler19cd7ec2021-02-12 14:16:301538 _linkifyWithCustomLinkifier(
1539 string: string, linkifier: (arg0: string, arg1: string, arg2?: number, arg3?: number) => Node): DocumentFragment {
Sigurd Schneider8f4ac862020-10-13 13:30:111540 if (string.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:221541 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:111542 document.createElement('span'), string, getLongStringVisibleLength());
Sigurd Schneider45f32c32020-10-13 13:32:051543 const fragment = document.createDocumentFragment();
Connor Moody1a5c0d32019-12-19 07:23:361544 fragment.appendChild(propertyValue.element);
1545 return fragment;
Tim van der Lippe1d6e57a2019-09-30 11:55:341546 }
Sigurd Schneider45f32c32020-10-13 13:32:051547 const container = document.createDocumentFragment();
Tim van der Lippeeaacb722020-01-10 12:16:001548 const tokens = ConsoleViewMessage._tokenizeMessageText(string);
Blink Reformat4c46d092018-04-07 15:32:371549 for (const token of tokens) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341550 if (!token.text) {
Erik Luofc2214f2018-11-21 19:54:581551 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341552 }
Blink Reformat4c46d092018-04-07 15:32:371553 switch (token.type) {
1554 case 'url': {
1555 const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text);
Tim van der Lippe9b2f8712020-02-12 17:46:221556 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL);
Kim-Anh Tran9e49a452020-02-17 09:46:101557 const sourceURL = Common.ParsedURL.ParsedURL.removeWasmFunctionInfoFromURL(splitResult.url);
Blink Reformat4c46d092018-04-07 15:32:371558 let linkNode;
Tim van der Lippe1d6e57a2019-09-30 11:55:341559 if (splitResult) {
Kim-Anh Tran9e49a452020-02-17 09:46:101560 linkNode = linkifier(token.text, sourceURL, splitResult.lineNumber, splitResult.columnNumber);
Tim van der Lippe1d6e57a2019-09-30 11:55:341561 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051562 linkNode = linkifier(token.text, '');
Tim van der Lippe1d6e57a2019-09-30 11:55:341563 }
Blink Reformat4c46d092018-04-07 15:32:371564 container.appendChild(linkNode);
1565 break;
1566 }
1567 default:
Sigurd Schneider45f32c32020-10-13 13:32:051568 container.appendChild(document.createTextNode(token.text));
Blink Reformat4c46d092018-04-07 15:32:371569 break;
1570 }
1571 }
1572 return container;
1573 }
1574
Jan Scheffler19cd7ec2021-02-12 14:16:301575 _linkifyStringAsFragment(string: string): DocumentFragment {
Erik Luofc2214f2018-11-21 19:54:581576 return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:051577 const options = {text, lineNumber, columnNumber};
Jan Scheffler19cd7ec2021-02-12 14:16:301578 const linkElement =
1579 Components.Linkifier.Linkifier.linkifyURL(url, (options as Components.Linkifier.LinkifyURLOptions));
Erik Luo383f21d2018-11-07 23:16:371580 linkElement.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:301581 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo383f21d2018-11-07 23:16:371582 return linkElement;
Blink Reformat4c46d092018-04-07 15:32:371583 });
1584 }
1585
Jan Scheffler19cd7ec2021-02-12 14:16:301586 static _tokenizeMessageText(string: string): {
1587 type?: string, text: string,
1588 }[] {
Sigurd Schneider30ac3dd2020-10-13 09:06:391589 const {tokenizerRegexes, tokenizerTypes} = getOrCreateTokenizers();
Sigurd Schneider8f4ac862020-10-13 13:30:111590 if (string.length > getMaxTokenizableStringLength()) {
Blink Reformat4c46d092018-04-07 15:32:371591 return [{text: string, type: undefined}];
Tim van der Lippe1d6e57a2019-09-30 11:55:341592 }
Sigurd Schneider30ac3dd2020-10-13 09:06:391593 const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, tokenizerRegexes);
1594 return results.map(result => ({text: result.value, type: tokenizerTypes[result.regexIndex]}));
Blink Reformat4c46d092018-04-07 15:32:371595 }
1596
Jan Scheffler19cd7ec2021-02-12 14:16:301597 groupKey(): string {
Tim van der Lippe1d6e57a2019-09-30 11:55:341598 if (!this._groupKey) {
Blink Reformat4c46d092018-04-07 15:32:371599 this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle();
Tim van der Lippe1d6e57a2019-09-30 11:55:341600 }
Blink Reformat4c46d092018-04-07 15:32:371601 return this._groupKey;
1602 }
1603
Jan Scheffler19cd7ec2021-02-12 14:16:301604 groupTitle(): string {
Tim van der Lippeeaacb722020-01-10 12:16:001605 const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText);
Blink Reformat4c46d092018-04-07 15:32:371606 const result = tokens.reduce((acc, token) => {
Jan Scheffler19cd7ec2021-02-12 14:16:301607 let text: Common.UIString.LocalizedString|string = token.text;
Tim van der Lippe1d6e57a2019-09-30 11:55:341608 if (token.type === 'url') {
Christy Chen9c6d8982021-02-08 02:28:311609 text = i18nString(UIStrings.url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341610 } else if (token.type === 'time') {
Christy Chen9c6d8982021-02-08 02:28:311611 text = i18nString(UIStrings.tookNms);
Tim van der Lippe1d6e57a2019-09-30 11:55:341612 } else if (token.type === 'event') {
Christy Chen9c6d8982021-02-08 02:28:311613 text = i18nString(UIStrings.someEvent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341614 } else if (token.type === 'milestone') {
Christy Chen9c6d8982021-02-08 02:28:311615 text = i18nString(UIStrings.Mxx);
Tim van der Lippe1d6e57a2019-09-30 11:55:341616 } else if (token.type === 'autofill') {
Christy Chen9c6d8982021-02-08 02:28:311617 text = i18nString(UIStrings.attribute);
Tim van der Lippe1d6e57a2019-09-30 11:55:341618 }
Blink Reformat4c46d092018-04-07 15:32:371619 return acc + text;
1620 }, '');
1621 return result.replace(/[%]o/g, '');
1622 }
Paul Lewisbf7aa3c2019-11-20 17:03:381623}
Blink Reformat4c46d092018-04-07 15:32:371624
Jan Scheffler19cd7ec2021-02-12 14:16:301625let tokenizerRegexes: RegExp[]|null = null;
1626let tokenizerTypes: string[]|null = null;
Sigurd Schneider30ac3dd2020-10-13 09:06:391627
Jan Scheffler19cd7ec2021-02-12 14:16:301628function getOrCreateTokenizers(): {
1629 tokenizerRegexes: Array<RegExp>,
1630 tokenizerTypes: Array<string>,
1631} {
Sigurd Schneider30ac3dd2020-10-13 09:06:391632 if (!tokenizerRegexes || !tokenizerTypes) {
1633 const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f';
1634 const linkStringRegex = new RegExp(
1635 '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes +
1636 '"\')}\\],:;.!?]',
1637 'u');
1638 const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/;
1639 const timeRegex = /took [\d]+ms/;
1640 const eventRegex = /'\w+' event/;
1641 const milestoneRegex = /\sM[6-7]\d/;
1642 const autofillRegex = /\(suggested: \"[\w-]+\"\)/;
Jan Scheffler19cd7ec2021-02-12 14:16:301643 const handlers = new Map<RegExp, string>();
Sigurd Schneider30ac3dd2020-10-13 09:06:391644 handlers.set(linkStringRegex, 'url');
1645 handlers.set(pathLineRegex, 'url');
1646 handlers.set(timeRegex, 'time');
1647 handlers.set(eventRegex, 'event');
1648 handlers.set(milestoneRegex, 'milestone');
1649 handlers.set(autofillRegex, 'autofill');
1650 tokenizerRegexes = Array.from(handlers.keys());
1651 tokenizerTypes = Array.from(handlers.values());
1652 return {tokenizerRegexes, tokenizerTypes};
1653 }
1654 return {tokenizerRegexes, tokenizerTypes};
1655}
1656
Paul Lewisbf7aa3c2019-11-20 17:03:381657export class ConsoleGroupViewMessage extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301658 _collapsed: boolean;
1659 _expandGroupIcon: UI.Icon.Icon|null;
1660 _onToggle: () => void;
1661
1662 constructor(
1663 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
1664 onToggle: () => void, onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Blink Reformat4c46d092018-04-07 15:32:371665 console.assert(consoleMessage.isGroupStartMessage());
Tim van der Lippeb45d9a02019-11-05 17:24:411666 super(consoleMessage, linkifier, nestingLevel, onResize);
Tim van der Lippe9b2f8712020-02-12 17:46:221667 this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed;
Blink Reformat4c46d092018-04-07 15:32:371668 this._expandGroupIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:001669 this._onToggle = onToggle;
Blink Reformat4c46d092018-04-07 15:32:371670 }
1671
Jan Scheffler19cd7ec2021-02-12 14:16:301672 _setCollapsed(collapsed: boolean): void {
Blink Reformat4c46d092018-04-07 15:32:371673 this._collapsed = collapsed;
Tim van der Lippe1d6e57a2019-09-30 11:55:341674 if (this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371675 this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down');
Tim van der Lippe1d6e57a2019-09-30 11:55:341676 }
Erik Luo8ef5d0c2018-09-25 21:16:001677 this._onToggle.call(null);
Blink Reformat4c46d092018-04-07 15:32:371678 }
1679
Jan Scheffler19cd7ec2021-02-12 14:16:301680 collapsed(): boolean {
Blink Reformat4c46d092018-04-07 15:32:371681 return this._collapsed;
1682 }
1683
Jan Scheffler19cd7ec2021-02-12 14:16:301684 maybeHandleOnKeyDown(event: KeyboardEvent): boolean {
Erik Luo0b8282e2018-10-08 20:37:461685 const focusedChildIndex = this._focusedChildIndex();
1686 if (focusedChildIndex === -1) {
1687 if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) {
1688 this._setCollapsed(!this._collapsed);
1689 return true;
1690 }
Erik Luo8ef5d0c2018-09-25 21:16:001691 }
1692 return super.maybeHandleOnKeyDown(event);
1693 }
1694
Jan Scheffler19cd7ec2021-02-12 14:16:301695 toMessageElement(): HTMLElement {
1696 let element: HTMLElement|null = this._element || null;
Sigurd Schneider45f32c32020-10-13 13:32:051697 if (!element) {
1698 element = super.toMessageElement();
Erik Luo8ef5d0c2018-09-25 21:16:001699 const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down';
Tim van der Lippe9b2f8712020-02-12 17:46:221700 this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon');
Erik Luob5bfff42018-09-20 02:52:391701 // Intercept focus to avoid highlight on click.
Sigurd Schneider45f32c32020-10-13 13:32:051702 this.contentElement().tabIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341703 if (this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371704 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341705 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051706 element.insertBefore(this._expandGroupIcon, this._contentElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:341707 }
Sigurd Schneider45f32c32020-10-13 13:32:051708 element.addEventListener('click', () => this._setCollapsed(!this._collapsed));
Blink Reformat4c46d092018-04-07 15:32:371709 }
Sigurd Schneider45f32c32020-10-13 13:32:051710 return element;
Blink Reformat4c46d092018-04-07 15:32:371711 }
1712
Jan Scheffler19cd7ec2021-02-12 14:16:301713 _showRepeatCountElement(): void {
Blink Reformat4c46d092018-04-07 15:32:371714 super._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341715 if (this._repeatCountElement && this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371716 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341717 }
Blink Reformat4c46d092018-04-07 15:32:371718 }
Paul Lewisbf7aa3c2019-11-20 17:03:381719}
Blink Reformat4c46d092018-04-07 15:32:371720
Sigurd Schneiderca7b4ff2020-10-14 07:45:471721export class ConsoleCommand extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301722 _formattedCommand: HTMLElement|null;
1723
1724 constructor(
1725 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
1726 onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471727 super(consoleMessage, linkifier, nestingLevel, onResize);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471728 this._formattedCommand = null;
1729 }
1730
Jan Scheffler19cd7ec2021-02-12 14:16:301731 contentElement(): HTMLElement {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471732 const contentElement = this.getContentElement();
1733 if (contentElement) {
1734 return contentElement;
1735 }
Jan Scheffler19cd7ec2021-02-12 14:16:301736 const newContentElement = (document.createElement('div') as HTMLElement);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471737 this.setContentElement(newContentElement);
1738 newContentElement.classList.add('console-user-command');
1739 const icon = UI.Icon.Icon.create('smallicon-user-command', 'command-result-icon');
1740 newContentElement.appendChild(icon);
1741
1742 elementToMessage.set(newContentElement, this);
Jan Scheffler19cd7ec2021-02-12 14:16:301743 this._formattedCommand = (document.createElement('span') as HTMLElement);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471744 this._formattedCommand.classList.add('source-code');
1745 this._formattedCommand.textContent = Platform.StringUtilities.replaceControlCharacters(this.text);
1746 newContentElement.appendChild(this._formattedCommand);
1747
1748 if (this._formattedCommand.textContent.length < MaxLengthToIgnoreHighlighter) {
Andres Olivaresf59e6de2021-02-18 13:10:251749 const javascriptSyntaxHighlighter = new TextEditor.SyntaxHighlighter.SyntaxHighlighter('text/javascript', true);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471750 javascriptSyntaxHighlighter.syntaxHighlightNode(this._formattedCommand).then(this._updateSearch.bind(this));
1751 } else {
1752 this._updateSearch();
1753 }
1754
1755 this.updateTimestamp();
1756 return newContentElement;
1757 }
1758
Jan Scheffler19cd7ec2021-02-12 14:16:301759 _updateSearch(): void {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471760 this.setSearchRegex(this.searchRegex());
1761 }
1762}
1763
1764export class ConsoleCommandResult extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301765 contentElement(): HTMLElement {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471766 const element = super.contentElement();
1767 if (!element.classList.contains('console-user-command-result')) {
1768 element.classList.add('console-user-command-result');
1769 if (this.consoleMessage().level === SDK.ConsoleModel.MessageLevel.Info) {
1770 const icon = UI.Icon.Icon.create('smallicon-command-result', 'command-result-icon');
1771 element.insertBefore(icon, element.firstChild);
1772 }
1773 }
1774 return element;
1775 }
1776}
1777
Sigurd Schneider8bfb4212020-10-27 10:27:371778export class ConsoleTableMessageView extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301779 _dataGrid: DataGrid.SortableDataGrid.SortableDataGrid<unknown>|null;
1780
1781 constructor(
1782 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
1783 onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Sigurd Schneider8bfb4212020-10-27 10:27:371784 super(consoleMessage, linkifier, nestingLevel, onResize);
1785 console.assert(consoleMessage.type === SDK.ConsoleModel.MessageType.Table);
Sigurd Schneider8bfb4212020-10-27 10:27:371786 this._dataGrid = null;
1787 }
1788
Jan Scheffler19cd7ec2021-02-12 14:16:301789 wasShown(): void {
Sigurd Schneider8bfb4212020-10-27 10:27:371790 if (this._dataGrid) {
1791 this._dataGrid.updateWidths();
1792 }
1793 super.wasShown();
1794 }
1795
Jan Scheffler19cd7ec2021-02-12 14:16:301796 onResize(): void {
Sigurd Schneider8bfb4212020-10-27 10:27:371797 if (!this.isVisible()) {
1798 return;
1799 }
1800 if (this._dataGrid) {
1801 this._dataGrid.onResize();
1802 }
1803 }
1804
Jan Scheffler19cd7ec2021-02-12 14:16:301805 contentElement(): HTMLElement {
Sigurd Schneider8bfb4212020-10-27 10:27:371806 const contentElement = this.getContentElement();
1807 if (contentElement) {
1808 return contentElement;
1809 }
1810
Jan Scheffler19cd7ec2021-02-12 14:16:301811 const newContentElement = (document.createElement('div') as HTMLElement);
Sigurd Schneider8bfb4212020-10-27 10:27:371812 newContentElement.classList.add('console-message');
1813 if (this._messageLevelIcon) {
1814 newContentElement.appendChild(this._messageLevelIcon);
1815 }
1816 this.setContentElement(newContentElement);
1817
1818 newContentElement.appendChild(this._buildTableMessage());
1819 this.updateTimestamp();
1820 return newContentElement;
1821 }
1822
Jan Scheffler19cd7ec2021-02-12 14:16:301823 _buildTableMessage(): HTMLElement {
1824 const formattedMessage = (document.createElement('span') as HTMLElement);
Sigurd Schneider8bfb4212020-10-27 10:27:371825 formattedMessage.classList.add('source-code');
1826 this._anchorElement = this._buildMessageAnchor();
1827 if (this._anchorElement) {
1828 formattedMessage.appendChild(this._anchorElement);
1829 }
1830
1831 const table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null;
1832 if (!table) {
1833 return this._buildMessage();
1834 }
1835 const actualTable = parameterToRemoteObject(this._message.runtimeModel())(table);
1836 if (!actualTable || !actualTable.preview) {
1837 return this._buildMessage();
1838 }
1839
1840 const rawValueColumnSymbol = Symbol('rawValueColumn');
Jan Scheffler19cd7ec2021-02-12 14:16:301841 const columnNames: (string|symbol)[] = [];
Sigurd Schneider8bfb4212020-10-27 10:27:371842 const preview = actualTable.preview;
1843 const rows = [];
1844 for (let i = 0; i < preview.properties.length; ++i) {
1845 const rowProperty = preview.properties[i];
Jan Scheffler19cd7ec2021-02-12 14:16:301846 let rowSubProperties: Protocol.Runtime.PropertyPreview[];
Sigurd Schneiderb393a432020-11-06 12:08:211847 if (rowProperty.valuePreview && rowProperty.valuePreview.properties.length) {
Sigurd Schneider8bfb4212020-10-27 10:27:371848 rowSubProperties = rowProperty.valuePreview.properties;
1849 } else if (rowProperty.value) {
Jan Scheffler19cd7ec2021-02-12 14:16:301850 rowSubProperties =
1851 [{name: rawValueColumnSymbol as unknown as string, type: rowProperty.type, value: rowProperty.value}];
Sigurd Schneider8bfb4212020-10-27 10:27:371852 } else {
1853 continue;
1854 }
1855
Jan Scheffler19cd7ec2021-02-12 14:16:301856 const rowValue = new Map<string|symbol, HTMLElement>();
Sigurd Schneider8bfb4212020-10-27 10:27:371857 const maxColumnsToRender = 20;
1858 for (let j = 0; j < rowSubProperties.length; ++j) {
1859 const cellProperty = rowSubProperties[j];
Jan Scheffler19cd7ec2021-02-12 14:16:301860 let columnRendered: true|boolean = columnNames.indexOf(cellProperty.name) !== -1;
Sigurd Schneider8bfb4212020-10-27 10:27:371861 if (!columnRendered) {
1862 if (columnNames.length === maxColumnsToRender) {
1863 continue;
1864 }
1865 columnRendered = true;
1866 columnNames.push(cellProperty.name);
1867 }
1868
1869 if (columnRendered) {
1870 const cellElement =
1871 this._renderPropertyPreviewOrAccessor(actualTable, cellProperty, [rowProperty, cellProperty]);
1872 cellElement.classList.add('console-message-nowrap-below');
1873 rowValue.set(cellProperty.name, cellElement);
1874 }
1875 }
1876 rows.push({rowName: rowProperty.name, rowValue});
1877 }
1878
1879 const flatValues = [];
1880 for (const {rowName, rowValue} of rows) {
1881 flatValues.push(rowName);
1882 for (let j = 0; j < columnNames.length; ++j) {
1883 flatValues.push(rowValue.get(columnNames[j]));
1884 }
1885 }
Christy Chen9c6d8982021-02-08 02:28:311886 columnNames.unshift(i18nString(UIStrings.index));
Sigurd Schneider8bfb4212020-10-27 10:27:371887 const columnDisplayNames =
Christy Chen9c6d8982021-02-08 02:28:311888 columnNames.map(name => name === rawValueColumnSymbol ? i18nString(UIStrings.value) : name.toString());
Sigurd Schneider8bfb4212020-10-27 10:27:371889
1890 if (flatValues.length) {
Christy Chen9c6d8982021-02-08 02:28:311891 this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create(
1892 columnDisplayNames, flatValues, i18nString(UIStrings.console));
Sigurd Schneider8bfb4212020-10-27 10:27:371893 if (this._dataGrid) {
1894 this._dataGrid.setStriped(true);
1895 this._dataGrid.setFocusable(false);
1896
1897 const formattedResult = document.createElement('span');
1898 formattedResult.classList.add('console-message-text');
1899 const tableElement = formattedResult.createChild('div', 'console-message-formatted-table');
1900 const dataGridContainer = tableElement.createChild('span');
1901 tableElement.appendChild(this._formatParameter(actualTable, true, false));
1902 dataGridContainer.appendChild(this._dataGrid.element);
1903 formattedMessage.appendChild(formattedResult);
1904 this._dataGrid.renderInline();
1905 }
1906 }
1907 return formattedMessage;
1908 }
1909
Jan Scheffler19cd7ec2021-02-12 14:16:301910 approximateFastHeight(): number {
Sigurd Schneider8bfb4212020-10-27 10:27:371911 const table = this._message.parameters && this._message.parameters[0];
1912 if (table && typeof table !== 'string' && table.preview) {
1913 return defaultConsoleRowHeight * table.preview.properties.length;
1914 }
1915 return defaultConsoleRowHeight;
1916 }
1917}
1918
Sigurd Schneiderca7b4ff2020-10-14 07:45:471919/**
1920 * The maximum length before strings are considered too long for syntax highlighting.
1921 * @const
Sigurd Schneiderca7b4ff2020-10-14 07:45:471922 */
Jan Scheffler19cd7ec2021-02-12 14:16:301923const MaxLengthToIgnoreHighlighter: number = 10000;
Sigurd Schneiderca7b4ff2020-10-14 07:45:471924
Blink Reformat4c46d092018-04-07 15:32:371925/**
1926 * @const
Blink Reformat4c46d092018-04-07 15:32:371927 */
Jan Scheffler19cd7ec2021-02-12 14:16:301928export const MaxLengthForLinks: number = 40;
Blink Reformat4c46d092018-04-07 15:32:371929
Sigurd Schneider17c74452021-02-15 12:14:101930let maxTokenizableStringLength = 10000;
1931let longStringVisibleLength = 5000;
Sigurd Schneider8f4ac862020-10-13 13:30:111932
Jan Scheffler19cd7ec2021-02-12 14:16:301933export const getMaxTokenizableStringLength = (): number => {
Sigurd Schneider17c74452021-02-15 12:14:101934 return maxTokenizableStringLength;
Sigurd Schneider8f4ac862020-10-13 13:30:111935};
1936
Jan Scheffler19cd7ec2021-02-12 14:16:301937export const setMaxTokenizableStringLength = (length: number): void => {
Sigurd Schneider17c74452021-02-15 12:14:101938 maxTokenizableStringLength = length;
Sigurd Schneider8f4ac862020-10-13 13:30:111939};
1940
Jan Scheffler19cd7ec2021-02-12 14:16:301941export const getLongStringVisibleLength = (): number => {
Sigurd Schneider17c74452021-02-15 12:14:101942 return longStringVisibleLength;
Sigurd Schneider8f4ac862020-10-13 13:30:111943};
1944
Jan Scheffler19cd7ec2021-02-12 14:16:301945export const setLongStringVisibleLength = (length: number): void => {
Sigurd Schneider17c74452021-02-15 12:14:101946 longStringVisibleLength = length;
Sigurd Schneider8f4ac862020-10-13 13:30:111947};