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