Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 31 | import * as Components from '../components/components.js'; |
| 32 | import * as SDK from '../sdk/sdk.js'; |
| 33 | import * as UI from '../ui/ui.js'; |
| 34 | |
Paul Lewis | d85dc38 | 2020-01-14 17:13:56 | [diff] [blame] | 35 | import {CustomPreviewComponent} from './CustomPreviewComponent.js'; |
| 36 | import {ObjectPropertiesSection} from './ObjectPropertiesSection.js'; |
| 37 | |
| 38 | export class ObjectPopoverHelper { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | /** |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 40 | * @param {?Components.Linkifier.Linkifier} linkifier |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | * @param {boolean} resultHighlightedAsDOM |
| 42 | */ |
| 43 | constructor(linkifier, resultHighlightedAsDOM) { |
| 44 | this._linkifier = linkifier; |
| 45 | this._resultHighlightedAsDOM = resultHighlightedAsDOM; |
| 46 | } |
| 47 | |
| 48 | dispose() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 49 | if (this._resultHighlightedAsDOM) { |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 50 | SDK.OverlayModel.OverlayModel.hideDOMNodeHighlight(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 51 | } |
| 52 | if (this._linkifier) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | this._linkifier.dispose(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 54 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 58 | * @param {!SDK.RemoteObject.RemoteObject} result |
| 59 | * @param {!UI.GlassPane.GlassPane} popover |
Tim van der Lippe | 3574172 | 2019-11-21 15:36:20 | [diff] [blame] | 60 | * @return {!Promise<?ObjectPopoverHelper>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | */ |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 62 | static async buildObjectPopover(result, popover) { |
Simon Zünd | 9f9ea0f | 2020-10-20 06:31:02 | [diff] [blame] | 63 | const description = (result.description || '').trimEndWithMaxLength(MaxPopoverTextLength); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | let popoverContentElement = null; |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 65 | if (result.type === 'object') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | let linkifier = null; |
| 67 | let resultHighlightedAsDOM = false; |
| 68 | if (result.subtype === 'node') { |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 69 | SDK.OverlayModel.OverlayModel.highlightObjectAsDOMNode(result); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | resultHighlightedAsDOM = true; |
| 71 | } |
| 72 | |
| 73 | if (result.customPreview()) { |
Paul Lewis | d85dc38 | 2020-01-14 17:13:56 | [diff] [blame] | 74 | const customPreviewComponent = new CustomPreviewComponent(result); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | customPreviewComponent.expandIfPossible(); |
| 76 | popoverContentElement = customPreviewComponent.element; |
| 77 | } else { |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 78 | popoverContentElement = document.createElement('div'); |
| 79 | popoverContentElement.classList.add('object-popover-content'); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 80 | UI.Utils.appendStyle(popoverContentElement, 'object_ui/objectPopover.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | const titleElement = popoverContentElement.createChild('div', 'monospace object-popover-title'); |
| 82 | titleElement.createChild('span').textContent = description; |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 83 | linkifier = new Components.Linkifier.Linkifier(); |
Paul Lewis | d85dc38 | 2020-01-14 17:13:56 | [diff] [blame] | 84 | const section = new ObjectPropertiesSection( |
Erik Luo | 3971e8a | 2018-08-09 07:27:08 | [diff] [blame] | 85 | result, '', linkifier, undefined, undefined, undefined, true /* showOverflow */); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | section.element.classList.add('object-popover-tree'); |
| 87 | section.titleLessMode(); |
| 88 | popoverContentElement.appendChild(section.element); |
| 89 | } |
Philip Pfaffe | e687f61 | 2020-09-02 08:06:57 | [diff] [blame] | 90 | popoverContentElement.dataset.stableNameForTest = 'object-popover-content'; |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 91 | popover.setMaxContentSize(new UI.Geometry.Size(300, 250)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | popover.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactSize); |
| 93 | popover.contentElement.appendChild(popoverContentElement); |
Tim van der Lippe | 3574172 | 2019-11-21 15:36:20 | [diff] [blame] | 94 | return new ObjectPopoverHelper(linkifier, resultHighlightedAsDOM); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 95 | } |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 96 | |
Simon Zünd | 9f9ea0f | 2020-10-20 06:31:02 | [diff] [blame] | 97 | popoverContentElement = document.createElement('span'); |
Philip Pfaffe | e687f61 | 2020-09-02 08:06:57 | [diff] [blame] | 98 | popoverContentElement.dataset.stableNameForTest = 'object-popover-content'; |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 99 | UI.Utils.appendStyle(popoverContentElement, 'object_ui/objectValue.css', {enableLegacyPatching: true}); |
| 100 | UI.Utils.appendStyle(popoverContentElement, 'object_ui/objectPopover.css', {enableLegacyPatching: true}); |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 101 | const valueElement = popoverContentElement.createChild('span', 'monospace object-value-' + result.type); |
| 102 | valueElement.style.whiteSpace = 'pre'; |
| 103 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 104 | if (result.type === 'string') { |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 105 | UI.UIUtils.createTextChildren(valueElement, `"${description}"`); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 106 | } else if (result.type !== 'function') { |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 107 | valueElement.textContent = description; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 108 | } |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 109 | |
| 110 | if (result.type !== 'function') { |
| 111 | popover.contentElement.appendChild(popoverContentElement); |
Tim van der Lippe | 3574172 | 2019-11-21 15:36:20 | [diff] [blame] | 112 | return new ObjectPopoverHelper(null, false); |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 113 | } |
| 114 | |
Paul Lewis | d85dc38 | 2020-01-14 17:13:56 | [diff] [blame] | 115 | ObjectPropertiesSection.formatObjectAsFunction(result, valueElement, true); |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 116 | const response = await result.debuggerModel().functionDetailsPromise(result); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 117 | if (!response) { |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 118 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 119 | } |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 120 | |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 121 | const container = document.createElement('div'); |
| 122 | container.classList.add('object-popover-container'); |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 123 | const title = container.createChild('div', 'function-popover-title source-code'); |
| 124 | const functionName = title.createChild('span', 'function-name'); |
Tim van der Lippe | 64a94d8 | 2020-01-24 11:52:24 | [diff] [blame] | 125 | functionName.textContent = UI.UIUtils.beautifyFunctionName(response.functionName); |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 126 | |
| 127 | const rawLocation = response.location; |
| 128 | const linkContainer = title.createChild('div', 'function-title-link-container'); |
Simon Zünd | 9f9ea0f | 2020-10-20 06:31:02 | [diff] [blame] | 129 | const script = rawLocation && rawLocation.script(); |
| 130 | const sourceURL = script && script.sourceURL; |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 131 | let linkifier = null; |
| 132 | if (sourceURL) { |
Alex Rudenko | ca59b11 | 2020-05-27 09:29:14 | [diff] [blame] | 133 | linkifier = new Components.Linkifier.Linkifier(undefined, undefined, popover.positionContent.bind(popover)); |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 134 | linkContainer.appendChild( |
| 135 | linkifier.linkifyRawLocation(/** @type {!SDK.DebuggerModel.Location} */ (rawLocation), sourceURL)); |
Alexei Filippov | 02dfcd8 | 2018-06-26 19:23:09 | [diff] [blame] | 136 | } |
| 137 | container.appendChild(popoverContentElement); |
| 138 | popover.contentElement.appendChild(container); |
Tim van der Lippe | 3574172 | 2019-11-21 15:36:20 | [diff] [blame] | 139 | return new ObjectPopoverHelper(linkifier, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 140 | } |
Tim van der Lippe | 3574172 | 2019-11-21 15:36:20 | [diff] [blame] | 141 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 143 | const MaxPopoverTextLength = 10000; |