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