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