blob: 15dd03d2129b4291638614af3b332d3fc095c09d [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 Lippe586c8022021-03-18 15:18:2037import * as Components from '../../components/components.js';
Tim van der Lippe76961572021-04-06 10:48:0738import * as Common from '../../core/common/common.js';
Tim van der Lippebb352e62021-04-01 17:57:2839import * as i18n from '../../core/i18n/i18n.js';
Tim van der Lippeaa1ed7a2021-03-31 14:38:2740import * as Platform from '../../core/platform/platform.js';
Tim van der Lippee00b92f2021-03-31 16:52:1741import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippe959b6f02021-04-07 09:07:5942import * as Bindings from '../../models/bindings/bindings.js';
Tim van der Lippefca98ed2021-04-08 14:10:1443import * as TextUtils from '../../models/text_utils/text_utils.js';
Tim van der Lippe99aeaf32021-04-09 10:33:3444import * as Workspace from '../../models/workspace/workspace.js';
Tim van der Lippe586c8022021-03-18 15:18:2045import * as TextEditor from '../../text_editor/text_editor.js';
Tim van der Lippe586c8022021-03-18 15:18:2046import * as ThemeSupport from '../../theme_support/theme_support.js';
Tim van der Lippe8499fe22021-04-12 16:42:4747import * as DataGrid from '../../ui/legacy/components/data_grid/data_grid.js';
Tim van der Lippe0dd7a052021-04-12 16:44:2348import * as ObjectUI from '../../ui/legacy/components/object_ui/object_ui.js';
Tim van der Lippeaa61faf2021-04-07 15:32:0749import * as UI from '../../ui/legacy/legacy.js';
Tim van der Lippe9b2f8712020-02-12 17:46:2250
Tim van der Lippebfbb58f2021-02-25 17:34:1951import type {ConsoleViewportElement} from './ConsoleViewport.js';
Tim van der Lippeeaacb722020-01-10 12:16:0052
Simon Zündfbfd1072021-03-01 07:38:5353const UIStrings = {
Christy Chen9c6d8982021-02-08 02:28:3154 /**
Peter Marshallf295a4b2021-02-26 09:48:0355 * @description Message element text content in Console View Message of the Console panel. Shown
56 * when the user tried to run console.clear() but the 'Preserve log' option is enabled, which stops
57 * the log from being cleared.
Christy Chen9c6d8982021-02-08 02:28:3158 */
59 consoleclearWasPreventedDueTo: '`console.clear()` was prevented due to \'Preserve log\'',
60 /**
Peter Marshallb8bd00f2021-02-24 08:25:1861 * @description Text shown in the Console panel after the user has cleared the console, which
62 * removes all messages from the console so that it is empty.
Christy Chen9c6d8982021-02-08 02:28:3163 */
64 consoleWasCleared: 'Console was cleared',
65 /**
66 *@description Message element title in Console View Message of the Console panel
67 *@example {Ctrl+L} PH1
68 */
69 clearAllMessagesWithS: 'Clear all messages with {PH1}',
70 /**
71 *@description Message prefix in Console View Message of the Console panel
72 */
73 assertionFailed: 'Assertion failed: ',
74 /**
75 *@description Message text in Console View Message of the Console panel
76 *@example {console.log(1)} PH1
77 */
Peter Marshall40dd7d92021-02-19 11:07:3778 violationS: '`[Violation]` {PH1}',
Christy Chen9c6d8982021-02-08 02:28:3179 /**
80 *@description Message text in Console View Message of the Console panel
81 *@example {console.log(1)} PH1
82 */
Peter Marshall40dd7d92021-02-19 11:07:3783 interventionS: '`[Intervention]` {PH1}',
Christy Chen9c6d8982021-02-08 02:28:3184 /**
85 *@description Message text in Console View Message of the Console panel
86 *@example {console.log(1)} PH1
87 */
Peter Marshall40dd7d92021-02-19 11:07:3788 deprecationS: '`[Deprecation]` {PH1}',
Christy Chen9c6d8982021-02-08 02:28:3189 /**
90 *@description Note title in Console View Message of the Console panel
91 */
92 thisValueWillNotBeCollectedUntil: 'This value will not be collected until console is cleared.',
93 /**
94 *@description Note title in Console View Message of the Console panel
95 */
96 thisValueWasEvaluatedUponFirst: 'This value was evaluated upon first expanding. It may have changed since then.',
97 /**
98 *@description Note title in Console View Message of the Console panel
99 */
100 functionWasResolvedFromBound: 'Function was resolved from bound function.',
101 /**
Peter Marshallf625dc82021-03-02 08:10:57102 * @description Shown in the Console panel when an exception is thrown when trying to access a
103 * property on an object. Should be translated.
Christy Chen9c6d8982021-02-08 02:28:31104 */
105 exception: '<exception>',
106 /**
107 *@description Text to indicate an item is a warning
108 */
109 warning: 'Warning',
110 /**
111 *@description Text for errors
112 */
113 error: 'Error',
114 /**
Peter Marshall2bdcc642021-03-03 10:02:09115 * @description Announced by the screen reader to indicate how many times a particular message in
116 * the console was repeated.
Christy Chen9c6d8982021-02-08 02:28:31117 */
Peter Marshall2bdcc642021-03-03 10:02:09118 repeatS: '{n, plural, =1 {Repeated # time} other {Repeated # times}}',
Christy Chen9c6d8982021-02-08 02:28:31119 /**
Peter Marshall2bdcc642021-03-03 10:02:09120 * @description Announced by the screen reader to indicate how many times a particular warning
121 * message in the console was repeated.
Christy Chen9c6d8982021-02-08 02:28:31122 */
Peter Marshall2bdcc642021-03-03 10:02:09123 warningS: '{n, plural, =1 {Warning, Repeated # time} other {Warning, Repeated # times}}',
Christy Chen9c6d8982021-02-08 02:28:31124 /**
Peter Marshall2bdcc642021-03-03 10:02:09125 * @description Announced by the screen reader to indicate how many times a particular error
126 * message in the console was repeated.
Christy Chen9c6d8982021-02-08 02:28:31127 */
Peter Marshall2bdcc642021-03-03 10:02:09128 errorS: '{n, plural, =1 {Error, Repeated # time} other {Error, Repeated # times}}',
Christy Chen9c6d8982021-02-08 02:28:31129 /**
Tim van der Lippee4b96c62021-02-17 12:43:16130 *@description Text appended to grouped console messages that are related to URL requests
Christy Chen9c6d8982021-02-08 02:28:31131 */
132 url: '<URL>',
133 /**
Tim van der Lippee4b96c62021-02-17 12:43:16134 *@description Text appended to grouped console messages about tasks that took longer than N ms
Christy Chen9c6d8982021-02-08 02:28:31135 */
136 tookNms: 'took <N>ms',
137 /**
Tim van der Lippee4b96c62021-02-17 12:43:16138 *@description Text appended to grouped console messages about tasks that are related to some DOM event
Christy Chen9c6d8982021-02-08 02:28:31139 */
140 someEvent: '<some> event',
141 /**
Tim van der Lippee4b96c62021-02-17 12:43:16142 *@description Text appended to grouped console messages about tasks that are related to a particular milestone
Christy Chen9c6d8982021-02-08 02:28:31143 */
144 Mxx: ' M<XX>',
145 /**
Tim van der Lippee4b96c62021-02-17 12:43:16146 *@description Text appended to grouped console messages about tasks that are related to autofill completions
Christy Chen9c6d8982021-02-08 02:28:31147 */
148 attribute: '<attribute>',
149 /**
150 *@description Text for the index of something
151 */
152 index: '(index)',
153 /**
154 *@description Text for the value of something
155 */
156 value: 'Value',
157 /**
158 *@description Title of the Console tool
159 */
160 console: 'Console',
161};
Tim van der Lippe586c8022021-03-18 15:18:20162const str_ = i18n.i18n.registerUIStrings('panels/console/ConsoleViewMessage.ts', UIStrings);
Christy Chen9c6d8982021-02-08 02:28:31163const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Jan Scheffler19cd7ec2021-02-12 14:16:30164const elementToMessage = new WeakMap<Element, ConsoleViewMessage>();
Sigurd Schneiderca7b4ff2020-10-14 07:45:47165
Jan Scheffler19cd7ec2021-02-12 14:16:30166export const getMessageForElement = (element: Element): ConsoleViewMessage|undefined => {
Sigurd Schneiderca7b4ff2020-10-14 07:45:47167 return elementToMessage.get(element);
168};
169
Sigurd Schneider8bfb4212020-10-27 10:27:37170// This value reflects the 18px min-height of .console-message, plus the
171// 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
172const defaultConsoleRowHeight = 19;
173
Jan Scheffler19cd7ec2021-02-12 14:16:30174const parameterToRemoteObject = (runtimeModel: SDK.RuntimeModel.RuntimeModel|null): (
175 parameter?: SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|string) => SDK.RemoteObject.RemoteObject =>
176 (parameter?: string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject): SDK.RemoteObject.RemoteObject => {
Sigurd Schneider8bfb4212020-10-27 10:27:37177 if (parameter instanceof SDK.RemoteObject.RemoteObject) {
178 return parameter;
179 }
180 if (!runtimeModel) {
181 return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter);
182 }
183 if (typeof parameter === 'object') {
184 return runtimeModel.createRemoteObject(parameter);
185 }
186 return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
187 };
188
Jan Scheffler19cd7ec2021-02-12 14:16:30189export class ConsoleViewMessage implements ConsoleViewportElement {
190 _message: SDK.ConsoleModel.ConsoleMessage;
191 _linkifier: Components.Linkifier.Linkifier;
192 _repeatCount: number;
193 _closeGroupDecorationCount: number;
194 _nestingLevel: number;
195 _selectableChildren: {
196 element: HTMLElement,
197 forceSelect: () => void,
198 }[];
199 _messageResized: (arg0: Common.EventTarget.EventTargetEvent) => void;
200 _element: HTMLElement|null;
201 _previewFormatter: ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter;
202 _searchRegex: RegExp|null;
203 _messageLevelIcon: UI.Icon.Icon|null;
204 _traceExpanded: boolean;
205 _expandTrace: ((arg0: boolean) => void)|null;
206 _anchorElement: HTMLElement|null;
207 _contentElement: HTMLElement|null;
208 _nestingLevelMarkers: HTMLElement[]|null;
209 _searchHighlightNodes: Element[];
210 _searchHighlightNodeChanges: UI.UIUtils.HighlightChange[];
211 _isVisible: boolean;
212 _cachedHeight: number;
213 _messagePrefix: string;
214 _timestampElement: HTMLElement|null;
215 _inSimilarGroup: boolean;
216 _similarGroupMarker: HTMLElement|null;
217 _lastInSimilarGroup: boolean;
218 _groupKey: string;
219 _repeatCountElement: UI.UIUtils.DevToolsSmallBubble|null;
220
221 constructor(
222 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
223 onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Blink Reformat4c46d092018-04-07 15:32:37224 this._message = consoleMessage;
225 this._linkifier = linkifier;
Blink Reformat4c46d092018-04-07 15:32:37226 this._repeatCount = 1;
227 this._closeGroupDecorationCount = 0;
228 this._nestingLevel = nestingLevel;
Erik Luo383f21d2018-11-07 23:16:37229 this._selectableChildren = [];
Erik Luo840be6b2018-12-03 20:54:27230 this._messageResized = onResize;
Sigurd Schneider53e98632020-10-26 15:29:50231 this._element = null;
Blink Reformat4c46d092018-04-07 15:32:37232
Tim van der Lippe9b2f8712020-02-12 17:46:22233 this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter();
Blink Reformat4c46d092018-04-07 15:32:37234 this._searchRegex = null;
Blink Reformat4c46d092018-04-07 15:32:37235 this._messageLevelIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:00236 this._traceExpanded = false;
Erik Luo8ef5d0c2018-09-25 21:16:00237 this._expandTrace = null;
John Emaubb2897a2019-10-04 17:37:32238 this._anchorElement = null;
Sigurd Schneider45f32c32020-10-13 13:32:05239 this._contentElement = null;
Sigurd Schneider45f32c32020-10-13 13:32:05240 this._nestingLevelMarkers = null;
Sigurd Schneider45f32c32020-10-13 13:32:05241 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:52242 this._searchHighlightNodeChanges = [];
Sigurd Schneider53e98632020-10-26 15:29:50243 this._isVisible = false;
244 this._cachedHeight = 0;
245 this._messagePrefix = '';
Sigurd Schneider53e98632020-10-26 15:29:50246 this._timestampElement = null;
247 this._inSimilarGroup = false;
Sigurd Schneider53e98632020-10-26 15:29:50248 this._similarGroupMarker = null;
249 this._lastInSimilarGroup = false;
250 this._groupKey = '';
Sigurd Schneider53e98632020-10-26 15:29:50251 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:37252 }
253
Jan Scheffler19cd7ec2021-02-12 14:16:30254 element(): HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37255 return this.toMessageElement();
256 }
257
Jan Scheffler19cd7ec2021-02-12 14:16:30258 wasShown(): void {
Blink Reformat4c46d092018-04-07 15:32:37259 this._isVisible = true;
260 }
261
Jan Scheffler19cd7ec2021-02-12 14:16:30262 onResize(): void {
Blink Reformat4c46d092018-04-07 15:32:37263 }
264
Jan Scheffler19cd7ec2021-02-12 14:16:30265 willHide(): void {
Blink Reformat4c46d092018-04-07 15:32:37266 this._isVisible = false;
Erik Luo4b002322018-07-30 21:23:31267 this._cachedHeight = this.element().offsetHeight;
Blink Reformat4c46d092018-04-07 15:32:37268 }
269
Jan Scheffler19cd7ec2021-02-12 14:16:30270 isVisible(): boolean {
Sigurd Schneider8bfb4212020-10-27 10:27:37271 return this._isVisible;
272 }
273
Jan Scheffler19cd7ec2021-02-12 14:16:30274 fastHeight(): number {
Tim van der Lippe1d6e57a2019-09-30 11:55:34275 if (this._cachedHeight) {
Blink Reformat4c46d092018-04-07 15:32:37276 return this._cachedHeight;
Tim van der Lippe1d6e57a2019-09-30 11:55:34277 }
Sigurd Schneider8bfb4212020-10-27 10:27:37278 return this.approximateFastHeight();
279 }
280
Jan Scheffler19cd7ec2021-02-12 14:16:30281 approximateFastHeight(): number {
Blink Reformat4c46d092018-04-07 15:32:37282 return defaultConsoleRowHeight;
283 }
284
Jan Scheffler19cd7ec2021-02-12 14:16:30285 consoleMessage(): SDK.ConsoleModel.ConsoleMessage {
Blink Reformat4c46d092018-04-07 15:32:37286 return this._message;
287 }
288
Jan Scheffler19cd7ec2021-02-12 14:16:30289 _buildMessage(): HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37290 let messageElement;
Jan Scheffler19cd7ec2021-02-12 14:16:30291 let messageText: Common.UIString.LocalizedString|string = this._message.messageText;
Tim van der Lippe9b2f8712020-02-12 17:46:22292 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:37293 switch (this._message.type) {
Tim van der Lippe9b2f8712020-02-12 17:46:22294 case SDK.ConsoleModel.MessageType.Trace:
Blink Reformat4c46d092018-04-07 15:32:37295 messageElement = this._format(this._message.parameters || ['console.trace']);
296 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22297 case SDK.ConsoleModel.MessageType.Clear:
Tim van der Lippef49e2322020-05-01 15:03:09298 messageElement = document.createElement('span');
299 messageElement.classList.add('console-info');
Paul Lewis2d7d65c2020-03-16 17:26:30300 if (Common.Settings.Settings.instance().moduleSetting('preserveConsoleLog').get()) {
Christy Chen9c6d8982021-02-08 02:28:31301 messageElement.textContent = i18nString(UIStrings.consoleclearWasPreventedDueTo);
Tim van der Lippe1d6e57a2019-09-30 11:55:34302 } else {
Christy Chen9c6d8982021-02-08 02:28:31303 messageElement.textContent = i18nString(UIStrings.consoleWasCleared);
Tim van der Lippe1d6e57a2019-09-30 11:55:34304 }
Tim van der Lippe420e5e32020-11-23 16:58:46305 UI.Tooltip.Tooltip.install(
Christy Chen9c6d8982021-02-08 02:28:31306 messageElement, i18nString(UIStrings.clearAllMessagesWithS, {
Jan Scheffler19cd7ec2021-02-12 14:16:30307 PH1: UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction('console.clear'),
Christy Chen9c6d8982021-02-08 02:28:31308 }));
Blink Reformat4c46d092018-04-07 15:32:37309 break;
Tim van der Lippe9b2f8712020-02-12 17:46:22310 case SDK.ConsoleModel.MessageType.Dir: {
Blink Reformat4c46d092018-04-07 15:32:37311 const obj = this._message.parameters ? this._message.parameters[0] : undefined;
312 const args = ['%O', obj];
313 messageElement = this._format(args);
314 break;
315 }
Tim van der Lippe9b2f8712020-02-12 17:46:22316 case SDK.ConsoleModel.MessageType.Profile:
317 case SDK.ConsoleModel.MessageType.ProfileEnd:
Blink Reformat4c46d092018-04-07 15:32:37318 messageElement = this._format([messageText]);
319 break;
320 default: {
Sigurd Schneider45f32c32020-10-13 13:32:05321 if (this._message.type === SDK.ConsoleModel.MessageType.Assert) {
Christy Chen9c6d8982021-02-08 02:28:31322 this._messagePrefix = i18nString(UIStrings.assertionFailed);
Sigurd Schneider45f32c32020-10-13 13:32:05323 }
324 if (this._message.parameters && this._message.parameters.length === 1) {
325 const parameter = this._message.parameters[0];
326 if (typeof parameter !== 'string' && parameter.type === 'string') {
Jan Scheffler19cd7ec2021-02-12 14:16:30327 messageElement = this._tryFormatAsError((parameter.value as string));
Sigurd Schneider45f32c32020-10-13 13:32:05328 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34329 }
Blink Reformat4c46d092018-04-07 15:32:37330 const args = this._message.parameters || [messageText];
331 messageElement = messageElement || this._format(args);
332 }
333 }
334 } else {
Tim van der Lippe9b2f8712020-02-12 17:46:22335 if (this._message.source === SDK.ConsoleModel.MessageSource.Network) {
Erik Luofc2214f2018-11-21 19:54:58336 messageElement = this._formatAsNetworkRequest() || this._format([messageText]);
337 } else {
Jan Scheffler19cd7ec2021-02-12 14:16:30338 const messageInParameters = this._message.parameters && messageText === (this._message.parameters[0] as string);
Peter Marshall40dd7d92021-02-19 11:07:37339 // These terms are locked because the console message will not be translated anyway.
Tim van der Lippe9b2f8712020-02-12 17:46:22340 if (this._message.source === SDK.ConsoleModel.MessageSource.Violation) {
Christy Chen9c6d8982021-02-08 02:28:31341 messageText = i18nString(UIStrings.violationS, {PH1: messageText});
Tim van der Lippe9b2f8712020-02-12 17:46:22342 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Intervention) {
Christy Chen9c6d8982021-02-08 02:28:31343 messageText = i18nString(UIStrings.interventionS, {PH1: messageText});
Tim van der Lippe9b2f8712020-02-12 17:46:22344 } else if (this._message.source === SDK.ConsoleModel.MessageSource.Deprecation) {
Christy Chen9c6d8982021-02-08 02:28:31345 messageText = i18nString(UIStrings.deprecationS, {PH1: messageText});
Tim van der Lippe1d6e57a2019-09-30 11:55:34346 }
Blink Reformat4c46d092018-04-07 15:32:37347 const args = this._message.parameters || [messageText];
Tim van der Lippe1d6e57a2019-09-30 11:55:34348 if (messageInParameters) {
Blink Reformat4c46d092018-04-07 15:32:37349 args[0] = messageText;
Tim van der Lippe1d6e57a2019-09-30 11:55:34350 }
Blink Reformat4c46d092018-04-07 15:32:37351 messageElement = this._format(args);
352 }
353 }
354 messageElement.classList.add('console-message-text');
355
Jan Scheffler19cd7ec2021-02-12 14:16:30356 const formattedMessage = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09357 formattedMessage.classList.add('source-code');
Erik Luo5976c8c2018-07-24 02:03:09358 this._anchorElement = this._buildMessageAnchor();
Tim van der Lippe1d6e57a2019-09-30 11:55:34359 if (this._anchorElement) {
Erik Luo5976c8c2018-07-24 02:03:09360 formattedMessage.appendChild(this._anchorElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34361 }
Blink Reformat4c46d092018-04-07 15:32:37362 formattedMessage.appendChild(messageElement);
363 return formattedMessage;
364 }
365
Jan Scheffler19cd7ec2021-02-12 14:16:30366 _formatAsNetworkRequest(): HTMLElement|null {
Tim van der Lippe9b2f8712020-02-12 17:46:22367 const request = SDK.NetworkLog.NetworkLog.requestForConsoleMessage(this._message);
Tim van der Lippe1d6e57a2019-09-30 11:55:34368 if (!request) {
Erik Luofc2214f2018-11-21 19:54:58369 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34370 }
Jan Scheffler19cd7ec2021-02-12 14:16:30371 const messageElement = (document.createElement('span') as HTMLElement);
Tim van der Lippe9b2f8712020-02-12 17:46:22372 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Sigurd Schneider23c52972020-10-13 09:31:14373 UI.UIUtils.createTextChild(messageElement, request.requestMethod + ' ');
Tim van der Lippe9b2f8712020-02-12 17:46:22374 const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url());
Erik Luo182bece2018-11-29 03:15:22375 // Focus is handled by the viewport.
376 linkElement.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:30377 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22378 messageElement.appendChild(linkElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34379 if (request.failed) {
Sigurd Schneider23c52972020-10-13 09:31:14380 UI.UIUtils.createTextChildren(messageElement, ' ', request.localizedFailDescription || '');
Tim van der Lippe1d6e57a2019-09-30 11:55:34381 }
382 if (request.statusCode !== 0) {
Sigurd Schneider23c52972020-10-13 09:31:14383 UI.UIUtils.createTextChildren(messageElement, ' ', String(request.statusCode));
Tim van der Lippe1d6e57a2019-09-30 11:55:34384 }
385 if (request.statusText) {
Sigurd Schneider23c52972020-10-13 09:31:14386 UI.UIUtils.createTextChildren(messageElement, ' (', request.statusText, ')');
Tim van der Lippe1d6e57a2019-09-30 11:55:34387 }
Erik Luofc2214f2018-11-21 19:54:58388 } else {
Erik Luoad5f3942019-03-26 20:53:44389 const messageText = this._message.messageText;
390 const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:05391 const linkElement = url === request.url() ?
392 Components.Linkifier.Linkifier.linkifyRevealable(
Jan Scheffler19cd7ec2021-02-12 14:16:30393 (request as SDK.NetworkRequest.NetworkRequest), url, request.url()) :
Sigurd Schneider45f32c32020-10-13 13:32:05394 Components.Linkifier.Linkifier.linkifyURL(
Jan Scheffler19cd7ec2021-02-12 14:16:30395 url, ({text, lineNumber, columnNumber} as Components.Linkifier.LinkifyURLOptions));
Erik Luo182bece2018-11-29 03:15:22396 linkElement.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:30397 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22398 return linkElement;
399 });
Erik Luofc2214f2018-11-21 19:54:58400 messageElement.appendChild(fragment);
401 }
402 return messageElement;
403 }
404
Jan Scheffler19cd7ec2021-02-12 14:16:30405 _buildMessageAnchor(): HTMLElement|null {
406 const linkify = (message: SDK.ConsoleModel.ConsoleMessage): HTMLElement|null => {
Sigurd Schneider45f32c32020-10-13 13:32:05407 if (message.scriptId) {
408 return this._linkifyScriptId(message.scriptId, message.url || '', message.line, message.column);
409 }
410 if (message.stackTrace && message.stackTrace.callFrames.length) {
411 return this._linkifyStackTraceTopFrame(message.stackTrace);
412 }
413 if (message.url && message.url !== 'undefined') {
414 return this._linkifyLocation(message.url, message.line, message.column);
415 }
416 return null;
417 };
418 const anchorElement = linkify(this._message);
Blink Reformat4c46d092018-04-07 15:32:37419 // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
420 if (anchorElement) {
John Emauf7e30fb2019-10-04 19:12:32421 anchorElement.tabIndex = -1;
422 this._selectableChildren.push({
423 element: anchorElement,
Jan Scheffler19cd7ec2021-02-12 14:16:30424 forceSelect: (): void => anchorElement.focus(),
John Emauf7e30fb2019-10-04 19:12:32425 });
Jan Scheffler19cd7ec2021-02-12 14:16:30426 const anchorWrapperElement = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09427 anchorWrapperElement.classList.add('console-message-anchor');
Blink Reformat4c46d092018-04-07 15:32:37428 anchorWrapperElement.appendChild(anchorElement);
Sigurd Schneider23c52972020-10-13 09:31:14429 UI.UIUtils.createTextChild(anchorWrapperElement, ' ');
Blink Reformat4c46d092018-04-07 15:32:37430 return anchorWrapperElement;
431 }
432 return null;
433 }
434
Jan Scheffler19cd7ec2021-02-12 14:16:30435 _buildMessageWithStackTrace(runtimeModel: SDK.RuntimeModel.RuntimeModel): HTMLElement {
436 const toggleElement = (document.createElement('div') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09437 toggleElement.classList.add('console-message-stack-trace-toggle');
Blink Reformat4c46d092018-04-07 15:32:37438 const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
Brandon Waldermana004e2a2021-04-06 18:41:59439 UI.ARIAUtils.markAsTree(contentElement);
Blink Reformat4c46d092018-04-07 15:32:37440
441 const messageElement = this._buildMessage();
Tim van der Lippe9b2f8712020-02-12 17:46:22442 const icon = UI.Icon.Icon.create('smallicon-triangle-right', 'console-message-expand-icon');
Blink Reformat4c46d092018-04-07 15:32:37443 const clickableElement = contentElement.createChild('div');
Brandon Waldermana004e2a2021-04-06 18:41:59444 UI.ARIAUtils.markAsTreeitem(clickableElement);
445 UI.ARIAUtils.setExpanded(clickableElement, false);
Blink Reformat4c46d092018-04-07 15:32:37446 clickableElement.appendChild(icon);
Erik Luob5bfff42018-09-20 02:52:39447 // Intercept focus to avoid highlight on click.
448 clickableElement.tabIndex = -1;
Blink Reformat4c46d092018-04-07 15:32:37449 clickableElement.appendChild(messageElement);
450 const stackTraceElement = contentElement.createChild('div');
451 const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
Wolfgang Beyer08261c72021-03-09 09:21:34452 runtimeModel.target(), this._linkifier, {stackTrace: this._message.stackTrace, tabStops: undefined});
Erik Luo182bece2018-11-29 03:15:22453 stackTraceElement.appendChild(stackTracePreview.element);
454 for (const linkElement of stackTracePreview.links) {
Jan Scheffler19cd7ec2021-02-12 14:16:30455 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo182bece2018-11-29 03:15:22456 }
Blink Reformat4c46d092018-04-07 15:32:37457 stackTraceElement.classList.add('hidden');
Brandon Waldermana004e2a2021-04-06 18:41:59458 UI.ARIAUtils.markAsGroup(stackTraceElement);
Jan Scheffler19cd7ec2021-02-12 14:16:30459 this._expandTrace = (expand: boolean): void => {
Blink Reformat4c46d092018-04-07 15:32:37460 icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right');
461 stackTraceElement.classList.toggle('hidden', !expand);
Brandon Waldermana004e2a2021-04-06 18:41:59462 UI.ARIAUtils.setExpanded(clickableElement, expand);
Erik Luo8ef5d0c2018-09-25 21:16:00463 this._traceExpanded = expand;
464 };
Blink Reformat4c46d092018-04-07 15:32:37465
Jan Scheffler19cd7ec2021-02-12 14:16:30466 const toggleStackTrace = (event: Event): void => {
Tim van der Lippe9b2f8712020-02-12 17:46:22467 if (UI.UIUtils.isEditing() || contentElement.hasSelection()) {
Blink Reformat4c46d092018-04-07 15:32:37468 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34469 }
Sigurd Schneider45f32c32020-10-13 13:32:05470 this._expandTrace && this._expandTrace(stackTraceElement.classList.contains('hidden'));
Blink Reformat4c46d092018-04-07 15:32:37471 event.consume();
Sigurd Schneider45f32c32020-10-13 13:32:05472 };
Blink Reformat4c46d092018-04-07 15:32:37473
Sigurd Schneider45f32c32020-10-13 13:32:05474 clickableElement.addEventListener('click', toggleStackTrace, false);
Tim van der Lippe9b2f8712020-02-12 17:46:22475 if (this._message.type === SDK.ConsoleModel.MessageType.Trace) {
Erik Luo8ef5d0c2018-09-25 21:16:00476 this._expandTrace(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34477 }
Blink Reformat4c46d092018-04-07 15:32:37478
Sigurd Schneider45f32c32020-10-13 13:32:05479 // @ts-ignore
Erik Luo8ef5d0c2018-09-25 21:16:00480 toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true);
Blink Reformat4c46d092018-04-07 15:32:37481 return toggleElement;
482 }
483
Jan Scheffler19cd7ec2021-02-12 14:16:30484 _linkifyLocation(url: string, lineNumber: number, columnNumber: number): HTMLElement|null {
Sigurd Schneider45f32c32020-10-13 13:32:05485 const runtimeModel = this._message.runtimeModel();
486 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37487 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34488 }
Blink Reformat4c46d092018-04-07 15:32:37489 return this._linkifier.linkifyScriptLocation(
Sigurd Schneider45f32c32020-10-13 13:32:05490 runtimeModel.target(), /* scriptId */ null, url, lineNumber,
Philip Pfaffe068b01d2021-03-22 09:46:26491 {columnNumber, className: undefined, tabStop: undefined, inlineFrameIndex: 0});
Blink Reformat4c46d092018-04-07 15:32:37492 }
493
Jan Scheffler19cd7ec2021-02-12 14:16:30494 _linkifyStackTraceTopFrame(stackTrace: Protocol.Runtime.StackTrace): HTMLElement|null {
Sigurd Schneider45f32c32020-10-13 13:32:05495 const runtimeModel = this._message.runtimeModel();
496 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37497 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34498 }
Sigurd Schneider45f32c32020-10-13 13:32:05499 return this._linkifier.linkifyStackTraceTopFrame(runtimeModel.target(), stackTrace);
Blink Reformat4c46d092018-04-07 15:32:37500 }
501
Jan Scheffler19cd7ec2021-02-12 14:16:30502 _linkifyScriptId(scriptId: string, url: string, lineNumber: number, columnNumber: number): HTMLElement|null {
Sigurd Schneider45f32c32020-10-13 13:32:05503 const runtimeModel = this._message.runtimeModel();
504 if (!runtimeModel) {
Blink Reformat4c46d092018-04-07 15:32:37505 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34506 }
Blink Reformat4c46d092018-04-07 15:32:37507 return this._linkifier.linkifyScriptLocation(
Philip Pfaffe068b01d2021-03-22 09:46:26508 runtimeModel.target(), scriptId, url, lineNumber,
509 {columnNumber, className: undefined, tabStop: undefined, inlineFrameIndex: 0});
Blink Reformat4c46d092018-04-07 15:32:37510 }
511
Jan Scheffler19cd7ec2021-02-12 14:16:30512 _format(rawParameters: (string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|undefined)[]):
513 HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37514 // This node is used like a Builder. Values are continually appended onto it.
Jan Scheffler19cd7ec2021-02-12 14:16:30515 const formattedResult = (document.createElement('span') as HTMLElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34516 if (this._messagePrefix) {
Pavel Feldman9f0f0a32018-12-18 02:09:13517 formattedResult.createChild('span').textContent = this._messagePrefix;
Tim van der Lippe1d6e57a2019-09-30 11:55:34518 }
519 if (!rawParameters.length) {
Blink Reformat4c46d092018-04-07 15:32:37520 return formattedResult;
Tim van der Lippe1d6e57a2019-09-30 11:55:34521 }
Blink Reformat4c46d092018-04-07 15:32:37522
523 // Formatting code below assumes that parameters are all wrappers whereas frontend console
524 // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
525 // FIXME: Only pass runtime wrappers here.
Sigurd Schneider8bfb4212020-10-27 10:27:37526 let parameters = rawParameters.map(parameterToRemoteObject(this._message.runtimeModel()));
Blink Reformat4c46d092018-04-07 15:32:37527
528 // There can be string log and string eval result. We distinguish between them based on message type.
529 const shouldFormatMessage =
Jan Scheffler19cd7ec2021-02-12 14:16:30530 SDK.RemoteObject.RemoteObject.type((parameters as SDK.RemoteObject.RemoteObject[])[0]) === 'string' &&
Tim van der Lippe9b2f8712020-02-12 17:46:22531 (this._message.type !== SDK.ConsoleModel.MessageType.Result ||
532 this._message.level === SDK.ConsoleModel.MessageLevel.Error);
Blink Reformat4c46d092018-04-07 15:32:37533
534 // Multiple parameters with the first being a format string. Save unused substitutions.
535 if (shouldFormatMessage) {
536 const result = this._formatWithSubstitutionString(
Jan Scheffler19cd7ec2021-02-12 14:16:30537 (parameters[0].description as string), parameters.slice(1), formattedResult);
Sigurd Schneider45f32c32020-10-13 13:32:05538 parameters = Array.from(result.unusedSubstitutions || []);
Tim van der Lippe1d6e57a2019-09-30 11:55:34539 if (parameters.length) {
Sigurd Schneider23c52972020-10-13 09:31:14540 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34541 }
Blink Reformat4c46d092018-04-07 15:32:37542 }
543
544 // Single parameter, or unused substitutions from above.
545 for (let i = 0; i < parameters.length; ++i) {
546 // Inline strings when formatting.
Tim van der Lippe1d6e57a2019-09-30 11:55:34547 if (shouldFormatMessage && parameters[i].type === 'string') {
Sigurd Schneider45f32c32020-10-13 13:32:05548 formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description || ''));
Tim van der Lippe1d6e57a2019-09-30 11:55:34549 } else {
Blink Reformat4c46d092018-04-07 15:32:37550 formattedResult.appendChild(this._formatParameter(parameters[i], false, true));
Tim van der Lippe1d6e57a2019-09-30 11:55:34551 }
552 if (i < parameters.length - 1) {
Sigurd Schneider23c52972020-10-13 09:31:14553 UI.UIUtils.createTextChild(formattedResult, ' ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34554 }
Blink Reformat4c46d092018-04-07 15:32:37555 }
556 return formattedResult;
557 }
558
Jan Scheffler19cd7ec2021-02-12 14:16:30559 _formatParameter(output: SDK.RemoteObject.RemoteObject, forceObjectFormat?: boolean, includePreview?: boolean):
560 HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:34561 if (output.customPreview()) {
Jan Scheffler19cd7ec2021-02-12 14:16:30562 return new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output).element as HTMLElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34563 }
Blink Reformat4c46d092018-04-07 15:32:37564
Alfonso Castaño20d22cd2020-10-28 16:23:58565 const outputType = forceObjectFormat ? 'object' : (output.subtype || output.type);
Blink Reformat4c46d092018-04-07 15:32:37566 let element;
Alfonso Castaño20d22cd2020-10-28 16:23:58567 switch (outputType) {
Blink Reformat4c46d092018-04-07 15:32:37568 case 'error':
569 element = this._formatParameterAsError(output);
570 break;
571 case 'function':
572 element = this._formatParameterAsFunction(output, includePreview);
573 break;
574 case 'array':
575 case 'arraybuffer':
576 case 'blob':
577 case 'dataview':
578 case 'generator':
579 case 'iterator':
580 case 'map':
581 case 'object':
582 case 'promise':
583 case 'proxy':
584 case 'set':
585 case 'typedarray':
Benedikt Meurer5658dd22021-03-18 09:37:23586 case 'wasmvalue':
Blink Reformat4c46d092018-04-07 15:32:37587 case 'weakmap':
588 case 'weakset':
Benedikt Meurerdead3152020-12-07 08:43:40589 case 'webassemblymemory':
Blink Reformat4c46d092018-04-07 15:32:37590 element = this._formatParameterAsObject(output, includePreview);
591 break;
592 case 'node':
593 element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false);
594 break;
Alfonso Castaño20d22cd2020-10-28 16:23:58595 case 'trustedtype':
596 element = this._formatParameterAsObject(output, false);
597 break;
Blink Reformat4c46d092018-04-07 15:32:37598 case 'string':
599 element = this._formatParameterAsString(output);
600 break;
601 case 'boolean':
602 case 'date':
603 case 'null':
604 case 'number':
605 case 'regexp':
606 case 'symbol':
607 case 'undefined':
608 case 'bigint':
609 element = this._formatParameterAsValue(output);
610 break;
611 default:
612 element = this._formatParameterAsValue(output);
Alfonso Castaño20d22cd2020-10-28 16:23:58613 console.error(`Tried to format remote object of unknown type ${outputType}.`);
Blink Reformat4c46d092018-04-07 15:32:37614 }
Alfonso Castaño20d22cd2020-10-28 16:23:58615 element.classList.add(`object-value-${outputType}`);
Blink Reformat4c46d092018-04-07 15:32:37616 element.classList.add('source-code');
617 return element;
618 }
619
Jan Scheffler19cd7ec2021-02-12 14:16:30620 _formatParameterAsValue(obj: SDK.RemoteObject.RemoteObject): HTMLElement {
621 const result = (document.createElement('span') as HTMLElement);
Blink Reformat4c46d092018-04-07 15:32:37622 const description = obj.description || '';
Sigurd Schneider8f4ac862020-10-13 13:30:11623 if (description.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:22624 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:11625 document.createElement('span'), description, getLongStringVisibleLength());
Connor Moody1a5c0d32019-12-19 07:23:36626 result.appendChild(propertyValue.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34627 } else {
Sigurd Schneider23c52972020-10-13 09:31:14628 UI.UIUtils.createTextChild(result, description);
Tim van der Lippe1d6e57a2019-09-30 11:55:34629 }
630 if (obj.objectId) {
Blink Reformat4c46d092018-04-07 15:32:37631 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34632 }
Blink Reformat4c46d092018-04-07 15:32:37633 return result;
634 }
635
Jan Scheffler19cd7ec2021-02-12 14:16:30636 _formatParameterAsTrustedType(obj: SDK.RemoteObject.RemoteObject): HTMLElement {
637 const result = (document.createElement('span') as HTMLElement);
Alfonso Castaño20d22cd2020-10-28 16:23:58638 const trustedContentSpan = document.createElement('span');
639 trustedContentSpan.appendChild(this._formatParameterAsString(obj));
640 trustedContentSpan.classList.add('object-value-string');
Alfonso Castañodfe8ca32020-10-29 13:03:09641 UI.UIUtils.createTextChild(result, `${obj.className} `);
Alfonso Castaño20d22cd2020-10-28 16:23:58642 result.appendChild(trustedContentSpan);
Alfonso Castaño20d22cd2020-10-28 16:23:58643 return result;
644 }
645
Jan Scheffler19cd7ec2021-02-12 14:16:30646 _formatParameterAsObject(obj: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement {
647 const titleElement = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:09648 titleElement.classList.add('console-object');
Blink Reformat4c46d092018-04-07 15:32:37649 if (includePreview && obj.preview) {
650 titleElement.classList.add('console-object-preview');
651 this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
652 } else if (obj.type === 'function') {
653 const functionElement = titleElement.createChild('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22654 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
Blink Reformat4c46d092018-04-07 15:32:37655 titleElement.classList.add('object-value-function');
Alfonso Castaño20d22cd2020-10-28 16:23:58656 } else if (obj.subtype === 'trustedtype') {
657 titleElement.appendChild(this._formatParameterAsTrustedType(obj));
Blink Reformat4c46d092018-04-07 15:32:37658 } else {
Sigurd Schneider23c52972020-10-13 09:31:14659 UI.UIUtils.createTextChild(titleElement, obj.description || '');
Blink Reformat4c46d092018-04-07 15:32:37660 }
661
Tim van der Lippe1d6e57a2019-09-30 11:55:34662 if (!obj.hasChildren || obj.customPreview()) {
Blink Reformat4c46d092018-04-07 15:32:37663 return titleElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34664 }
Blink Reformat4c46d092018-04-07 15:32:37665
666 const note = titleElement.createChild('span', 'object-state-note info-note');
Tim van der Lippe9b2f8712020-02-12 17:46:22667 if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) {
Christy Chen9c6d8982021-02-08 02:28:31668 UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWillNotBeCollectedUntil));
Tim van der Lippe1d6e57a2019-09-30 11:55:34669 } else {
Christy Chen9c6d8982021-02-08 02:28:31670 UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWasEvaluatedUponFirst));
Tim van der Lippe1d6e57a2019-09-30 11:55:34671 }
Blink Reformat4c46d092018-04-07 15:32:37672
Tim van der Lippe9b2f8712020-02-12 17:46:22673 const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier);
Blink Reformat4c46d092018-04-07 15:32:37674 section.element.classList.add('console-view-object-properties-section');
675 section.enableContextMenu();
Erik Luocc14b812018-11-03 01:33:09676 section.setShowSelectionOnKeyboardFocus(true, true);
Erik Luo383f21d2018-11-07 23:16:37677 this._selectableChildren.push(section);
Erik Luo840be6b2018-12-03 20:54:27678 section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
679 section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
680 section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
Blink Reformat4c46d092018-04-07 15:32:37681 return section.element;
682 }
683
Jan Scheffler19cd7ec2021-02-12 14:16:30684 _formatParameterAsFunction(func: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement {
685 const result = (document.createElement('span') as HTMLElement);
Tim van der Lippe9b2f8712020-02-12 17:46:22686 SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37687 return result;
688
Jan Scheffler19cd7ec2021-02-12 14:16:30689 function formatTargetFunction(this: ConsoleViewMessage, targetFunction: SDK.RemoteObject.RemoteObject): void {
Sigurd Schneider53f33522020-10-08 15:00:49690 const functionElement = document.createElement('span');
Tim van der Lippe9b2f8712020-02-12 17:46:22691 const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(
Joey Arhard78a58f2018-12-05 01:59:45692 targetFunction, functionElement, true, includePreview);
Blink Reformat4c46d092018-04-07 15:32:37693 result.appendChild(functionElement);
694 if (targetFunction !== func) {
695 const note = result.createChild('span', 'object-info-state-note');
Christy Chen9c6d8982021-02-08 02:28:31696 UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.functionWasResolvedFromBound));
Blink Reformat4c46d092018-04-07 15:32:37697 }
698 result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false);
Joey Arhard78a58f2018-12-05 01:59:45699 promise.then(() => this._formattedParameterAsFunctionForTest());
Blink Reformat4c46d092018-04-07 15:32:37700 }
701 }
702
Jan Scheffler19cd7ec2021-02-12 14:16:30703 _formattedParameterAsFunctionForTest(): void {
Joey Arhard78a58f2018-12-05 01:59:45704 }
705
Jan Scheffler19cd7ec2021-02-12 14:16:30706 _contextMenuEventFired(obj: SDK.RemoteObject.RemoteObject, event: Event): void {
Tim van der Lippe9b2f8712020-02-12 17:46:22707 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37708 contextMenu.appendApplicableItems(obj);
709 contextMenu.show();
710 }
711
Jan Scheffler19cd7ec2021-02-12 14:16:30712 _renderPropertyPreviewOrAccessor(
713 object: SDK.RemoteObject.RemoteObject|null, property: Protocol.Runtime.PropertyPreview, propertyPath: {
714 name: (string|symbol),
715 }[]): HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:34716 if (property.type === 'accessor') {
Sigurd Schneider45f32c32020-10-13 13:32:05717 return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34718 }
Blink Reformat4c46d092018-04-07 15:32:37719 return this._previewFormatter.renderPropertyPreview(
Alfonso Castaño20d22cd2020-10-28 16:23:58720 property.type, 'subtype' in property ? property.subtype : undefined, null, property.value);
Blink Reformat4c46d092018-04-07 15:32:37721 }
722
Jan Scheffler19cd7ec2021-02-12 14:16:30723 _formatParameterAsNode(remoteObject: SDK.RemoteObject.RemoteObject): HTMLElement {
724 const result = document.createElement('span');
Blink Reformat4c46d092018-04-07 15:32:37725
Tim van der Lippe9b2f8712020-02-12 17:46:22726 const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:34727 if (!domModel) {
Blink Reformat4c46d092018-04-07 15:32:37728 return result;
Tim van der Lippe1d6e57a2019-09-30 11:55:34729 }
Jan Scheffler19cd7ec2021-02-12 14:16:30730 domModel.pushObjectAsNodeToFrontend(remoteObject).then(async (node: SDK.DOMModel.DOMNode|null) => {
Blink Reformat4c46d092018-04-07 15:32:37731 if (!node) {
732 result.appendChild(this._formatParameterAsObject(remoteObject, false));
733 return;
734 }
Jan Scheffler19cd7ec2021-02-12 14:16:30735 const renderResult = await UI.UIUtils.Renderer.render((node as Object));
Erik Luofc6a6302018-11-02 06:48:52736 if (renderResult) {
Erik Luo840be6b2018-12-03 20:54:27737 if (renderResult.tree) {
Erik Luo383f21d2018-11-07 23:16:37738 this._selectableChildren.push(renderResult.tree);
Erik Luo840be6b2018-12-03 20:54:27739 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized);
740 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized);
741 renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized);
742 }
Erik Luofc6a6302018-11-02 06:48:52743 result.appendChild(renderResult.node);
744 } else {
745 result.appendChild(this._formatParameterAsObject(remoteObject, false));
746 }
Erik Luo54fdd912018-11-01 17:57:01747 this._formattedParameterAsNodeForTest();
Blink Reformat4c46d092018-04-07 15:32:37748 });
749
750 return result;
751 }
752
Jan Scheffler19cd7ec2021-02-12 14:16:30753 _formattedParameterAsNodeForTest(): void {
Blink Reformat4c46d092018-04-07 15:32:37754 }
755
Jan Scheffler19cd7ec2021-02-12 14:16:30756 _formatParameterAsString(output: SDK.RemoteObject.RemoteObject): HTMLElement {
Benedikt Meurer62b49da2021-02-18 09:15:55757 // Properly escape double quotes here, so users don't get surprised
758 // when they copy strings from the console (https://crbug.com/1178530).
759 const description = output.description ?? '';
760 const text = JSON.stringify(description);
Jan Scheffler19cd7ec2021-02-12 14:16:30761 const result = (document.createElement('span') as HTMLElement);
Benedikt Meurer62b49da2021-02-18 09:15:55762 result.appendChild(this._linkifyStringAsFragment(text));
Blink Reformat4c46d092018-04-07 15:32:37763 return result;
764 }
765
Jan Scheffler19cd7ec2021-02-12 14:16:30766 _formatParameterAsError(output: SDK.RemoteObject.RemoteObject): HTMLElement {
767 const result = (document.createElement('span') as HTMLElement);
Blink Reformat4c46d092018-04-07 15:32:37768 const errorSpan = this._tryFormatAsError(output.description || '');
Erik Luo383f21d2018-11-07 23:16:37769 result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || ''));
Blink Reformat4c46d092018-04-07 15:32:37770 return result;
771 }
772
Jan Scheffler19cd7ec2021-02-12 14:16:30773 _formatAsArrayEntry(output: SDK.RemoteObject.RemoteObject): HTMLElement {
Alfonso Castaño20d22cd2020-10-28 16:23:58774 return this._previewFormatter.renderPropertyPreview(
775 output.type, output.subtype, output.className, output.description);
Blink Reformat4c46d092018-04-07 15:32:37776 }
777
Jan Scheffler19cd7ec2021-02-12 14:16:30778 _formatAsAccessorProperty(object: SDK.RemoteObject.RemoteObject|null, propertyPath: string[], isArrayEntry: boolean):
779 HTMLElement {
Tim van der Lippe9b2f8712020-02-12 17:46:22780 const rootElement =
781 ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
782 object, propertyPath, onInvokeGetterClick.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37783
Jan Scheffler19cd7ec2021-02-12 14:16:30784 function onInvokeGetterClick(this: ConsoleViewMessage, result: SDK.RemoteObject.CallFunctionResult): void {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18785 const wasThrown = result.wasThrown;
786 const object = result.object;
Tim van der Lippe1d6e57a2019-09-30 11:55:34787 if (!object) {
Blink Reformat4c46d092018-04-07 15:32:37788 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34789 }
Blink Reformat4c46d092018-04-07 15:32:37790 rootElement.removeChildren();
791 if (wasThrown) {
792 const element = rootElement.createChild('span');
Christy Chen9c6d8982021-02-08 02:28:31793 element.textContent = i18nString(UIStrings.exception);
Jan Scheffler19cd7ec2021-02-12 14:16:30794 UI.Tooltip.Tooltip.install(element, (object.description as string));
Blink Reformat4c46d092018-04-07 15:32:37795 } else if (isArrayEntry) {
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18796 rootElement.appendChild(this._formatAsArrayEntry(object));
Blink Reformat4c46d092018-04-07 15:32:37797 } else {
798 // Make a PropertyPreview from the RemoteObject similar to the backend logic.
799 const maxLength = 100;
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18800 const type = object.type;
801 const subtype = object.subtype;
Blink Reformat4c46d092018-04-07 15:32:37802 let description = '';
Alexey Kozyatinskiy330bffb2018-09-21 19:20:18803 if (type !== 'function' && object.description) {
Alfonso Castaño20d22cd2020-10-28 16:23:58804 if (type === 'string' || subtype === 'regexp' || subtype === 'trustedtype') {
Tim van der Lippe213266c2021-01-18 15:48:31805 description = Platform.StringUtilities.trimMiddle(object.description, maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34806 } else {
Tim van der Lippe3cc3af32021-01-19 14:25:26807 description = Platform.StringUtilities.trimEndWithMaxLength(object.description, maxLength);
Tim van der Lippe1d6e57a2019-09-30 11:55:34808 }
Blink Reformat4c46d092018-04-07 15:32:37809 }
Alfonso Castaño20d22cd2020-10-28 16:23:58810 rootElement.appendChild(
811 this._previewFormatter.renderPropertyPreview(type, subtype, object.className, description));
Blink Reformat4c46d092018-04-07 15:32:37812 }
813 }
814
815 return rootElement;
816 }
817
Jan Scheffler19cd7ec2021-02-12 14:16:30818 _formatWithSubstitutionString(
819 format: string, parameters: SDK.RemoteObject.RemoteObject[], formattedResult: HTMLElement): {
820 formattedResult: Element,
821 unusedSubstitutions: ArrayLike<SDK.RemoteObject.RemoteObject>|null,
822 } {
823 function parameterFormatter(
824 this: ConsoleViewMessage, force: boolean, includePreview: boolean,
825 obj?: string|SDK.RemoteObject.RemoteObject): string|HTMLElement|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05826 if (obj instanceof SDK.RemoteObject.RemoteObject) {
827 return this._formatParameter(obj, force, includePreview);
828 }
829 return stringFormatter(obj);
Blink Reformat4c46d092018-04-07 15:32:37830 }
831
Jan Scheffler19cd7ec2021-02-12 14:16:30832 function stringFormatter(obj?: string|SDK.RemoteObject.RemoteObject): string|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05833 if (obj === undefined) {
834 return undefined;
835 }
836 if (typeof obj === 'string') {
837 return obj;
838 }
Blink Reformat4c46d092018-04-07 15:32:37839 return obj.description;
840 }
841
Jan Scheffler19cd7ec2021-02-12 14:16:30842 function floatFormatter(obj?: string|SDK.RemoteObject.RemoteObject): number|string|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05843 if (obj instanceof SDK.RemoteObject.RemoteObject) {
844 if (typeof obj.value !== 'number') {
845 return 'NaN';
846 }
847 return obj.value;
Tim van der Lippe1d6e57a2019-09-30 11:55:34848 }
Sigurd Schneider45f32c32020-10-13 13:32:05849 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37850 }
851
Jan Scheffler19cd7ec2021-02-12 14:16:30852 function integerFormatter(obj?: string|SDK.RemoteObject.RemoteObject): string|number|undefined {
Sigurd Schneider45f32c32020-10-13 13:32:05853 if (obj instanceof SDK.RemoteObject.RemoteObject) {
854 if (obj.type === 'bigint') {
855 return obj.description;
856 }
857 if (typeof obj.value !== 'number') {
858 return 'NaN';
859 }
860 return Math.floor(obj.value);
Tim van der Lippe1d6e57a2019-09-30 11:55:34861 }
Sigurd Schneider45f32c32020-10-13 13:32:05862 return undefined;
Blink Reformat4c46d092018-04-07 15:32:37863 }
864
Jan Scheffler19cd7ec2021-02-12 14:16:30865 function bypassFormatter(obj?: string|SDK.RemoteObject.RemoteObject): Node|string {
Blink Reformat4c46d092018-04-07 15:32:37866 return (obj instanceof Node) ? obj : '';
867 }
868
Jan Scheffler19cd7ec2021-02-12 14:16:30869 let currentStyle: Map<string, {value: string, priority: string}>|null = null;
870 function styleFormatter(obj?: string|SDK.RemoteObject.RemoteObject): void {
Sigurd Schneider45f32c32020-10-13 13:32:05871 currentStyle = new Map();
Sigurd Schneider53f33522020-10-08 15:00:49872 const buffer = document.createElement('span');
Sigurd Schneider45f32c32020-10-13 13:32:05873 if (obj === undefined) {
874 return;
875 }
876 if (typeof obj === 'string' || !obj.description) {
877 return;
878 }
Blink Reformat4c46d092018-04-07 15:32:37879 buffer.setAttribute('style', obj.description);
Sigurd Schneider45f32c32020-10-13 13:32:05880 for (const property of buffer.style) {
Mathias Bynens5165a7a2020-06-10 05:51:43881 if (isAllowedProperty(property)) {
Sigurd Schneider45f32c32020-10-13 13:32:05882 const info = {
883 value: buffer.style.getPropertyValue(property),
Jan Scheffler19cd7ec2021-02-12 14:16:30884 priority: buffer.style.getPropertyPriority(property),
Sigurd Schneider45f32c32020-10-13 13:32:05885 };
886 currentStyle.set(property, info);
Tim van der Lippe1d6e57a2019-09-30 11:55:34887 }
Blink Reformat4c46d092018-04-07 15:32:37888 }
889 }
890
Jan Scheffler19cd7ec2021-02-12 14:16:30891 function isAllowedProperty(property: string): boolean {
Blink Reformat4c46d092018-04-07 15:32:37892 // Make sure that allowed properties do not interfere with link visibility.
893 const prefixes = [
Jan Scheffler19cd7ec2021-02-12 14:16:30894 'background',
895 'border',
896 'color',
897 'font',
898 'line',
899 'margin',
900 'padding',
901 'text',
902 '-webkit-background',
903 '-webkit-border',
904 '-webkit-font',
905 '-webkit-margin',
906 '-webkit-padding',
907 '-webkit-text',
Blink Reformat4c46d092018-04-07 15:32:37908 ];
Sigurd Schneider45f32c32020-10-13 13:32:05909 for (const prefix of prefixes) {
910 if (property.startsWith(prefix)) {
Blink Reformat4c46d092018-04-07 15:32:37911 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:34912 }
Blink Reformat4c46d092018-04-07 15:32:37913 }
914 return false;
915 }
916
Jan Scheffler19cd7ec2021-02-12 14:16:30917 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration)
918 // eslint-disable-next-line @typescript-eslint/no-explicit-any
919 const formatters: Record<string, Platform.StringUtilities.FormatterFunction<any>> = {};
Blink Reformat4c46d092018-04-07 15:32:37920 // Firebug uses %o for formatting objects.
921 formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */);
922 formatters.s = stringFormatter;
923 formatters.f = floatFormatter;
924 // Firebug allows both %i and %d for formatting integers.
925 formatters.i = integerFormatter;
926 formatters.d = integerFormatter;
927
928 // Firebug uses %c for styling the message.
929 formatters.c = styleFormatter;
930
931 // Support %O to force object formatting, instead of the type-based %o formatting.
932 formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */);
933
934 formatters._ = bypassFormatter;
935
Jan Scheffler19cd7ec2021-02-12 14:16:30936 function append(this: ConsoleViewMessage, a: HTMLElement, b?: string|Node): HTMLElement {
Blink Reformat4c46d092018-04-07 15:32:37937 if (b instanceof Node) {
938 a.appendChild(b);
Erik Luo17926392018-05-17 22:06:12939 return a;
940 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34941 if (typeof b === 'undefined') {
Erik Luo17926392018-05-17 22:06:12942 return a;
Tim van der Lippe1d6e57a2019-09-30 11:55:34943 }
Erik Luo17926392018-05-17 22:06:12944 if (!currentStyle) {
Erik Luo383f21d2018-11-07 23:16:37945 a.appendChild(this._linkifyStringAsFragment(String(b)));
Erik Luo17926392018-05-17 22:06:12946 return a;
947 }
948 const lines = String(b).split('\n');
949 for (let i = 0; i < lines.length; i++) {
950 const line = lines[i];
Erik Luo383f21d2018-11-07 23:16:37951 const lineFragment = this._linkifyStringAsFragment(line);
Jan Scheffler19cd7ec2021-02-12 14:16:30952 const wrapper = (document.createElement('span') as HTMLElement);
Erik Luo17926392018-05-17 22:06:12953 wrapper.style.setProperty('contain', 'paint');
954 wrapper.style.setProperty('display', 'inline-block');
955 wrapper.style.setProperty('max-width', '100%');
956 wrapper.appendChild(lineFragment);
957 applyCurrentStyle(wrapper);
958 for (const child of wrapper.children) {
Sigurd Schneider53f33522020-10-08 15:00:49959 if (child.classList.contains('devtools-link') && child instanceof HTMLElement) {
Erik Luo17926392018-05-17 22:06:12960 this._applyForcedVisibleStyle(child);
Tim van der Lippe1d6e57a2019-09-30 11:55:34961 }
Blink Reformat4c46d092018-04-07 15:32:37962 }
Erik Luo17926392018-05-17 22:06:12963 a.appendChild(wrapper);
Tim van der Lippe1d6e57a2019-09-30 11:55:34964 if (i < lines.length - 1) {
Sigurd Schneider53f33522020-10-08 15:00:49965 a.appendChild(document.createElement('br'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34966 }
Blink Reformat4c46d092018-04-07 15:32:37967 }
968 return a;
969 }
970
Jan Scheffler19cd7ec2021-02-12 14:16:30971 function applyCurrentStyle(element: HTMLElement): void {
Sigurd Schneider45f32c32020-10-13 13:32:05972 if (!currentStyle) {
973 return;
974 }
975 for (const [property, {value, priority}] of currentStyle.entries()) {
Jan Scheffler19cd7ec2021-02-12 14:16:30976 element.style.setProperty((property as string), value, priority);
Tim van der Lippe1d6e57a2019-09-30 11:55:34977 }
Blink Reformat4c46d092018-04-07 15:32:37978 }
979
Tim van der Lippe93b57c32020-02-20 17:38:44980 // Platform.StringUtilities.format does treat formattedResult like a Builder, result is an object.
981 return Platform.StringUtilities.format(format, parameters, formatters, formattedResult, append.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37982 }
983
Jan Scheffler19cd7ec2021-02-12 14:16:30984 _applyForcedVisibleStyle(element: HTMLElement): void {
Blink Reformat4c46d092018-04-07 15:32:37985 element.style.setProperty('-webkit-text-stroke', '0', 'important');
986 element.style.setProperty('text-decoration', 'underline', 'important');
987
Paul Lewisca569a52020-09-09 16:11:51988 const themedColor = ThemeSupport.ThemeSupport.instance().patchColorText(
989 'rgb(33%, 33%, 33%)', ThemeSupport.ThemeSupport.ColorUsage.Foreground);
Blink Reformat4c46d092018-04-07 15:32:37990 element.style.setProperty('color', themedColor, 'important');
991
992 let backgroundColor = 'hsl(0, 0%, 100%)';
Tim van der Lippe9b2f8712020-02-12 17:46:22993 if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Blink Reformat4c46d092018-04-07 15:32:37994 backgroundColor = 'hsl(0, 100%, 97%)';
Tim van der Lippe9b2f8712020-02-12 17:46:22995 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:37996 backgroundColor = 'hsl(50, 100%, 95%)';
Tim van der Lippe1d6e57a2019-09-30 11:55:34997 }
Paul Lewisca569a52020-09-09 16:11:51998 const themedBackgroundColor = ThemeSupport.ThemeSupport.instance().patchColorText(
999 backgroundColor, ThemeSupport.ThemeSupport.ColorUsage.Background);
Blink Reformat4c46d092018-04-07 15:32:371000 element.style.setProperty('background-color', themedBackgroundColor, 'important');
1001 }
1002
Jan Scheffler19cd7ec2021-02-12 14:16:301003 matchesFilterRegex(regexObject: RegExp): boolean {
Blink Reformat4c46d092018-04-07 15:32:371004 regexObject.lastIndex = 0;
Erik Luo5976c8c2018-07-24 02:03:091005 const contentElement = this.contentElement();
1006 const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : '';
Tim van der Lipped7cfd142021-01-07 12:17:241007 return (Boolean(anchorText) && regexObject.test(anchorText.trim())) ||
Erik Luo5976c8c2018-07-24 02:03:091008 regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
Blink Reformat4c46d092018-04-07 15:32:371009 }
1010
Jan Scheffler19cd7ec2021-02-12 14:16:301011 matchesFilterText(filter: string): boolean {
Blink Reformat4c46d092018-04-07 15:32:371012 const text = this.contentElement().deepTextContent();
1013 return text.toLowerCase().includes(filter.toLowerCase());
1014 }
1015
Jan Scheffler19cd7ec2021-02-12 14:16:301016 updateTimestamp(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341017 if (!this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371018 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341019 }
Blink Reformat4c46d092018-04-07 15:32:371020
Paul Lewis2d7d65c2020-03-16 17:26:301021 if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341022 if (!this._timestampElement) {
Jan Scheffler19cd7ec2021-02-12 14:16:301023 this._timestampElement = (document.createElement('span') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:091024 this._timestampElement.classList.add('console-timestamp');
Tim van der Lippe1d6e57a2019-09-30 11:55:341025 }
Tim van der Lippe9b2f8712020-02-12 17:46:221026 this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' ';
Tim van der Lippe70842f32020-11-23 16:56:571027 UI.Tooltip.Tooltip.install(this._timestampElement, UI.UIUtils.formatTimestamp(this._message.timestamp, true));
Blink Reformat4c46d092018-04-07 15:32:371028 this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild);
1029 } else if (this._timestampElement) {
1030 this._timestampElement.remove();
Sigurd Schneider53e98632020-10-26 15:29:501031 this._timestampElement = null;
Blink Reformat4c46d092018-04-07 15:32:371032 }
Blink Reformat4c46d092018-04-07 15:32:371033 }
1034
Jan Scheffler19cd7ec2021-02-12 14:16:301035 nestingLevel(): number {
Blink Reformat4c46d092018-04-07 15:32:371036 return this._nestingLevel;
1037 }
1038
Jan Scheffler19cd7ec2021-02-12 14:16:301039 setInSimilarGroup(inSimilarGroup: boolean, isLast?: boolean): void {
Blink Reformat4c46d092018-04-07 15:32:371040 this._inSimilarGroup = inSimilarGroup;
Tim van der Lipped7cfd142021-01-07 12:17:241041 this._lastInSimilarGroup = inSimilarGroup && Boolean(isLast);
Blink Reformat4c46d092018-04-07 15:32:371042 if (this._similarGroupMarker && !inSimilarGroup) {
1043 this._similarGroupMarker.remove();
1044 this._similarGroupMarker = null;
1045 } else if (this._element && !this._similarGroupMarker && inSimilarGroup) {
Jan Scheffler19cd7ec2021-02-12 14:16:301046 this._similarGroupMarker = (document.createElement('div') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:091047 this._similarGroupMarker.classList.add('nesting-level-marker');
Blink Reformat4c46d092018-04-07 15:32:371048 this._element.insertBefore(this._similarGroupMarker, this._element.firstChild);
1049 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1050 }
1051 }
1052
Jan Scheffler19cd7ec2021-02-12 14:16:301053 isLastInSimilarGroup(): boolean {
Tim van der Lipped7cfd142021-01-07 12:17:241054 return Boolean(this._inSimilarGroup) && Boolean(this._lastInSimilarGroup);
Blink Reformat4c46d092018-04-07 15:32:371055 }
1056
Jan Scheffler19cd7ec2021-02-12 14:16:301057 resetCloseGroupDecorationCount(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341058 if (!this._closeGroupDecorationCount) {
Blink Reformat4c46d092018-04-07 15:32:371059 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341060 }
Blink Reformat4c46d092018-04-07 15:32:371061 this._closeGroupDecorationCount = 0;
1062 this._updateCloseGroupDecorations();
1063 }
1064
Jan Scheffler19cd7ec2021-02-12 14:16:301065 incrementCloseGroupDecorationCount(): void {
Blink Reformat4c46d092018-04-07 15:32:371066 ++this._closeGroupDecorationCount;
1067 this._updateCloseGroupDecorations();
1068 }
1069
Jan Scheffler19cd7ec2021-02-12 14:16:301070 _updateCloseGroupDecorations(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341071 if (!this._nestingLevelMarkers) {
Blink Reformat4c46d092018-04-07 15:32:371072 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341073 }
Blink Reformat4c46d092018-04-07 15:32:371074 for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) {
1075 const marker = this._nestingLevelMarkers[i];
1076 marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount);
1077 }
1078 }
1079
Jan Scheffler19cd7ec2021-02-12 14:16:301080 _focusedChildIndex(): number {
Tim van der Lippe1d6e57a2019-09-30 11:55:341081 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461082 return -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341083 }
Erik Luo383f21d2018-11-07 23:16:371084 return this._selectableChildren.findIndex(child => child.element.hasFocus());
Erik Luo0b8282e2018-10-08 20:37:461085 }
1086
Jan Scheffler19cd7ec2021-02-12 14:16:301087 _onKeyDown(event: KeyboardEvent): void {
Sigurd Schneider45f32c32020-10-13 13:32:051088 if (UI.UIUtils.isEditing() || !this._element || !this._element.hasFocus() || this._element.hasSelection()) {
Erik Luo8ef5d0c2018-09-25 21:16:001089 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341090 }
1091 if (this.maybeHandleOnKeyDown(event)) {
Erik Luo8ef5d0c2018-09-25 21:16:001092 event.consume(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:341093 }
Erik Luo8ef5d0c2018-09-25 21:16:001094 }
1095
Jan Scheffler19cd7ec2021-02-12 14:16:301096 maybeHandleOnKeyDown(event: KeyboardEvent): boolean {
Erik Luo8ef5d0c2018-09-25 21:16:001097 // Handle trace expansion.
Erik Luo0b8282e2018-10-08 20:37:461098 const focusedChildIndex = this._focusedChildIndex();
1099 const isWrapperFocused = focusedChildIndex === -1;
1100 if (this._expandTrace && isWrapperFocused) {
Erik Luo8ef5d0c2018-09-25 21:16:001101 if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) {
1102 this._expandTrace(!this._traceExpanded);
1103 return true;
1104 }
1105 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341106 if (!this._selectableChildren.length) {
Erik Luo0b8282e2018-10-08 20:37:461107 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:341108 }
Erik Luo0b8282e2018-10-08 20:37:461109
1110 if (event.key === 'ArrowLeft') {
Sigurd Schneider45f32c32020-10-13 13:32:051111 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461112 return true;
1113 }
1114 if (event.key === 'ArrowRight') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341115 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461116 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341117 }
Erik Luo0b8282e2018-10-08 20:37:461118 }
1119 if (event.key === 'ArrowUp') {
Erik Luo182bece2018-11-29 03:15:221120 const firstVisibleChild = this._nearestVisibleChild(0);
1121 if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
Sigurd Schneider45f32c32020-10-13 13:32:051122 this._element && this._element.focus();
Erik Luo0b8282e2018-10-08 20:37:461123 return true;
Mathias Bynensf06e8c02020-02-28 13:58:281124 }
1125 if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461126 return true;
1127 }
1128 }
1129 if (event.key === 'ArrowDown') {
Tim van der Lippe1d6e57a2019-09-30 11:55:341130 if (isWrapperFocused && this._selectNearestVisibleChild(0)) {
Erik Luo0b8282e2018-10-08 20:37:461131 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341132 }
1133 if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) {
Erik Luo0b8282e2018-10-08 20:37:461134 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:341135 }
Erik Luo0b8282e2018-10-08 20:37:461136 }
Erik Luo8ef5d0c2018-09-25 21:16:001137 return false;
1138 }
1139
Jan Scheffler19cd7ec2021-02-12 14:16:301140 _selectNearestVisibleChild(fromIndex: number, backwards?: boolean): boolean {
Erik Luo182bece2018-11-29 03:15:221141 const nearestChild = this._nearestVisibleChild(fromIndex, backwards);
1142 if (nearestChild) {
Erik Luo31c21f62018-12-13 03:39:391143 nearestChild.forceSelect();
Erik Luo182bece2018-11-29 03:15:221144 return true;
1145 }
1146 return false;
1147 }
1148
Jan Scheffler19cd7ec2021-02-12 14:16:301149 _nearestVisibleChild(fromIndex: number, backwards?: boolean): {
1150 element: Element,
1151 forceSelect: () => void,
1152 }|null {
Erik Luo182bece2018-11-29 03:15:221153 const childCount = this._selectableChildren.length;
Tim van der Lippe1d6e57a2019-09-30 11:55:341154 if (fromIndex < 0 || fromIndex >= childCount) {
Erik Luo182bece2018-11-29 03:15:221155 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341156 }
Erik Luo182bece2018-11-29 03:15:221157 const direction = backwards ? -1 : 1;
1158 let index = fromIndex;
1159
1160 while (!this._selectableChildren[index].element.offsetParent) {
1161 index += direction;
Tim van der Lippe1d6e57a2019-09-30 11:55:341162 if (index < 0 || index >= childCount) {
Erik Luo182bece2018-11-29 03:15:221163 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341164 }
Erik Luo182bece2018-11-29 03:15:221165 }
1166 return this._selectableChildren[index];
1167 }
1168
Jan Scheffler19cd7ec2021-02-12 14:16:301169 focusLastChildOrSelf(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341170 if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) {
Erik Luo0b8282e2018-10-08 20:37:461171 this._element.focus();
Tim van der Lippe1d6e57a2019-09-30 11:55:341172 }
Erik Luo0b8282e2018-10-08 20:37:461173 }
1174
Jan Scheffler19cd7ec2021-02-12 14:16:301175 setContentElement(element: HTMLElement): void {
Sigurd Schneiderb2953b22020-10-09 09:30:151176 console.assert(!this._contentElement, 'Cannot set content element twice');
1177 this._contentElement = element;
1178 }
1179
Jan Scheffler19cd7ec2021-02-12 14:16:301180 getContentElement(): HTMLElement|null {
Sigurd Schneiderb2953b22020-10-09 09:30:151181 return this._contentElement;
1182 }
1183
Jan Scheffler19cd7ec2021-02-12 14:16:301184 contentElement(): HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:341185 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371186 return this._contentElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:341187 }
Blink Reformat4c46d092018-04-07 15:32:371188
Jan Scheffler19cd7ec2021-02-12 14:16:301189 const contentElement = (document.createElement('div') as HTMLElement);
Tim van der Lippef49e2322020-05-01 15:03:091190 contentElement.classList.add('console-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341191 if (this._messageLevelIcon) {
Blink Reformat4c46d092018-04-07 15:32:371192 contentElement.appendChild(this._messageLevelIcon);
Tim van der Lippe1d6e57a2019-09-30 11:55:341193 }
Blink Reformat4c46d092018-04-07 15:32:371194 this._contentElement = contentElement;
1195
Sigurd Schneider45f32c32020-10-13 13:32:051196 const runtimeModel = this._message.runtimeModel();
Blink Reformat4c46d092018-04-07 15:32:371197 let formattedMessage;
Tim van der Lipped7cfd142021-01-07 12:17:241198 const shouldIncludeTrace = Boolean(this._message.stackTrace) &&
Tim van der Lippe9b2f8712020-02-12 17:46:221199 (this._message.source === SDK.ConsoleModel.MessageSource.Network ||
1200 this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1201 this._message.level === SDK.ConsoleModel.MessageLevel.Error ||
1202 this._message.level === SDK.ConsoleModel.MessageLevel.Warning ||
1203 this._message.type === SDK.ConsoleModel.MessageType.Trace);
Sigurd Schneider45f32c32020-10-13 13:32:051204 if (runtimeModel && shouldIncludeTrace) {
1205 formattedMessage = this._buildMessageWithStackTrace(runtimeModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:341206 } else {
Blink Reformat4c46d092018-04-07 15:32:371207 formattedMessage = this._buildMessage();
Tim van der Lippe1d6e57a2019-09-30 11:55:341208 }
Blink Reformat4c46d092018-04-07 15:32:371209 contentElement.appendChild(formattedMessage);
1210
1211 this.updateTimestamp();
1212 return this._contentElement;
1213 }
1214
Jan Scheffler19cd7ec2021-02-12 14:16:301215 toMessageElement(): HTMLElement {
Tim van der Lippe1d6e57a2019-09-30 11:55:341216 if (this._element) {
Blink Reformat4c46d092018-04-07 15:32:371217 return this._element;
Tim van der Lippe1d6e57a2019-09-30 11:55:341218 }
Jan Scheffler19cd7ec2021-02-12 14:16:301219 this._element = (document.createElement('div') as HTMLElement);
Pavel Feldmandb310912019-01-30 00:31:201220 this._element.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:301221 this._element.addEventListener('keydown', (this._onKeyDown.bind(this) as EventListener));
Blink Reformat4c46d092018-04-07 15:32:371222 this.updateMessageElement();
1223 return this._element;
1224 }
1225
Jan Scheffler19cd7ec2021-02-12 14:16:301226 updateMessageElement(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341227 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371228 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341229 }
Blink Reformat4c46d092018-04-07 15:32:371230
1231 this._element.className = 'console-message-wrapper';
1232 this._element.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:341233 if (this._message.isGroupStartMessage()) {
Blink Reformat4c46d092018-04-07 15:32:371234 this._element.classList.add('console-group-title');
Tim van der Lippe1d6e57a2019-09-30 11:55:341235 }
Tim van der Lippe9b2f8712020-02-12 17:46:221236 if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) {
Blink Reformat4c46d092018-04-07 15:32:371237 this._element.classList.add('console-from-api');
Tim van der Lippe1d6e57a2019-09-30 11:55:341238 }
Blink Reformat4c46d092018-04-07 15:32:371239 if (this._inSimilarGroup) {
Jan Scheffler19cd7ec2021-02-12 14:16:301240 this._similarGroupMarker = (this._element.createChild('div', 'nesting-level-marker') as HTMLElement);
Blink Reformat4c46d092018-04-07 15:32:371241 this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup);
1242 }
1243
1244 this._nestingLevelMarkers = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341245 for (let i = 0; i < this._nestingLevel; ++i) {
Blink Reformat4c46d092018-04-07 15:32:371246 this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker'));
Tim van der Lippe1d6e57a2019-09-30 11:55:341247 }
Blink Reformat4c46d092018-04-07 15:32:371248 this._updateCloseGroupDecorations();
Sigurd Schneiderca7b4ff2020-10-14 07:45:471249 elementToMessage.set(this._element, this);
Blink Reformat4c46d092018-04-07 15:32:371250
1251 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221252 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371253 this._element.classList.add('console-verbose-level');
Blink Reformat4c46d092018-04-07 15:32:371254 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221255 case SDK.ConsoleModel.MessageLevel.Info:
Blink Reformat4c46d092018-04-07 15:32:371256 this._element.classList.add('console-info-level');
Tim van der Lippe9b2f8712020-02-12 17:46:221257 if (this._message.type === SDK.ConsoleModel.MessageType.System) {
Blink Reformat4c46d092018-04-07 15:32:371258 this._element.classList.add('console-system-type');
Tim van der Lippe1d6e57a2019-09-30 11:55:341259 }
Blink Reformat4c46d092018-04-07 15:32:371260 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221261 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371262 this._element.classList.add('console-warning-level');
Blink Reformat4c46d092018-04-07 15:32:371263 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221264 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371265 this._element.classList.add('console-error-level');
Blink Reformat4c46d092018-04-07 15:32:371266 break;
1267 }
Erik Luofd3e7d42018-09-25 02:12:351268 this._updateMessageLevelIcon();
Tim van der Lippe1d6e57a2019-09-30 11:55:341269 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371270 this._element.classList.add('console-warning-level');
Tim van der Lippe1d6e57a2019-09-30 11:55:341271 }
Blink Reformat4c46d092018-04-07 15:32:371272
1273 this._element.appendChild(this.contentElement());
Tim van der Lippe1d6e57a2019-09-30 11:55:341274 if (this._repeatCount > 1) {
Blink Reformat4c46d092018-04-07 15:32:371275 this._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341276 }
Blink Reformat4c46d092018-04-07 15:32:371277 }
1278
Jan Scheffler19cd7ec2021-02-12 14:16:301279 _shouldRenderAsWarning(): boolean {
Tim van der Lippe9b2f8712020-02-12 17:46:221280 return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose ||
1281 this._message.level === SDK.ConsoleModel.MessageLevel.Info) &&
1282 (this._message.source === SDK.ConsoleModel.MessageSource.Violation ||
1283 this._message.source === SDK.ConsoleModel.MessageSource.Deprecation ||
1284 this._message.source === SDK.ConsoleModel.MessageSource.Intervention ||
1285 this._message.source === SDK.ConsoleModel.MessageSource.Recommendation);
Blink Reformat4c46d092018-04-07 15:32:371286 }
1287
Jan Scheffler19cd7ec2021-02-12 14:16:301288 _updateMessageLevelIcon(): void {
Erik Luofd3e7d42018-09-25 02:12:351289 let iconType = '';
1290 let accessibleName = '';
Tim van der Lippe9b2f8712020-02-12 17:46:221291 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Erik Luofd3e7d42018-09-25 02:12:351292 iconType = 'smallicon-warning';
Christy Chen9c6d8982021-02-08 02:28:311293 accessibleName = i18nString(UIStrings.warning);
Tim van der Lippe9b2f8712020-02-12 17:46:221294 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Erik Luofd3e7d42018-09-25 02:12:351295 iconType = 'smallicon-error';
Christy Chen9c6d8982021-02-08 02:28:311296 accessibleName = i18nString(UIStrings.error);
Erik Luofd3e7d42018-09-25 02:12:351297 }
Sigurd Schneider45f32c32020-10-13 13:32:051298 if (!this._messageLevelIcon) {
1299 if (!iconType) {
1300 return;
1301 }
Tim van der Lippe9b2f8712020-02-12 17:46:221302 this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon');
Tim van der Lippe1d6e57a2019-09-30 11:55:341303 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371304 this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341305 }
Blink Reformat4c46d092018-04-07 15:32:371306 }
1307 this._messageLevelIcon.setIconType(iconType);
Erik Luofd3e7d42018-09-25 02:12:351308 UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371309 }
1310
Jan Scheffler19cd7ec2021-02-12 14:16:301311 repeatCount(): number {
Blink Reformat4c46d092018-04-07 15:32:371312 return this._repeatCount || 1;
1313 }
1314
Jan Scheffler19cd7ec2021-02-12 14:16:301315 resetIncrementRepeatCount(): void {
Blink Reformat4c46d092018-04-07 15:32:371316 this._repeatCount = 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341317 if (!this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371318 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341319 }
Blink Reformat4c46d092018-04-07 15:32:371320
1321 this._repeatCountElement.remove();
Tim van der Lippe1d6e57a2019-09-30 11:55:341322 if (this._contentElement) {
Blink Reformat4c46d092018-04-07 15:32:371323 this._contentElement.classList.remove('repeated-message');
Tim van der Lippe1d6e57a2019-09-30 11:55:341324 }
Sigurd Schneider53e98632020-10-26 15:29:501325 this._repeatCountElement = null;
Blink Reformat4c46d092018-04-07 15:32:371326 }
1327
Jan Scheffler19cd7ec2021-02-12 14:16:301328 incrementRepeatCount(): void {
Blink Reformat4c46d092018-04-07 15:32:371329 this._repeatCount++;
1330 this._showRepeatCountElement();
1331 }
1332
Jan Scheffler19cd7ec2021-02-12 14:16:301333 setRepeatCount(repeatCount: number): void {
Blink Reformat4c46d092018-04-07 15:32:371334 this._repeatCount = repeatCount;
1335 this._showRepeatCountElement();
1336 }
Jan Scheffler19cd7ec2021-02-12 14:16:301337 _showRepeatCountElement(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:341338 if (!this._element) {
Blink Reformat4c46d092018-04-07 15:32:371339 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341340 }
Blink Reformat4c46d092018-04-07 15:32:371341
1342 if (!this._repeatCountElement) {
Sigurd Schneider45f32c32020-10-13 13:32:051343 this._repeatCountElement =
Jan Scheffler19cd7ec2021-02-12 14:16:301344 (document.createElement('span', {is: 'dt-small-bubble'}) as UI.UIUtils.DevToolsSmallBubble);
Tim van der Lippeee954d42020-05-04 10:35:571345 this._repeatCountElement.classList.add('console-message-repeat-count');
Blink Reformat4c46d092018-04-07 15:32:371346 switch (this._message.level) {
Tim van der Lippe9b2f8712020-02-12 17:46:221347 case SDK.ConsoleModel.MessageLevel.Warning:
Blink Reformat4c46d092018-04-07 15:32:371348 this._repeatCountElement.type = 'warning';
1349 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221350 case SDK.ConsoleModel.MessageLevel.Error:
Blink Reformat4c46d092018-04-07 15:32:371351 this._repeatCountElement.type = 'error';
1352 break;
Tim van der Lippe9b2f8712020-02-12 17:46:221353 case SDK.ConsoleModel.MessageLevel.Verbose:
Blink Reformat4c46d092018-04-07 15:32:371354 this._repeatCountElement.type = 'verbose';
1355 break;
1356 default:
1357 this._repeatCountElement.type = 'info';
1358 }
Tim van der Lippe1d6e57a2019-09-30 11:55:341359 if (this._shouldRenderAsWarning()) {
Blink Reformat4c46d092018-04-07 15:32:371360 this._repeatCountElement.type = 'warning';
Tim van der Lippe1d6e57a2019-09-30 11:55:341361 }
Blink Reformat4c46d092018-04-07 15:32:371362
1363 this._element.insertBefore(this._repeatCountElement, this._contentElement);
Sigurd Schneider45f32c32020-10-13 13:32:051364 this.contentElement().classList.add('repeated-message');
Blink Reformat4c46d092018-04-07 15:32:371365 }
Sigurd Schneider45f32c32020-10-13 13:32:051366 this._repeatCountElement.textContent = `${this._repeatCount}`;
Peter Marshall2bdcc642021-03-03 10:02:091367
1368 let accessibleName;
Tim van der Lippe9b2f8712020-02-12 17:46:221369 if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) {
Peter Marshall2bdcc642021-03-03 10:02:091370 accessibleName = i18nString(UIStrings.warningS, {n: this._repeatCount});
Tim van der Lippe9b2f8712020-02-12 17:46:221371 } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) {
Peter Marshall2bdcc642021-03-03 10:02:091372 accessibleName = i18nString(UIStrings.errorS, {n: this._repeatCount});
1373 } else {
1374 accessibleName = i18nString(UIStrings.repeatS, {n: this._repeatCount});
Tim van der Lippe1d6e57a2019-09-30 11:55:341375 }
Erik Luofd3e7d42018-09-25 02:12:351376 UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName);
Blink Reformat4c46d092018-04-07 15:32:371377 }
1378
Jan Scheffler19cd7ec2021-02-12 14:16:301379 get text(): string {
Blink Reformat4c46d092018-04-07 15:32:371380 return this._message.messageText;
1381 }
1382
Jan Scheffler19cd7ec2021-02-12 14:16:301383 toExportString(): string {
Blink Reformat4c46d092018-04-07 15:32:371384 const lines = [];
1385 const nodes = this.contentElement().childTextNodes();
Tim van der Lippe9b2f8712020-02-12 17:46:221386 const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join('');
Tim van der Lippe1d6e57a2019-09-30 11:55:341387 for (let i = 0; i < this.repeatCount(); ++i) {
Blink Reformat4c46d092018-04-07 15:32:371388 lines.push(messageContent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341389 }
Blink Reformat4c46d092018-04-07 15:32:371390 return lines.join('\n');
1391 }
1392
Jan Scheffler19cd7ec2021-02-12 14:16:301393 setSearchRegex(regex: RegExp|null): void {
Sigurd Schneidere8e75cf2020-10-13 08:17:521394 if (this._searchHighlightNodeChanges && this._searchHighlightNodeChanges.length) {
1395 UI.UIUtils.revertDomChanges(this._searchHighlightNodeChanges);
Tim van der Lippe1d6e57a2019-09-30 11:55:341396 }
Blink Reformat4c46d092018-04-07 15:32:371397 this._searchRegex = regex;
1398 this._searchHighlightNodes = [];
Sigurd Schneidere8e75cf2020-10-13 08:17:521399 this._searchHighlightNodeChanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341400 if (!this._searchRegex) {
Blink Reformat4c46d092018-04-07 15:32:371401 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341402 }
Blink Reformat4c46d092018-04-07 15:32:371403
1404 const text = this.contentElement().deepTextContent();
1405 let match;
1406 this._searchRegex.lastIndex = 0;
1407 const sourceRanges = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:341408 while ((match = this._searchRegex.exec(text)) && match[0]) {
Tim van der Lippe9b2f8712020-02-12 17:46:221409 sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length));
Tim van der Lippe1d6e57a2019-09-30 11:55:341410 }
Blink Reformat4c46d092018-04-07 15:32:371411
1412 if (sourceRanges.length) {
1413 this._searchHighlightNodes =
Sigurd Schneidere8e75cf2020-10-13 08:17:521414 UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHighlightNodeChanges);
Blink Reformat4c46d092018-04-07 15:32:371415 }
1416 }
1417
Jan Scheffler19cd7ec2021-02-12 14:16:301418 searchRegex(): RegExp|null {
Blink Reformat4c46d092018-04-07 15:32:371419 return this._searchRegex;
1420 }
1421
Jan Scheffler19cd7ec2021-02-12 14:16:301422 searchCount(): number {
Blink Reformat4c46d092018-04-07 15:32:371423 return this._searchHighlightNodes.length;
1424 }
1425
Jan Scheffler19cd7ec2021-02-12 14:16:301426 searchHighlightNode(index: number): Element {
Blink Reformat4c46d092018-04-07 15:32:371427 return this._searchHighlightNodes[index];
1428 }
1429
Philip Pfaffe068b01d2021-03-22 09:46:261430 async _getInlineFrames(
1431 debuggerModel: SDK.DebuggerModel.DebuggerModel, url: string, lineNumber: number|undefined,
1432 columnNumber: number|undefined): Promise<{frames: Bindings.DebuggerLanguagePlugins.FunctionInfo[]}> {
1433 const debuggerWorkspaceBinding = Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance();
1434 if (debuggerWorkspaceBinding.pluginManager) {
1435 const projects = Workspace.Workspace.WorkspaceImpl.instance().projects();
1436 const uiSourceCodes = projects.map(project => project.uiSourceCodeForURL(url)).flat().filter(f => Boolean(f)) as
1437 Workspace.UISourceCode.UISourceCode[];
1438 const scripts =
1439 uiSourceCodes.map(uiSourceCode => debuggerWorkspaceBinding.scriptsForUISourceCode(uiSourceCode)).flat();
1440 if (scripts.length) {
1441 const location =
1442 new SDK.DebuggerModel.Location(debuggerModel, scripts[0].scriptId, lineNumber || 0, columnNumber);
1443 return debuggerWorkspaceBinding.pluginManager.getFunctionInfo(scripts[0], location);
1444 }
1445 }
1446
1447 return {frames: []};
1448 }
1449
1450 // Expand inline stack frames in the formatted error in the stackTrace element, inserting new elements before the
1451 // insertBefore anchor.
1452 async _expandInlineStackFrames(
1453 debuggerModel: SDK.DebuggerModel.DebuggerModel, prefix: string, suffix: string, url: string,
1454 lineNumber: number|undefined, columnNumber: number|undefined, stackTrace: HTMLElement,
1455 insertBefore: HTMLElement): Promise<boolean> {
1456 const {frames} = await this._getInlineFrames(debuggerModel, url, lineNumber, columnNumber);
1457 if (!frames.length) {
1458 return false;
1459 }
1460
1461 for (let f = 0; f < frames.length; ++f) {
1462 const {name} = frames[f];
1463 const formattedLine = document.createElement('span');
1464 formattedLine.appendChild(this._linkifyStringAsFragment(`${prefix} ${name} (`));
1465 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
1466 debuggerModel.target(), null, url, lineNumber,
1467 {columnNumber, className: undefined, tabStop: undefined, inlineFrameIndex: f});
1468 scriptLocationLink.tabIndex = -1;
1469 this._selectableChildren.push({element: scriptLocationLink, forceSelect: (): void => scriptLocationLink.focus()});
1470 formattedLine.appendChild(scriptLocationLink);
1471 formattedLine.appendChild(this._linkifyStringAsFragment(suffix));
1472 stackTrace.insertBefore(formattedLine, insertBefore);
1473 }
1474 return true;
1475 }
1476
Jan Scheffler19cd7ec2021-02-12 14:16:301477 _tryFormatAsError(string: string): HTMLElement|null {
1478 function startsWith(prefix: string): boolean {
Blink Reformat4c46d092018-04-07 15:32:371479 return string.startsWith(prefix);
1480 }
1481
Sigurd Schneider45f32c32020-10-13 13:32:051482 const runtimeModel = this._message.runtimeModel();
Sigurd Schneider8d0fe542021-03-17 12:28:291483 // TODO: Consider removing these in favor of a simpler regex.
Mathias Bynensfdcb3012021-03-17 14:07:211484 const errorPrefixes = [
1485 'AggregateError',
1486 'Error',
1487 'EvalError',
1488 'RangeError',
1489 'ReferenceError',
1490 'SyntaxError',
1491 'TypeError',
1492 'URIError',
1493 ];
Sigurd Schneider8d0fe542021-03-17 12:28:291494 if (!runtimeModel || !errorPrefixes.some(startsWith) && !/^[\w.]+Error\b/.test(string)) {
Blink Reformat4c46d092018-04-07 15:32:371495 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341496 }
Sigurd Schneider45f32c32020-10-13 13:32:051497 const debuggerModel = runtimeModel.debuggerModel();
1498 const baseURL = runtimeModel.target().inspectedURL();
Blink Reformat4c46d092018-04-07 15:32:371499
1500 const lines = string.split('\n');
Philip Pfaffe068b01d2021-03-22 09:46:261501 const linkInfos = [];
1502 for (const line of lines) {
1503 const isCallFrameLine = /^\s*at\s/.test(line);
1504 if (!isCallFrameLine && linkInfos.length && linkInfos[linkInfos.length - 1].link) {
Blink Reformat4c46d092018-04-07 15:32:371505 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341506 }
Blink Reformat4c46d092018-04-07 15:32:371507
Tim van der Lippe1d6e57a2019-09-30 11:55:341508 if (!isCallFrameLine) {
Philip Pfaffe068b01d2021-03-22 09:46:261509 linkInfos.push({line});
Blink Reformat4c46d092018-04-07 15:32:371510 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341511 }
Blink Reformat4c46d092018-04-07 15:32:371512
1513 let openBracketIndex = -1;
1514 let closeBracketIndex = -1;
Yang Guo39256bd2019-07-18 06:02:251515 const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g;
1516 const inBrackets = /\([^\)\(]+\)/g;
Jan Scheffler19cd7ec2021-02-12 14:16:301517 let lastMatch: RegExpExecArray|null = null;
Yang Guo39256bd2019-07-18 06:02:251518 let currentMatch;
Philip Pfaffe068b01d2021-03-22 09:46:261519 while ((currentMatch = inBracketsWithLineAndColumn.exec(line))) {
Yang Guo39256bd2019-07-18 06:02:251520 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341521 }
Yang Guo39256bd2019-07-18 06:02:251522 if (!lastMatch) {
Philip Pfaffe068b01d2021-03-22 09:46:261523 while ((currentMatch = inBrackets.exec(line))) {
Yang Guo39256bd2019-07-18 06:02:251524 lastMatch = currentMatch;
Tim van der Lippe1d6e57a2019-09-30 11:55:341525 }
Yang Guo39256bd2019-07-18 06:02:251526 }
1527 if (lastMatch) {
1528 openBracketIndex = lastMatch.index;
1529 closeBracketIndex = lastMatch.index + lastMatch[0].length - 1;
Blink Reformat4c46d092018-04-07 15:32:371530 }
1531 const hasOpenBracket = openBracketIndex !== -1;
Philip Pfaffe068b01d2021-03-22 09:46:261532 let left = hasOpenBracket ? openBracketIndex + 1 : line.indexOf('at') + 3;
1533 if (!hasOpenBracket && line.indexOf('async ') === left) {
Benedikt Meureraef3b592021-03-17 16:34:401534 left += 6;
1535 }
Philip Pfaffe068b01d2021-03-22 09:46:261536 const right = hasOpenBracket ? closeBracketIndex : line.length;
1537 const linkCandidate = line.substring(left, right);
Tim van der Lippe9b2f8712020-02-12 17:46:221538 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate);
Tim van der Lippe1d6e57a2019-09-30 11:55:341539 if (!splitResult) {
Blink Reformat4c46d092018-04-07 15:32:371540 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341541 }
Blink Reformat4c46d092018-04-07 15:32:371542
Tim van der Lippe1d6e57a2019-09-30 11:55:341543 if (splitResult.url === '<anonymous>') {
Philip Pfaffe068b01d2021-03-22 09:46:261544 linkInfos.push({line});
Blink Reformat4c46d092018-04-07 15:32:371545 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341546 }
Blink Reformat4c46d092018-04-07 15:32:371547 let url = parseOrScriptMatch(splitResult.url);
Tim van der Lippe9b2f8712020-02-12 17:46:221548 if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) {
1549 url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url));
Tim van der Lippe1d6e57a2019-09-30 11:55:341550 }
1551 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371552 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341553 }
Blink Reformat4c46d092018-04-07 15:32:371554
Philip Pfaffe068b01d2021-03-22 09:46:261555 linkInfos.push({
1556 line,
1557 link: {
1558 url,
1559 enclosedInBraces: hasOpenBracket,
1560 positionLeft: left,
1561 positionRight: right,
1562 lineNumber: splitResult.lineNumber,
1563 columnNumber: splitResult.columnNumber,
1564 },
Blink Reformat4c46d092018-04-07 15:32:371565 });
1566 }
1567
Philip Pfaffe068b01d2021-03-22 09:46:261568 if (!linkInfos.length) {
Blink Reformat4c46d092018-04-07 15:32:371569 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341570 }
Blink Reformat4c46d092018-04-07 15:32:371571
Jan Scheffler19cd7ec2021-02-12 14:16:301572 const formattedResult = document.createElement('span');
Philip Pfaffe068b01d2021-03-22 09:46:261573 for (let i = 0; i < linkInfos.length; ++i) {
1574 const newline = i < linkInfos.length - 1 ? '\n' : '';
1575 const {line, link} = linkInfos[i];
1576 if (!link) {
1577 formattedResult.appendChild(this._linkifyStringAsFragment(`${line}${newline}`));
1578 continue;
1579 }
1580 const formattedLine = document.createElement('span');
1581 const prefix = line.substring(0, link.positionLeft);
1582 const suffix = `${line.substring(link.positionRight)}${newline}`;
1583
1584 formattedLine.appendChild(this._linkifyStringAsFragment(prefix));
Erik Luo182bece2018-11-29 03:15:221585 const scriptLocationLink = this._linkifier.linkifyScriptLocation(
Philip Pfaffe068b01d2021-03-22 09:46:261586 debuggerModel.target(), null, link.url, link.lineNumber,
1587 {columnNumber: link.columnNumber, className: undefined, tabStop: undefined, inlineFrameIndex: 0});
Erik Luo182bece2018-11-29 03:15:221588 scriptLocationLink.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:301589 this._selectableChildren.push({element: scriptLocationLink, forceSelect: (): void => scriptLocationLink.focus()});
Philip Pfaffe068b01d2021-03-22 09:46:261590 formattedLine.appendChild(scriptLocationLink);
1591 formattedLine.appendChild(this._linkifyStringAsFragment(suffix));
1592 formattedResult.appendChild(formattedLine);
Blink Reformat4c46d092018-04-07 15:32:371593
Philip Pfaffe068b01d2021-03-22 09:46:261594 if (!link.enclosedInBraces) {
1595 continue;
1596 }
1597
1598 const prefixWithoutFunction = prefix.substring(0, prefix.lastIndexOf(' ', prefix.length - 3));
1599
1600 // If we were able to parse the function name from the stack trace line, try to replace it with an expansion of
1601 // any inline frames.
1602 const selectableChildIndex = this._selectableChildren.length - 1;
1603 this._expandInlineStackFrames(
1604 debuggerModel, prefixWithoutFunction, suffix, link.url, link.lineNumber, link.columnNumber,
1605 formattedResult, formattedLine)
1606 .then(modified => {
1607 if (modified) {
1608 formattedResult.removeChild(formattedLine);
1609 this._selectableChildren.splice(selectableChildIndex, 1);
1610 }
1611 });
Tim van der Lippe1d6e57a2019-09-30 11:55:341612 }
Blink Reformat4c46d092018-04-07 15:32:371613
1614 return formattedResult;
1615
Jan Scheffler19cd7ec2021-02-12 14:16:301616 function parseOrScriptMatch(url: string|null): string|null {
Tim van der Lippe1d6e57a2019-09-30 11:55:341617 if (!url) {
Blink Reformat4c46d092018-04-07 15:32:371618 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:341619 }
Tim van der Lippe9b2f8712020-02-12 17:46:221620 const parsedURL = Common.ParsedURL.ParsedURL.fromString(url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341621 if (parsedURL) {
Blink Reformat4c46d092018-04-07 15:32:371622 return parsedURL.url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341623 }
1624 if (debuggerModel.scriptsForSourceURL(url).length) {
Blink Reformat4c46d092018-04-07 15:32:371625 return url;
Tim van der Lippe1d6e57a2019-09-30 11:55:341626 }
Blink Reformat4c46d092018-04-07 15:32:371627 return null;
1628 }
1629 }
1630
Jan Scheffler19cd7ec2021-02-12 14:16:301631 _linkifyWithCustomLinkifier(
1632 string: string, linkifier: (arg0: string, arg1: string, arg2?: number, arg3?: number) => Node): DocumentFragment {
Sigurd Schneider8f4ac862020-10-13 13:30:111633 if (string.length > getMaxTokenizableStringLength()) {
Tim van der Lippe9b2f8712020-02-12 17:46:221634 const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
Sigurd Schneider8f4ac862020-10-13 13:30:111635 document.createElement('span'), string, getLongStringVisibleLength());
Sigurd Schneider45f32c32020-10-13 13:32:051636 const fragment = document.createDocumentFragment();
Connor Moody1a5c0d32019-12-19 07:23:361637 fragment.appendChild(propertyValue.element);
1638 return fragment;
Tim van der Lippe1d6e57a2019-09-30 11:55:341639 }
Sigurd Schneider45f32c32020-10-13 13:32:051640 const container = document.createDocumentFragment();
Tim van der Lippeeaacb722020-01-10 12:16:001641 const tokens = ConsoleViewMessage._tokenizeMessageText(string);
Blink Reformat4c46d092018-04-07 15:32:371642 for (const token of tokens) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341643 if (!token.text) {
Erik Luofc2214f2018-11-21 19:54:581644 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341645 }
Blink Reformat4c46d092018-04-07 15:32:371646 switch (token.type) {
1647 case 'url': {
1648 const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text);
Tim van der Lippe9b2f8712020-02-12 17:46:221649 const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL);
Kim-Anh Tran9e49a452020-02-17 09:46:101650 const sourceURL = Common.ParsedURL.ParsedURL.removeWasmFunctionInfoFromURL(splitResult.url);
Blink Reformat4c46d092018-04-07 15:32:371651 let linkNode;
Tim van der Lippe1d6e57a2019-09-30 11:55:341652 if (splitResult) {
Kim-Anh Tran9e49a452020-02-17 09:46:101653 linkNode = linkifier(token.text, sourceURL, splitResult.lineNumber, splitResult.columnNumber);
Tim van der Lippe1d6e57a2019-09-30 11:55:341654 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051655 linkNode = linkifier(token.text, '');
Tim van der Lippe1d6e57a2019-09-30 11:55:341656 }
Blink Reformat4c46d092018-04-07 15:32:371657 container.appendChild(linkNode);
1658 break;
1659 }
1660 default:
Sigurd Schneider45f32c32020-10-13 13:32:051661 container.appendChild(document.createTextNode(token.text));
Blink Reformat4c46d092018-04-07 15:32:371662 break;
1663 }
1664 }
1665 return container;
1666 }
1667
Jan Scheffler19cd7ec2021-02-12 14:16:301668 _linkifyStringAsFragment(string: string): DocumentFragment {
Erik Luofc2214f2018-11-21 19:54:581669 return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => {
Sigurd Schneider45f32c32020-10-13 13:32:051670 const options = {text, lineNumber, columnNumber};
Jan Scheffler19cd7ec2021-02-12 14:16:301671 const linkElement =
1672 Components.Linkifier.Linkifier.linkifyURL(url, (options as Components.Linkifier.LinkifyURLOptions));
Erik Luo383f21d2018-11-07 23:16:371673 linkElement.tabIndex = -1;
Jan Scheffler19cd7ec2021-02-12 14:16:301674 this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
Erik Luo383f21d2018-11-07 23:16:371675 return linkElement;
Blink Reformat4c46d092018-04-07 15:32:371676 });
1677 }
1678
Jan Scheffler19cd7ec2021-02-12 14:16:301679 static _tokenizeMessageText(string: string): {
1680 type?: string, text: string,
1681 }[] {
Sigurd Schneider30ac3dd2020-10-13 09:06:391682 const {tokenizerRegexes, tokenizerTypes} = getOrCreateTokenizers();
Sigurd Schneider8f4ac862020-10-13 13:30:111683 if (string.length > getMaxTokenizableStringLength()) {
Blink Reformat4c46d092018-04-07 15:32:371684 return [{text: string, type: undefined}];
Tim van der Lippe1d6e57a2019-09-30 11:55:341685 }
Sigurd Schneider30ac3dd2020-10-13 09:06:391686 const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, tokenizerRegexes);
1687 return results.map(result => ({text: result.value, type: tokenizerTypes[result.regexIndex]}));
Blink Reformat4c46d092018-04-07 15:32:371688 }
1689
Jan Scheffler19cd7ec2021-02-12 14:16:301690 groupKey(): string {
Tim van der Lippe1d6e57a2019-09-30 11:55:341691 if (!this._groupKey) {
Blink Reformat4c46d092018-04-07 15:32:371692 this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle();
Tim van der Lippe1d6e57a2019-09-30 11:55:341693 }
Blink Reformat4c46d092018-04-07 15:32:371694 return this._groupKey;
1695 }
1696
Jan Scheffler19cd7ec2021-02-12 14:16:301697 groupTitle(): string {
Tim van der Lippeeaacb722020-01-10 12:16:001698 const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText);
Blink Reformat4c46d092018-04-07 15:32:371699 const result = tokens.reduce((acc, token) => {
Jan Scheffler19cd7ec2021-02-12 14:16:301700 let text: Common.UIString.LocalizedString|string = token.text;
Tim van der Lippe1d6e57a2019-09-30 11:55:341701 if (token.type === 'url') {
Christy Chen9c6d8982021-02-08 02:28:311702 text = i18nString(UIStrings.url);
Tim van der Lippe1d6e57a2019-09-30 11:55:341703 } else if (token.type === 'time') {
Christy Chen9c6d8982021-02-08 02:28:311704 text = i18nString(UIStrings.tookNms);
Tim van der Lippe1d6e57a2019-09-30 11:55:341705 } else if (token.type === 'event') {
Christy Chen9c6d8982021-02-08 02:28:311706 text = i18nString(UIStrings.someEvent);
Tim van der Lippe1d6e57a2019-09-30 11:55:341707 } else if (token.type === 'milestone') {
Christy Chen9c6d8982021-02-08 02:28:311708 text = i18nString(UIStrings.Mxx);
Tim van der Lippe1d6e57a2019-09-30 11:55:341709 } else if (token.type === 'autofill') {
Christy Chen9c6d8982021-02-08 02:28:311710 text = i18nString(UIStrings.attribute);
Tim van der Lippe1d6e57a2019-09-30 11:55:341711 }
Blink Reformat4c46d092018-04-07 15:32:371712 return acc + text;
1713 }, '');
1714 return result.replace(/[%]o/g, '');
1715 }
Paul Lewisbf7aa3c2019-11-20 17:03:381716}
Blink Reformat4c46d092018-04-07 15:32:371717
Jan Scheffler19cd7ec2021-02-12 14:16:301718let tokenizerRegexes: RegExp[]|null = null;
1719let tokenizerTypes: string[]|null = null;
Sigurd Schneider30ac3dd2020-10-13 09:06:391720
Jan Scheffler19cd7ec2021-02-12 14:16:301721function getOrCreateTokenizers(): {
1722 tokenizerRegexes: Array<RegExp>,
1723 tokenizerTypes: Array<string>,
1724} {
Sigurd Schneider30ac3dd2020-10-13 09:06:391725 if (!tokenizerRegexes || !tokenizerTypes) {
1726 const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f';
1727 const linkStringRegex = new RegExp(
1728 '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes +
1729 '"\')}\\],:;.!?]',
1730 'u');
1731 const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/;
1732 const timeRegex = /took [\d]+ms/;
1733 const eventRegex = /'\w+' event/;
1734 const milestoneRegex = /\sM[6-7]\d/;
1735 const autofillRegex = /\(suggested: \"[\w-]+\"\)/;
Jan Scheffler19cd7ec2021-02-12 14:16:301736 const handlers = new Map<RegExp, string>();
Sigurd Schneider30ac3dd2020-10-13 09:06:391737 handlers.set(linkStringRegex, 'url');
1738 handlers.set(pathLineRegex, 'url');
1739 handlers.set(timeRegex, 'time');
1740 handlers.set(eventRegex, 'event');
1741 handlers.set(milestoneRegex, 'milestone');
1742 handlers.set(autofillRegex, 'autofill');
1743 tokenizerRegexes = Array.from(handlers.keys());
1744 tokenizerTypes = Array.from(handlers.values());
1745 return {tokenizerRegexes, tokenizerTypes};
1746 }
1747 return {tokenizerRegexes, tokenizerTypes};
1748}
1749
Paul Lewisbf7aa3c2019-11-20 17:03:381750export class ConsoleGroupViewMessage extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301751 _collapsed: boolean;
1752 _expandGroupIcon: UI.Icon.Icon|null;
1753 _onToggle: () => void;
1754
1755 constructor(
1756 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
1757 onToggle: () => void, onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Blink Reformat4c46d092018-04-07 15:32:371758 console.assert(consoleMessage.isGroupStartMessage());
Tim van der Lippeb45d9a02019-11-05 17:24:411759 super(consoleMessage, linkifier, nestingLevel, onResize);
Tim van der Lippe9b2f8712020-02-12 17:46:221760 this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed;
Blink Reformat4c46d092018-04-07 15:32:371761 this._expandGroupIcon = null;
Erik Luo8ef5d0c2018-09-25 21:16:001762 this._onToggle = onToggle;
Blink Reformat4c46d092018-04-07 15:32:371763 }
1764
Jan Scheffler19cd7ec2021-02-12 14:16:301765 _setCollapsed(collapsed: boolean): void {
Blink Reformat4c46d092018-04-07 15:32:371766 this._collapsed = collapsed;
Tim van der Lippe1d6e57a2019-09-30 11:55:341767 if (this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371768 this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down');
Tim van der Lippe1d6e57a2019-09-30 11:55:341769 }
Erik Luo8ef5d0c2018-09-25 21:16:001770 this._onToggle.call(null);
Blink Reformat4c46d092018-04-07 15:32:371771 }
1772
Jan Scheffler19cd7ec2021-02-12 14:16:301773 collapsed(): boolean {
Blink Reformat4c46d092018-04-07 15:32:371774 return this._collapsed;
1775 }
1776
Jan Scheffler19cd7ec2021-02-12 14:16:301777 maybeHandleOnKeyDown(event: KeyboardEvent): boolean {
Erik Luo0b8282e2018-10-08 20:37:461778 const focusedChildIndex = this._focusedChildIndex();
1779 if (focusedChildIndex === -1) {
1780 if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) {
1781 this._setCollapsed(!this._collapsed);
1782 return true;
1783 }
Erik Luo8ef5d0c2018-09-25 21:16:001784 }
1785 return super.maybeHandleOnKeyDown(event);
1786 }
1787
Jan Scheffler19cd7ec2021-02-12 14:16:301788 toMessageElement(): HTMLElement {
1789 let element: HTMLElement|null = this._element || null;
Sigurd Schneider45f32c32020-10-13 13:32:051790 if (!element) {
1791 element = super.toMessageElement();
Erik Luo8ef5d0c2018-09-25 21:16:001792 const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down';
Tim van der Lippe9b2f8712020-02-12 17:46:221793 this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon');
Erik Luob5bfff42018-09-20 02:52:391794 // Intercept focus to avoid highlight on click.
Sigurd Schneider45f32c32020-10-13 13:32:051795 this.contentElement().tabIndex = -1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341796 if (this._repeatCountElement) {
Blink Reformat4c46d092018-04-07 15:32:371797 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341798 } else {
Sigurd Schneider45f32c32020-10-13 13:32:051799 element.insertBefore(this._expandGroupIcon, this._contentElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:341800 }
Sigurd Schneider45f32c32020-10-13 13:32:051801 element.addEventListener('click', () => this._setCollapsed(!this._collapsed));
Blink Reformat4c46d092018-04-07 15:32:371802 }
Sigurd Schneider45f32c32020-10-13 13:32:051803 return element;
Blink Reformat4c46d092018-04-07 15:32:371804 }
1805
Jan Scheffler19cd7ec2021-02-12 14:16:301806 _showRepeatCountElement(): void {
Blink Reformat4c46d092018-04-07 15:32:371807 super._showRepeatCountElement();
Tim van der Lippe1d6e57a2019-09-30 11:55:341808 if (this._repeatCountElement && this._expandGroupIcon) {
Blink Reformat4c46d092018-04-07 15:32:371809 this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild);
Tim van der Lippe1d6e57a2019-09-30 11:55:341810 }
Blink Reformat4c46d092018-04-07 15:32:371811 }
Paul Lewisbf7aa3c2019-11-20 17:03:381812}
Blink Reformat4c46d092018-04-07 15:32:371813
Sigurd Schneiderca7b4ff2020-10-14 07:45:471814export class ConsoleCommand extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301815 _formattedCommand: HTMLElement|null;
1816
1817 constructor(
1818 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
1819 onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471820 super(consoleMessage, linkifier, nestingLevel, onResize);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471821 this._formattedCommand = null;
1822 }
1823
Jan Scheffler19cd7ec2021-02-12 14:16:301824 contentElement(): HTMLElement {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471825 const contentElement = this.getContentElement();
1826 if (contentElement) {
1827 return contentElement;
1828 }
Jan Scheffler19cd7ec2021-02-12 14:16:301829 const newContentElement = (document.createElement('div') as HTMLElement);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471830 this.setContentElement(newContentElement);
1831 newContentElement.classList.add('console-user-command');
1832 const icon = UI.Icon.Icon.create('smallicon-user-command', 'command-result-icon');
1833 newContentElement.appendChild(icon);
1834
1835 elementToMessage.set(newContentElement, this);
Jan Scheffler19cd7ec2021-02-12 14:16:301836 this._formattedCommand = (document.createElement('span') as HTMLElement);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471837 this._formattedCommand.classList.add('source-code');
1838 this._formattedCommand.textContent = Platform.StringUtilities.replaceControlCharacters(this.text);
1839 newContentElement.appendChild(this._formattedCommand);
1840
1841 if (this._formattedCommand.textContent.length < MaxLengthToIgnoreHighlighter) {
Andres Olivaresf59e6de2021-02-18 13:10:251842 const javascriptSyntaxHighlighter = new TextEditor.SyntaxHighlighter.SyntaxHighlighter('text/javascript', true);
Sigurd Schneiderca7b4ff2020-10-14 07:45:471843 javascriptSyntaxHighlighter.syntaxHighlightNode(this._formattedCommand).then(this._updateSearch.bind(this));
1844 } else {
1845 this._updateSearch();
1846 }
1847
1848 this.updateTimestamp();
1849 return newContentElement;
1850 }
1851
Jan Scheffler19cd7ec2021-02-12 14:16:301852 _updateSearch(): void {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471853 this.setSearchRegex(this.searchRegex());
1854 }
1855}
1856
1857export class ConsoleCommandResult extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301858 contentElement(): HTMLElement {
Sigurd Schneiderca7b4ff2020-10-14 07:45:471859 const element = super.contentElement();
1860 if (!element.classList.contains('console-user-command-result')) {
1861 element.classList.add('console-user-command-result');
1862 if (this.consoleMessage().level === SDK.ConsoleModel.MessageLevel.Info) {
1863 const icon = UI.Icon.Icon.create('smallicon-command-result', 'command-result-icon');
1864 element.insertBefore(icon, element.firstChild);
1865 }
1866 }
1867 return element;
1868 }
1869}
1870
Sigurd Schneider8bfb4212020-10-27 10:27:371871export class ConsoleTableMessageView extends ConsoleViewMessage {
Jan Scheffler19cd7ec2021-02-12 14:16:301872 _dataGrid: DataGrid.SortableDataGrid.SortableDataGrid<unknown>|null;
1873
1874 constructor(
1875 consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number,
1876 onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) {
Sigurd Schneider8bfb4212020-10-27 10:27:371877 super(consoleMessage, linkifier, nestingLevel, onResize);
1878 console.assert(consoleMessage.type === SDK.ConsoleModel.MessageType.Table);
Sigurd Schneider8bfb4212020-10-27 10:27:371879 this._dataGrid = null;
1880 }
1881
Jan Scheffler19cd7ec2021-02-12 14:16:301882 wasShown(): void {
Sigurd Schneider8bfb4212020-10-27 10:27:371883 if (this._dataGrid) {
1884 this._dataGrid.updateWidths();
1885 }
1886 super.wasShown();
1887 }
1888
Jan Scheffler19cd7ec2021-02-12 14:16:301889 onResize(): void {
Sigurd Schneider8bfb4212020-10-27 10:27:371890 if (!this.isVisible()) {
1891 return;
1892 }
1893 if (this._dataGrid) {
1894 this._dataGrid.onResize();
1895 }
1896 }
1897
Jan Scheffler19cd7ec2021-02-12 14:16:301898 contentElement(): HTMLElement {
Sigurd Schneider8bfb4212020-10-27 10:27:371899 const contentElement = this.getContentElement();
1900 if (contentElement) {
1901 return contentElement;
1902 }
1903
Jan Scheffler19cd7ec2021-02-12 14:16:301904 const newContentElement = (document.createElement('div') as HTMLElement);
Sigurd Schneider8bfb4212020-10-27 10:27:371905 newContentElement.classList.add('console-message');
1906 if (this._messageLevelIcon) {
1907 newContentElement.appendChild(this._messageLevelIcon);
1908 }
1909 this.setContentElement(newContentElement);
1910
1911 newContentElement.appendChild(this._buildTableMessage());
1912 this.updateTimestamp();
1913 return newContentElement;
1914 }
1915
Jan Scheffler19cd7ec2021-02-12 14:16:301916 _buildTableMessage(): HTMLElement {
1917 const formattedMessage = (document.createElement('span') as HTMLElement);
Sigurd Schneider8bfb4212020-10-27 10:27:371918 formattedMessage.classList.add('source-code');
1919 this._anchorElement = this._buildMessageAnchor();
1920 if (this._anchorElement) {
1921 formattedMessage.appendChild(this._anchorElement);
1922 }
1923
1924 const table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null;
1925 if (!table) {
1926 return this._buildMessage();
1927 }
1928 const actualTable = parameterToRemoteObject(this._message.runtimeModel())(table);
1929 if (!actualTable || !actualTable.preview) {
1930 return this._buildMessage();
1931 }
1932
1933 const rawValueColumnSymbol = Symbol('rawValueColumn');
Jan Scheffler19cd7ec2021-02-12 14:16:301934 const columnNames: (string|symbol)[] = [];
Sigurd Schneider8bfb4212020-10-27 10:27:371935 const preview = actualTable.preview;
1936 const rows = [];
1937 for (let i = 0; i < preview.properties.length; ++i) {
1938 const rowProperty = preview.properties[i];
Jan Scheffler19cd7ec2021-02-12 14:16:301939 let rowSubProperties: Protocol.Runtime.PropertyPreview[];
Sigurd Schneiderb393a432020-11-06 12:08:211940 if (rowProperty.valuePreview && rowProperty.valuePreview.properties.length) {
Sigurd Schneider8bfb4212020-10-27 10:27:371941 rowSubProperties = rowProperty.valuePreview.properties;
1942 } else if (rowProperty.value) {
Jan Scheffler19cd7ec2021-02-12 14:16:301943 rowSubProperties =
1944 [{name: rawValueColumnSymbol as unknown as string, type: rowProperty.type, value: rowProperty.value}];
Sigurd Schneider8bfb4212020-10-27 10:27:371945 } else {
1946 continue;
1947 }
1948
Jan Scheffler19cd7ec2021-02-12 14:16:301949 const rowValue = new Map<string|symbol, HTMLElement>();
Sigurd Schneider8bfb4212020-10-27 10:27:371950 const maxColumnsToRender = 20;
1951 for (let j = 0; j < rowSubProperties.length; ++j) {
1952 const cellProperty = rowSubProperties[j];
Jan Scheffler19cd7ec2021-02-12 14:16:301953 let columnRendered: true|boolean = columnNames.indexOf(cellProperty.name) !== -1;
Sigurd Schneider8bfb4212020-10-27 10:27:371954 if (!columnRendered) {
1955 if (columnNames.length === maxColumnsToRender) {
1956 continue;
1957 }
1958 columnRendered = true;
1959 columnNames.push(cellProperty.name);
1960 }
1961
1962 if (columnRendered) {
1963 const cellElement =
1964 this._renderPropertyPreviewOrAccessor(actualTable, cellProperty, [rowProperty, cellProperty]);
1965 cellElement.classList.add('console-message-nowrap-below');
1966 rowValue.set(cellProperty.name, cellElement);
1967 }
1968 }
1969 rows.push({rowName: rowProperty.name, rowValue});
1970 }
1971
1972 const flatValues = [];
1973 for (const {rowName, rowValue} of rows) {
1974 flatValues.push(rowName);
1975 for (let j = 0; j < columnNames.length; ++j) {
1976 flatValues.push(rowValue.get(columnNames[j]));
1977 }
1978 }
Christy Chen9c6d8982021-02-08 02:28:311979 columnNames.unshift(i18nString(UIStrings.index));
Sigurd Schneider8bfb4212020-10-27 10:27:371980 const columnDisplayNames =
Christy Chen9c6d8982021-02-08 02:28:311981 columnNames.map(name => name === rawValueColumnSymbol ? i18nString(UIStrings.value) : name.toString());
Sigurd Schneider8bfb4212020-10-27 10:27:371982
1983 if (flatValues.length) {
Christy Chen9c6d8982021-02-08 02:28:311984 this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create(
1985 columnDisplayNames, flatValues, i18nString(UIStrings.console));
Sigurd Schneider8bfb4212020-10-27 10:27:371986 if (this._dataGrid) {
1987 this._dataGrid.setStriped(true);
1988 this._dataGrid.setFocusable(false);
1989
1990 const formattedResult = document.createElement('span');
1991 formattedResult.classList.add('console-message-text');
1992 const tableElement = formattedResult.createChild('div', 'console-message-formatted-table');
1993 const dataGridContainer = tableElement.createChild('span');
1994 tableElement.appendChild(this._formatParameter(actualTable, true, false));
1995 dataGridContainer.appendChild(this._dataGrid.element);
1996 formattedMessage.appendChild(formattedResult);
1997 this._dataGrid.renderInline();
1998 }
1999 }
2000 return formattedMessage;
2001 }
2002
Jan Scheffler19cd7ec2021-02-12 14:16:302003 approximateFastHeight(): number {
Sigurd Schneider8bfb4212020-10-27 10:27:372004 const table = this._message.parameters && this._message.parameters[0];
2005 if (table && typeof table !== 'string' && table.preview) {
2006 return defaultConsoleRowHeight * table.preview.properties.length;
2007 }
2008 return defaultConsoleRowHeight;
2009 }
2010}
2011
Sigurd Schneiderca7b4ff2020-10-14 07:45:472012/**
2013 * The maximum length before strings are considered too long for syntax highlighting.
2014 * @const
Sigurd Schneiderca7b4ff2020-10-14 07:45:472015 */
Jan Scheffler19cd7ec2021-02-12 14:16:302016const MaxLengthToIgnoreHighlighter: number = 10000;
Sigurd Schneiderca7b4ff2020-10-14 07:45:472017
Blink Reformat4c46d092018-04-07 15:32:372018/**
2019 * @const
Blink Reformat4c46d092018-04-07 15:32:372020 */
Jan Scheffler19cd7ec2021-02-12 14:16:302021export const MaxLengthForLinks: number = 40;
Blink Reformat4c46d092018-04-07 15:32:372022
Sigurd Schneider17c74452021-02-15 12:14:102023let maxTokenizableStringLength = 10000;
2024let longStringVisibleLength = 5000;
Sigurd Schneider8f4ac862020-10-13 13:30:112025
Jan Scheffler19cd7ec2021-02-12 14:16:302026export const getMaxTokenizableStringLength = (): number => {
Sigurd Schneider17c74452021-02-15 12:14:102027 return maxTokenizableStringLength;
Sigurd Schneider8f4ac862020-10-13 13:30:112028};
2029
Jan Scheffler19cd7ec2021-02-12 14:16:302030export const setMaxTokenizableStringLength = (length: number): void => {
Sigurd Schneider17c74452021-02-15 12:14:102031 maxTokenizableStringLength = length;
Sigurd Schneider8f4ac862020-10-13 13:30:112032};
2033
Jan Scheffler19cd7ec2021-02-12 14:16:302034export const getLongStringVisibleLength = (): number => {
Sigurd Schneider17c74452021-02-15 12:14:102035 return longStringVisibleLength;
Sigurd Schneider8f4ac862020-10-13 13:30:112036};
2037
Jan Scheffler19cd7ec2021-02-12 14:16:302038export const setLongStringVisibleLength = (length: number): void => {
Sigurd Schneider17c74452021-02-15 12:14:102039 longStringVisibleLength = length;
Sigurd Schneider8f4ac862020-10-13 13:30:112040};