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