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