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