Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1 | // 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 5 | /* |
| 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 Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 34 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 35 | /* eslint-disable rulesdir/no_underscored_properties */ |
| 36 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 37 | import * as Common from '../common/common.js'; |
| 38 | import * as Components from '../components/components.js'; |
| 39 | import * as DataGrid from '../data_grid/data_grid.js'; |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 40 | import * as i18n from '../i18n/i18n.js'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 41 | import * as ObjectUI from '../object_ui/object_ui.js'; |
Tim van der Lippe | 93b57c3 | 2020-02-20 17:38:44 | [diff] [blame] | 42 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 43 | import * as SDK from '../sdk/sdk.js'; |
| 44 | import * as TextUtils from '../text_utils/text_utils.js'; |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame] | 45 | import * as ThemeSupport from '../theme_support/theme_support.js'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 46 | import * as UI from '../ui/ui.js'; |
| 47 | |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 48 | import {ConsoleViewportElement} from './ConsoleViewport.js'; // eslint-disable-line no-unused-vars |
| 49 | |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 50 | export const UIStrings = { |
| 51 | /** |
| 52 | *@description Message element text content in Console View Message of the Console panel |
| 53 | */ |
| 54 | consoleclearWasPreventedDueTo: '`console.clear()` was prevented due to \'Preserve log\'', |
| 55 | /** |
| 56 | *@description Message element text content in Console View Message of the Console panel |
| 57 | */ |
| 58 | consoleWasCleared: 'Console was cleared', |
| 59 | /** |
| 60 | *@description Message element title in Console View Message of the Console panel |
| 61 | *@example {Ctrl+L} PH1 |
| 62 | */ |
| 63 | clearAllMessagesWithS: 'Clear all messages with {PH1}', |
| 64 | /** |
| 65 | *@description Message prefix in Console View Message of the Console panel |
| 66 | */ |
| 67 | assertionFailed: 'Assertion failed: ', |
| 68 | /** |
| 69 | *@description Message text in Console View Message of the Console panel |
| 70 | *@example {console.log(1)} PH1 |
| 71 | */ |
| 72 | violationS: '[Violation] {PH1}', |
| 73 | /** |
| 74 | *@description Message text in Console View Message of the Console panel |
| 75 | *@example {console.log(1)} PH1 |
| 76 | */ |
| 77 | interventionS: '[Intervention] {PH1}', |
| 78 | /** |
| 79 | *@description Message text in Console View Message of the Console panel |
| 80 | *@example {console.log(1)} PH1 |
| 81 | */ |
| 82 | deprecationS: '[Deprecation] {PH1}', |
| 83 | /** |
| 84 | *@description Note title in Console View Message of the Console panel |
| 85 | */ |
| 86 | thisValueWillNotBeCollectedUntil: 'This value will not be collected until console is cleared.', |
| 87 | /** |
| 88 | *@description Note title in Console View Message of the Console panel |
| 89 | */ |
| 90 | thisValueWasEvaluatedUponFirst: 'This value was evaluated upon first expanding. It may have changed since then.', |
| 91 | /** |
| 92 | *@description Note title in Console View Message of the Console panel |
| 93 | */ |
| 94 | functionWasResolvedFromBound: 'Function was resolved from bound function.', |
| 95 | /** |
| 96 | *@description Element text content in Console View Message of the Console panel |
| 97 | */ |
| 98 | exception: '<exception>', |
| 99 | /** |
| 100 | *@description Text to indicate an item is a warning |
| 101 | */ |
| 102 | warning: 'Warning', |
| 103 | /** |
| 104 | *@description Text for errors |
| 105 | */ |
| 106 | error: 'Error', |
| 107 | /** |
Tim van der Lippe | e4b96c6 | 2021-02-17 12:43:16 | [diff] [blame^] | 108 | *@description Tooltip text for how often this particular console message was reported |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 109 | *@example {3} PH1 |
| 110 | */ |
| 111 | repeatS: 'Repeat {PH1}', |
| 112 | /** |
| 113 | *@description Accessible name in Console View Message of the Console panel |
| 114 | *@example {Repeat 4} PH1 |
| 115 | */ |
| 116 | warningS: 'Warning {PH1}', |
| 117 | /** |
| 118 | *@description Accessible name in Console View Message of the Console panel |
| 119 | *@example {Repeat 4} PH1 |
| 120 | */ |
| 121 | errorS: 'Error {PH1}', |
| 122 | /** |
Tim van der Lippe | e4b96c6 | 2021-02-17 12:43:16 | [diff] [blame^] | 123 | *@description Text appended to grouped console messages that are related to URL requests |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 124 | */ |
| 125 | url: '<URL>', |
| 126 | /** |
Tim van der Lippe | e4b96c6 | 2021-02-17 12:43:16 | [diff] [blame^] | 127 | *@description Text appended to grouped console messages about tasks that took longer than N ms |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 128 | */ |
| 129 | tookNms: 'took <N>ms', |
| 130 | /** |
Tim van der Lippe | e4b96c6 | 2021-02-17 12:43:16 | [diff] [blame^] | 131 | *@description Text appended to grouped console messages about tasks that are related to some DOM event |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 132 | */ |
| 133 | someEvent: '<some> event', |
| 134 | /** |
Tim van der Lippe | e4b96c6 | 2021-02-17 12:43:16 | [diff] [blame^] | 135 | *@description Text appended to grouped console messages about tasks that are related to a particular milestone |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 136 | */ |
| 137 | Mxx: ' M<XX>', |
| 138 | /** |
Tim van der Lippe | e4b96c6 | 2021-02-17 12:43:16 | [diff] [blame^] | 139 | *@description Text appended to grouped console messages about tasks that are related to autofill completions |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 140 | */ |
| 141 | attribute: '<attribute>', |
| 142 | /** |
| 143 | *@description Text for the index of something |
| 144 | */ |
| 145 | index: '(index)', |
| 146 | /** |
| 147 | *@description Text for the value of something |
| 148 | */ |
| 149 | value: 'Value', |
| 150 | /** |
| 151 | *@description Title of the Console tool |
| 152 | */ |
| 153 | console: 'Console', |
| 154 | }; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 155 | const str_ = i18n.i18n.registerUIStrings('console/ConsoleViewMessage.ts', UIStrings); |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 156 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 157 | const elementToMessage = new WeakMap<Element, ConsoleViewMessage>(); |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 158 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 159 | export const getMessageForElement = (element: Element): ConsoleViewMessage|undefined => { |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 160 | return elementToMessage.get(element); |
| 161 | }; |
| 162 | |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 163 | // This value reflects the 18px min-height of .console-message, plus the |
| 164 | // 1px border of .console-message-wrapper. Keep in sync with consoleView.css. |
| 165 | const defaultConsoleRowHeight = 19; |
| 166 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 167 | const parameterToRemoteObject = (runtimeModel: SDK.RuntimeModel.RuntimeModel|null): ( |
| 168 | parameter?: SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|string) => SDK.RemoteObject.RemoteObject => |
| 169 | (parameter?: string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject): SDK.RemoteObject.RemoteObject => { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 170 | if (parameter instanceof SDK.RemoteObject.RemoteObject) { |
| 171 | return parameter; |
| 172 | } |
| 173 | if (!runtimeModel) { |
| 174 | return SDK.RemoteObject.RemoteObject.fromLocalObject(parameter); |
| 175 | } |
| 176 | if (typeof parameter === 'object') { |
| 177 | return runtimeModel.createRemoteObject(parameter); |
| 178 | } |
| 179 | return runtimeModel.createRemoteObjectFromPrimitiveValue(parameter); |
| 180 | }; |
| 181 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 182 | export class ConsoleViewMessage implements ConsoleViewportElement { |
| 183 | _message: SDK.ConsoleModel.ConsoleMessage; |
| 184 | _linkifier: Components.Linkifier.Linkifier; |
| 185 | _repeatCount: number; |
| 186 | _closeGroupDecorationCount: number; |
| 187 | _nestingLevel: number; |
| 188 | _selectableChildren: { |
| 189 | element: HTMLElement, |
| 190 | forceSelect: () => void, |
| 191 | }[]; |
| 192 | _messageResized: (arg0: Common.EventTarget.EventTargetEvent) => void; |
| 193 | _element: HTMLElement|null; |
| 194 | _previewFormatter: ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter; |
| 195 | _searchRegex: RegExp|null; |
| 196 | _messageLevelIcon: UI.Icon.Icon|null; |
| 197 | _traceExpanded: boolean; |
| 198 | _expandTrace: ((arg0: boolean) => void)|null; |
| 199 | _anchorElement: HTMLElement|null; |
| 200 | _contentElement: HTMLElement|null; |
| 201 | _nestingLevelMarkers: HTMLElement[]|null; |
| 202 | _searchHighlightNodes: Element[]; |
| 203 | _searchHighlightNodeChanges: UI.UIUtils.HighlightChange[]; |
| 204 | _isVisible: boolean; |
| 205 | _cachedHeight: number; |
| 206 | _messagePrefix: string; |
| 207 | _timestampElement: HTMLElement|null; |
| 208 | _inSimilarGroup: boolean; |
| 209 | _similarGroupMarker: HTMLElement|null; |
| 210 | _lastInSimilarGroup: boolean; |
| 211 | _groupKey: string; |
| 212 | _repeatCountElement: UI.UIUtils.DevToolsSmallBubble|null; |
| 213 | |
| 214 | constructor( |
| 215 | consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number, |
| 216 | onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | this._message = consoleMessage; |
| 218 | this._linkifier = linkifier; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 219 | this._repeatCount = 1; |
| 220 | this._closeGroupDecorationCount = 0; |
| 221 | this._nestingLevel = nestingLevel; |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 222 | this._selectableChildren = []; |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 223 | this._messageResized = onResize; |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 224 | this._element = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 225 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 226 | this._previewFormatter = new ObjectUI.RemoteObjectPreviewFormatter.RemoteObjectPreviewFormatter(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 227 | this._searchRegex = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 228 | this._messageLevelIcon = null; |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 229 | this._traceExpanded = false; |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 230 | this._expandTrace = null; |
John Emau | bb2897a | 2019-10-04 17:37:32 | [diff] [blame] | 231 | this._anchorElement = null; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 232 | this._contentElement = null; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 233 | this._nestingLevelMarkers = null; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 234 | this._searchHighlightNodes = []; |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 235 | this._searchHighlightNodeChanges = []; |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 236 | this._isVisible = false; |
| 237 | this._cachedHeight = 0; |
| 238 | this._messagePrefix = ''; |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 239 | this._timestampElement = null; |
| 240 | this._inSimilarGroup = false; |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 241 | this._similarGroupMarker = null; |
| 242 | this._lastInSimilarGroup = false; |
| 243 | this._groupKey = ''; |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 244 | this._repeatCountElement = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 245 | } |
| 246 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 247 | element(): HTMLElement { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 248 | return this.toMessageElement(); |
| 249 | } |
| 250 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 251 | wasShown(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 252 | this._isVisible = true; |
| 253 | } |
| 254 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 255 | onResize(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 256 | } |
| 257 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 258 | willHide(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 259 | this._isVisible = false; |
Erik Luo | 4b00232 | 2018-07-30 21:23:31 | [diff] [blame] | 260 | this._cachedHeight = this.element().offsetHeight; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 261 | } |
| 262 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 263 | isVisible(): boolean { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 264 | return this._isVisible; |
| 265 | } |
| 266 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 267 | fastHeight(): number { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 268 | if (this._cachedHeight) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 269 | return this._cachedHeight; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 270 | } |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 271 | return this.approximateFastHeight(); |
| 272 | } |
| 273 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 274 | approximateFastHeight(): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 275 | return defaultConsoleRowHeight; |
| 276 | } |
| 277 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 278 | consoleMessage(): SDK.ConsoleModel.ConsoleMessage { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 279 | return this._message; |
| 280 | } |
| 281 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 282 | _buildMessage(): HTMLElement { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 283 | let messageElement; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 284 | let messageText: Common.UIString.LocalizedString|string = this._message.messageText; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 285 | if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | switch (this._message.type) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 287 | case SDK.ConsoleModel.MessageType.Trace: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 288 | messageElement = this._format(this._message.parameters || ['console.trace']); |
| 289 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 290 | case SDK.ConsoleModel.MessageType.Clear: |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 291 | messageElement = document.createElement('span'); |
| 292 | messageElement.classList.add('console-info'); |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 293 | if (Common.Settings.Settings.instance().moduleSetting('preserveConsoleLog').get()) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 294 | messageElement.textContent = i18nString(UIStrings.consoleclearWasPreventedDueTo); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 295 | } else { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 296 | messageElement.textContent = i18nString(UIStrings.consoleWasCleared); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 297 | } |
Tim van der Lippe | 420e5e3 | 2020-11-23 16:58:46 | [diff] [blame] | 298 | UI.Tooltip.Tooltip.install( |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 299 | messageElement, i18nString(UIStrings.clearAllMessagesWithS, { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 300 | PH1: UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction('console.clear'), |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 301 | })); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 302 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 303 | case SDK.ConsoleModel.MessageType.Dir: { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 304 | const obj = this._message.parameters ? this._message.parameters[0] : undefined; |
| 305 | const args = ['%O', obj]; |
| 306 | messageElement = this._format(args); |
| 307 | break; |
| 308 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 309 | case SDK.ConsoleModel.MessageType.Profile: |
| 310 | case SDK.ConsoleModel.MessageType.ProfileEnd: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 311 | messageElement = this._format([messageText]); |
| 312 | break; |
| 313 | default: { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 314 | if (this._message.type === SDK.ConsoleModel.MessageType.Assert) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 315 | this._messagePrefix = i18nString(UIStrings.assertionFailed); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 316 | } |
| 317 | if (this._message.parameters && this._message.parameters.length === 1) { |
| 318 | const parameter = this._message.parameters[0]; |
| 319 | if (typeof parameter !== 'string' && parameter.type === 'string') { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 320 | messageElement = this._tryFormatAsError((parameter.value as string)); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 321 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 322 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 323 | const args = this._message.parameters || [messageText]; |
| 324 | messageElement = messageElement || this._format(args); |
| 325 | } |
| 326 | } |
| 327 | } else { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 328 | if (this._message.source === SDK.ConsoleModel.MessageSource.Network) { |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 329 | messageElement = this._formatAsNetworkRequest() || this._format([messageText]); |
| 330 | } else { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 331 | const messageInParameters = this._message.parameters && messageText === (this._message.parameters[0] as string); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 332 | if (this._message.source === SDK.ConsoleModel.MessageSource.Violation) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 333 | messageText = i18nString(UIStrings.violationS, {PH1: messageText}); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 334 | } else if (this._message.source === SDK.ConsoleModel.MessageSource.Intervention) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 335 | messageText = i18nString(UIStrings.interventionS, {PH1: messageText}); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 336 | } else if (this._message.source === SDK.ConsoleModel.MessageSource.Deprecation) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 337 | messageText = i18nString(UIStrings.deprecationS, {PH1: messageText}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 338 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 339 | const args = this._message.parameters || [messageText]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 340 | if (messageInParameters) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 341 | args[0] = messageText; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 342 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 343 | messageElement = this._format(args); |
| 344 | } |
| 345 | } |
| 346 | messageElement.classList.add('console-message-text'); |
| 347 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 348 | const formattedMessage = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 349 | formattedMessage.classList.add('source-code'); |
Erik Luo | 5976c8c | 2018-07-24 02:03:09 | [diff] [blame] | 350 | this._anchorElement = this._buildMessageAnchor(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 351 | if (this._anchorElement) { |
Erik Luo | 5976c8c | 2018-07-24 02:03:09 | [diff] [blame] | 352 | formattedMessage.appendChild(this._anchorElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 353 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 354 | formattedMessage.appendChild(messageElement); |
| 355 | return formattedMessage; |
| 356 | } |
| 357 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 358 | _formatAsNetworkRequest(): HTMLElement|null { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 359 | const request = SDK.NetworkLog.NetworkLog.requestForConsoleMessage(this._message); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 360 | if (!request) { |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 361 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 362 | } |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 363 | const messageElement = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 364 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 365 | UI.UIUtils.createTextChild(messageElement, request.requestMethod + ' '); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 366 | const linkElement = Components.Linkifier.Linkifier.linkifyRevealable(request, request.url(), request.url()); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 367 | // Focus is handled by the viewport. |
| 368 | linkElement.tabIndex = -1; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 369 | this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 370 | messageElement.appendChild(linkElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 371 | if (request.failed) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 372 | UI.UIUtils.createTextChildren(messageElement, ' ', request.localizedFailDescription || ''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 373 | } |
| 374 | if (request.statusCode !== 0) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 375 | UI.UIUtils.createTextChildren(messageElement, ' ', String(request.statusCode)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 376 | } |
| 377 | if (request.statusText) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 378 | UI.UIUtils.createTextChildren(messageElement, ' (', request.statusText, ')'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 379 | } |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 380 | } else { |
Erik Luo | ad5f394 | 2019-03-26 20:53:44 | [diff] [blame] | 381 | const messageText = this._message.messageText; |
| 382 | const fragment = this._linkifyWithCustomLinkifier(messageText, (text, url, lineNumber, columnNumber) => { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 383 | const linkElement = url === request.url() ? |
| 384 | Components.Linkifier.Linkifier.linkifyRevealable( |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 385 | (request as SDK.NetworkRequest.NetworkRequest), url, request.url()) : |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 386 | Components.Linkifier.Linkifier.linkifyURL( |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 387 | url, ({text, lineNumber, columnNumber} as Components.Linkifier.LinkifyURLOptions)); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 388 | linkElement.tabIndex = -1; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 389 | this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 390 | return linkElement; |
| 391 | }); |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 392 | messageElement.appendChild(fragment); |
| 393 | } |
| 394 | return messageElement; |
| 395 | } |
| 396 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 397 | _buildMessageAnchor(): HTMLElement|null { |
| 398 | const linkify = (message: SDK.ConsoleModel.ConsoleMessage): HTMLElement|null => { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 399 | if (message.scriptId) { |
| 400 | return this._linkifyScriptId(message.scriptId, message.url || '', message.line, message.column); |
| 401 | } |
| 402 | if (message.stackTrace && message.stackTrace.callFrames.length) { |
| 403 | return this._linkifyStackTraceTopFrame(message.stackTrace); |
| 404 | } |
| 405 | if (message.url && message.url !== 'undefined') { |
| 406 | return this._linkifyLocation(message.url, message.line, message.column); |
| 407 | } |
| 408 | return null; |
| 409 | }; |
| 410 | const anchorElement = linkify(this._message); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 411 | // Append a space to prevent the anchor text from being glued to the console message when the user selects and copies the console messages. |
| 412 | if (anchorElement) { |
John Emau | f7e30fb | 2019-10-04 19:12:32 | [diff] [blame] | 413 | anchorElement.tabIndex = -1; |
| 414 | this._selectableChildren.push({ |
| 415 | element: anchorElement, |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 416 | forceSelect: (): void => anchorElement.focus(), |
John Emau | f7e30fb | 2019-10-04 19:12:32 | [diff] [blame] | 417 | }); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 418 | const anchorWrapperElement = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 419 | anchorWrapperElement.classList.add('console-message-anchor'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 420 | anchorWrapperElement.appendChild(anchorElement); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 421 | UI.UIUtils.createTextChild(anchorWrapperElement, ' '); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 422 | return anchorWrapperElement; |
| 423 | } |
| 424 | return null; |
| 425 | } |
| 426 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 427 | _buildMessageWithStackTrace(runtimeModel: SDK.RuntimeModel.RuntimeModel): HTMLElement { |
| 428 | const toggleElement = (document.createElement('div') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 429 | toggleElement.classList.add('console-message-stack-trace-toggle'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 430 | const contentElement = toggleElement.createChild('div', 'console-message-stack-trace-wrapper'); |
| 431 | |
| 432 | const messageElement = this._buildMessage(); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 433 | const icon = UI.Icon.Icon.create('smallicon-triangle-right', 'console-message-expand-icon'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 434 | const clickableElement = contentElement.createChild('div'); |
| 435 | clickableElement.appendChild(icon); |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 436 | // Intercept focus to avoid highlight on click. |
| 437 | clickableElement.tabIndex = -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 438 | clickableElement.appendChild(messageElement); |
| 439 | const stackTraceElement = contentElement.createChild('div'); |
| 440 | const stackTracePreview = Components.JSPresentationUtils.buildStackTracePreviewContents( |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 441 | runtimeModel.target(), this._linkifier, |
| 442 | {stackTrace: this._message.stackTrace, contentUpdated: undefined, tabStops: undefined}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 443 | stackTraceElement.appendChild(stackTracePreview.element); |
| 444 | for (const linkElement of stackTracePreview.links) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 445 | this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 446 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 447 | stackTraceElement.classList.add('hidden'); |
Brandon Goddard | 04a5a76 | 2019-12-10 16:45:53 | [diff] [blame] | 448 | UI.ARIAUtils.markAsTreeitem(this.element()); |
| 449 | UI.ARIAUtils.setExpanded(this.element(), false); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 450 | this._expandTrace = (expand: boolean): void => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 451 | icon.setIconType(expand ? 'smallicon-triangle-down' : 'smallicon-triangle-right'); |
| 452 | stackTraceElement.classList.toggle('hidden', !expand); |
Brandon Goddard | 04a5a76 | 2019-12-10 16:45:53 | [diff] [blame] | 453 | UI.ARIAUtils.setExpanded(this.element(), expand); |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 454 | this._traceExpanded = expand; |
| 455 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 457 | const toggleStackTrace = (event: Event): void => { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 458 | if (UI.UIUtils.isEditing() || contentElement.hasSelection()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 459 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 460 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 461 | this._expandTrace && this._expandTrace(stackTraceElement.classList.contains('hidden')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 462 | event.consume(); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 463 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 464 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 465 | clickableElement.addEventListener('click', toggleStackTrace, false); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 466 | if (this._message.type === SDK.ConsoleModel.MessageType.Trace) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 467 | this._expandTrace(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 468 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 470 | // @ts-ignore |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 471 | toggleElement._expandStackTraceForTest = this._expandTrace.bind(this, true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 472 | return toggleElement; |
| 473 | } |
| 474 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 475 | _linkifyLocation(url: string, lineNumber: number, columnNumber: number): HTMLElement|null { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 476 | const runtimeModel = this._message.runtimeModel(); |
| 477 | if (!runtimeModel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 478 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 479 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 480 | return this._linkifier.linkifyScriptLocation( |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 481 | runtimeModel.target(), /* scriptId */ null, url, lineNumber, |
| 482 | {columnNumber, className: undefined, tabStop: undefined}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 483 | } |
| 484 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 485 | _linkifyStackTraceTopFrame(stackTrace: Protocol.Runtime.StackTrace): HTMLElement|null { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 486 | const runtimeModel = this._message.runtimeModel(); |
| 487 | if (!runtimeModel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 488 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 489 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 490 | return this._linkifier.linkifyStackTraceTopFrame(runtimeModel.target(), stackTrace); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 491 | } |
| 492 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 493 | _linkifyScriptId(scriptId: string, url: string, lineNumber: number, columnNumber: number): HTMLElement|null { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 494 | const runtimeModel = this._message.runtimeModel(); |
| 495 | if (!runtimeModel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 496 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 497 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 498 | return this._linkifier.linkifyScriptLocation( |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 499 | runtimeModel.target(), scriptId, url, lineNumber, {columnNumber, className: undefined, tabStop: undefined}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 500 | } |
| 501 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 502 | _format(rawParameters: (string|SDK.RemoteObject.RemoteObject|Protocol.Runtime.RemoteObject|undefined)[]): |
| 503 | HTMLElement { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 504 | // This node is used like a Builder. Values are continually appended onto it. |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 505 | const formattedResult = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 506 | if (this._messagePrefix) { |
Pavel Feldman | 9f0f0a3 | 2018-12-18 02:09:13 | [diff] [blame] | 507 | formattedResult.createChild('span').textContent = this._messagePrefix; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 508 | } |
| 509 | if (!rawParameters.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 510 | return formattedResult; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 511 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 512 | |
| 513 | // Formatting code below assumes that parameters are all wrappers whereas frontend console |
| 514 | // API allows passing arbitrary values as messages (strings, numbers, etc.). Wrap them here. |
| 515 | // FIXME: Only pass runtime wrappers here. |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 516 | let parameters = rawParameters.map(parameterToRemoteObject(this._message.runtimeModel())); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 517 | |
| 518 | // There can be string log and string eval result. We distinguish between them based on message type. |
| 519 | const shouldFormatMessage = |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 520 | SDK.RemoteObject.RemoteObject.type((parameters as SDK.RemoteObject.RemoteObject[])[0]) === 'string' && |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 521 | (this._message.type !== SDK.ConsoleModel.MessageType.Result || |
| 522 | this._message.level === SDK.ConsoleModel.MessageLevel.Error); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 523 | |
| 524 | // Multiple parameters with the first being a format string. Save unused substitutions. |
| 525 | if (shouldFormatMessage) { |
| 526 | const result = this._formatWithSubstitutionString( |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 527 | (parameters[0].description as string), parameters.slice(1), formattedResult); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 528 | parameters = Array.from(result.unusedSubstitutions || []); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 529 | if (parameters.length) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 530 | UI.UIUtils.createTextChild(formattedResult, ' '); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 531 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // Single parameter, or unused substitutions from above. |
| 535 | for (let i = 0; i < parameters.length; ++i) { |
| 536 | // Inline strings when formatting. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 537 | if (shouldFormatMessage && parameters[i].type === 'string') { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 538 | formattedResult.appendChild(this._linkifyStringAsFragment(parameters[i].description || '')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 539 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 540 | formattedResult.appendChild(this._formatParameter(parameters[i], false, true)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 541 | } |
| 542 | if (i < parameters.length - 1) { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 543 | UI.UIUtils.createTextChild(formattedResult, ' '); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 544 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 545 | } |
| 546 | return formattedResult; |
| 547 | } |
| 548 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 549 | _formatParameter(output: SDK.RemoteObject.RemoteObject, forceObjectFormat?: boolean, includePreview?: boolean): |
| 550 | HTMLElement { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 551 | if (output.customPreview()) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 552 | return new ObjectUI.CustomPreviewComponent.CustomPreviewComponent(output).element as HTMLElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 553 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 554 | |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 555 | const outputType = forceObjectFormat ? 'object' : (output.subtype || output.type); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 556 | let element; |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 557 | switch (outputType) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 558 | case 'error': |
| 559 | element = this._formatParameterAsError(output); |
| 560 | break; |
| 561 | case 'function': |
| 562 | element = this._formatParameterAsFunction(output, includePreview); |
| 563 | break; |
| 564 | case 'array': |
| 565 | case 'arraybuffer': |
| 566 | case 'blob': |
| 567 | case 'dataview': |
| 568 | case 'generator': |
| 569 | case 'iterator': |
| 570 | case 'map': |
| 571 | case 'object': |
| 572 | case 'promise': |
| 573 | case 'proxy': |
| 574 | case 'set': |
| 575 | case 'typedarray': |
| 576 | case 'weakmap': |
| 577 | case 'weakset': |
Benedikt Meurer | dead315 | 2020-12-07 08:43:40 | [diff] [blame] | 578 | case 'webassemblymemory': |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 579 | element = this._formatParameterAsObject(output, includePreview); |
| 580 | break; |
| 581 | case 'node': |
| 582 | element = output.isNode() ? this._formatParameterAsNode(output) : this._formatParameterAsObject(output, false); |
| 583 | break; |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 584 | case 'trustedtype': |
| 585 | element = this._formatParameterAsObject(output, false); |
| 586 | break; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 587 | case 'string': |
| 588 | element = this._formatParameterAsString(output); |
| 589 | break; |
| 590 | case 'boolean': |
| 591 | case 'date': |
| 592 | case 'null': |
| 593 | case 'number': |
| 594 | case 'regexp': |
| 595 | case 'symbol': |
| 596 | case 'undefined': |
| 597 | case 'bigint': |
| 598 | element = this._formatParameterAsValue(output); |
| 599 | break; |
| 600 | default: |
| 601 | element = this._formatParameterAsValue(output); |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 602 | console.error(`Tried to format remote object of unknown type ${outputType}.`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 603 | } |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 604 | element.classList.add(`object-value-${outputType}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 605 | element.classList.add('source-code'); |
| 606 | return element; |
| 607 | } |
| 608 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 609 | _formatParameterAsValue(obj: SDK.RemoteObject.RemoteObject): HTMLElement { |
| 610 | const result = (document.createElement('span') as HTMLElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 611 | const description = obj.description || ''; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 612 | if (description.length > getMaxTokenizableStringLength()) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 613 | const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue( |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 614 | document.createElement('span'), description, getLongStringVisibleLength()); |
Connor Moody | 1a5c0d3 | 2019-12-19 07:23:36 | [diff] [blame] | 615 | result.appendChild(propertyValue.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 616 | } else { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 617 | UI.UIUtils.createTextChild(result, description); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 618 | } |
| 619 | if (obj.objectId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 620 | result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 621 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 622 | return result; |
| 623 | } |
| 624 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 625 | _formatParameterAsTrustedType(obj: SDK.RemoteObject.RemoteObject): HTMLElement { |
| 626 | const result = (document.createElement('span') as HTMLElement); |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 627 | const trustedContentSpan = document.createElement('span'); |
| 628 | trustedContentSpan.appendChild(this._formatParameterAsString(obj)); |
| 629 | trustedContentSpan.classList.add('object-value-string'); |
Alfonso Castaño | dfe8ca3 | 2020-10-29 13:03:09 | [diff] [blame] | 630 | UI.UIUtils.createTextChild(result, `${obj.className} `); |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 631 | result.appendChild(trustedContentSpan); |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 632 | return result; |
| 633 | } |
| 634 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 635 | _formatParameterAsObject(obj: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement { |
| 636 | const titleElement = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 637 | titleElement.classList.add('console-object'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 638 | if (includePreview && obj.preview) { |
| 639 | titleElement.classList.add('console-object-preview'); |
| 640 | this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */); |
| 641 | } else if (obj.type === 'function') { |
| 642 | const functionElement = titleElement.createChild('span'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 643 | ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 644 | titleElement.classList.add('object-value-function'); |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 645 | } else if (obj.subtype === 'trustedtype') { |
| 646 | titleElement.appendChild(this._formatParameterAsTrustedType(obj)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 647 | } else { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 648 | UI.UIUtils.createTextChild(titleElement, obj.description || ''); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 649 | } |
| 650 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 651 | if (!obj.hasChildren || obj.customPreview()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 652 | return titleElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 653 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 654 | |
| 655 | const note = titleElement.createChild('span', 'object-state-note info-note'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 656 | if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 657 | UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWillNotBeCollectedUntil)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 658 | } else { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 659 | UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.thisValueWasEvaluatedUponFirst)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 660 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 661 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 662 | const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 663 | section.element.classList.add('console-view-object-properties-section'); |
| 664 | section.enableContextMenu(); |
Erik Luo | cc14b81 | 2018-11-03 01:33:09 | [diff] [blame] | 665 | section.setShowSelectionOnKeyboardFocus(true, true); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 666 | this._selectableChildren.push(section); |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 667 | section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized); |
| 668 | section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized); |
| 669 | section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 670 | return section.element; |
| 671 | } |
| 672 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 673 | _formatParameterAsFunction(func: SDK.RemoteObject.RemoteObject, includePreview?: boolean): HTMLElement { |
| 674 | const result = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 675 | SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 676 | return result; |
| 677 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 678 | function formatTargetFunction(this: ConsoleViewMessage, targetFunction: SDK.RemoteObject.RemoteObject): void { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 679 | const functionElement = document.createElement('span'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 680 | const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction( |
Joey Arhar | d78a58f | 2018-12-05 01:59:45 | [diff] [blame] | 681 | targetFunction, functionElement, true, includePreview); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 682 | result.appendChild(functionElement); |
| 683 | if (targetFunction !== func) { |
| 684 | const note = result.createChild('span', 'object-info-state-note'); |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 685 | UI.Tooltip.Tooltip.install(note, i18nString(UIStrings.functionWasResolvedFromBound)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 686 | } |
| 687 | result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false); |
Joey Arhar | d78a58f | 2018-12-05 01:59:45 | [diff] [blame] | 688 | promise.then(() => this._formattedParameterAsFunctionForTest()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 692 | _formattedParameterAsFunctionForTest(): void { |
Joey Arhar | d78a58f | 2018-12-05 01:59:45 | [diff] [blame] | 693 | } |
| 694 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 695 | _contextMenuEventFired(obj: SDK.RemoteObject.RemoteObject, event: Event): void { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 696 | const contextMenu = new UI.ContextMenu.ContextMenu(event); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 697 | contextMenu.appendApplicableItems(obj); |
| 698 | contextMenu.show(); |
| 699 | } |
| 700 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 701 | _renderPropertyPreviewOrAccessor( |
| 702 | object: SDK.RemoteObject.RemoteObject|null, property: Protocol.Runtime.PropertyPreview, propertyPath: { |
| 703 | name: (string|symbol), |
| 704 | }[]): HTMLElement { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 705 | if (property.type === 'accessor') { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 706 | return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 707 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 708 | return this._previewFormatter.renderPropertyPreview( |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 709 | property.type, 'subtype' in property ? property.subtype : undefined, null, property.value); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 710 | } |
| 711 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 712 | _formatParameterAsNode(remoteObject: SDK.RemoteObject.RemoteObject): HTMLElement { |
| 713 | const result = document.createElement('span'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 714 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 715 | const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 716 | if (!domModel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 717 | return result; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 718 | } |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 719 | domModel.pushObjectAsNodeToFrontend(remoteObject).then(async (node: SDK.DOMModel.DOMNode|null) => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 720 | if (!node) { |
| 721 | result.appendChild(this._formatParameterAsObject(remoteObject, false)); |
| 722 | return; |
| 723 | } |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 724 | const renderResult = await UI.UIUtils.Renderer.render((node as Object)); |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 725 | if (renderResult) { |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 726 | if (renderResult.tree) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 727 | this._selectableChildren.push(renderResult.tree); |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 728 | renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized); |
| 729 | renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized); |
| 730 | renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized); |
| 731 | } |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 732 | result.appendChild(renderResult.node); |
| 733 | } else { |
| 734 | result.appendChild(this._formatParameterAsObject(remoteObject, false)); |
| 735 | } |
Erik Luo | 54fdd91 | 2018-11-01 17:57:01 | [diff] [blame] | 736 | this._formattedParameterAsNodeForTest(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 737 | }); |
| 738 | |
| 739 | return result; |
| 740 | } |
| 741 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 742 | _formattedParameterAsNodeForTest(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 743 | } |
| 744 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 745 | _formatParameterAsString(output: SDK.RemoteObject.RemoteObject): HTMLElement { |
| 746 | const span = (document.createElement('span') as HTMLElement); |
Alex Rudenko | 2f57b45 | 2021-02-17 07:02:09 | [diff] [blame] | 747 | span.appendChild(this._linkifyStringAsFragment(output.description || '')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 748 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 749 | const result = (document.createElement('span') as HTMLElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 750 | result.createChild('span', 'object-value-string-quote').textContent = '"'; |
| 751 | result.appendChild(span); |
| 752 | result.createChild('span', 'object-value-string-quote').textContent = '"'; |
| 753 | return result; |
| 754 | } |
| 755 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 756 | _formatParameterAsError(output: SDK.RemoteObject.RemoteObject): HTMLElement { |
| 757 | const result = (document.createElement('span') as HTMLElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 758 | const errorSpan = this._tryFormatAsError(output.description || ''); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 759 | result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || '')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 760 | return result; |
| 761 | } |
| 762 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 763 | _formatAsArrayEntry(output: SDK.RemoteObject.RemoteObject): HTMLElement { |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 764 | return this._previewFormatter.renderPropertyPreview( |
| 765 | output.type, output.subtype, output.className, output.description); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 766 | } |
| 767 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 768 | _formatAsAccessorProperty(object: SDK.RemoteObject.RemoteObject|null, propertyPath: string[], isArrayEntry: boolean): |
| 769 | HTMLElement { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 770 | const rootElement = |
| 771 | ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan( |
| 772 | object, propertyPath, onInvokeGetterClick.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 773 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 774 | function onInvokeGetterClick(this: ConsoleViewMessage, result: SDK.RemoteObject.CallFunctionResult): void { |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 775 | const wasThrown = result.wasThrown; |
| 776 | const object = result.object; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 777 | if (!object) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 778 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 779 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 780 | rootElement.removeChildren(); |
| 781 | if (wasThrown) { |
| 782 | const element = rootElement.createChild('span'); |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 783 | element.textContent = i18nString(UIStrings.exception); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 784 | UI.Tooltip.Tooltip.install(element, (object.description as string)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 785 | } else if (isArrayEntry) { |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 786 | rootElement.appendChild(this._formatAsArrayEntry(object)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 787 | } else { |
| 788 | // Make a PropertyPreview from the RemoteObject similar to the backend logic. |
| 789 | const maxLength = 100; |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 790 | const type = object.type; |
| 791 | const subtype = object.subtype; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 792 | let description = ''; |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 793 | if (type !== 'function' && object.description) { |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 794 | if (type === 'string' || subtype === 'regexp' || subtype === 'trustedtype') { |
Tim van der Lippe | 213266c | 2021-01-18 15:48:31 | [diff] [blame] | 795 | description = Platform.StringUtilities.trimMiddle(object.description, maxLength); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 796 | } else { |
Tim van der Lippe | 3cc3af3 | 2021-01-19 14:25:26 | [diff] [blame] | 797 | description = Platform.StringUtilities.trimEndWithMaxLength(object.description, maxLength); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 798 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 799 | } |
Alfonso Castaño | 20d22cd | 2020-10-28 16:23:58 | [diff] [blame] | 800 | rootElement.appendChild( |
| 801 | this._previewFormatter.renderPropertyPreview(type, subtype, object.className, description)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| 805 | return rootElement; |
| 806 | } |
| 807 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 808 | _formatWithSubstitutionString( |
| 809 | format: string, parameters: SDK.RemoteObject.RemoteObject[], formattedResult: HTMLElement): { |
| 810 | formattedResult: Element, |
| 811 | unusedSubstitutions: ArrayLike<SDK.RemoteObject.RemoteObject>|null, |
| 812 | } { |
| 813 | function parameterFormatter( |
| 814 | this: ConsoleViewMessage, force: boolean, includePreview: boolean, |
| 815 | obj?: string|SDK.RemoteObject.RemoteObject): string|HTMLElement|undefined { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 816 | if (obj instanceof SDK.RemoteObject.RemoteObject) { |
| 817 | return this._formatParameter(obj, force, includePreview); |
| 818 | } |
| 819 | return stringFormatter(obj); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 820 | } |
| 821 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 822 | function stringFormatter(obj?: string|SDK.RemoteObject.RemoteObject): string|undefined { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 823 | if (obj === undefined) { |
| 824 | return undefined; |
| 825 | } |
| 826 | if (typeof obj === 'string') { |
| 827 | return obj; |
| 828 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 829 | return obj.description; |
| 830 | } |
| 831 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 832 | function floatFormatter(obj?: string|SDK.RemoteObject.RemoteObject): number|string|undefined { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 833 | if (obj instanceof SDK.RemoteObject.RemoteObject) { |
| 834 | if (typeof obj.value !== 'number') { |
| 835 | return 'NaN'; |
| 836 | } |
| 837 | return obj.value; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 838 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 839 | return undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 840 | } |
| 841 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 842 | function integerFormatter(obj?: string|SDK.RemoteObject.RemoteObject): string|number|undefined { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 843 | if (obj instanceof SDK.RemoteObject.RemoteObject) { |
| 844 | if (obj.type === 'bigint') { |
| 845 | return obj.description; |
| 846 | } |
| 847 | if (typeof obj.value !== 'number') { |
| 848 | return 'NaN'; |
| 849 | } |
| 850 | return Math.floor(obj.value); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 851 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 852 | return undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 853 | } |
| 854 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 855 | function bypassFormatter(obj?: string|SDK.RemoteObject.RemoteObject): Node|string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 856 | return (obj instanceof Node) ? obj : ''; |
| 857 | } |
| 858 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 859 | let currentStyle: Map<string, {value: string, priority: string}>|null = null; |
| 860 | function styleFormatter(obj?: string|SDK.RemoteObject.RemoteObject): void { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 861 | currentStyle = new Map(); |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 862 | const buffer = document.createElement('span'); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 863 | if (obj === undefined) { |
| 864 | return; |
| 865 | } |
| 866 | if (typeof obj === 'string' || !obj.description) { |
| 867 | return; |
| 868 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 869 | buffer.setAttribute('style', obj.description); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 870 | for (const property of buffer.style) { |
Mathias Bynens | 5165a7a | 2020-06-10 05:51:43 | [diff] [blame] | 871 | if (isAllowedProperty(property)) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 872 | const info = { |
| 873 | value: buffer.style.getPropertyValue(property), |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 874 | priority: buffer.style.getPropertyPriority(property), |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 875 | }; |
| 876 | currentStyle.set(property, info); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 877 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 881 | function isAllowedProperty(property: string): boolean { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 882 | // Make sure that allowed properties do not interfere with link visibility. |
| 883 | const prefixes = [ |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 884 | 'background', |
| 885 | 'border', |
| 886 | 'color', |
| 887 | 'font', |
| 888 | 'line', |
| 889 | 'margin', |
| 890 | 'padding', |
| 891 | 'text', |
| 892 | '-webkit-background', |
| 893 | '-webkit-border', |
| 894 | '-webkit-font', |
| 895 | '-webkit-margin', |
| 896 | '-webkit-padding', |
| 897 | '-webkit-text', |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 898 | ]; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 899 | for (const prefix of prefixes) { |
| 900 | if (property.startsWith(prefix)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 901 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 902 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 903 | } |
| 904 | return false; |
| 905 | } |
| 906 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 907 | // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration) |
| 908 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 909 | const formatters: Record<string, Platform.StringUtilities.FormatterFunction<any>> = {}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 910 | // Firebug uses %o for formatting objects. |
| 911 | formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */); |
| 912 | formatters.s = stringFormatter; |
| 913 | formatters.f = floatFormatter; |
| 914 | // Firebug allows both %i and %d for formatting integers. |
| 915 | formatters.i = integerFormatter; |
| 916 | formatters.d = integerFormatter; |
| 917 | |
| 918 | // Firebug uses %c for styling the message. |
| 919 | formatters.c = styleFormatter; |
| 920 | |
| 921 | // Support %O to force object formatting, instead of the type-based %o formatting. |
| 922 | formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */); |
| 923 | |
| 924 | formatters._ = bypassFormatter; |
| 925 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 926 | function append(this: ConsoleViewMessage, a: HTMLElement, b?: string|Node): HTMLElement { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 927 | if (b instanceof Node) { |
| 928 | a.appendChild(b); |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 929 | return a; |
| 930 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 931 | if (typeof b === 'undefined') { |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 932 | return a; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 933 | } |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 934 | if (!currentStyle) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 935 | a.appendChild(this._linkifyStringAsFragment(String(b))); |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 936 | return a; |
| 937 | } |
| 938 | const lines = String(b).split('\n'); |
| 939 | for (let i = 0; i < lines.length; i++) { |
| 940 | const line = lines[i]; |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 941 | const lineFragment = this._linkifyStringAsFragment(line); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 942 | const wrapper = (document.createElement('span') as HTMLElement); |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 943 | wrapper.style.setProperty('contain', 'paint'); |
| 944 | wrapper.style.setProperty('display', 'inline-block'); |
| 945 | wrapper.style.setProperty('max-width', '100%'); |
| 946 | wrapper.appendChild(lineFragment); |
| 947 | applyCurrentStyle(wrapper); |
| 948 | for (const child of wrapper.children) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 949 | if (child.classList.contains('devtools-link') && child instanceof HTMLElement) { |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 950 | this._applyForcedVisibleStyle(child); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 951 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 952 | } |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 953 | a.appendChild(wrapper); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 954 | if (i < lines.length - 1) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 955 | a.appendChild(document.createElement('br')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 956 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 957 | } |
| 958 | return a; |
| 959 | } |
| 960 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 961 | function applyCurrentStyle(element: HTMLElement): void { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 962 | if (!currentStyle) { |
| 963 | return; |
| 964 | } |
| 965 | for (const [property, {value, priority}] of currentStyle.entries()) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 966 | element.style.setProperty((property as string), value, priority); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 967 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 968 | } |
| 969 | |
Tim van der Lippe | 93b57c3 | 2020-02-20 17:38:44 | [diff] [blame] | 970 | // Platform.StringUtilities.format does treat formattedResult like a Builder, result is an object. |
| 971 | return Platform.StringUtilities.format(format, parameters, formatters, formattedResult, append.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 972 | } |
| 973 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 974 | _applyForcedVisibleStyle(element: HTMLElement): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 975 | element.style.setProperty('-webkit-text-stroke', '0', 'important'); |
| 976 | element.style.setProperty('text-decoration', 'underline', 'important'); |
| 977 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame] | 978 | const themedColor = ThemeSupport.ThemeSupport.instance().patchColorText( |
| 979 | 'rgb(33%, 33%, 33%)', ThemeSupport.ThemeSupport.ColorUsage.Foreground); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 980 | element.style.setProperty('color', themedColor, 'important'); |
| 981 | |
| 982 | let backgroundColor = 'hsl(0, 0%, 100%)'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 983 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 984 | backgroundColor = 'hsl(0, 100%, 97%)'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 985 | } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 986 | backgroundColor = 'hsl(50, 100%, 95%)'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 987 | } |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame] | 988 | const themedBackgroundColor = ThemeSupport.ThemeSupport.instance().patchColorText( |
| 989 | backgroundColor, ThemeSupport.ThemeSupport.ColorUsage.Background); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 990 | element.style.setProperty('background-color', themedBackgroundColor, 'important'); |
| 991 | } |
| 992 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 993 | matchesFilterRegex(regexObject: RegExp): boolean { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 994 | regexObject.lastIndex = 0; |
Erik Luo | 5976c8c | 2018-07-24 02:03:09 | [diff] [blame] | 995 | const contentElement = this.contentElement(); |
| 996 | const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : ''; |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 997 | return (Boolean(anchorText) && regexObject.test(anchorText.trim())) || |
Erik Luo | 5976c8c | 2018-07-24 02:03:09 | [diff] [blame] | 998 | regexObject.test(contentElement.deepTextContent().slice(anchorText.length)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 999 | } |
| 1000 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1001 | matchesFilterText(filter: string): boolean { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1002 | const text = this.contentElement().deepTextContent(); |
| 1003 | return text.toLowerCase().includes(filter.toLowerCase()); |
| 1004 | } |
| 1005 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1006 | updateTimestamp(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1007 | if (!this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1008 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1009 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1010 | |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 1011 | if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1012 | if (!this._timestampElement) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1013 | this._timestampElement = (document.createElement('span') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 1014 | this._timestampElement.classList.add('console-timestamp'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1015 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1016 | this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' '; |
Tim van der Lippe | 70842f3 | 2020-11-23 16:56:57 | [diff] [blame] | 1017 | UI.Tooltip.Tooltip.install(this._timestampElement, UI.UIUtils.formatTimestamp(this._message.timestamp, true)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1018 | this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild); |
| 1019 | } else if (this._timestampElement) { |
| 1020 | this._timestampElement.remove(); |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1021 | this._timestampElement = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1022 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1023 | } |
| 1024 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1025 | nestingLevel(): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1026 | return this._nestingLevel; |
| 1027 | } |
| 1028 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1029 | setInSimilarGroup(inSimilarGroup: boolean, isLast?: boolean): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1030 | this._inSimilarGroup = inSimilarGroup; |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1031 | this._lastInSimilarGroup = inSimilarGroup && Boolean(isLast); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1032 | if (this._similarGroupMarker && !inSimilarGroup) { |
| 1033 | this._similarGroupMarker.remove(); |
| 1034 | this._similarGroupMarker = null; |
| 1035 | } else if (this._element && !this._similarGroupMarker && inSimilarGroup) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1036 | this._similarGroupMarker = (document.createElement('div') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 1037 | this._similarGroupMarker.classList.add('nesting-level-marker'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1038 | this._element.insertBefore(this._similarGroupMarker, this._element.firstChild); |
| 1039 | this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1043 | isLastInSimilarGroup(): boolean { |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1044 | return Boolean(this._inSimilarGroup) && Boolean(this._lastInSimilarGroup); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1045 | } |
| 1046 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1047 | resetCloseGroupDecorationCount(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1048 | if (!this._closeGroupDecorationCount) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1049 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1050 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1051 | this._closeGroupDecorationCount = 0; |
| 1052 | this._updateCloseGroupDecorations(); |
| 1053 | } |
| 1054 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1055 | incrementCloseGroupDecorationCount(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1056 | ++this._closeGroupDecorationCount; |
| 1057 | this._updateCloseGroupDecorations(); |
| 1058 | } |
| 1059 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1060 | _updateCloseGroupDecorations(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1061 | if (!this._nestingLevelMarkers) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1062 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1063 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1064 | for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) { |
| 1065 | const marker = this._nestingLevelMarkers[i]; |
| 1066 | marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount); |
| 1067 | } |
| 1068 | } |
| 1069 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1070 | _focusedChildIndex(): number { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1071 | if (!this._selectableChildren.length) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1072 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1073 | } |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1074 | return this._selectableChildren.findIndex(child => child.element.hasFocus()); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1075 | } |
| 1076 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1077 | _onKeyDown(event: KeyboardEvent): void { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1078 | if (UI.UIUtils.isEditing() || !this._element || !this._element.hasFocus() || this._element.hasSelection()) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1079 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1080 | } |
| 1081 | if (this.maybeHandleOnKeyDown(event)) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1082 | event.consume(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1083 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1084 | } |
| 1085 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1086 | maybeHandleOnKeyDown(event: KeyboardEvent): boolean { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1087 | // Handle trace expansion. |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1088 | const focusedChildIndex = this._focusedChildIndex(); |
| 1089 | const isWrapperFocused = focusedChildIndex === -1; |
| 1090 | if (this._expandTrace && isWrapperFocused) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1091 | if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) { |
| 1092 | this._expandTrace(!this._traceExpanded); |
| 1093 | return true; |
| 1094 | } |
| 1095 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1096 | if (!this._selectableChildren.length) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1097 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1098 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1099 | |
| 1100 | if (event.key === 'ArrowLeft') { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1101 | this._element && this._element.focus(); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1102 | return true; |
| 1103 | } |
| 1104 | if (event.key === 'ArrowRight') { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1105 | if (isWrapperFocused && this._selectNearestVisibleChild(0)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1106 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1107 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1108 | } |
| 1109 | if (event.key === 'ArrowUp') { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1110 | const firstVisibleChild = this._nearestVisibleChild(0); |
| 1111 | if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1112 | this._element && this._element.focus(); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1113 | return true; |
Mathias Bynens | f06e8c0 | 2020-02-28 13:58:28 | [diff] [blame] | 1114 | } |
| 1115 | if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1116 | return true; |
| 1117 | } |
| 1118 | } |
| 1119 | if (event.key === 'ArrowDown') { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1120 | if (isWrapperFocused && this._selectNearestVisibleChild(0)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1121 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1122 | } |
| 1123 | if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1124 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1125 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1126 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
| 1129 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1130 | _selectNearestVisibleChild(fromIndex: number, backwards?: boolean): boolean { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1131 | const nearestChild = this._nearestVisibleChild(fromIndex, backwards); |
| 1132 | if (nearestChild) { |
Erik Luo | 31c21f6 | 2018-12-13 03:39:39 | [diff] [blame] | 1133 | nearestChild.forceSelect(); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1134 | return true; |
| 1135 | } |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1139 | _nearestVisibleChild(fromIndex: number, backwards?: boolean): { |
| 1140 | element: Element, |
| 1141 | forceSelect: () => void, |
| 1142 | }|null { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1143 | const childCount = this._selectableChildren.length; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1144 | if (fromIndex < 0 || fromIndex >= childCount) { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1145 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1146 | } |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1147 | const direction = backwards ? -1 : 1; |
| 1148 | let index = fromIndex; |
| 1149 | |
| 1150 | while (!this._selectableChildren[index].element.offsetParent) { |
| 1151 | index += direction; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1152 | if (index < 0 || index >= childCount) { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1153 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1154 | } |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1155 | } |
| 1156 | return this._selectableChildren[index]; |
| 1157 | } |
| 1158 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1159 | focusLastChildOrSelf(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1160 | if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1161 | this._element.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1162 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1163 | } |
| 1164 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1165 | setContentElement(element: HTMLElement): void { |
Sigurd Schneider | b2953b2 | 2020-10-09 09:30:15 | [diff] [blame] | 1166 | console.assert(!this._contentElement, 'Cannot set content element twice'); |
| 1167 | this._contentElement = element; |
| 1168 | } |
| 1169 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1170 | getContentElement(): HTMLElement|null { |
Sigurd Schneider | b2953b2 | 2020-10-09 09:30:15 | [diff] [blame] | 1171 | return this._contentElement; |
| 1172 | } |
| 1173 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1174 | contentElement(): HTMLElement { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1175 | if (this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1176 | return this._contentElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1177 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1178 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1179 | const contentElement = (document.createElement('div') as HTMLElement); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 1180 | contentElement.classList.add('console-message'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1181 | if (this._messageLevelIcon) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1182 | contentElement.appendChild(this._messageLevelIcon); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1183 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1184 | this._contentElement = contentElement; |
| 1185 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1186 | const runtimeModel = this._message.runtimeModel(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1187 | let formattedMessage; |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1188 | const shouldIncludeTrace = Boolean(this._message.stackTrace) && |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1189 | (this._message.source === SDK.ConsoleModel.MessageSource.Network || |
| 1190 | this._message.source === SDK.ConsoleModel.MessageSource.Violation || |
| 1191 | this._message.level === SDK.ConsoleModel.MessageLevel.Error || |
| 1192 | this._message.level === SDK.ConsoleModel.MessageLevel.Warning || |
| 1193 | this._message.type === SDK.ConsoleModel.MessageType.Trace); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1194 | if (runtimeModel && shouldIncludeTrace) { |
| 1195 | formattedMessage = this._buildMessageWithStackTrace(runtimeModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1196 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1197 | formattedMessage = this._buildMessage(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1198 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1199 | contentElement.appendChild(formattedMessage); |
| 1200 | |
| 1201 | this.updateTimestamp(); |
| 1202 | return this._contentElement; |
| 1203 | } |
| 1204 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1205 | toMessageElement(): HTMLElement { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1206 | if (this._element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1207 | return this._element; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1208 | } |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1209 | this._element = (document.createElement('div') as HTMLElement); |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 1210 | this._element.tabIndex = -1; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1211 | this._element.addEventListener('keydown', (this._onKeyDown.bind(this) as EventListener)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1212 | this.updateMessageElement(); |
| 1213 | return this._element; |
| 1214 | } |
| 1215 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1216 | updateMessageElement(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1217 | if (!this._element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1218 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1219 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1220 | |
| 1221 | this._element.className = 'console-message-wrapper'; |
| 1222 | this._element.removeChildren(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1223 | if (this._message.isGroupStartMessage()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1224 | this._element.classList.add('console-group-title'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1225 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1226 | if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1227 | this._element.classList.add('console-from-api'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1228 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1229 | if (this._inSimilarGroup) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1230 | this._similarGroupMarker = (this._element.createChild('div', 'nesting-level-marker') as HTMLElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1231 | this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup); |
| 1232 | } |
| 1233 | |
| 1234 | this._nestingLevelMarkers = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1235 | for (let i = 0; i < this._nestingLevel; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1236 | this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1237 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1238 | this._updateCloseGroupDecorations(); |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1239 | elementToMessage.set(this._element, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1240 | |
| 1241 | switch (this._message.level) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1242 | case SDK.ConsoleModel.MessageLevel.Verbose: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1243 | this._element.classList.add('console-verbose-level'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1244 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1245 | case SDK.ConsoleModel.MessageLevel.Info: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1246 | this._element.classList.add('console-info-level'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1247 | if (this._message.type === SDK.ConsoleModel.MessageType.System) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1248 | this._element.classList.add('console-system-type'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1249 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1250 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1251 | case SDK.ConsoleModel.MessageLevel.Warning: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1252 | this._element.classList.add('console-warning-level'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1253 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1254 | case SDK.ConsoleModel.MessageLevel.Error: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1255 | this._element.classList.add('console-error-level'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1256 | break; |
| 1257 | } |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1258 | this._updateMessageLevelIcon(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1259 | if (this._shouldRenderAsWarning()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1260 | this._element.classList.add('console-warning-level'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1261 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1262 | |
| 1263 | this._element.appendChild(this.contentElement()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1264 | if (this._repeatCount > 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1265 | this._showRepeatCountElement(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1266 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1267 | } |
| 1268 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1269 | _shouldRenderAsWarning(): boolean { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1270 | return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose || |
| 1271 | this._message.level === SDK.ConsoleModel.MessageLevel.Info) && |
| 1272 | (this._message.source === SDK.ConsoleModel.MessageSource.Violation || |
| 1273 | this._message.source === SDK.ConsoleModel.MessageSource.Deprecation || |
| 1274 | this._message.source === SDK.ConsoleModel.MessageSource.Intervention || |
| 1275 | this._message.source === SDK.ConsoleModel.MessageSource.Recommendation); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1276 | } |
| 1277 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1278 | _updateMessageLevelIcon(): void { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1279 | let iconType = ''; |
| 1280 | let accessibleName = ''; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1281 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1282 | iconType = 'smallicon-warning'; |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1283 | accessibleName = i18nString(UIStrings.warning); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1284 | } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1285 | iconType = 'smallicon-error'; |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1286 | accessibleName = i18nString(UIStrings.error); |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1287 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1288 | if (!this._messageLevelIcon) { |
| 1289 | if (!iconType) { |
| 1290 | return; |
| 1291 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1292 | this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1293 | if (this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1294 | this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1295 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1296 | } |
| 1297 | this._messageLevelIcon.setIconType(iconType); |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1298 | UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1299 | } |
| 1300 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1301 | repeatCount(): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1302 | return this._repeatCount || 1; |
| 1303 | } |
| 1304 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1305 | resetIncrementRepeatCount(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1306 | this._repeatCount = 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1307 | if (!this._repeatCountElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1308 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1309 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1310 | |
| 1311 | this._repeatCountElement.remove(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1312 | if (this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1313 | this._contentElement.classList.remove('repeated-message'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1314 | } |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1315 | this._repeatCountElement = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1316 | } |
| 1317 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1318 | incrementRepeatCount(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1319 | this._repeatCount++; |
| 1320 | this._showRepeatCountElement(); |
| 1321 | } |
| 1322 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1323 | setRepeatCount(repeatCount: number): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1324 | this._repeatCount = repeatCount; |
| 1325 | this._showRepeatCountElement(); |
| 1326 | } |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1327 | _showRepeatCountElement(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1328 | if (!this._element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1329 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1330 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1331 | |
| 1332 | if (!this._repeatCountElement) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1333 | this._repeatCountElement = |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1334 | (document.createElement('span', {is: 'dt-small-bubble'}) as UI.UIUtils.DevToolsSmallBubble); |
Tim van der Lippe | ee954d4 | 2020-05-04 10:35:57 | [diff] [blame] | 1335 | this._repeatCountElement.classList.add('console-message-repeat-count'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1336 | switch (this._message.level) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1337 | case SDK.ConsoleModel.MessageLevel.Warning: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1338 | this._repeatCountElement.type = 'warning'; |
| 1339 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1340 | case SDK.ConsoleModel.MessageLevel.Error: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1341 | this._repeatCountElement.type = 'error'; |
| 1342 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1343 | case SDK.ConsoleModel.MessageLevel.Verbose: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1344 | this._repeatCountElement.type = 'verbose'; |
| 1345 | break; |
| 1346 | default: |
| 1347 | this._repeatCountElement.type = 'info'; |
| 1348 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1349 | if (this._shouldRenderAsWarning()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1350 | this._repeatCountElement.type = 'warning'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1351 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1352 | |
| 1353 | this._element.insertBefore(this._repeatCountElement, this._contentElement); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1354 | this.contentElement().classList.add('repeated-message'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1355 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1356 | this._repeatCountElement.textContent = `${this._repeatCount}`; |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1357 | let accessibleName = i18nString(UIStrings.repeatS, {PH1: this._repeatCount}); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1358 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1359 | accessibleName = i18nString(UIStrings.warningS, {PH1: accessibleName}); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1360 | } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1361 | accessibleName = i18nString(UIStrings.errorS, {PH1: accessibleName}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1362 | } |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1363 | UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1364 | } |
| 1365 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1366 | get text(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1367 | return this._message.messageText; |
| 1368 | } |
| 1369 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1370 | toExportString(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1371 | const lines = []; |
| 1372 | const nodes = this.contentElement().childTextNodes(); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1373 | const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join(''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1374 | for (let i = 0; i < this.repeatCount(); ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1375 | lines.push(messageContent); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1376 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1377 | return lines.join('\n'); |
| 1378 | } |
| 1379 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1380 | setSearchRegex(regex: RegExp|null): void { |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 1381 | if (this._searchHighlightNodeChanges && this._searchHighlightNodeChanges.length) { |
| 1382 | UI.UIUtils.revertDomChanges(this._searchHighlightNodeChanges); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1383 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1384 | this._searchRegex = regex; |
| 1385 | this._searchHighlightNodes = []; |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 1386 | this._searchHighlightNodeChanges = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1387 | if (!this._searchRegex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1388 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1389 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1390 | |
| 1391 | const text = this.contentElement().deepTextContent(); |
| 1392 | let match; |
| 1393 | this._searchRegex.lastIndex = 0; |
| 1394 | const sourceRanges = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1395 | while ((match = this._searchRegex.exec(text)) && match[0]) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1396 | sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1397 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1398 | |
| 1399 | if (sourceRanges.length) { |
| 1400 | this._searchHighlightNodes = |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 1401 | UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHighlightNodeChanges); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1405 | searchRegex(): RegExp|null { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1406 | return this._searchRegex; |
| 1407 | } |
| 1408 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1409 | searchCount(): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1410 | return this._searchHighlightNodes.length; |
| 1411 | } |
| 1412 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1413 | searchHighlightNode(index: number): Element { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1414 | return this._searchHighlightNodes[index]; |
| 1415 | } |
| 1416 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1417 | _tryFormatAsError(string: string): HTMLElement|null { |
| 1418 | function startsWith(prefix: string): boolean { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1419 | return string.startsWith(prefix); |
| 1420 | } |
| 1421 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1422 | const runtimeModel = this._message.runtimeModel(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1423 | const errorPrefixes = |
| 1424 | ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError']; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1425 | if (!runtimeModel || !errorPrefixes.some(startsWith)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1426 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1427 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1428 | const debuggerModel = runtimeModel.debuggerModel(); |
| 1429 | const baseURL = runtimeModel.target().inspectedURL(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1430 | |
| 1431 | const lines = string.split('\n'); |
| 1432 | const links = []; |
| 1433 | let position = 0; |
| 1434 | for (let i = 0; i < lines.length; ++i) { |
| 1435 | position += i > 0 ? lines[i - 1].length + 1 : 0; |
| 1436 | const isCallFrameLine = /^\s*at\s/.test(lines[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1437 | if (!isCallFrameLine && links.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1438 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1439 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1440 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1441 | if (!isCallFrameLine) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1442 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1443 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1444 | |
| 1445 | let openBracketIndex = -1; |
| 1446 | let closeBracketIndex = -1; |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1447 | const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g; |
| 1448 | const inBrackets = /\([^\)\(]+\)/g; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1449 | let lastMatch: RegExpExecArray|null = null; |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1450 | let currentMatch; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1451 | while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i]))) { |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1452 | lastMatch = currentMatch; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1453 | } |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1454 | if (!lastMatch) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1455 | while ((currentMatch = inBrackets.exec(lines[i]))) { |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1456 | lastMatch = currentMatch; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1457 | } |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1458 | } |
| 1459 | if (lastMatch) { |
| 1460 | openBracketIndex = lastMatch.index; |
| 1461 | closeBracketIndex = lastMatch.index + lastMatch[0].length - 1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1462 | } |
| 1463 | const hasOpenBracket = openBracketIndex !== -1; |
| 1464 | const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3; |
| 1465 | const right = hasOpenBracket ? closeBracketIndex : lines[i].length; |
| 1466 | const linkCandidate = lines[i].substring(left, right); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1467 | const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1468 | if (!splitResult) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1469 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1470 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1471 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1472 | if (splitResult.url === '<anonymous>') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1473 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1474 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1475 | let url = parseOrScriptMatch(splitResult.url); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1476 | if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) { |
| 1477 | url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1478 | } |
| 1479 | if (!url) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1480 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1481 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1482 | |
| 1483 | links.push({ |
| 1484 | url: url, |
| 1485 | positionLeft: position + left, |
| 1486 | positionRight: position + right, |
| 1487 | lineNumber: splitResult.lineNumber, |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1488 | columnNumber: splitResult.columnNumber, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1489 | }); |
| 1490 | } |
| 1491 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1492 | if (!links.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1493 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1494 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1495 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1496 | const formattedResult = document.createElement('span'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1497 | let start = 0; |
| 1498 | for (let i = 0; i < links.length; ++i) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1499 | formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft))); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1500 | const scriptLocationLink = this._linkifier.linkifyScriptLocation( |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1501 | debuggerModel.target(), null, links[i].url, links[i].lineNumber, |
| 1502 | {columnNumber: links[i].columnNumber, className: undefined, tabStop: undefined}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1503 | scriptLocationLink.tabIndex = -1; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1504 | this._selectableChildren.push({element: scriptLocationLink, forceSelect: (): void => scriptLocationLink.focus()}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1505 | formattedResult.appendChild(scriptLocationLink); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1506 | start = links[i].positionRight; |
| 1507 | } |
| 1508 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1509 | if (start !== string.length) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1510 | formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start))); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1511 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1512 | |
| 1513 | return formattedResult; |
| 1514 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1515 | function parseOrScriptMatch(url: string|null): string|null { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1516 | if (!url) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1517 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1518 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1519 | const parsedURL = Common.ParsedURL.ParsedURL.fromString(url); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1520 | if (parsedURL) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1521 | return parsedURL.url; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1522 | } |
| 1523 | if (debuggerModel.scriptsForSourceURL(url).length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1524 | return url; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1525 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1526 | return null; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1530 | _linkifyWithCustomLinkifier( |
| 1531 | string: string, linkifier: (arg0: string, arg1: string, arg2?: number, arg3?: number) => Node): DocumentFragment { |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1532 | if (string.length > getMaxTokenizableStringLength()) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1533 | const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue( |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1534 | document.createElement('span'), string, getLongStringVisibleLength()); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1535 | const fragment = document.createDocumentFragment(); |
Connor Moody | 1a5c0d3 | 2019-12-19 07:23:36 | [diff] [blame] | 1536 | fragment.appendChild(propertyValue.element); |
| 1537 | return fragment; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1538 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1539 | const container = document.createDocumentFragment(); |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 1540 | const tokens = ConsoleViewMessage._tokenizeMessageText(string); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1541 | for (const token of tokens) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1542 | if (!token.text) { |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 1543 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1544 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1545 | switch (token.type) { |
| 1546 | case 'url': { |
| 1547 | const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1548 | const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL); |
Kim-Anh Tran | 9e49a45 | 2020-02-17 09:46:10 | [diff] [blame] | 1549 | const sourceURL = Common.ParsedURL.ParsedURL.removeWasmFunctionInfoFromURL(splitResult.url); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1550 | let linkNode; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1551 | if (splitResult) { |
Kim-Anh Tran | 9e49a45 | 2020-02-17 09:46:10 | [diff] [blame] | 1552 | linkNode = linkifier(token.text, sourceURL, splitResult.lineNumber, splitResult.columnNumber); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1553 | } else { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1554 | linkNode = linkifier(token.text, ''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1555 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1556 | container.appendChild(linkNode); |
| 1557 | break; |
| 1558 | } |
| 1559 | default: |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1560 | container.appendChild(document.createTextNode(token.text)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | return container; |
| 1565 | } |
| 1566 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1567 | _linkifyStringAsFragment(string: string): DocumentFragment { |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 1568 | return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1569 | const options = {text, lineNumber, columnNumber}; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1570 | const linkElement = |
| 1571 | Components.Linkifier.Linkifier.linkifyURL(url, (options as Components.Linkifier.LinkifyURLOptions)); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1572 | linkElement.tabIndex = -1; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1573 | this._selectableChildren.push({element: linkElement, forceSelect: (): void => linkElement.focus()}); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1574 | return linkElement; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1575 | }); |
| 1576 | } |
| 1577 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1578 | static _tokenizeMessageText(string: string): { |
| 1579 | type?: string, text: string, |
| 1580 | }[] { |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1581 | const {tokenizerRegexes, tokenizerTypes} = getOrCreateTokenizers(); |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1582 | if (string.length > getMaxTokenizableStringLength()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1583 | return [{text: string, type: undefined}]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1584 | } |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1585 | const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, tokenizerRegexes); |
| 1586 | return results.map(result => ({text: result.value, type: tokenizerTypes[result.regexIndex]})); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1587 | } |
| 1588 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1589 | groupKey(): string { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1590 | if (!this._groupKey) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1591 | this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1592 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1593 | return this._groupKey; |
| 1594 | } |
| 1595 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1596 | groupTitle(): string { |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 1597 | const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1598 | const result = tokens.reduce((acc, token) => { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1599 | let text: Common.UIString.LocalizedString|string = token.text; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1600 | if (token.type === 'url') { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1601 | text = i18nString(UIStrings.url); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1602 | } else if (token.type === 'time') { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1603 | text = i18nString(UIStrings.tookNms); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1604 | } else if (token.type === 'event') { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1605 | text = i18nString(UIStrings.someEvent); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1606 | } else if (token.type === 'milestone') { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1607 | text = i18nString(UIStrings.Mxx); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1608 | } else if (token.type === 'autofill') { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1609 | text = i18nString(UIStrings.attribute); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1610 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1611 | return acc + text; |
| 1612 | }, ''); |
| 1613 | return result.replace(/[%]o/g, ''); |
| 1614 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 1615 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1616 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1617 | let tokenizerRegexes: RegExp[]|null = null; |
| 1618 | let tokenizerTypes: string[]|null = null; |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1619 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1620 | function getOrCreateTokenizers(): { |
| 1621 | tokenizerRegexes: Array<RegExp>, |
| 1622 | tokenizerTypes: Array<string>, |
| 1623 | } { |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1624 | if (!tokenizerRegexes || !tokenizerTypes) { |
| 1625 | const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f'; |
| 1626 | const linkStringRegex = new RegExp( |
| 1627 | '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes + |
| 1628 | '"\')}\\],:;.!?]', |
| 1629 | 'u'); |
| 1630 | const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/; |
| 1631 | const timeRegex = /took [\d]+ms/; |
| 1632 | const eventRegex = /'\w+' event/; |
| 1633 | const milestoneRegex = /\sM[6-7]\d/; |
| 1634 | const autofillRegex = /\(suggested: \"[\w-]+\"\)/; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1635 | const handlers = new Map<RegExp, string>(); |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1636 | handlers.set(linkStringRegex, 'url'); |
| 1637 | handlers.set(pathLineRegex, 'url'); |
| 1638 | handlers.set(timeRegex, 'time'); |
| 1639 | handlers.set(eventRegex, 'event'); |
| 1640 | handlers.set(milestoneRegex, 'milestone'); |
| 1641 | handlers.set(autofillRegex, 'autofill'); |
| 1642 | tokenizerRegexes = Array.from(handlers.keys()); |
| 1643 | tokenizerTypes = Array.from(handlers.values()); |
| 1644 | return {tokenizerRegexes, tokenizerTypes}; |
| 1645 | } |
| 1646 | return {tokenizerRegexes, tokenizerTypes}; |
| 1647 | } |
| 1648 | |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 1649 | export class ConsoleGroupViewMessage extends ConsoleViewMessage { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1650 | _collapsed: boolean; |
| 1651 | _expandGroupIcon: UI.Icon.Icon|null; |
| 1652 | _onToggle: () => void; |
| 1653 | |
| 1654 | constructor( |
| 1655 | consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number, |
| 1656 | onToggle: () => void, onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1657 | console.assert(consoleMessage.isGroupStartMessage()); |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 1658 | super(consoleMessage, linkifier, nestingLevel, onResize); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1659 | this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1660 | this._expandGroupIcon = null; |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1661 | this._onToggle = onToggle; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1662 | } |
| 1663 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1664 | _setCollapsed(collapsed: boolean): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1665 | this._collapsed = collapsed; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1666 | if (this._expandGroupIcon) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1667 | this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1668 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1669 | this._onToggle.call(null); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1670 | } |
| 1671 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1672 | collapsed(): boolean { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1673 | return this._collapsed; |
| 1674 | } |
| 1675 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1676 | maybeHandleOnKeyDown(event: KeyboardEvent): boolean { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1677 | const focusedChildIndex = this._focusedChildIndex(); |
| 1678 | if (focusedChildIndex === -1) { |
| 1679 | if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) { |
| 1680 | this._setCollapsed(!this._collapsed); |
| 1681 | return true; |
| 1682 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1683 | } |
| 1684 | return super.maybeHandleOnKeyDown(event); |
| 1685 | } |
| 1686 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1687 | toMessageElement(): HTMLElement { |
| 1688 | let element: HTMLElement|null = this._element || null; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1689 | if (!element) { |
| 1690 | element = super.toMessageElement(); |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1691 | const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1692 | this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon'); |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 1693 | // Intercept focus to avoid highlight on click. |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1694 | this.contentElement().tabIndex = -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1695 | if (this._repeatCountElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1696 | this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1697 | } else { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1698 | element.insertBefore(this._expandGroupIcon, this._contentElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1699 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1700 | element.addEventListener('click', () => this._setCollapsed(!this._collapsed)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1701 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1702 | return element; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1703 | } |
| 1704 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1705 | _showRepeatCountElement(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1706 | super._showRepeatCountElement(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1707 | if (this._repeatCountElement && this._expandGroupIcon) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1708 | this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1709 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1710 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 1711 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1712 | |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1713 | export class ConsoleCommand extends ConsoleViewMessage { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1714 | _formattedCommand: HTMLElement|null; |
| 1715 | |
| 1716 | constructor( |
| 1717 | consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number, |
| 1718 | onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) { |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1719 | super(consoleMessage, linkifier, nestingLevel, onResize); |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1720 | this._formattedCommand = null; |
| 1721 | } |
| 1722 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1723 | contentElement(): HTMLElement { |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1724 | const contentElement = this.getContentElement(); |
| 1725 | if (contentElement) { |
| 1726 | return contentElement; |
| 1727 | } |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1728 | const newContentElement = (document.createElement('div') as HTMLElement); |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1729 | this.setContentElement(newContentElement); |
| 1730 | newContentElement.classList.add('console-user-command'); |
| 1731 | const icon = UI.Icon.Icon.create('smallicon-user-command', 'command-result-icon'); |
| 1732 | newContentElement.appendChild(icon); |
| 1733 | |
| 1734 | elementToMessage.set(newContentElement, this); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1735 | this._formattedCommand = (document.createElement('span') as HTMLElement); |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1736 | this._formattedCommand.classList.add('source-code'); |
| 1737 | this._formattedCommand.textContent = Platform.StringUtilities.replaceControlCharacters(this.text); |
| 1738 | newContentElement.appendChild(this._formattedCommand); |
| 1739 | |
| 1740 | if (this._formattedCommand.textContent.length < MaxLengthToIgnoreHighlighter) { |
| 1741 | const javascriptSyntaxHighlighter = new UI.SyntaxHighlighter.SyntaxHighlighter('text/javascript', true); |
| 1742 | javascriptSyntaxHighlighter.syntaxHighlightNode(this._formattedCommand).then(this._updateSearch.bind(this)); |
| 1743 | } else { |
| 1744 | this._updateSearch(); |
| 1745 | } |
| 1746 | |
| 1747 | this.updateTimestamp(); |
| 1748 | return newContentElement; |
| 1749 | } |
| 1750 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1751 | _updateSearch(): void { |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1752 | this.setSearchRegex(this.searchRegex()); |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | export class ConsoleCommandResult extends ConsoleViewMessage { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1757 | contentElement(): HTMLElement { |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1758 | const element = super.contentElement(); |
| 1759 | if (!element.classList.contains('console-user-command-result')) { |
| 1760 | element.classList.add('console-user-command-result'); |
| 1761 | if (this.consoleMessage().level === SDK.ConsoleModel.MessageLevel.Info) { |
| 1762 | const icon = UI.Icon.Icon.create('smallicon-command-result', 'command-result-icon'); |
| 1763 | element.insertBefore(icon, element.firstChild); |
| 1764 | } |
| 1765 | } |
| 1766 | return element; |
| 1767 | } |
| 1768 | } |
| 1769 | |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1770 | export class ConsoleTableMessageView extends ConsoleViewMessage { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1771 | _dataGrid: DataGrid.SortableDataGrid.SortableDataGrid<unknown>|null; |
| 1772 | |
| 1773 | constructor( |
| 1774 | consoleMessage: SDK.ConsoleModel.ConsoleMessage, linkifier: Components.Linkifier.Linkifier, nestingLevel: number, |
| 1775 | onResize: (arg0: Common.EventTarget.EventTargetEvent) => void) { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1776 | super(consoleMessage, linkifier, nestingLevel, onResize); |
| 1777 | console.assert(consoleMessage.type === SDK.ConsoleModel.MessageType.Table); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1778 | this._dataGrid = null; |
| 1779 | } |
| 1780 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1781 | wasShown(): void { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1782 | if (this._dataGrid) { |
| 1783 | this._dataGrid.updateWidths(); |
| 1784 | } |
| 1785 | super.wasShown(); |
| 1786 | } |
| 1787 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1788 | onResize(): void { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1789 | if (!this.isVisible()) { |
| 1790 | return; |
| 1791 | } |
| 1792 | if (this._dataGrid) { |
| 1793 | this._dataGrid.onResize(); |
| 1794 | } |
| 1795 | } |
| 1796 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1797 | contentElement(): HTMLElement { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1798 | const contentElement = this.getContentElement(); |
| 1799 | if (contentElement) { |
| 1800 | return contentElement; |
| 1801 | } |
| 1802 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1803 | const newContentElement = (document.createElement('div') as HTMLElement); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1804 | newContentElement.classList.add('console-message'); |
| 1805 | if (this._messageLevelIcon) { |
| 1806 | newContentElement.appendChild(this._messageLevelIcon); |
| 1807 | } |
| 1808 | this.setContentElement(newContentElement); |
| 1809 | |
| 1810 | newContentElement.appendChild(this._buildTableMessage()); |
| 1811 | this.updateTimestamp(); |
| 1812 | return newContentElement; |
| 1813 | } |
| 1814 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1815 | _buildTableMessage(): HTMLElement { |
| 1816 | const formattedMessage = (document.createElement('span') as HTMLElement); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1817 | formattedMessage.classList.add('source-code'); |
| 1818 | this._anchorElement = this._buildMessageAnchor(); |
| 1819 | if (this._anchorElement) { |
| 1820 | formattedMessage.appendChild(this._anchorElement); |
| 1821 | } |
| 1822 | |
| 1823 | const table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null; |
| 1824 | if (!table) { |
| 1825 | return this._buildMessage(); |
| 1826 | } |
| 1827 | const actualTable = parameterToRemoteObject(this._message.runtimeModel())(table); |
| 1828 | if (!actualTable || !actualTable.preview) { |
| 1829 | return this._buildMessage(); |
| 1830 | } |
| 1831 | |
| 1832 | const rawValueColumnSymbol = Symbol('rawValueColumn'); |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1833 | const columnNames: (string|symbol)[] = []; |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1834 | const preview = actualTable.preview; |
| 1835 | const rows = []; |
| 1836 | for (let i = 0; i < preview.properties.length; ++i) { |
| 1837 | const rowProperty = preview.properties[i]; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1838 | let rowSubProperties: Protocol.Runtime.PropertyPreview[]; |
Sigurd Schneider | b393a43 | 2020-11-06 12:08:21 | [diff] [blame] | 1839 | if (rowProperty.valuePreview && rowProperty.valuePreview.properties.length) { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1840 | rowSubProperties = rowProperty.valuePreview.properties; |
| 1841 | } else if (rowProperty.value) { |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1842 | rowSubProperties = |
| 1843 | [{name: rawValueColumnSymbol as unknown as string, type: rowProperty.type, value: rowProperty.value}]; |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1844 | } else { |
| 1845 | continue; |
| 1846 | } |
| 1847 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1848 | const rowValue = new Map<string|symbol, HTMLElement>(); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1849 | const maxColumnsToRender = 20; |
| 1850 | for (let j = 0; j < rowSubProperties.length; ++j) { |
| 1851 | const cellProperty = rowSubProperties[j]; |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1852 | let columnRendered: true|boolean = columnNames.indexOf(cellProperty.name) !== -1; |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1853 | if (!columnRendered) { |
| 1854 | if (columnNames.length === maxColumnsToRender) { |
| 1855 | continue; |
| 1856 | } |
| 1857 | columnRendered = true; |
| 1858 | columnNames.push(cellProperty.name); |
| 1859 | } |
| 1860 | |
| 1861 | if (columnRendered) { |
| 1862 | const cellElement = |
| 1863 | this._renderPropertyPreviewOrAccessor(actualTable, cellProperty, [rowProperty, cellProperty]); |
| 1864 | cellElement.classList.add('console-message-nowrap-below'); |
| 1865 | rowValue.set(cellProperty.name, cellElement); |
| 1866 | } |
| 1867 | } |
| 1868 | rows.push({rowName: rowProperty.name, rowValue}); |
| 1869 | } |
| 1870 | |
| 1871 | const flatValues = []; |
| 1872 | for (const {rowName, rowValue} of rows) { |
| 1873 | flatValues.push(rowName); |
| 1874 | for (let j = 0; j < columnNames.length; ++j) { |
| 1875 | flatValues.push(rowValue.get(columnNames[j])); |
| 1876 | } |
| 1877 | } |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1878 | columnNames.unshift(i18nString(UIStrings.index)); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1879 | const columnDisplayNames = |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1880 | columnNames.map(name => name === rawValueColumnSymbol ? i18nString(UIStrings.value) : name.toString()); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1881 | |
| 1882 | if (flatValues.length) { |
Christy Chen | 9c6d898 | 2021-02-08 02:28:31 | [diff] [blame] | 1883 | this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create( |
| 1884 | columnDisplayNames, flatValues, i18nString(UIStrings.console)); |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1885 | if (this._dataGrid) { |
| 1886 | this._dataGrid.setStriped(true); |
| 1887 | this._dataGrid.setFocusable(false); |
| 1888 | |
| 1889 | const formattedResult = document.createElement('span'); |
| 1890 | formattedResult.classList.add('console-message-text'); |
| 1891 | const tableElement = formattedResult.createChild('div', 'console-message-formatted-table'); |
| 1892 | const dataGridContainer = tableElement.createChild('span'); |
| 1893 | tableElement.appendChild(this._formatParameter(actualTable, true, false)); |
| 1894 | dataGridContainer.appendChild(this._dataGrid.element); |
| 1895 | formattedMessage.appendChild(formattedResult); |
| 1896 | this._dataGrid.renderInline(); |
| 1897 | } |
| 1898 | } |
| 1899 | return formattedMessage; |
| 1900 | } |
| 1901 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1902 | approximateFastHeight(): number { |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame] | 1903 | const table = this._message.parameters && this._message.parameters[0]; |
| 1904 | if (table && typeof table !== 'string' && table.preview) { |
| 1905 | return defaultConsoleRowHeight * table.preview.properties.length; |
| 1906 | } |
| 1907 | return defaultConsoleRowHeight; |
| 1908 | } |
| 1909 | } |
| 1910 | |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1911 | /** |
| 1912 | * The maximum length before strings are considered too long for syntax highlighting. |
| 1913 | * @const |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1914 | */ |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1915 | const MaxLengthToIgnoreHighlighter: number = 10000; |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1916 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1917 | /** |
| 1918 | * @const |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1919 | */ |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1920 | export const MaxLengthForLinks: number = 40; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1921 | |
Sigurd Schneider | 17c7445 | 2021-02-15 12:14:10 | [diff] [blame] | 1922 | let maxTokenizableStringLength = 10000; |
| 1923 | let longStringVisibleLength = 5000; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1924 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1925 | export const getMaxTokenizableStringLength = (): number => { |
Sigurd Schneider | 17c7445 | 2021-02-15 12:14:10 | [diff] [blame] | 1926 | return maxTokenizableStringLength; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1927 | }; |
| 1928 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1929 | export const setMaxTokenizableStringLength = (length: number): void => { |
Sigurd Schneider | 17c7445 | 2021-02-15 12:14:10 | [diff] [blame] | 1930 | maxTokenizableStringLength = length; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1931 | }; |
| 1932 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1933 | export const getLongStringVisibleLength = (): number => { |
Sigurd Schneider | 17c7445 | 2021-02-15 12:14:10 | [diff] [blame] | 1934 | return longStringVisibleLength; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1935 | }; |
| 1936 | |
Jan Scheffler | 19cd7ec | 2021-02-12 14:16:30 | [diff] [blame] | 1937 | export const setLongStringVisibleLength = (length: number): void => { |
Sigurd Schneider | 17c7445 | 2021-02-15 12:14:10 | [diff] [blame] | 1938 | longStringVisibleLength = length; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1939 | }; |