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