chrome-devtools-frontend
Version:
Chrome DevTools UI
1,208 lines (1,112 loc) • 81.9 kB
text/typescript
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
* Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2009 Joseph Pecoraro
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import * as Common from '../../core/common/common.js';
import * as i18n from '../../core/i18n/i18n.js';
import * as Platform from '../../core/platform/platform.js';
import * as SDK from '../../core/sdk/sdk.js';
import * as Protocol from '../../generated/protocol.js';
import * as Bindings from '../../models/bindings/bindings.js';
import type * as IssuesManager from '../../models/issues_manager/issues_manager.js';
import * as Logs from '../../models/logs/logs.js';
import * as Host from '../../core/host/host.js';
import * as TextUtils from '../../models/text_utils/text_utils.js';
import * as Workspace from '../../models/workspace/workspace.js';
import * as CodeHighlighter from '../../ui/components/code_highlighter/code_highlighter.js';
import * as IconButton from '../../ui/components/icon_button/icon_button.js';
import * as IssueCounter from '../../ui/components/issue_counter/issue_counter.js';
import * as RequestLinkIcon from '../../ui/components/request_link_icon/request_link_icon.js';
import * as DataGrid from '../../ui/legacy/components/data_grid/data_grid.js';
import * as ObjectUI from '../../ui/legacy/components/object_ui/object_ui.js';
import * as Components from '../../ui/legacy/components/utils/utils.js';
import * as UI from '../../ui/legacy/legacy.js';
// eslint-disable-next-line rulesdir/es_modules_import
import objectValueStyles from '../../ui/legacy/components/object_ui/objectValue.css.js';
import {type Chrome} from '../../../extension-api/ExtensionAPI.js'; // eslint-disable-line rulesdir/es_modules_import
import {format, updateStyle} from './ConsoleFormat.js';
import {type ConsoleViewportElement} from './ConsoleViewport.js';
import consoleViewStyles from './consoleView.css.js';
import {augmentErrorStackWithScriptIds, parseSourcePositionsFromErrorStack} from './ErrorStackParser.js';
const UIStrings = {
/**
* @description Message element text content in Console View Message of the Console panel. Shown
* when the user tried to run console.clear() but the 'Preserve log' option is enabled, which stops
* the log from being cleared.
*/
consoleclearWasPreventedDueTo: '`console.clear()` was prevented due to \'Preserve log\'',
/**
* @description Text shown in the Console panel after the user has cleared the console, which
* removes all messages from the console so that it is empty.
*/
consoleWasCleared: 'Console was cleared',
/**
*@description Message element title in Console View Message of the Console panel
*@example {Ctrl+L} PH1
*/
clearAllMessagesWithS: 'Clear all messages with {PH1}',
/**
*@description Message prefix in Console View Message of the Console panel
*/
assertionFailed: 'Assertion failed: ',
/**
*@description Message text in Console View Message of the Console panel
*@example {console.log(1)} PH1
*/
violationS: '`[Violation]` {PH1}',
/**
*@description Message text in Console View Message of the Console panel
*@example {console.log(1)} PH1
*/
interventionS: '`[Intervention]` {PH1}',
/**
*@description Message text in Console View Message of the Console panel
*@example {console.log(1)} PH1
*/
deprecationS: '`[Deprecation]` {PH1}',
/**
*@description Note title in Console View Message of the Console panel
*/
thisValueWillNotBeCollectedUntil: 'This value will not be collected until console is cleared.',
/**
*@description Note title in Console View Message of the Console panel
*/
thisValueWasEvaluatedUponFirst: 'This value was evaluated upon first expanding. It may have changed since then.',
/**
*@description Note title in Console View Message of the Console panel
*/
functionWasResolvedFromBound: 'Function was resolved from bound function.',
/**
* @description Shown in the Console panel when an exception is thrown when trying to access a
* property on an object. Should be translated.
*/
exception: '<exception>',
/**
*@description Text to indicate an item is a warning
*/
warning: 'Warning',
/**
*@description Text for errors
*/
error: 'Error',
/**
* @description Accessible label for an icon. The icon is used to mark console messages that
* originate from a logpoint. Logpoints are special breakpoints that log a user-provided JavaScript
* expression to the DevTools console.
*/
logpoint: 'Logpoint',
/**
* @description Accessible label for an icon. The icon is used to mark console messages that
* originate from conditional breakpoints.
*/
cndBreakpoint: 'Conditional Breakpoint',
/**
* @description Announced by the screen reader to indicate how many times a particular message in
* the console was repeated.
*/
repeatS: '{n, plural, =1 {Repeated # time} other {Repeated # times}}',
/**
* @description Announced by the screen reader to indicate how many times a particular warning
* message in the console was repeated.
*/
warningS: '{n, plural, =1 {Warning, Repeated # time} other {Warning, Repeated # times}}',
/**
* @description Announced by the screen reader to indicate how many times a particular error
* message in the console was repeated.
*/
errorS: '{n, plural, =1 {Error, Repeated # time} other {Error, Repeated # times}}',
/**
*@description Text appended to grouped console messages that are related to URL requests
*/
url: '<URL>',
/**
*@description Text appended to grouped console messages about tasks that took longer than N ms
*/
tookNms: 'took <N>ms',
/**
*@description Text appended to grouped console messages about tasks that are related to some DOM event
*/
someEvent: '<some> event',
/**
*@description Text appended to grouped console messages about tasks that are related to a particular milestone
*/
Mxx: ' M<XX>',
/**
*@description Text appended to grouped console messages about tasks that are related to autofill completions
*/
attribute: '<attribute>',
/**
*@description Text for the index of something
*/
index: '(index)',
/**
*@description Text for the value of something
*/
value: 'Value',
/**
*@description Title of the Console tool
*/
console: 'Console',
/**
*@description Message to indicate a console message with a stack table is expanded
*/
stackMessageExpanded: 'Stack table expanded',
/**
*@description Message to indicate a console message with a stack table is collapsed
*/
stackMessageCollapsed: 'Stack table collapsed',
};
const str_ = i18n.i18n.registerUIStrings('panels/console/ConsoleViewMessage.ts', UIStrings);
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
const elementToMessage = new WeakMap<Element, ConsoleViewMessage>();
export const getMessageForElement = (element: Element): ConsoleViewMessage|undefined => {
return elementToMessage.get(element);
};
// This value reflects the 18px min-height of .console-message, plus the
// 1px border of .console-message-wrapper. Keep in sync with consoleView.css.
const defaultConsoleRowHeight = 19;
const parameterToRemoteObject = (runtimeModel: SDK.RuntimeModel.RuntimeModel|null): (
parameter?: SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|string) => SDK.RemoteObject.RemoteObject =>
(parameter?: string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject): SDK.RemoteObject.RemoteObject => {
if (parameter instanceof SDK.RemoteObject.RemoteObject) {
return parameter;
}
if (!runtimeModel) {
return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter);
}
if (typeof parameter === 'object') {
return runtimeModel.createRemoteObject(parameter);
}
return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter);
};
export class ConsoleViewMessage implements ConsoleViewportElement {
protected message: SDK.ConsoleModel.ConsoleMessage;
private readonly linkifier: Components.Linkifier.Linkifier;
private repeatCountInternal: number;
private closeGroupDecorationCount: number;
private consoleGroupInternal: ConsoleGroupViewMessage|null;
private selectableChildren: {
element: HTMLElement,
forceSelect: () => void,
}[];
private readonly messageResized: (arg0: Common.EventTarget.EventTargetEvent<UI.TreeOutline.TreeElement>) => void;
protected elementInternal: HTMLElement|null;
private readonly previewFormatter: ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter;
private searchRegexInternal: RegExp|null;
protected messageIcon: IconButton.Icon.Icon|null;
private traceExpanded: boolean;
private expandTrace: ((arg0: boolean) => void)|null;
protected anchorElement: HTMLElement|null;
protected contentElementInternal: HTMLElement|null;
private nestingLevelMarkers: HTMLElement[]|null;
private searchHighlightNodes: Element[];
private searchHighlightNodeChanges: UI.UIUtils.HighlightChange[];
private isVisibleInternal: boolean;
private cachedHeight: number;
private messagePrefix: string;
private timestampElement: HTMLElement|null;
private inSimilarGroup: boolean;
private similarGroupMarker: HTMLElement|null;
private lastInSimilarGroup: boolean;
private groupKeyInternal: string;
protected repeatCountElement: UI.UIUtils.DevToolsSmallBubble|null;
private requestResolver: Logs.RequestResolver.RequestResolver;
private issueResolver: IssuesManager.IssueResolver.IssueResolver;
#adjacentUserCommandResult: boolean = false;
/** Formatting Error#stack is asynchronous. Allow tests to wait for the result */
#formatErrorStackPromiseForTest = Promise.resolve();
constructor(
consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier,
requestResolver: Logs.RequestResolver.RequestResolver, issueResolver: IssuesManager.IssueResolver.IssueResolver,
onResize: (arg0: Common.EventTarget.EventTargetEvent<UI.TreeOutline.TreeElement>) => void) {
this.message = consoleMessage;
this.linkifier = linkifier;
this.requestResolver = requestResolver;
this.issueResolver = issueResolver;
this.repeatCountInternal = 1;
this.closeGroupDecorationCount = 0;
this.selectableChildren = [];
this.messageResized = onResize;
this.elementInternal = null;
this.previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter();
this.searchRegexInternal = null;
this.messageIcon = null;
this.traceExpanded = false;
this.expandTrace = null;
this.anchorElement = null;
this.contentElementInternal = null;
this.nestingLevelMarkers = null;
this.searchHighlightNodes = [];
this.searchHighlightNodeChanges = [];
this.isVisibleInternal = false;
this.cachedHeight = 0;
this.messagePrefix = '';
this.timestampElement = null;
this.inSimilarGroup = false;
this.similarGroupMarker = null;
this.lastInSimilarGroup = false;
this.groupKeyInternal = '';
this.repeatCountElement = null;
this.consoleGroupInternal = null;
}
element(): HTMLElement {
return this.toMessageElement();
}
wasShown(): void {
this.isVisibleInternal = true;
}
onResize(): void {
}
willHide(): void {
this.isVisibleInternal = false;
this.cachedHeight = this.element().offsetHeight;
}
isVisible(): boolean {
return this.isVisibleInternal;
}
fastHeight(): number {
if (this.cachedHeight) {
return this.cachedHeight;
}
return this.approximateFastHeight();
}
approximateFastHeight(): number {
return defaultConsoleRowHeight;
}
consoleMessage(): SDK.ConsoleModel.ConsoleMessage {
return this.message;
}
formatErrorStackPromiseForTest(): Promise<void> {
return this.#formatErrorStackPromiseForTest;
}
protected buildMessage(): HTMLElement {
let messageElement;
let messageText: Common.UIString.LocalizedString|string = this.message.messageText;
if (this.message.source === SDK.ConsoleModel.FrontendMessageSource.ConsoleAPI) {
switch (this.message.type) {
case Protocol.Runtime.ConsoleAPICalledEventType.Trace:
messageElement = this.format(this.message.parameters || ['console.trace']);
break;
case Protocol.Runtime.ConsoleAPICalledEventType.Clear:
messageElement = document.createElement('span');
messageElement.classList.add('console-info');
if (Common.Settings.Settings.instance().moduleSetting('preserveConsoleLog').get()) {
messageElement.textContent = i18nString(UIStrings.consoleclearWasPreventedDueTo);
} else {
messageElement.textContent = i18nString(UIStrings.consoleWasCleared);
}
UI.Tooltip.Tooltip.install(
messageElement, i18nString(UIStrings.clearAllMessagesWithS, {
PH1: String(UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction('console.clear')),
}));
break;
case Protocol.Runtime.ConsoleAPICalledEventType.Dir: {
const obj = this.message.parameters ? this.message.parameters[0] : undefined;
const args = ['%O', obj];
messageElement = this.format(args);
break;
}
case Protocol.Runtime.ConsoleAPICalledEventType.Profile:
case Protocol.Runtime.ConsoleAPICalledEventType.ProfileEnd:
messageElement = this.format([messageText]);
break;
default: {
if (this.message.type === Protocol.Runtime.ConsoleAPICalledEventType.Assert) {
this.messagePrefix = i18nString(UIStrings.assertionFailed);
}
if (this.message.parameters && this.message.parameters.length === 1) {
const parameter = this.message.parameters[0];
if (typeof parameter !== 'string' && parameter.type === 'string') {
messageElement = this.tryFormatAsError((parameter.value as string));
}
}
const args = this.message.parameters || [messageText];
messageElement = messageElement || this.format(args);
}
}
} else {
if (this.message.source === Protocol.Log.LogEntrySource.Network) {
messageElement = this.formatAsNetworkRequest() || this.format([messageText]);
} else {
const messageInParameters = this.message.parameters && messageText === (this.message.parameters[0] as string);
// These terms are locked because the console message will not be translated anyway.
if (this.message.source === Protocol.Log.LogEntrySource.Violation) {
messageText = i18nString(UIStrings.violationS, {PH1: messageText});
} else if (this.message.source === Protocol.Log.LogEntrySource.Intervention) {
messageText = i18nString(UIStrings.interventionS, {PH1: messageText});
} else if (this.message.source === Protocol.Log.LogEntrySource.Deprecation) {
messageText = i18nString(UIStrings.deprecationS, {PH1: messageText});
}
const args = this.message.parameters || [messageText];
if (messageInParameters) {
args[0] = messageText;
}
messageElement = this.format(args);
}
}
messageElement.classList.add('console-message-text');
const formattedMessage = document.createElement('span');
formattedMessage.classList.add('source-code');
this.anchorElement = this.buildMessageAnchor();
if (this.anchorElement) {
formattedMessage.appendChild(this.anchorElement);
}
formattedMessage.appendChild(messageElement);
return formattedMessage;
}
private formatAsNetworkRequest(): HTMLElement|null {
const request = Logs.NetworkLog.NetworkLog.requestForConsoleMessage(this.message);
if (!request) {
return null;
}
const messageElement = document.createElement('span');
if (this.message.level === Protocol.Log.LogEntryLevel.Error) {
UI.UIUtils.createTextChild(messageElement, request.requestMethod + ' ');
const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url());
// Focus is handled by the viewport.
linkElement.tabIndex = -1;
this.selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
messageElement.appendChild(linkElement);
if (request.failed) {
UI.UIUtils.createTextChildren(messageElement, ' ', request.localizedFailDescription || '');
}
if (request.statusCode !== 0) {
UI.UIUtils.createTextChildren(messageElement, ' ', String(request.statusCode));
}
if (request.statusText) {
UI.UIUtils.createTextChildren(messageElement, ' (', request.statusText, ')');
}
} else {
const messageText = this.message.messageText;
const fragment = this.linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => {
const linkElement = url === request.url() ?
Components.Linkifier.Linkifier.linkifyRevealable(
(request as SDK.NetworkRequest.NetworkRequest), url, request.url()) :
Components.Linkifier.Linkifier.linkifyURL(
url, ({text, lineNumber, columnNumber} as Components.Linkifier.LinkifyURLOptions));
linkElement.tabIndex = -1;
this.selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
return linkElement;
});
messageElement.appendChild(fragment);
}
return messageElement;
}
private createAffectedResourceLinks(): HTMLElement[] {
const elements = [];
const requestId = this.message.getAffectedResources()?.requestId;
if (requestId) {
const icon = new RequestLinkIcon.RequestLinkIcon.RequestLinkIcon();
icon.classList.add('resource-links');
icon.data = {
affectedRequest: {requestId},
requestResolver: this.requestResolver,
displayURL: false,
};
elements.push(icon);
}
const issueId = this.message.getAffectedResources()?.issueId;
if (issueId) {
const icon = new IssueCounter.IssueLinkIcon.IssueLinkIcon();
icon.classList.add('resource-links');
icon.data = {issueId, issueResolver: this.issueResolver};
elements.push(icon);
}
return elements;
}
#getLinkifierMetric(): Host.UserMetrics.Action|undefined {
const request = Logs.NetworkLog.NetworkLog.requestForConsoleMessage(this.message);
if (request?.resourceType().isStyleSheet()) {
return Host.UserMetrics.Action.StyleSheetInitiatorLinkClicked;
}
return undefined;
}
protected buildMessageAnchor(): HTMLElement|null {
const runtimeModel = this.message.runtimeModel();
if (!runtimeModel) {
return null;
}
const linkify = ({stackFrameWithBreakpoint, scriptId, stackTrace, url, line, column}:
SDK.ConsoleModel.ConsoleMessage): HTMLElement|null => {
const userMetric = this.#getLinkifierMetric();
if (stackFrameWithBreakpoint) {
return this.linkifier.maybeLinkifyConsoleCallFrame(runtimeModel.target(), stackFrameWithBreakpoint, {
inlineFrameIndex: 0,
revealBreakpoint: true,
userMetric,
});
}
if (scriptId) {
return this.linkifier.linkifyScriptLocation(
runtimeModel.target(), scriptId, url || Platform.DevToolsPath.EmptyUrlString, line,
{columnNumber: column, inlineFrameIndex: 0, userMetric});
}
if (stackTrace && stackTrace.callFrames.length) {
return this.linkifier.linkifyStackTraceTopFrame(runtimeModel.target(), stackTrace);
}
if (url && url !== 'undefined') {
return this.linkifier.linkifyScriptLocation(
runtimeModel.target(), /* scriptId */ null, url, line,
{columnNumber: column, inlineFrameIndex: 0, userMetric});
}
return null;
};
const anchorElement = linkify(this.message);
// Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages.
if (anchorElement) {
anchorElement.tabIndex = -1;
this.selectableChildren.push({
element: anchorElement,
forceSelect: (): void => anchorElement.focus(),
});
const anchorWrapperElement = document.createElement('span');
anchorWrapperElement.classList.add('console-message-anchor');
anchorWrapperElement.appendChild(anchorElement);
for (const element of this.createAffectedResourceLinks()) {
UI.UIUtils.createTextChild(anchorWrapperElement, ' ');
anchorWrapperElement.append(element);
}
UI.UIUtils.createTextChild(anchorWrapperElement, ' ');
return anchorWrapperElement;
}
return null;
}
private buildMessageWithStackTrace(runtimeModel: SDK.RuntimeModel.RuntimeModel): HTMLElement {
const toggleElement = document.createElement('div');
toggleElement.classList.add('console-message-stack-trace-toggle');
const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper');
const messageElement = this.buildMessage();
const icon = UI.Icon.Icon.create('triangle-right', 'console-message-expand-icon');
const clickableElement = contentElement.createChild('div');
UI.ARIAUtils.setExpanded(clickableElement, false);
clickableElement.appendChild(icon);
// Intercept focus to avoid highlight on click.
clickableElement.tabIndex = -1;
clickableElement.appendChild(messageElement);
const stackTraceElement = contentElement.createChild('div');
const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents(
runtimeModel.target(), this.linkifier, {stackTrace: this.message.stackTrace, tabStops: undefined});
stackTraceElement.appendChild(stackTracePreview.element);
for (const linkElement of stackTracePreview.links) {
this.selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()});
}
stackTraceElement.classList.add('hidden');
UI.ARIAUtils.setAccessibleName(
contentElement, `${messageElement.textContent} ${i18nString(UIStrings.stackMessageCollapsed)}`);
UI.ARIAUtils.markAsGroup(stackTraceElement);
this.expandTrace = (expand: boolean): void => {
icon.setIconType(expand ? 'triangle-down' : 'triangle-right');
stackTraceElement.classList.toggle('hidden', !expand);
const stackTableState =
expand ? i18nString(UIStrings.stackMessageExpanded) : i18nString(UIStrings.stackMessageCollapsed);
UI.ARIAUtils.setAccessibleName(contentElement, `${messageElement.textContent} ${stackTableState}`);
UI.ARIAUtils.alert(stackTableState);
UI.ARIAUtils.setExpanded(clickableElement, expand);
this.traceExpanded = expand;
};
const toggleStackTrace = (event: Event): void => {
if (UI.UIUtils.isEditing() || contentElement.hasSelection()) {
return;
}
this.expandTrace && this.expandTrace(stackTraceElement.classList.contains('hidden'));
event.consume();
};
clickableElement.addEventListener('click', toggleStackTrace, false);
if (this.message.type === Protocol.Runtime.ConsoleAPICalledEventType.Trace &&
Common.Settings.Settings.instance().moduleSetting('consoleTraceExpand').get()) {
this.expandTrace(true);
}
// @ts-ignore
toggleElement._expandStackTraceForTest = this.expandTrace.bind(this, true);
return toggleElement;
}
private format(rawParameters: (string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|undefined)[]):
HTMLElement {
// This node is used like a Builder. Values are continually appended onto it.
const formattedResult = document.createElement('span');
if (this.messagePrefix) {
formattedResult.createChild('span').textContent = this.messagePrefix;
}
if (!rawParameters.length) {
return formattedResult;
}
// Formatting code below assumes that parameters are all wrappers whereas frontend console
// API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here.
// FIXME: Only pass runtime wrappers here.
let parameters = rawParameters.map(parameterToRemoteObject(this.message.runtimeModel()));
// There can be string log and string eval result. We distinguish between them based on message type.
const shouldFormatMessage =
SDK.RemoteObject.RemoteObject.type((parameters as SDK.RemoteObject.RemoteObject[])[0]) === 'string' &&
(this.message.type !== SDK.ConsoleModel.FrontendMessageType.Result ||
this.message.level === Protocol.Log.LogEntryLevel.Error);
// Multiple parameters with the first being a format string. Save unused substitutions.
if (shouldFormatMessage) {
parameters = this.formatWithSubstitutionString(
(parameters[0].description as string), parameters.slice(1), formattedResult);
if (parameters.length) {
UI.UIUtils.createTextChild(formattedResult, ' ');
}
}
// Single parameter, or unused substitutions from above.
for (let i = 0; i < parameters.length; ++i) {
// Inline strings when formatting.
if (shouldFormatMessage && parameters[i].type === 'string') {
formattedResult.appendChild(this.linkifyStringAsFragment(parameters[i].description || ''));
} else {
formattedResult.appendChild(this.formatParameter(parameters[i], false, true));
}
if (i < parameters.length - 1) {
UI.UIUtils.createTextChild(formattedResult, ' ');
}
}
return formattedResult;
}
protected formatParameter(
output: SDK.RemoteObject.RemoteObject, forceObjectFormat?: boolean, includePreview?: boolean): HTMLElement {
if (output.customPreview()) {
return new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output).element as HTMLElement;
}
const outputType = forceObjectFormat ? 'object' : (output.subtype || output.type);
let element;
switch (outputType) {
case 'error':
element = this.formatParameterAsError(output);
break;
case 'function':
element = this.formatParameterAsFunction(output, includePreview);
break;
case 'array':
case 'arraybuffer':
case 'blob':
case 'dataview':
case 'generator':
case 'iterator':
case 'map':
case 'object':
case 'promise':
case 'proxy':
case 'set':
case 'typedarray':
case 'wasmvalue':
case 'weakmap':
case 'weakset':
case 'webassemblymemory':
element = this.formatParameterAsObject(output, includePreview);
break;
case 'node':
element = output.isNode() ? this.formatParameterAsNode(output) : this.formatParameterAsObject(output, false);
break;
case 'trustedtype':
element = this.formatParameterAsObject(output, false);
break;
case 'string':
element = this.formatParameterAsString(output);
break;
case 'boolean':
case 'date':
case 'null':
case 'number':
case 'regexp':
case 'symbol':
case 'undefined':
case 'bigint':
element = this.formatParameterAsValue(output);
break;
default:
element = this.formatParameterAsValue(output);
console.error(`Tried to format remote object of unknown type ${outputType}.`);
}
element.classList.add(`object-value-${outputType}`);
element.classList.add('source-code');
return element;
}
private formatParameterAsValue(obj: SDK.RemoteObject.RemoteObject): HTMLElement {
const result = document.createElement('span');
const description = obj.description || '';
if (description.length > getMaxTokenizableStringLength()) {
const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue(
document.createElement('span'), description, getLongStringVisibleLength());
result.appendChild(propertyValue.element);
} else {
UI.UIUtils.createTextChild(result, description);
}
result.addEventListener('contextmenu', this.contextMenuEventFired.bind(this, obj), false);
return result;
}
private formatParameterAsTrustedType(obj: SDK.RemoteObject.RemoteObject): HTMLElement {
const result = document.createElement('span');
const trustedContentSpan = document.createElement('span');
trustedContentSpan.appendChild(this.formatParameterAsString(obj));
trustedContentSpan.classList.add('object-value-string');
UI.UIUtils.createTextChild(result, `${obj.className} `);
result.appendChild(trustedContentSpan);
return result;
}
private formatParameterAsObject(obj: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement {
const titleElement = document.createElement('span');
titleElement.classList.add('console-object');
if (includePreview && obj.preview) {
titleElement.classList.add('console-object-preview');
this.previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */);
ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.appendMemoryIcon(titleElement, obj);
} else if (obj.type === 'function') {
const functionElement = titleElement.createChild('span');
void ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false);
titleElement.classList.add('object-value-function');
} else if (obj.subtype === 'trustedtype') {
titleElement.appendChild(this.formatParameterAsTrustedType(obj));
} else {
UI.UIUtils.createTextChild(titleElement, obj.description || '');
}
if (!obj.hasChildren || obj.customPreview()) {
return titleElement;
}
const note = titleElement.createChild('span', 'object-state-note info-note');
if (this.message.type === SDK.ConsoleModel.FrontendMessageType.QueryObjectResult) {
UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWillNotBeCollectedUntil));
} else {
UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWasEvaluatedUponFirst));
}
const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this.linkifier);
section.element.classList.add('console-view-object-properties-section');
section.enableContextMenu();
section.setShowSelectionOnKeyboardFocus(true, true);
this.selectableChildren.push(section);
section.addEventListener(UI.TreeOutline.Events.ElementAttached, this.messageResized);
section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this.messageResized);
section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this.messageResized);
return section.element;
}
private formatParameterAsFunction(originalFunction: SDK.RemoteObject.RemoteObject, includePreview?: boolean):
HTMLElement {
const result = document.createElement('span');
void SDK.RemoteObject.RemoteFunction.objectAsFunction(originalFunction)
.targetFunction()
.then(formatTargetFunction.bind(this));
return result;
function formatTargetFunction(this: ConsoleViewMessage, targetFunction: SDK.RemoteObject.RemoteObject): void {
const functionElement = document.createElement('span');
const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(
targetFunction, functionElement, true, includePreview);
result.appendChild(functionElement);
if (targetFunction !== originalFunction) {
const note = result.createChild('span', 'object-state-note info-note');
UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.functionWasResolvedFromBound));
}
result.addEventListener('contextmenu', this.contextMenuEventFired.bind(this, originalFunction), false);
void promise.then(() => this.formattedParameterAsFunctionForTest());
}
}
private formattedParameterAsFunctionForTest(): void {
}
private contextMenuEventFired(obj: SDK.RemoteObject.RemoteObject, event: Event): void {
const contextMenu = new UI.ContextMenu.ContextMenu(event);
contextMenu.appendApplicableItems(obj);
void contextMenu.show();
}
protected renderPropertyPreviewOrAccessor(
object: SDK.RemoteObject.RemoteObject|null, property: Protocol.Runtime.PropertyPreview, propertyPath: {
name: (string|symbol),
}[]): HTMLElement {
if (property.type === 'accessor') {
return this.formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false);
}
return this.previewFormatter.renderPropertyPreview(
property.type, 'subtype' in property ? property.subtype : undefined, null, property.value);
}
private formatParameterAsNode(remoteObject: SDK.RemoteObject.RemoteObject): HTMLElement {
const result = document.createElement('span');
const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel);
if (!domModel) {
return result;
}
void domModel.pushObjectAsNodeToFrontend(remoteObject).then(async (node: SDK.DOMModel.DOMNode|null) => {
if (!node) {
result.appendChild(this.formatParameterAsObject(remoteObject, false));
return;
}
const renderResult = await UI.UIUtils.Renderer.render((node as Object));
if (renderResult) {
if (renderResult.tree) {
this.selectableChildren.push(renderResult.tree);
renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this.messageResized);
renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this.messageResized);
renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this.messageResized);
}
result.appendChild(renderResult.node);
} else {
result.appendChild(this.formatParameterAsObject(remoteObject, false));
}
this.formattedParameterAsNodeForTest();
});
return result;
}
private formattedParameterAsNodeForTest(): void {
}
private formatParameterAsString(output: SDK.RemoteObject.RemoteObject): HTMLElement {
const description = output.description ?? '';
const text = Platform.StringUtilities.formatAsJSLiteral(description);
const result = document.createElement('span');
result.addEventListener('contextmenu', this.contextMenuEventFired.bind(this, output), false);
result.appendChild(this.linkifyStringAsFragment(text));
return result;
}
private formatParameterAsError(output: SDK.RemoteObject.RemoteObject): HTMLElement {
const result = document.createElement('span');
const errorStack = output.description || '';
// Combine the ExceptionDetails for this error object with the parsed Error#stack.
// The Exceptiondetails include script IDs for stack frames, which allows more accurate
// linking.
this.#formatErrorStackPromiseForTest = this.retrieveExceptionDetails(output).then(exceptionDetails => {
const errorSpan = this.tryFormatAsError(errorStack, exceptionDetails);
result.appendChild(errorSpan ?? this.linkifyStringAsFragment(errorStack));
});
return result;
}
private async retrieveExceptionDetails(errorObject: SDK.RemoteObject.RemoteObject):
Promise<Protocol.Runtime.ExceptionDetails|undefined> {
const runtimeModel = this.message.runtimeModel();
if (runtimeModel && errorObject.objectId) {
return runtimeModel.getExceptionDetails(errorObject.objectId);
}
return undefined;
}
private formatAsArrayEntry(output: SDK.RemoteObject.RemoteObject): HTMLElement {
return this.previewFormatter.renderPropertyPreview(
output.type, output.subtype, output.className, output.description);
}
private formatAsAccessorProperty(
object: SDK.RemoteObject.RemoteObject|null, propertyPath: string[], isArrayEntry: boolean): HTMLElement {
const rootElement =
ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan(
object, propertyPath, onInvokeGetterClick.bind(this));
function onInvokeGetterClick(this: ConsoleViewMessage, result: SDK.RemoteObject.CallFunctionResult): void {
const wasThrown = result.wasThrown;
const object = result.object;
if (!object) {
return;
}
rootElement.removeChildren();
if (wasThrown) {
const element = rootElement.createChild('span');
element.textContent = i18nString(UIStrings.exception);
UI.Tooltip.Tooltip.install(element, (object.description as string));
} else if (isArrayEntry) {
rootElement.appendChild(this.formatAsArrayEntry(object));
} else {
// Make a PropertyPreview from the RemoteObject similar to the backend logic.
const maxLength = 100;
const type = object.type;
const subtype = object.subtype;
let description = '';
if (type !== 'function' && object.description) {
if (type === 'string' || subtype === 'regexp' || subtype === 'trustedtype') {
description = Platform.StringUtilities.trimMiddle(object.description, maxLength);
} else {
description = Platform.StringUtilities.trimEndWithMaxLength(object.description, maxLength);
}
}
rootElement.appendChild(
this.previewFormatter.renderPropertyPreview(type, subtype, object.className, description));
}
}
return rootElement;
}
private formatWithSubstitutionString(
formatString: string, parameters: SDK.RemoteObject.RemoteObject[],
formattedResult: HTMLElement): SDK.RemoteObject.RemoteObject[] {
const currentStyle = new Map();
const {tokens, args} = format(formatString, parameters);
for (const token of tokens) {
switch (token.type) {
case 'generic': {
formattedResult.append(this.formatParameter(token.value, true /* force */, false /* includePreview */));
break;
}
case 'optimal': {
formattedResult.append(this.formatParameter(token.value, false /* force */, true /* includePreview */));
break;
}
case 'string': {
if (currentStyle.size === 0) {
formattedResult.append(this.linkifyStringAsFragment(token.value));
} else {
const lines = token.value.split('\n');
for (let i = 0; i < lines.length; i++) {
if (i > 0) {
formattedResult.append(document.createElement('br'));
}
const wrapper = document.createElement('span');
wrapper.style.setProperty('contain', 'paint');
wrapper.style.setProperty('display', 'inline-block');
wrapper.style.setProperty('max-width', '100%');
wrapper.appendChild(this.linkifyStringAsFragment(lines[i]));
for (const [property, {value, priority}] of currentStyle) {
wrapper.style.setProperty(property, value, priority);
}
formattedResult.append(wrapper);
}
}
break;
}
case 'style':
// Make sure that allowed properties do not interfere with link visibility.
updateStyle(currentStyle, token.value);
break;
}
}
return args;
}
matchesFilterRegex(regexObject: RegExp): boolean {
regexObject.lastIndex = 0;
const contentElement = this.contentElement();
const anchorText = this.anchorElement ? this.anchorElement.deepTextContent() : '';
return (Boolean(anchorText) && regexObject.test(anchorText.trim())) ||
regexObject.test(contentElement.deepTextContent().slice(anchorText.length));
}
matchesFilterText(filter: string): boolean {
const text = this.contentElement().deepTextContent();
return text.toLowerCase().includes(filter.toLowerCase());
}
updateTimestamp(): void {
if (!this.contentElementInternal) {
return;
}
if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) {
if (!this.timestampElement) {
this.timestampElement = document.createElement('span');
this.timestampElement.classList.add('console-timestamp');
}
this.timestampElement.textContent = UI.UIUtils.formatTimestamp(this.message.timestamp, false) + ' ';
UI.Tooltip.Tooltip.install(this.timestampElement, UI.UIUtils.formatTimestamp(this.message.timestamp, true));
this.contentElementInternal.insertBefore(this.timestampElement, this.contentElementInternal.firstChild);
} else if (this.timestampElement) {
this.timestampElement.remove();
this.timestampElement = null;
}
}
nestingLevel(): number {
let nestingLevel = 0;
for (let group = this.consoleGroup(); group !== null; group = group.consoleGroup()) {
nestingLevel++;
}
return nestingLevel;
}
setConsoleGroup(group: ConsoleGroupViewMessage): void {
console.assert(this.consoleGroupInternal === null);
this.consoleGroupInternal = group;
}
clearConsoleGroup(): void {
this.consoleGroupInternal = null;
}
consoleGroup(): ConsoleGroupViewMessage|null {
return this.consoleGroupInternal;
}
setInSimilarGroup(inSimilarGroup: boolean, isLast?: boolean): void {
this.inSimilarGroup = inSimilarGroup;
this.lastInSimilarGroup = inSimilarGroup && Boolean(isLast);
if (this.similarGroupMarker && !inSimilarGroup) {
this.similarGroupMarker.remove();
this.similarGroupMarker = null;
} else if (this.elementInternal && !this.similarGroupMarker && inSimilarGroup) {
this.similarGroupMarker = document.createElement('div');
this.similarGroupMarker.classList.add('nesting-level-marker');
this.elementInternal.insertBefore(this.similarGroupMarker, this.elementInternal.firstChild);
this.similarGroupMarker.classList.toggle('group-closed', this.lastInSimilarGroup);
}
}
isLastInSimilarGroup(): boolean {
return Boolean(this.inSimilarGroup) && Boolean(this.lastInSimilarGroup);
}
resetCloseGroupDecorationCount(): void {
if (!this.closeGroupDecorationCount) {
return;
}
this.closeGroupDecorationCount = 0;
this.updateCloseGroupDecorations();
}
incrementCloseGroupDecorationCount(): void {
++this.closeGroupDecorationCount;
this.updateCloseGroupDecorations();
}
private updateCloseGroupDecorations(): void {
if (!this.nestingLevelMarkers) {
return;
}
for (let i = 0, n = this.nestingLevelMarkers.length; i < n; ++i) {
const marker = this.nestingLevelMarkers[i];
marker.classList.toggle('group-closed', n - i <= this.closeGroupDecorationCount);
}
}
protected focusedChildIndex(): number {
if (!this.selectableChildren.length) {
return -1;
}
return this.selectableChildren.findIndex(child => child.element.hasFocus());
}
private onKeyDown(event: KeyboardEvent): void {
if (UI.UIUtils.isEditing() || !this.elementInternal || !this.elementInternal.hasFocus() ||
this.elementInternal.hasSelection()) {
return;
}
if (this.maybeHandleOnKeyDown(event)) {
event.consume(true);
}
}
maybeHandleOnKeyDown(event: KeyboardEvent): boolean {
// Handle trace expansion.
const focusedChildIndex = this.focusedChildIndex();
const isWrapperFocused = focusedChildIndex === -1;
if (this.expandTrace && isWrapperFocused) {
if ((event.key === 'ArrowLeft' && this.traceExpanded) || (event.key === 'ArrowRight' && !this.traceExpanded)) {
this.expandTrace(!this.traceExpanded);
return true;
}
}
if (!this.selectableChildren.length) {
return false;
}
if (event.key === 'ArrowLeft') {
this.elementInternal && this.elementInternal.focus();
return true;
}
if (event.key === 'ArrowRight') {
if (isWrapperFocused && this.selectNearestVisibleChild(0)) {
return true;
}
}
if (event.key === 'ArrowUp') {
const firstVisibleChild = this.nearestVisibleChild(0);
if (this.selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) {
this.elementInternal && this.elementInternal.focus();
return true;
}
if (this.selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) {
return true;
}
}
if (event.key === 'ArrowDown') {
if (isWrapperFocused && this.selectNearestVisibleChild(0)) {
return true;
}
if (!isWrapperFocused && this.selectNearestVisibleChild(focusedChildIndex + 1)) {
return true;
}
}
return false;
}
private selectNearestVisibleChild(fromIndex: number, backwards?: boolean): boolean {
const nearestChild = this.nearestVisibleChild(fromIndex, backwards);
if (nearestChild) {
nearestChild.forceSelect();
return true;
}
return false;
}
private nearestVisibleChild(fromIndex: number, backwards?: boolean): {
element: Element,
forceSelect: () => void,
}|null {
const childCount = this.selectableChildren.length;
if (fromIndex < 0 || fromIndex >= childCount) {
return null;
}
const direction = backwards ? -1 : 1;
let index = fromIndex;
while (!this.selectableChildren[index].element.offsetParent) {
index += direction;
if (index < 0 || index >= childCount) {
return null;
}
}
return this.selectableChildren[index];
}
focusLastChildOrSelf(): void {
if (this.elementInternal &&
!this.selectNearestVisibleChild(this.selectableChildren.length - 1, true /* backwards */)) {
this.elementInternal.focus();
}
}
setContentElement(element: HTMLElement): void {
console.assert(!this.contentElementInternal, 'Cannot set content element twice');
this.contentElementInternal = element;
}
getContentElement(): HTMLElement|null {
return this.contentElementInternal;
}
contentElement(): HTMLElement {
if (this.contentElementInternal) {
return this.contentElementInternal;
}
const contentElement = document.createElement('div');
contentElement.classList.add('console-message');
if (this.messageIcon) {
contentElement.appendChild(this.messageIcon);
}
this.contentElementInternal = contentElement;
const runtimeModel = this.message.runtimeModel();
let formattedMessage;
const shouldIncludeTrace = Boolean(this.message.stackTrace) &&
(this.message.source === Protocol.Log.LogEntrySource.Network ||
this.message.source === Protocol.Log.LogEntrySource.Violation ||
this.message.level === Protocol.Log.LogEntryLevel.Error ||
this.message.level === Protocol.Log.LogEntryLevel.Warning ||
this.message.type === Protocol.Runtime.ConsoleAPICalledEventType.Trace);
if (runtimeModel && shouldIncludeTrace) {
formattedMessage = this.buildMessageWithStackTrace(runtimeModel);
} else {
formattedMessage = this.buildMessage();
}
contentElement.appendChild(formattedMessage);
this.updateTimestamp();
return this.contentElementInternal;
}
toMessageElement(): HTMLElement {
if (this.elementInternal) {
return this.elementInternal;
}
this.elementInternal = document.createElement('div');
this.elementInternal.tabIndex = -1;
this.elementInternal.addEventListener('keydown', (this.onKeyDown.bind(this) as EventListener));
this.updateMessageElement();
this.elementInternal.classList.toggle('console-adjacent-user-command-result', this.#adjacentUserCommandResult);
return this.elementInternal;
}
updateMessageElement(): void {
if (!this.elementInternal) {
return;
}
this.elementInternal.className = 'console-message-wrapper';
this.elementInternal.removeChildren();
if (this.message.isGroupStartMessage()) {
this.elementInternal.classList.add('console-group-title');
}
if (this.message.source === SDK.ConsoleModel.FrontendMessageSource.ConsoleA