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 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 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 | |
| 502 | const type = forceObjectFormat ? 'object' : (output.subtype || output.type); |
| 503 | let element; |
| 504 | switch (type) { |
| 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; |
| 530 | case 'string': |
| 531 | element = this._formatParameterAsString(output); |
| 532 | break; |
| 533 | case 'boolean': |
| 534 | case 'date': |
| 535 | case 'null': |
| 536 | case 'number': |
| 537 | case 'regexp': |
| 538 | case 'symbol': |
| 539 | case 'undefined': |
| 540 | case 'bigint': |
| 541 | element = this._formatParameterAsValue(output); |
| 542 | break; |
| 543 | default: |
| 544 | element = this._formatParameterAsValue(output); |
| 545 | console.error('Tried to format remote object of unknown type.'); |
| 546 | } |
| 547 | element.classList.add('object-value-' + type); |
| 548 | element.classList.add('source-code'); |
| 549 | return element; |
| 550 | } |
| 551 | |
| 552 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 553 | * @param {!SDK.RemoteObject.RemoteObject} obj |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 554 | * @return {!HTMLElement} |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 555 | * @suppress {accessControls} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 556 | */ |
| 557 | _formatParameterAsValue(obj) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 558 | const result = /** @type {!HTMLElement} */ (document.createElement('span')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 559 | const description = obj.description || ''; |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 560 | if (description.length > getMaxTokenizableStringLength()) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 561 | const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue( |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 562 | document.createElement('span'), description, getLongStringVisibleLength()); |
Connor Moody | 1a5c0d3 | 2019-12-19 07:23:36 | [diff] [blame] | 563 | result.appendChild(propertyValue.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 564 | } else { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 565 | UI.UIUtils.createTextChild(result, description); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 566 | } |
| 567 | if (obj.objectId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 568 | result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, obj), false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 569 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | return result; |
| 571 | } |
| 572 | |
| 573 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 574 | * @param {!SDK.RemoteObject.RemoteObject} obj |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 575 | * @param {boolean=} includePreview |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 576 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 577 | */ |
| 578 | _formatParameterAsObject(obj, includePreview) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 579 | const titleElement = /** @type {!HTMLElement} */ (document.createElement('span')); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 580 | titleElement.classList.add('console-object'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 581 | if (includePreview && obj.preview) { |
| 582 | titleElement.classList.add('console-object-preview'); |
| 583 | this._previewFormatter.appendObjectPreview(titleElement, obj.preview, false /* isEntry */); |
| 584 | } else if (obj.type === 'function') { |
| 585 | const functionElement = titleElement.createChild('span'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 586 | ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction(obj, functionElement, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 587 | titleElement.classList.add('object-value-function'); |
| 588 | } else { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 589 | UI.UIUtils.createTextChild(titleElement, obj.description || ''); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 590 | } |
| 591 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 592 | if (!obj.hasChildren || obj.customPreview()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 593 | return titleElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 594 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 595 | |
| 596 | const note = titleElement.createChild('span', 'object-state-note info-note'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 597 | if (this._message.type === SDK.ConsoleModel.MessageType.QueryObjectResult) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 598 | 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] | 599 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 600 | note.title = ls`Value below was evaluated just now.`; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 601 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 602 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 603 | const section = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(obj, titleElement, this._linkifier); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 604 | section.element.classList.add('console-view-object-properties-section'); |
| 605 | section.enableContextMenu(); |
Erik Luo | cc14b81 | 2018-11-03 01:33:09 | [diff] [blame] | 606 | section.setShowSelectionOnKeyboardFocus(true, true); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 607 | this._selectableChildren.push(section); |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 608 | section.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized); |
| 609 | section.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized); |
| 610 | section.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 611 | return section.element; |
| 612 | } |
| 613 | |
| 614 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 615 | * @param {!SDK.RemoteObject.RemoteObject} func |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 616 | * @param {boolean=} includePreview |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 617 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 618 | */ |
| 619 | _formatParameterAsFunction(func, includePreview) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 620 | const result = /** @type {!HTMLElement} */ (document.createElement('span')); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 621 | SDK.RemoteObject.RemoteFunction.objectAsFunction(func).targetFunction().then(formatTargetFunction.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 622 | return result; |
| 623 | |
| 624 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 625 | * @param {!SDK.RemoteObject.RemoteObject} targetFunction |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 626 | * @this {ConsoleViewMessage} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 627 | */ |
| 628 | function formatTargetFunction(targetFunction) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 629 | const functionElement = document.createElement('span'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 630 | const promise = ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.formatObjectAsFunction( |
Joey Arhar | d78a58f | 2018-12-05 01:59:45 | [diff] [blame] | 631 | targetFunction, functionElement, true, includePreview); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 632 | result.appendChild(functionElement); |
| 633 | if (targetFunction !== func) { |
| 634 | const note = result.createChild('span', 'object-info-state-note'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 635 | note.title = Common.UIString.UIString('Function was resolved from bound function.'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 636 | } |
| 637 | result.addEventListener('contextmenu', this._contextMenuEventFired.bind(this, targetFunction), false); |
Joey Arhar | d78a58f | 2018-12-05 01:59:45 | [diff] [blame] | 638 | promise.then(() => this._formattedParameterAsFunctionForTest()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Joey Arhar | d78a58f | 2018-12-05 01:59:45 | [diff] [blame] | 642 | _formattedParameterAsFunctionForTest() { |
| 643 | } |
| 644 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 645 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 646 | * @param {!SDK.RemoteObject.RemoteObject} obj |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 647 | * @param {!Event} event |
| 648 | */ |
| 649 | _contextMenuEventFired(obj, event) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 650 | const contextMenu = new UI.ContextMenu.ContextMenu(event); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 651 | contextMenu.appendApplicableItems(obj); |
| 652 | contextMenu.show(); |
| 653 | } |
| 654 | |
| 655 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 656 | * @param {?SDK.RemoteObject.RemoteObject} object |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 657 | * @param {!Protocol.Runtime.PropertyPreview|!{name:(string|symbol), type: !Protocol.Runtime.PropertyPreviewType, value: (string|undefined)}} property |
| 658 | * @param {!Array.<!{name:(string|symbol)}>} propertyPath |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 659 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 660 | */ |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 661 | _renderPropertyPreviewOrAccessor(object, property, propertyPath) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 662 | if (property.type === 'accessor') { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 663 | return this._formatAsAccessorProperty(object, propertyPath.map(property => property.name.toString()), false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 664 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 665 | return this._previewFormatter.renderPropertyPreview( |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 666 | property.type, 'subtype' in property ? property.subtype : undefined, property.value); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 670 | * @param {!SDK.RemoteObject.RemoteObject} remoteObject |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 671 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 672 | */ |
| 673 | _formatParameterAsNode(remoteObject) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 674 | const result = /** @type {!HTMLElement} */ (document.createElement('span')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 675 | |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 676 | const domModel = remoteObject.runtimeModel().target().model(SDK.DOMModel.DOMModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 677 | if (!domModel) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 678 | return result; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 679 | } |
Erik Luo | 54fdd91 | 2018-11-01 17:57:01 | [diff] [blame] | 680 | domModel.pushObjectAsNodeToFrontend(remoteObject).then(async node => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 681 | if (!node) { |
| 682 | result.appendChild(this._formatParameterAsObject(remoteObject, false)); |
| 683 | return; |
| 684 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 685 | const renderResult = await UI.UIUtils.Renderer.render(/** @type {!Object} */ (node)); |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 686 | if (renderResult) { |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 687 | if (renderResult.tree) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 688 | this._selectableChildren.push(renderResult.tree); |
Erik Luo | 840be6b | 2018-12-03 20:54:27 | [diff] [blame] | 689 | renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementAttached, this._messageResized); |
| 690 | renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementExpanded, this._messageResized); |
| 691 | renderResult.tree.addEventListener(UI.TreeOutline.Events.ElementCollapsed, this._messageResized); |
| 692 | } |
Erik Luo | fc6a630 | 2018-11-02 06:48:52 | [diff] [blame] | 693 | result.appendChild(renderResult.node); |
| 694 | } else { |
| 695 | result.appendChild(this._formatParameterAsObject(remoteObject, false)); |
| 696 | } |
Erik Luo | 54fdd91 | 2018-11-01 17:57:01 | [diff] [blame] | 697 | this._formattedParameterAsNodeForTest(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 698 | }); |
| 699 | |
| 700 | return result; |
| 701 | } |
| 702 | |
| 703 | _formattedParameterAsNodeForTest() { |
| 704 | } |
| 705 | |
| 706 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 707 | * @param {!SDK.RemoteObject.RemoteObject} output |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 708 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 709 | */ |
| 710 | _formatParameterAsString(output) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 711 | const span = /** @type {!HTMLElement} */ (document.createElement('span')); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 712 | span.appendChild(this._linkifyStringAsFragment(output.description || '')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 713 | |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 714 | const result = /** @type {!HTMLElement} */ (document.createElement('span')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 715 | result.createChild('span', 'object-value-string-quote').textContent = '"'; |
| 716 | result.appendChild(span); |
| 717 | result.createChild('span', 'object-value-string-quote').textContent = '"'; |
| 718 | return result; |
| 719 | } |
| 720 | |
| 721 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 722 | * @param {!SDK.RemoteObject.RemoteObject} output |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 723 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 724 | */ |
| 725 | _formatParameterAsError(output) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 726 | const result = /** @type {!HTMLElement} */ (document.createElement('span')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 727 | const errorSpan = this._tryFormatAsError(output.description || ''); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 728 | result.appendChild(errorSpan ? errorSpan : this._linkifyStringAsFragment(output.description || '')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 729 | return result; |
| 730 | } |
| 731 | |
| 732 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 733 | * @param {!SDK.RemoteObject.RemoteObject} output |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 734 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 735 | */ |
| 736 | _formatAsArrayEntry(output) { |
| 737 | return this._previewFormatter.renderPropertyPreview(output.type, output.subtype, output.description); |
| 738 | } |
| 739 | |
| 740 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 741 | * @param {?SDK.RemoteObject.RemoteObject} object |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 742 | * @param {!Array.<string>} propertyPath |
| 743 | * @param {boolean} isArrayEntry |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 744 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 745 | */ |
| 746 | _formatAsAccessorProperty(object, propertyPath, isArrayEntry) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 747 | const rootElement = |
| 748 | ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement.createRemoteObjectAccessorPropertySpan( |
| 749 | object, propertyPath, onInvokeGetterClick.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 750 | |
| 751 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 752 | * @param {!SDK.RemoteObject.CallFunctionResult} result |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 753 | * @this {ConsoleViewMessage} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 754 | */ |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 755 | function onInvokeGetterClick(result) { |
| 756 | const wasThrown = result.wasThrown; |
| 757 | const object = result.object; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 758 | if (!object) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 759 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 760 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 761 | rootElement.removeChildren(); |
| 762 | if (wasThrown) { |
| 763 | const element = rootElement.createChild('span'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 764 | element.textContent = Common.UIString.UIString('<exception>'); |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 765 | element.title = /** @type {string} */ (object.description); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 766 | } else if (isArrayEntry) { |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 767 | rootElement.appendChild(this._formatAsArrayEntry(object)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 768 | } else { |
| 769 | // Make a PropertyPreview from the RemoteObject similar to the backend logic. |
| 770 | const maxLength = 100; |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 771 | const type = object.type; |
| 772 | const subtype = object.subtype; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 773 | let description = ''; |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 774 | if (type !== 'function' && object.description) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 775 | if (type === 'string' || subtype === 'regexp') { |
Alexey Kozyatinskiy | 330bffb | 2018-09-21 19:20:18 | [diff] [blame] | 776 | description = object.description.trimMiddle(maxLength); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 777 | } else { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 778 | description = object.description.trimEndWithMaxLength(maxLength); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 779 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 780 | } |
| 781 | rootElement.appendChild(this._previewFormatter.renderPropertyPreview(type, subtype, description)); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | return rootElement; |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * @param {string} format |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 790 | * @param {!Array.<!SDK.RemoteObject.RemoteObject>} parameters |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 791 | * @param {!HTMLElement} formattedResult |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 792 | * @return {!{formattedResult:!Element, unusedSubstitutions: ?ArrayLike<!SDK.RemoteObject.RemoteObject>}} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 793 | */ |
| 794 | _formatWithSubstitutionString(format, parameters, formattedResult) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 795 | /** |
| 796 | * @param {boolean} force |
| 797 | * @param {boolean} includePreview |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 798 | * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj |
| 799 | * @return {!HTMLElement|string|undefined} |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 800 | * @this {ConsoleViewMessage} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 801 | */ |
| 802 | function parameterFormatter(force, includePreview, obj) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 803 | if (obj instanceof SDK.RemoteObject.RemoteObject) { |
| 804 | return this._formatParameter(obj, force, includePreview); |
| 805 | } |
| 806 | return stringFormatter(obj); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 807 | } |
| 808 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 809 | /** |
| 810 | * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj |
| 811 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 812 | function stringFormatter(obj) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 813 | if (obj === undefined) { |
| 814 | return undefined; |
| 815 | } |
| 816 | if (typeof obj === 'string') { |
| 817 | return obj; |
| 818 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 819 | return obj.description; |
| 820 | } |
| 821 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 822 | /** |
| 823 | * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj |
| 824 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 825 | function floatFormatter(obj) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 826 | if (obj instanceof SDK.RemoteObject.RemoteObject) { |
| 827 | if (typeof obj.value !== 'number') { |
| 828 | return 'NaN'; |
| 829 | } |
| 830 | return obj.value; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 831 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 832 | return undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 833 | } |
| 834 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 835 | /** |
| 836 | * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj |
| 837 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 838 | function integerFormatter(obj) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 839 | if (obj instanceof SDK.RemoteObject.RemoteObject) { |
| 840 | if (obj.type === 'bigint') { |
| 841 | return obj.description; |
| 842 | } |
| 843 | if (typeof obj.value !== 'number') { |
| 844 | return 'NaN'; |
| 845 | } |
| 846 | return Math.floor(obj.value); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 847 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 848 | return undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 849 | } |
| 850 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 851 | /** |
| 852 | * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj |
| 853 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 854 | function bypassFormatter(obj) { |
| 855 | return (obj instanceof Node) ? obj : ''; |
| 856 | } |
| 857 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 858 | /** @type {?Map<string, !{value:string, priority:string}>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 859 | let currentStyle = null; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 860 | /** |
| 861 | * @param {!SDK.RemoteObject.RemoteObject|string|{description:string}|undefined} obj |
| 862 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 863 | function styleFormatter(obj) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 864 | currentStyle = new Map(); |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 865 | const buffer = document.createElement('span'); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 866 | if (obj === undefined) { |
| 867 | return; |
| 868 | } |
| 869 | if (typeof obj === 'string' || !obj.description) { |
| 870 | return; |
| 871 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 872 | buffer.setAttribute('style', obj.description); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 873 | for (const property of buffer.style) { |
Mathias Bynens | 5165a7a | 2020-06-10 05:51:43 | [diff] [blame] | 874 | if (isAllowedProperty(property)) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 875 | const info = { |
| 876 | value: buffer.style.getPropertyValue(property), |
| 877 | priority: buffer.style.getPropertyPriority(property) |
| 878 | }; |
| 879 | currentStyle.set(property, info); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 880 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 884 | /** |
| 885 | * @param {string} property |
| 886 | */ |
Mathias Bynens | 5165a7a | 2020-06-10 05:51:43 | [diff] [blame] | 887 | function isAllowedProperty(property) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 888 | // Make sure that allowed properties do not interfere with link visibility. |
| 889 | const prefixes = [ |
| 890 | 'background', 'border', 'color', 'font', 'line', 'margin', 'padding', 'text', '-webkit-background', |
| 891 | '-webkit-border', '-webkit-font', '-webkit-margin', '-webkit-padding', '-webkit-text' |
| 892 | ]; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 893 | for (const prefix of prefixes) { |
| 894 | if (property.startsWith(prefix)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 895 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 896 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 897 | } |
| 898 | return false; |
| 899 | } |
| 900 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 901 | /** @type {!Object.<string, function((string|!{description: string}|undefined|!SDK.RemoteObject.RemoteObject), !Platform.StringUtilities.FORMATTER_TOKEN):*>} */ |
| 902 | const formatters = {}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 903 | // Firebug uses %o for formatting objects. |
| 904 | formatters.o = parameterFormatter.bind(this, false /* force */, true /* includePreview */); |
| 905 | formatters.s = stringFormatter; |
| 906 | formatters.f = floatFormatter; |
| 907 | // Firebug allows both %i and %d for formatting integers. |
| 908 | formatters.i = integerFormatter; |
| 909 | formatters.d = integerFormatter; |
| 910 | |
| 911 | // Firebug uses %c for styling the message. |
| 912 | formatters.c = styleFormatter; |
| 913 | |
| 914 | // Support %O to force object formatting, instead of the type-based %o formatting. |
| 915 | formatters.O = parameterFormatter.bind(this, true /* force */, false /* includePreview */); |
| 916 | |
| 917 | formatters._ = bypassFormatter; |
| 918 | |
| 919 | /** |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 920 | * @param {!HTMLElement} a |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 921 | * @param {*} b |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 922 | * @this {!ConsoleViewMessage} |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 923 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 924 | */ |
| 925 | function append(a, b) { |
| 926 | if (b instanceof Node) { |
| 927 | a.appendChild(b); |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 928 | return a; |
| 929 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 930 | if (typeof b === 'undefined') { |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 931 | return a; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 932 | } |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 933 | if (!currentStyle) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 934 | a.appendChild(this._linkifyStringAsFragment(String(b))); |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 935 | return a; |
| 936 | } |
| 937 | const lines = String(b).split('\n'); |
| 938 | for (let i = 0; i < lines.length; i++) { |
| 939 | const line = lines[i]; |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 940 | const lineFragment = this._linkifyStringAsFragment(line); |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 941 | const wrapper = /** @type {!HTMLElement} */ (document.createElement('span')); |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 942 | wrapper.style.setProperty('contain', 'paint'); |
| 943 | wrapper.style.setProperty('display', 'inline-block'); |
| 944 | wrapper.style.setProperty('max-width', '100%'); |
| 945 | wrapper.appendChild(lineFragment); |
| 946 | applyCurrentStyle(wrapper); |
| 947 | for (const child of wrapper.children) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 948 | if (child.classList.contains('devtools-link') && child instanceof HTMLElement) { |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 949 | this._applyForcedVisibleStyle(child); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 950 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 951 | } |
Erik Luo | 1792639 | 2018-05-17 22:06:12 | [diff] [blame] | 952 | a.appendChild(wrapper); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 953 | if (i < lines.length - 1) { |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 954 | a.appendChild(document.createElement('br')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 955 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 956 | } |
| 957 | return a; |
| 958 | } |
| 959 | |
| 960 | /** |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 961 | * @param {!HTMLElement} element |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 962 | */ |
| 963 | function applyCurrentStyle(element) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 964 | if (!currentStyle) { |
| 965 | return; |
| 966 | } |
| 967 | for (const [property, {value, priority}] of currentStyle.entries()) { |
| 968 | element.style.setProperty(/** @type {string} */ (property), value, priority); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 969 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 970 | } |
| 971 | |
Tim van der Lippe | 93b57c3 | 2020-02-20 17:38:44 | [diff] [blame] | 972 | // Platform.StringUtilities.format does treat formattedResult like a Builder, result is an object. |
| 973 | return Platform.StringUtilities.format(format, parameters, formatters, formattedResult, append.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /** |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 977 | * @param {!HTMLElement} element |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 978 | */ |
| 979 | _applyForcedVisibleStyle(element) { |
| 980 | element.style.setProperty('-webkit-text-stroke', '0', 'important'); |
| 981 | element.style.setProperty('text-decoration', 'underline', 'important'); |
| 982 | |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame] | 983 | const themedColor = ThemeSupport.ThemeSupport.instance().patchColorText( |
| 984 | 'rgb(33%, 33%, 33%)', ThemeSupport.ThemeSupport.ColorUsage.Foreground); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 985 | element.style.setProperty('color', themedColor, 'important'); |
| 986 | |
| 987 | let backgroundColor = 'hsl(0, 0%, 100%)'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 988 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 989 | backgroundColor = 'hsl(0, 100%, 97%)'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 990 | } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning || this._shouldRenderAsWarning()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 991 | backgroundColor = 'hsl(50, 100%, 95%)'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 992 | } |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame] | 993 | const themedBackgroundColor = ThemeSupport.ThemeSupport.instance().patchColorText( |
| 994 | backgroundColor, ThemeSupport.ThemeSupport.ColorUsage.Background); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 995 | element.style.setProperty('background-color', themedBackgroundColor, 'important'); |
| 996 | } |
| 997 | |
| 998 | /** |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 999 | * @param {!RegExp} regexObject |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1000 | * @return {boolean} |
| 1001 | */ |
| 1002 | matchesFilterRegex(regexObject) { |
| 1003 | regexObject.lastIndex = 0; |
Erik Luo | 5976c8c | 2018-07-24 02:03:09 | [diff] [blame] | 1004 | const contentElement = this.contentElement(); |
| 1005 | const anchorText = this._anchorElement ? this._anchorElement.deepTextContent() : ''; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1006 | return (!!anchorText && regexObject.test(anchorText.trim())) || |
Erik Luo | 5976c8c | 2018-07-24 02:03:09 | [diff] [blame] | 1007 | regexObject.test(contentElement.deepTextContent().slice(anchorText.length)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * @param {string} filter |
| 1012 | * @return {boolean} |
| 1013 | */ |
| 1014 | matchesFilterText(filter) { |
| 1015 | const text = this.contentElement().deepTextContent(); |
| 1016 | return text.toLowerCase().includes(filter.toLowerCase()); |
| 1017 | } |
| 1018 | |
| 1019 | updateTimestamp() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1020 | if (!this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1021 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1022 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1023 | |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 1024 | if (Common.Settings.Settings.instance().moduleSetting('consoleTimestampsEnabled').get()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1025 | if (!this._timestampElement) { |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1026 | this._timestampElement = /** @type {!HTMLElement} */ (document.createElement('span')); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 1027 | this._timestampElement.classList.add('console-timestamp'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1028 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1029 | this._timestampElement.textContent = UI.UIUtils.formatTimestamp(this._message.timestamp, false) + ' '; |
| 1030 | this._timestampElement.title = UI.UIUtils.formatTimestamp(this._message.timestamp, true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1031 | this._contentElement.insertBefore(this._timestampElement, this._contentElement.firstChild); |
| 1032 | } else if (this._timestampElement) { |
| 1033 | this._timestampElement.remove(); |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1034 | this._timestampElement = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1035 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * @return {number} |
| 1040 | */ |
| 1041 | nestingLevel() { |
| 1042 | return this._nestingLevel; |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * @param {boolean} inSimilarGroup |
| 1047 | * @param {boolean=} isLast |
| 1048 | */ |
| 1049 | setInSimilarGroup(inSimilarGroup, isLast) { |
| 1050 | this._inSimilarGroup = inSimilarGroup; |
| 1051 | this._lastInSimilarGroup = inSimilarGroup && !!isLast; |
| 1052 | if (this._similarGroupMarker && !inSimilarGroup) { |
| 1053 | this._similarGroupMarker.remove(); |
| 1054 | this._similarGroupMarker = null; |
| 1055 | } else if (this._element && !this._similarGroupMarker && inSimilarGroup) { |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1056 | this._similarGroupMarker = /** @type {!HTMLElement} */ (document.createElement('div')); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 1057 | this._similarGroupMarker.classList.add('nesting-level-marker'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1058 | this._element.insertBefore(this._similarGroupMarker, this._element.firstChild); |
| 1059 | this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * @return {boolean} |
| 1065 | */ |
| 1066 | isLastInSimilarGroup() { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1067 | return !!this._inSimilarGroup && !!this._lastInSimilarGroup; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | resetCloseGroupDecorationCount() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1071 | if (!this._closeGroupDecorationCount) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1072 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1073 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1074 | this._closeGroupDecorationCount = 0; |
| 1075 | this._updateCloseGroupDecorations(); |
| 1076 | } |
| 1077 | |
| 1078 | incrementCloseGroupDecorationCount() { |
| 1079 | ++this._closeGroupDecorationCount; |
| 1080 | this._updateCloseGroupDecorations(); |
| 1081 | } |
| 1082 | |
| 1083 | _updateCloseGroupDecorations() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1084 | if (!this._nestingLevelMarkers) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1085 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1086 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1087 | for (let i = 0, n = this._nestingLevelMarkers.length; i < n; ++i) { |
| 1088 | const marker = this._nestingLevelMarkers[i]; |
| 1089 | marker.classList.toggle('group-closed', n - i <= this._closeGroupDecorationCount); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /** |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1094 | * @return {number} |
| 1095 | */ |
| 1096 | _focusedChildIndex() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1097 | if (!this._selectableChildren.length) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1098 | return -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1099 | } |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1100 | return this._selectableChildren.findIndex(child => child.element.hasFocus()); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /** |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1104 | * @param {!KeyboardEvent} event |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1105 | */ |
| 1106 | _onKeyDown(event) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1107 | if (UI.UIUtils.isEditing() || !this._element || !this._element.hasFocus() || this._element.hasSelection()) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1108 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1109 | } |
| 1110 | if (this.maybeHandleOnKeyDown(event)) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1111 | event.consume(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1112 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * @protected |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1117 | * @param {!KeyboardEvent} event |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1118 | */ |
| 1119 | maybeHandleOnKeyDown(event) { |
| 1120 | // Handle trace expansion. |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1121 | const focusedChildIndex = this._focusedChildIndex(); |
| 1122 | const isWrapperFocused = focusedChildIndex === -1; |
| 1123 | if (this._expandTrace && isWrapperFocused) { |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1124 | if ((event.key === 'ArrowLeft' && this._traceExpanded) || (event.key === 'ArrowRight' && !this._traceExpanded)) { |
| 1125 | this._expandTrace(!this._traceExpanded); |
| 1126 | return true; |
| 1127 | } |
| 1128 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1129 | if (!this._selectableChildren.length) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1130 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1131 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1132 | |
| 1133 | if (event.key === 'ArrowLeft') { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1134 | this._element && this._element.focus(); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1135 | return true; |
| 1136 | } |
| 1137 | if (event.key === 'ArrowRight') { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1138 | if (isWrapperFocused && this._selectNearestVisibleChild(0)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1139 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1140 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1141 | } |
| 1142 | if (event.key === 'ArrowUp') { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1143 | const firstVisibleChild = this._nearestVisibleChild(0); |
| 1144 | if (this._selectableChildren[focusedChildIndex] === firstVisibleChild && firstVisibleChild) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1145 | this._element && this._element.focus(); |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1146 | return true; |
Mathias Bynens | f06e8c0 | 2020-02-28 13:58:28 | [diff] [blame] | 1147 | } |
| 1148 | if (this._selectNearestVisibleChild(focusedChildIndex - 1, true /* backwards */)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1149 | return true; |
| 1150 | } |
| 1151 | } |
| 1152 | if (event.key === 'ArrowDown') { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1153 | if (isWrapperFocused && this._selectNearestVisibleChild(0)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1154 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1155 | } |
| 1156 | if (!isWrapperFocused && this._selectNearestVisibleChild(focusedChildIndex + 1)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1157 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1158 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1159 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
| 1162 | |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1163 | /** |
| 1164 | * @param {number} fromIndex |
| 1165 | * @param {boolean=} backwards |
| 1166 | * @return {boolean} |
| 1167 | */ |
| 1168 | _selectNearestVisibleChild(fromIndex, backwards) { |
| 1169 | const nearestChild = this._nearestVisibleChild(fromIndex, backwards); |
| 1170 | if (nearestChild) { |
Erik Luo | 31c21f6 | 2018-12-13 03:39:39 | [diff] [blame] | 1171 | nearestChild.forceSelect(); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1172 | return true; |
| 1173 | } |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * @param {number} fromIndex |
| 1179 | * @param {boolean=} backwards |
Erik Luo | 31c21f6 | 2018-12-13 03:39:39 | [diff] [blame] | 1180 | * @return {?{element: !Element, forceSelect: function()}} |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1181 | */ |
| 1182 | _nearestVisibleChild(fromIndex, backwards) { |
| 1183 | const childCount = this._selectableChildren.length; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1184 | if (fromIndex < 0 || fromIndex >= childCount) { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1185 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1186 | } |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1187 | const direction = backwards ? -1 : 1; |
| 1188 | let index = fromIndex; |
| 1189 | |
| 1190 | while (!this._selectableChildren[index].element.offsetParent) { |
| 1191 | index += direction; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1192 | if (index < 0 || index >= childCount) { |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1193 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1194 | } |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1195 | } |
| 1196 | return this._selectableChildren[index]; |
| 1197 | } |
| 1198 | |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1199 | /** |
| 1200 | * @override |
| 1201 | */ |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1202 | focusLastChildOrSelf() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1203 | if (this._element && !this._selectNearestVisibleChild(this._selectableChildren.length - 1, true /* backwards */)) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1204 | this._element.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1205 | } |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /** |
Sigurd Schneider | b2953b2 | 2020-10-09 09:30:15 | [diff] [blame] | 1209 | * @protected |
| 1210 | * @param {!HTMLElement} element |
| 1211 | */ |
| 1212 | setContentElement(element) { |
| 1213 | console.assert(!this._contentElement, 'Cannot set content element twice'); |
| 1214 | this._contentElement = element; |
| 1215 | } |
| 1216 | |
Sigurd Schneider | b2953b2 | 2020-10-09 09:30:15 | [diff] [blame] | 1217 | /** |
| 1218 | * @protected |
| 1219 | * @return {?HTMLElement} |
| 1220 | */ |
| 1221 | getContentElement() { |
| 1222 | return this._contentElement; |
| 1223 | } |
| 1224 | |
| 1225 | /** |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1226 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1227 | */ |
| 1228 | contentElement() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1229 | if (this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1230 | return this._contentElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1231 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1232 | |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1233 | const contentElement = /** @type {!HTMLElement} */ (document.createElement('div')); |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 1234 | contentElement.classList.add('console-message'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1235 | if (this._messageLevelIcon) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1236 | contentElement.appendChild(this._messageLevelIcon); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1237 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1238 | this._contentElement = contentElement; |
| 1239 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1240 | const runtimeModel = this._message.runtimeModel(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1241 | let formattedMessage; |
| 1242 | const shouldIncludeTrace = !!this._message.stackTrace && |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1243 | (this._message.source === SDK.ConsoleModel.MessageSource.Network || |
| 1244 | this._message.source === SDK.ConsoleModel.MessageSource.Violation || |
| 1245 | this._message.level === SDK.ConsoleModel.MessageLevel.Error || |
| 1246 | this._message.level === SDK.ConsoleModel.MessageLevel.Warning || |
| 1247 | this._message.type === SDK.ConsoleModel.MessageType.Trace); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1248 | if (runtimeModel && shouldIncludeTrace) { |
| 1249 | formattedMessage = this._buildMessageWithStackTrace(runtimeModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1250 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1251 | formattedMessage = this._buildMessage(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1252 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1253 | contentElement.appendChild(formattedMessage); |
| 1254 | |
| 1255 | this.updateTimestamp(); |
| 1256 | return this._contentElement; |
| 1257 | } |
| 1258 | |
| 1259 | /** |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1260 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1261 | */ |
| 1262 | toMessageElement() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1263 | if (this._element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1264 | return this._element; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1265 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1266 | |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1267 | this._element = /** @type {!HTMLElement} */ (document.createElement('div')); |
Pavel Feldman | db31091 | 2019-01-30 00:31:20 | [diff] [blame] | 1268 | this._element.tabIndex = -1; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1269 | this._element.addEventListener('keydown', /** @type {!EventListener} */ (this._onKeyDown.bind(this))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1270 | this.updateMessageElement(); |
| 1271 | return this._element; |
| 1272 | } |
| 1273 | |
| 1274 | updateMessageElement() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1275 | if (!this._element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1276 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1277 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1278 | |
| 1279 | this._element.className = 'console-message-wrapper'; |
| 1280 | this._element.removeChildren(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1281 | if (this._message.isGroupStartMessage()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1282 | this._element.classList.add('console-group-title'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1283 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1284 | if (this._message.source === SDK.ConsoleModel.MessageSource.ConsoleAPI) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1285 | this._element.classList.add('console-from-api'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1286 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1287 | if (this._inSimilarGroup) { |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1288 | this._similarGroupMarker = /** @type {!HTMLElement} */ (this._element.createChild('div', 'nesting-level-marker')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1289 | this._similarGroupMarker.classList.toggle('group-closed', this._lastInSimilarGroup); |
| 1290 | } |
| 1291 | |
| 1292 | this._nestingLevelMarkers = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1293 | for (let i = 0; i < this._nestingLevel; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1294 | this._nestingLevelMarkers.push(this._element.createChild('div', 'nesting-level-marker')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1295 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1296 | this._updateCloseGroupDecorations(); |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1297 | elementToMessage.set(this._element, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1298 | |
| 1299 | switch (this._message.level) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1300 | case SDK.ConsoleModel.MessageLevel.Verbose: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1301 | this._element.classList.add('console-verbose-level'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1302 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1303 | case SDK.ConsoleModel.MessageLevel.Info: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1304 | this._element.classList.add('console-info-level'); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1305 | if (this._message.type === SDK.ConsoleModel.MessageType.System) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1306 | this._element.classList.add('console-system-type'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1307 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1308 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1309 | case SDK.ConsoleModel.MessageLevel.Warning: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1310 | this._element.classList.add('console-warning-level'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1311 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1312 | case SDK.ConsoleModel.MessageLevel.Error: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1313 | this._element.classList.add('console-error-level'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1314 | break; |
| 1315 | } |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1316 | this._updateMessageLevelIcon(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1317 | if (this._shouldRenderAsWarning()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1318 | this._element.classList.add('console-warning-level'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1319 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1320 | |
| 1321 | this._element.appendChild(this.contentElement()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1322 | if (this._repeatCount > 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1323 | this._showRepeatCountElement(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1324 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * @return {boolean} |
| 1329 | */ |
| 1330 | _shouldRenderAsWarning() { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1331 | return (this._message.level === SDK.ConsoleModel.MessageLevel.Verbose || |
| 1332 | this._message.level === SDK.ConsoleModel.MessageLevel.Info) && |
| 1333 | (this._message.source === SDK.ConsoleModel.MessageSource.Violation || |
| 1334 | this._message.source === SDK.ConsoleModel.MessageSource.Deprecation || |
| 1335 | this._message.source === SDK.ConsoleModel.MessageSource.Intervention || |
| 1336 | this._message.source === SDK.ConsoleModel.MessageSource.Recommendation); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1337 | } |
| 1338 | |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1339 | _updateMessageLevelIcon() { |
| 1340 | let iconType = ''; |
| 1341 | let accessibleName = ''; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1342 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1343 | iconType = 'smallicon-warning'; |
| 1344 | accessibleName = ls`Warning`; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1345 | } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1346 | iconType = 'smallicon-error'; |
| 1347 | accessibleName = ls`Error`; |
| 1348 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1349 | if (!this._messageLevelIcon) { |
| 1350 | if (!iconType) { |
| 1351 | return; |
| 1352 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1353 | this._messageLevelIcon = UI.Icon.Icon.create('', 'message-level-icon'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1354 | if (this._contentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1355 | this._contentElement.insertBefore(this._messageLevelIcon, this._contentElement.firstChild); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1356 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1357 | } |
| 1358 | this._messageLevelIcon.setIconType(iconType); |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1359 | UI.ARIAUtils.setAccessibleName(this._messageLevelIcon, accessibleName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | /** |
| 1363 | * @return {number} |
| 1364 | */ |
| 1365 | repeatCount() { |
| 1366 | return this._repeatCount || 1; |
| 1367 | } |
| 1368 | |
| 1369 | resetIncrementRepeatCount() { |
| 1370 | this._repeatCount = 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1371 | if (!this._repeatCountElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1372 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1373 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1374 | |
| 1375 | this._repeatCountElement.remove(); |
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.classList.remove('repeated-message'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1378 | } |
Sigurd Schneider | 53e9863 | 2020-10-26 15:29:50 | [diff] [blame] | 1379 | this._repeatCountElement = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | incrementRepeatCount() { |
| 1383 | this._repeatCount++; |
| 1384 | this._showRepeatCountElement(); |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * @param {number} repeatCount |
| 1389 | */ |
| 1390 | setRepeatCount(repeatCount) { |
| 1391 | this._repeatCount = repeatCount; |
| 1392 | this._showRepeatCountElement(); |
| 1393 | } |
| 1394 | |
Tim van der Lippe | ee954d4 | 2020-05-04 10:35:57 | [diff] [blame] | 1395 | /** |
| 1396 | * @suppress {checkTypes} |
| 1397 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1398 | _showRepeatCountElement() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1399 | if (!this._element) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1400 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1401 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1402 | |
| 1403 | if (!this._repeatCountElement) { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1404 | this._repeatCountElement = |
| 1405 | /** @type {!UI.UIUtils.DevToolsSmallBubble} */ (document.createElement('span', {is: 'dt-small-bubble'})); |
Tim van der Lippe | ee954d4 | 2020-05-04 10:35:57 | [diff] [blame] | 1406 | this._repeatCountElement.classList.add('console-message-repeat-count'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1407 | switch (this._message.level) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1408 | case SDK.ConsoleModel.MessageLevel.Warning: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1409 | this._repeatCountElement.type = 'warning'; |
| 1410 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1411 | case SDK.ConsoleModel.MessageLevel.Error: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1412 | this._repeatCountElement.type = 'error'; |
| 1413 | break; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1414 | case SDK.ConsoleModel.MessageLevel.Verbose: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1415 | this._repeatCountElement.type = 'verbose'; |
| 1416 | break; |
| 1417 | default: |
| 1418 | this._repeatCountElement.type = 'info'; |
| 1419 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1420 | if (this._shouldRenderAsWarning()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1421 | this._repeatCountElement.type = 'warning'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1422 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1423 | |
| 1424 | this._element.insertBefore(this._repeatCountElement, this._contentElement); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1425 | this.contentElement().classList.add('repeated-message'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1426 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1427 | this._repeatCountElement.textContent = `${this._repeatCount}`; |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1428 | let accessibleName = ls`Repeat ${this._repeatCount}`; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1429 | if (this._message.level === SDK.ConsoleModel.MessageLevel.Warning) { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1430 | accessibleName = ls`Warning ${accessibleName}`; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1431 | } else if (this._message.level === SDK.ConsoleModel.MessageLevel.Error) { |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1432 | accessibleName = ls`Error ${accessibleName}`; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1433 | } |
Erik Luo | fd3e7d4 | 2018-09-25 02:12:35 | [diff] [blame] | 1434 | UI.ARIAUtils.setAccessibleName(this._repeatCountElement, accessibleName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | get text() { |
| 1438 | return this._message.messageText; |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * @return {string} |
| 1443 | */ |
| 1444 | toExportString() { |
| 1445 | const lines = []; |
| 1446 | const nodes = this.contentElement().childTextNodes(); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1447 | const messageContent = nodes.map(Components.Linkifier.Linkifier.untruncatedNodeText).join(''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1448 | for (let i = 0; i < this.repeatCount(); ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1449 | lines.push(messageContent); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1450 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1451 | return lines.join('\n'); |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * @param {?RegExp} regex |
| 1456 | */ |
| 1457 | setSearchRegex(regex) { |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 1458 | if (this._searchHighlightNodeChanges && this._searchHighlightNodeChanges.length) { |
| 1459 | UI.UIUtils.revertDomChanges(this._searchHighlightNodeChanges); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1460 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1461 | this._searchRegex = regex; |
| 1462 | this._searchHighlightNodes = []; |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 1463 | this._searchHighlightNodeChanges = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1464 | if (!this._searchRegex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1465 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1466 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1467 | |
| 1468 | const text = this.contentElement().deepTextContent(); |
| 1469 | let match; |
| 1470 | this._searchRegex.lastIndex = 0; |
| 1471 | const sourceRanges = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1472 | while ((match = this._searchRegex.exec(text)) && match[0]) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1473 | sourceRanges.push(new TextUtils.TextRange.SourceRange(match.index, match[0].length)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1474 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1475 | |
| 1476 | if (sourceRanges.length) { |
| 1477 | this._searchHighlightNodes = |
Sigurd Schneider | e8e75cf | 2020-10-13 08:17:52 | [diff] [blame] | 1478 | UI.UIUtils.highlightSearchResults(this.contentElement(), sourceRanges, this._searchHighlightNodeChanges); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * @return {?RegExp} |
| 1484 | */ |
| 1485 | searchRegex() { |
| 1486 | return this._searchRegex; |
| 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * @return {number} |
| 1491 | */ |
| 1492 | searchCount() { |
| 1493 | return this._searchHighlightNodes.length; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1497 | * @param {number} index |
| 1498 | * @return {!Element} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1499 | */ |
| 1500 | searchHighlightNode(index) { |
| 1501 | return this._searchHighlightNodes[index]; |
| 1502 | } |
| 1503 | |
| 1504 | /** |
| 1505 | * @param {string} string |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1506 | * @return {?HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1507 | */ |
| 1508 | _tryFormatAsError(string) { |
| 1509 | /** |
| 1510 | * @param {string} prefix |
| 1511 | */ |
| 1512 | function startsWith(prefix) { |
| 1513 | return string.startsWith(prefix); |
| 1514 | } |
| 1515 | |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1516 | const runtimeModel = this._message.runtimeModel(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1517 | const errorPrefixes = |
| 1518 | ['EvalError', 'ReferenceError', 'SyntaxError', 'TypeError', 'RangeError', 'Error', 'URIError']; |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1519 | if (!runtimeModel || !errorPrefixes.some(startsWith)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1520 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1521 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1522 | const debuggerModel = runtimeModel.debuggerModel(); |
| 1523 | const baseURL = runtimeModel.target().inspectedURL(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1524 | |
| 1525 | const lines = string.split('\n'); |
| 1526 | const links = []; |
| 1527 | let position = 0; |
| 1528 | for (let i = 0; i < lines.length; ++i) { |
| 1529 | position += i > 0 ? lines[i - 1].length + 1 : 0; |
| 1530 | const isCallFrameLine = /^\s*at\s/.test(lines[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1531 | if (!isCallFrameLine && links.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1532 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1533 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1534 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1535 | if (!isCallFrameLine) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1536 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1537 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1538 | |
| 1539 | let openBracketIndex = -1; |
| 1540 | let closeBracketIndex = -1; |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1541 | const inBracketsWithLineAndColumn = /\([^\)\(]+:\d+:\d+\)/g; |
| 1542 | const inBrackets = /\([^\)\(]+\)/g; |
| 1543 | let lastMatch = null; |
| 1544 | let currentMatch; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1545 | while ((currentMatch = inBracketsWithLineAndColumn.exec(lines[i]))) { |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1546 | lastMatch = currentMatch; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1547 | } |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1548 | if (!lastMatch) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1549 | while ((currentMatch = inBrackets.exec(lines[i]))) { |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1550 | lastMatch = currentMatch; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1551 | } |
Yang Guo | 39256bd | 2019-07-18 06:02:25 | [diff] [blame] | 1552 | } |
| 1553 | if (lastMatch) { |
| 1554 | openBracketIndex = lastMatch.index; |
| 1555 | closeBracketIndex = lastMatch.index + lastMatch[0].length - 1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1556 | } |
| 1557 | const hasOpenBracket = openBracketIndex !== -1; |
| 1558 | const left = hasOpenBracket ? openBracketIndex + 1 : lines[i].indexOf('at') + 3; |
| 1559 | const right = hasOpenBracket ? closeBracketIndex : lines[i].length; |
| 1560 | const linkCandidate = lines[i].substring(left, right); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1561 | const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(linkCandidate); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1562 | if (!splitResult) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1563 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1564 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1565 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1566 | if (splitResult.url === '<anonymous>') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1567 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1568 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1569 | let url = parseOrScriptMatch(splitResult.url); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1570 | if (!url && Common.ParsedURL.ParsedURL.isRelativeURL(splitResult.url)) { |
| 1571 | url = parseOrScriptMatch(Common.ParsedURL.ParsedURL.completeURL(baseURL, splitResult.url)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1572 | } |
| 1573 | if (!url) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1574 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1575 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1576 | |
| 1577 | links.push({ |
| 1578 | url: url, |
| 1579 | positionLeft: position + left, |
| 1580 | positionRight: position + right, |
| 1581 | lineNumber: splitResult.lineNumber, |
| 1582 | columnNumber: splitResult.columnNumber |
| 1583 | }); |
| 1584 | } |
| 1585 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1586 | if (!links.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1587 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1588 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1589 | |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1590 | const formattedResult = /** @type {!HTMLElement} */ (document.createElement('span')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1591 | let start = 0; |
| 1592 | for (let i = 0; i < links.length; ++i) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1593 | formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start, links[i].positionLeft))); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1594 | const scriptLocationLink = this._linkifier.linkifyScriptLocation( |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1595 | debuggerModel.target(), null, links[i].url, links[i].lineNumber, |
| 1596 | {columnNumber: links[i].columnNumber, className: undefined, tabStop: undefined}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1597 | scriptLocationLink.tabIndex = -1; |
Erik Luo | 31c21f6 | 2018-12-13 03:39:39 | [diff] [blame] | 1598 | this._selectableChildren.push({element: scriptLocationLink, forceSelect: () => scriptLocationLink.focus()}); |
Erik Luo | 182bece | 2018-11-29 03:15:22 | [diff] [blame] | 1599 | formattedResult.appendChild(scriptLocationLink); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1600 | start = links[i].positionRight; |
| 1601 | } |
| 1602 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1603 | if (start !== string.length) { |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1604 | formattedResult.appendChild(this._linkifyStringAsFragment(string.substring(start))); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1605 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1606 | |
| 1607 | return formattedResult; |
| 1608 | |
| 1609 | /** |
| 1610 | * @param {?string} url |
| 1611 | * @return {?string} |
| 1612 | */ |
| 1613 | function parseOrScriptMatch(url) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1614 | if (!url) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1615 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1616 | } |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1617 | const parsedURL = Common.ParsedURL.ParsedURL.fromString(url); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1618 | if (parsedURL) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1619 | return parsedURL.url; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1620 | } |
| 1621 | if (debuggerModel.scriptsForSourceURL(url).length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1622 | return url; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1623 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1624 | return null; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * @param {string} string |
| 1630 | * @param {function(string,string,number=,number=):!Node} linkifier |
| 1631 | * @return {!DocumentFragment} |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 1632 | * @suppress {accessControls} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1633 | */ |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 1634 | _linkifyWithCustomLinkifier(string, linkifier) { |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1635 | if (string.length > getMaxTokenizableStringLength()) { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1636 | const propertyValue = new ObjectUI.ObjectPropertiesSection.ExpandableTextPropertyValue( |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1637 | document.createElement('span'), string, getLongStringVisibleLength()); |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1638 | const fragment = document.createDocumentFragment(); |
Connor Moody | 1a5c0d3 | 2019-12-19 07:23:36 | [diff] [blame] | 1639 | fragment.appendChild(propertyValue.element); |
| 1640 | return fragment; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1641 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1642 | const container = document.createDocumentFragment(); |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 1643 | const tokens = ConsoleViewMessage._tokenizeMessageText(string); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1644 | for (const token of tokens) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1645 | if (!token.text) { |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 1646 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1647 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1648 | switch (token.type) { |
| 1649 | case 'url': { |
| 1650 | const realURL = (token.text.startsWith('www.') ? 'http://' + token.text : token.text); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1651 | const splitResult = Common.ParsedURL.ParsedURL.splitLineAndColumn(realURL); |
Kim-Anh Tran | 9e49a45 | 2020-02-17 09:46:10 | [diff] [blame] | 1652 | const sourceURL = Common.ParsedURL.ParsedURL.removeWasmFunctionInfoFromURL(splitResult.url); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1653 | let linkNode; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1654 | if (splitResult) { |
Kim-Anh Tran | 9e49a45 | 2020-02-17 09:46:10 | [diff] [blame] | 1655 | linkNode = linkifier(token.text, sourceURL, splitResult.lineNumber, splitResult.columnNumber); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1656 | } else { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1657 | linkNode = linkifier(token.text, ''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1658 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1659 | container.appendChild(linkNode); |
| 1660 | break; |
| 1661 | } |
| 1662 | default: |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1663 | container.appendChild(document.createTextNode(token.text)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1664 | break; |
| 1665 | } |
| 1666 | } |
| 1667 | return container; |
| 1668 | } |
| 1669 | |
| 1670 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1671 | * @param {string} string |
| 1672 | * @return {!DocumentFragment} |
| 1673 | */ |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1674 | _linkifyStringAsFragment(string) { |
Erik Luo | fc2214f | 2018-11-21 19:54:58 | [diff] [blame] | 1675 | return this._linkifyWithCustomLinkifier(string, (text, url, lineNumber, columnNumber) => { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1676 | const options = {text, lineNumber, columnNumber}; |
| 1677 | const linkElement = Components.Linkifier.Linkifier.linkifyURL( |
| 1678 | url, /** @type {!Components.Linkifier.LinkifyURLOptions} */ (options)); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1679 | linkElement.tabIndex = -1; |
Erik Luo | 31c21f6 | 2018-12-13 03:39:39 | [diff] [blame] | 1680 | this._selectableChildren.push({element: linkElement, forceSelect: () => linkElement.focus()}); |
Erik Luo | 383f21d | 2018-11-07 23:16:37 | [diff] [blame] | 1681 | return linkElement; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1682 | }); |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * @param {string} string |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1687 | * @return {!Array<{type: (string|undefined), text: string}>} |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 1688 | * @suppress {accessControls} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1689 | */ |
| 1690 | static _tokenizeMessageText(string) { |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1691 | const {tokenizerRegexes, tokenizerTypes} = getOrCreateTokenizers(); |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 1692 | if (string.length > getMaxTokenizableStringLength()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1693 | return [{text: string, type: undefined}]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1694 | } |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1695 | const results = TextUtils.TextUtils.Utils.splitStringByRegexes(string, tokenizerRegexes); |
| 1696 | return results.map(result => ({text: result.value, type: tokenizerTypes[result.regexIndex]})); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /** |
| 1700 | * @return {string} |
| 1701 | */ |
| 1702 | groupKey() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1703 | if (!this._groupKey) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1704 | this._groupKey = this._message.groupCategoryKey() + ':' + this.groupTitle(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1705 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1706 | return this._groupKey; |
| 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * @return {string} |
| 1711 | */ |
| 1712 | groupTitle() { |
Tim van der Lippe | eaacb72 | 2020-01-10 12:16:00 | [diff] [blame] | 1713 | const tokens = ConsoleViewMessage._tokenizeMessageText(this._message.messageText); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1714 | const result = tokens.reduce((acc, token) => { |
| 1715 | let text = token.text; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1716 | if (token.type === 'url') { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1717 | text = Common.UIString.UIString('<URL>'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1718 | } else if (token.type === 'time') { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1719 | text = Common.UIString.UIString('took <N>ms'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1720 | } else if (token.type === 'event') { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1721 | text = Common.UIString.UIString('<some> event'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1722 | } else if (token.type === 'milestone') { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1723 | text = Common.UIString.UIString(' M<XX>'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1724 | } else if (token.type === 'autofill') { |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1725 | text = Common.UIString.UIString('<attribute>'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1726 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1727 | return acc + text; |
| 1728 | }, ''); |
| 1729 | return result.replace(/[%]o/g, ''); |
| 1730 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 1731 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1732 | |
Sigurd Schneider | 30ac3dd | 2020-10-13 09:06:39 | [diff] [blame] | 1733 | /** @type {?Array<!RegExp>} */ |
| 1734 | let tokenizerRegexes = null; |
| 1735 | /** @type {?Array<string>} */ |
| 1736 | let tokenizerTypes = null; |
| 1737 | |
| 1738 | /** |
| 1739 | * @return {!{tokenizerRegexes:!Array<!RegExp>, tokenizerTypes:Array<string>}} |
| 1740 | */ |
| 1741 | function getOrCreateTokenizers() { |
| 1742 | if (!tokenizerRegexes || !tokenizerTypes) { |
| 1743 | const controlCodes = '\\u0000-\\u0020\\u007f-\\u009f'; |
| 1744 | const linkStringRegex = new RegExp( |
| 1745 | '(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\\/\\/|data:|www\\.)[^\\s' + controlCodes + '"]{2,}[^\\s' + controlCodes + |
| 1746 | '"\')}\\],:;.!?]', |
| 1747 | 'u'); |
| 1748 | const pathLineRegex = /(?:\/[\w\.-]*)+\:[\d]+/; |
| 1749 | const timeRegex = /took [\d]+ms/; |
| 1750 | const eventRegex = /'\w+' event/; |
| 1751 | const milestoneRegex = /\sM[6-7]\d/; |
| 1752 | const autofillRegex = /\(suggested: \"[\w-]+\"\)/; |
| 1753 | /** @type {!Map<!RegExp, string>} */ |
| 1754 | const handlers = new Map(); |
| 1755 | handlers.set(linkStringRegex, 'url'); |
| 1756 | handlers.set(pathLineRegex, 'url'); |
| 1757 | handlers.set(timeRegex, 'time'); |
| 1758 | handlers.set(eventRegex, 'event'); |
| 1759 | handlers.set(milestoneRegex, 'milestone'); |
| 1760 | handlers.set(autofillRegex, 'autofill'); |
| 1761 | tokenizerRegexes = Array.from(handlers.keys()); |
| 1762 | tokenizerTypes = Array.from(handlers.values()); |
| 1763 | return {tokenizerRegexes, tokenizerTypes}; |
| 1764 | } |
| 1765 | return {tokenizerRegexes, tokenizerTypes}; |
| 1766 | } |
| 1767 | |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 1768 | export class ConsoleGroupViewMessage extends ConsoleViewMessage { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1769 | /** |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1770 | * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage |
| 1771 | * @param {!Components.Linkifier.Linkifier} linkifier |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1772 | * @param {number} nestingLevel |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1773 | * @param {function(): void} onToggle |
| 1774 | * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1775 | */ |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 1776 | constructor(consoleMessage, linkifier, nestingLevel, onToggle, onResize) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1777 | console.assert(consoleMessage.isGroupStartMessage()); |
Tim van der Lippe | b45d9a0 | 2019-11-05 17:24:41 | [diff] [blame] | 1778 | super(consoleMessage, linkifier, nestingLevel, onResize); |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1779 | this._collapsed = consoleMessage.type === SDK.ConsoleModel.MessageType.StartGroupCollapsed; |
| 1780 | /** @type {?UI.Icon.Icon} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1781 | this._expandGroupIcon = null; |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1782 | this._onToggle = onToggle; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | /** |
| 1786 | * @param {boolean} collapsed |
| 1787 | */ |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1788 | _setCollapsed(collapsed) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1789 | this._collapsed = collapsed; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1790 | if (this._expandGroupIcon) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1791 | this._expandGroupIcon.setIconType(this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1792 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1793 | this._onToggle.call(null); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * @return {boolean} |
| 1798 | */ |
| 1799 | collapsed() { |
| 1800 | return this._collapsed; |
| 1801 | } |
| 1802 | |
| 1803 | /** |
| 1804 | * @override |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1805 | * @param {!KeyboardEvent} event |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1806 | */ |
| 1807 | maybeHandleOnKeyDown(event) { |
Erik Luo | 0b8282e | 2018-10-08 20:37:46 | [diff] [blame] | 1808 | const focusedChildIndex = this._focusedChildIndex(); |
| 1809 | if (focusedChildIndex === -1) { |
| 1810 | if ((event.key === 'ArrowLeft' && !this._collapsed) || (event.key === 'ArrowRight' && this._collapsed)) { |
| 1811 | this._setCollapsed(!this._collapsed); |
| 1812 | return true; |
| 1813 | } |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1814 | } |
| 1815 | return super.maybeHandleOnKeyDown(event); |
| 1816 | } |
| 1817 | |
| 1818 | /** |
| 1819 | * @override |
Sigurd Schneider | 53f3352 | 2020-10-08 15:00:49 | [diff] [blame] | 1820 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1821 | */ |
| 1822 | toMessageElement() { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1823 | /** @type {?HTMLElement} */ |
| 1824 | let element = this._element || null; |
| 1825 | if (!element) { |
| 1826 | element = super.toMessageElement(); |
Erik Luo | 8ef5d0c | 2018-09-25 21:16:00 | [diff] [blame] | 1827 | const iconType = this._collapsed ? 'smallicon-triangle-right' : 'smallicon-triangle-down'; |
Tim van der Lippe | 9b2f871 | 2020-02-12 17:46:22 | [diff] [blame] | 1828 | this._expandGroupIcon = UI.Icon.Icon.create(iconType, 'expand-group-icon'); |
Erik Luo | b5bfff4 | 2018-09-20 02:52:39 | [diff] [blame] | 1829 | // Intercept focus to avoid highlight on click. |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1830 | this.contentElement().tabIndex = -1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1831 | if (this._repeatCountElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1832 | this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1833 | } else { |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1834 | element.insertBefore(this._expandGroupIcon, this._contentElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1835 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1836 | element.addEventListener('click', () => this._setCollapsed(!this._collapsed)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1837 | } |
Sigurd Schneider | 45f32c3 | 2020-10-13 13:32:05 | [diff] [blame] | 1838 | return element; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /** |
| 1842 | * @override |
| 1843 | */ |
| 1844 | _showRepeatCountElement() { |
| 1845 | super._showRepeatCountElement(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1846 | if (this._repeatCountElement && this._expandGroupIcon) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1847 | this._repeatCountElement.insertBefore(this._expandGroupIcon, this._repeatCountElement.firstChild); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1848 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1849 | } |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 1850 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1851 | |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 1852 | export class ConsoleCommand extends ConsoleViewMessage { |
| 1853 | /** |
| 1854 | * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage |
| 1855 | * @param {!Components.Linkifier.Linkifier} linkifier |
| 1856 | * @param {number} nestingLevel |
| 1857 | * @param {function(!Common.EventTarget.EventTargetEvent):void} onResize |
| 1858 | */ |
| 1859 | constructor(consoleMessage, linkifier, nestingLevel, onResize) { |
| 1860 | super(consoleMessage, linkifier, nestingLevel, onResize); |
| 1861 | /** @type {?HTMLElement} */ |
| 1862 | this._formattedCommand = null; |
| 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * @override |
| 1867 | * @return {!HTMLElement} |
| 1868 | */ |
| 1869 | contentElement() { |
| 1870 | const contentElement = this.getContentElement(); |
| 1871 | if (contentElement) { |
| 1872 | return contentElement; |
| 1873 | } |
| 1874 | const newContentElement = /** @type {!HTMLElement} */ (document.createElement('div')); |
| 1875 | this.setContentElement(newContentElement); |
| 1876 | newContentElement.classList.add('console-user-command'); |
| 1877 | const icon = UI.Icon.Icon.create('smallicon-user-command', 'command-result-icon'); |
| 1878 | newContentElement.appendChild(icon); |
| 1879 | |
| 1880 | elementToMessage.set(newContentElement, this); |
| 1881 | |
| 1882 | this._formattedCommand = /** @type {!HTMLElement} */ (document.createElement('span')); |
| 1883 | this._formattedCommand.classList.add('source-code'); |
| 1884 | this._formattedCommand.textContent = Platform.StringUtilities.replaceControlCharacters(this.text); |
| 1885 | newContentElement.appendChild(this._formattedCommand); |
| 1886 | |
| 1887 | if (this._formattedCommand.textContent.length < MaxLengthToIgnoreHighlighter) { |
| 1888 | const javascriptSyntaxHighlighter = new UI.SyntaxHighlighter.SyntaxHighlighter('text/javascript', true); |
| 1889 | javascriptSyntaxHighlighter.syntaxHighlightNode(this._formattedCommand).then(this._updateSearch.bind(this)); |
| 1890 | } else { |
| 1891 | this._updateSearch(); |
| 1892 | } |
| 1893 | |
| 1894 | this.updateTimestamp(); |
| 1895 | return newContentElement; |
| 1896 | } |
| 1897 | |
| 1898 | _updateSearch() { |
| 1899 | this.setSearchRegex(this.searchRegex()); |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | export class ConsoleCommandResult extends ConsoleViewMessage { |
| 1904 | /** |
| 1905 | * @override |
| 1906 | * @return {!HTMLElement} |
| 1907 | */ |
| 1908 | contentElement() { |
| 1909 | const element = super.contentElement(); |
| 1910 | if (!element.classList.contains('console-user-command-result')) { |
| 1911 | element.classList.add('console-user-command-result'); |
| 1912 | if (this.consoleMessage().level === SDK.ConsoleModel.MessageLevel.Info) { |
| 1913 | const icon = UI.Icon.Icon.create('smallicon-command-result', 'command-result-icon'); |
| 1914 | element.insertBefore(icon, element.firstChild); |
| 1915 | } |
| 1916 | } |
| 1917 | return element; |
| 1918 | } |
| 1919 | } |
| 1920 | |
Sigurd Schneider | 8bfb421 | 2020-10-27 10:27:37 | [diff] [blame^] | 1921 | export class ConsoleTableMessageView extends ConsoleViewMessage { |
| 1922 | /** |
| 1923 | * @param {!SDK.ConsoleModel.ConsoleMessage} consoleMessage |
| 1924 | * @param {!Components.Linkifier.Linkifier} linkifier |
| 1925 | * @param {number} nestingLevel |
| 1926 | * @param {function(!Common.EventTarget.EventTargetEvent): void} onResize |
| 1927 | */ |
| 1928 | constructor(consoleMessage, linkifier, nestingLevel, onResize) { |
| 1929 | super(consoleMessage, linkifier, nestingLevel, onResize); |
| 1930 | console.assert(consoleMessage.type === SDK.ConsoleModel.MessageType.Table); |
| 1931 | /** @type {?DataGrid.SortableDataGrid.SortableDataGrid<?>} */ |
| 1932 | this._dataGrid = null; |
| 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * @override |
| 1937 | */ |
| 1938 | wasShown() { |
| 1939 | if (this._dataGrid) { |
| 1940 | this._dataGrid.updateWidths(); |
| 1941 | } |
| 1942 | super.wasShown(); |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * @override |
| 1947 | */ |
| 1948 | onResize() { |
| 1949 | if (!this.isVisible()) { |
| 1950 | return; |
| 1951 | } |
| 1952 | if (this._dataGrid) { |
| 1953 | this._dataGrid.onResize(); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * @override |
| 1959 | * @return {!HTMLElement} |
| 1960 | */ |
| 1961 | contentElement() { |
| 1962 | const contentElement = this.getContentElement(); |
| 1963 | if (contentElement) { |
| 1964 | return contentElement; |
| 1965 | } |
| 1966 | |
| 1967 | const newContentElement = /** @type {!HTMLElement} */ (document.createElement('div')); |
| 1968 | newContentElement.classList.add('console-message'); |
| 1969 | if (this._messageLevelIcon) { |
| 1970 | newContentElement.appendChild(this._messageLevelIcon); |
| 1971 | } |
| 1972 | this.setContentElement(newContentElement); |
| 1973 | |
| 1974 | newContentElement.appendChild(this._buildTableMessage()); |
| 1975 | this.updateTimestamp(); |
| 1976 | return newContentElement; |
| 1977 | } |
| 1978 | |
| 1979 | /** |
| 1980 | * @return {!HTMLElement} |
| 1981 | */ |
| 1982 | _buildTableMessage() { |
| 1983 | const formattedMessage = /** @type {!HTMLElement} */ (document.createElement('span')); |
| 1984 | formattedMessage.classList.add('source-code'); |
| 1985 | this._anchorElement = this._buildMessageAnchor(); |
| 1986 | if (this._anchorElement) { |
| 1987 | formattedMessage.appendChild(this._anchorElement); |
| 1988 | } |
| 1989 | |
| 1990 | const table = this._message.parameters && this._message.parameters.length ? this._message.parameters[0] : null; |
| 1991 | if (!table) { |
| 1992 | return this._buildMessage(); |
| 1993 | } |
| 1994 | const actualTable = parameterToRemoteObject(this._message.runtimeModel())(table); |
| 1995 | if (!actualTable || !actualTable.preview) { |
| 1996 | return this._buildMessage(); |
| 1997 | } |
| 1998 | |
| 1999 | const rawValueColumnSymbol = Symbol('rawValueColumn'); |
| 2000 | /** @type {!Array<string|symbol>} */ |
| 2001 | const columnNames = []; |
| 2002 | const preview = actualTable.preview; |
| 2003 | const rows = []; |
| 2004 | for (let i = 0; i < preview.properties.length; ++i) { |
| 2005 | const rowProperty = preview.properties[i]; |
| 2006 | /** @type {!Array<!Protocol.Runtime.PropertyPreview|!{name:(string|symbol), type: !Protocol.Runtime.PropertyPreviewType, value: (string|undefined)}>} */ |
| 2007 | let rowSubProperties; |
| 2008 | if (rowProperty.valuePreview) { |
| 2009 | rowSubProperties = rowProperty.valuePreview.properties; |
| 2010 | } else if (rowProperty.value) { |
| 2011 | rowSubProperties = [{name: rawValueColumnSymbol, type: rowProperty.type, value: rowProperty.value}]; |
| 2012 | } else { |
| 2013 | continue; |
| 2014 | } |
| 2015 | |
| 2016 | /** @type {!Map<string|symbol, !HTMLElement>} */ |
| 2017 | const rowValue = new Map(); |
| 2018 | const maxColumnsToRender = 20; |
| 2019 | for (let j = 0; j < rowSubProperties.length; ++j) { |
| 2020 | const cellProperty = rowSubProperties[j]; |
| 2021 | let columnRendered = columnNames.indexOf(cellProperty.name) !== -1; |
| 2022 | if (!columnRendered) { |
| 2023 | if (columnNames.length === maxColumnsToRender) { |
| 2024 | continue; |
| 2025 | } |
| 2026 | columnRendered = true; |
| 2027 | columnNames.push(cellProperty.name); |
| 2028 | } |
| 2029 | |
| 2030 | if (columnRendered) { |
| 2031 | const cellElement = |
| 2032 | this._renderPropertyPreviewOrAccessor(actualTable, cellProperty, [rowProperty, cellProperty]); |
| 2033 | cellElement.classList.add('console-message-nowrap-below'); |
| 2034 | rowValue.set(cellProperty.name, cellElement); |
| 2035 | } |
| 2036 | } |
| 2037 | rows.push({rowName: rowProperty.name, rowValue}); |
| 2038 | } |
| 2039 | |
| 2040 | const flatValues = []; |
| 2041 | for (const {rowName, rowValue} of rows) { |
| 2042 | flatValues.push(rowName); |
| 2043 | for (let j = 0; j < columnNames.length; ++j) { |
| 2044 | flatValues.push(rowValue.get(columnNames[j])); |
| 2045 | } |
| 2046 | } |
| 2047 | columnNames.unshift(Common.UIString.UIString('(index)')); |
| 2048 | const columnDisplayNames = |
| 2049 | columnNames.map(name => name === rawValueColumnSymbol ? Common.UIString.UIString('Value') : name.toString()); |
| 2050 | |
| 2051 | if (flatValues.length) { |
| 2052 | this._dataGrid = DataGrid.SortableDataGrid.SortableDataGrid.create(columnDisplayNames, flatValues, ls`Console`); |
| 2053 | if (this._dataGrid) { |
| 2054 | this._dataGrid.setStriped(true); |
| 2055 | this._dataGrid.setFocusable(false); |
| 2056 | |
| 2057 | const formattedResult = document.createElement('span'); |
| 2058 | formattedResult.classList.add('console-message-text'); |
| 2059 | const tableElement = formattedResult.createChild('div', 'console-message-formatted-table'); |
| 2060 | const dataGridContainer = tableElement.createChild('span'); |
| 2061 | tableElement.appendChild(this._formatParameter(actualTable, true, false)); |
| 2062 | dataGridContainer.appendChild(this._dataGrid.element); |
| 2063 | formattedMessage.appendChild(formattedResult); |
| 2064 | this._dataGrid.renderInline(); |
| 2065 | } |
| 2066 | } |
| 2067 | return formattedMessage; |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * @override |
| 2072 | * @return {number} |
| 2073 | */ |
| 2074 | approximateFastHeight() { |
| 2075 | const table = this._message.parameters && this._message.parameters[0]; |
| 2076 | if (table && typeof table !== 'string' && table.preview) { |
| 2077 | return defaultConsoleRowHeight * table.preview.properties.length; |
| 2078 | } |
| 2079 | return defaultConsoleRowHeight; |
| 2080 | } |
| 2081 | } |
| 2082 | |
Sigurd Schneider | ca7b4ff | 2020-10-14 07:45:47 | [diff] [blame] | 2083 | /** |
| 2084 | * The maximum length before strings are considered too long for syntax highlighting. |
| 2085 | * @const |
| 2086 | * @type {number} |
| 2087 | */ |
| 2088 | const MaxLengthToIgnoreHighlighter = 10000; |
| 2089 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2090 | /** |
| 2091 | * @const |
| 2092 | * @type {number} |
| 2093 | */ |
Paul Lewis | bf7aa3c | 2019-11-20 17:03:38 | [diff] [blame] | 2094 | export const MaxLengthForLinks = 40; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2095 | |
Sigurd Schneider | 8f4ac86 | 2020-10-13 13:30:11 | [diff] [blame] | 2096 | let _MaxTokenizableStringLength = 10000; |
| 2097 | let _LongStringVisibleLength = 5000; |
| 2098 | |
| 2099 | export const getMaxTokenizableStringLength = () => { |
| 2100 | return _MaxTokenizableStringLength; |
| 2101 | }; |
| 2102 | |
| 2103 | /** |
| 2104 | * @param {number} length |
| 2105 | */ |
| 2106 | export const setMaxTokenizableStringLength = length => { |
| 2107 | _MaxTokenizableStringLength = length; |
| 2108 | }; |
| 2109 | |
| 2110 | export const getLongStringVisibleLength = () => { |
| 2111 | return _LongStringVisibleLength; |
| 2112 | }; |
| 2113 | |
| 2114 | /** |
| 2115 | * @param {number} length |
| 2116 | */ |
| 2117 | export const setLongStringVisibleLength = length => { |
| 2118 | _LongStringVisibleLength = length; |
| 2119 | }; |