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