Web Inspector: DOM Debugger: descendant breakpoints should be able to be enabled...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ContextMenuUtilities.js
1 /*
2  * Copyright (C) 2016 Devin Rousso <webkit@devinrousso.com>. 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.appendContextMenuItemsForSourceCode = function(contextMenu, sourceCodeOrLocation)
27 {
28     console.assert(contextMenu instanceof WI.ContextMenu);
29     if (!(contextMenu instanceof WI.ContextMenu))
30         return;
31
32     let sourceCode = sourceCodeOrLocation;
33     let location = null;
34     if (sourceCodeOrLocation instanceof WI.SourceCodeLocation) {
35         sourceCode = sourceCodeOrLocation.sourceCode;
36         location = sourceCodeOrLocation;
37     }
38
39     console.assert(sourceCode instanceof WI.SourceCode);
40     if (!(sourceCode instanceof WI.SourceCode))
41         return;
42
43     contextMenu.appendSeparator();
44
45     WI.appendContextMenuItemsForURL(contextMenu, sourceCode.url, {sourceCode, location});
46
47     if (sourceCode instanceof WI.Resource) {
48         if (sourceCode.urlComponents.scheme !== "data") {
49             contextMenu.appendItem(WI.UIString("Copy as cURL"), () => {
50                 InspectorFrontendHost.copyText(sourceCode.generateCURLCommand());
51             });
52
53             contextMenu.appendSeparator();
54
55             contextMenu.appendItem(WI.UIString("Copy HTTP Request"), () => {
56                 InspectorFrontendHost.copyText(sourceCode.stringifyHTTPRequest());
57             });
58
59             if (sourceCode.hasResponse()) {
60                 contextMenu.appendItem(WI.UIString("Copy HTTP Response"), () => {
61                     InspectorFrontendHost.copyText(sourceCode.stringifyHTTPResponse());
62                 });
63             }
64
65             contextMenu.appendSeparator();
66         }
67     }
68
69     contextMenu.appendItem(WI.UIString("Save File"), () => {
70         sourceCode.requestContent().then(() => {
71             const forceSaveAs = true;
72             WI.FileUtilities.save({
73                 url: sourceCode.url || "",
74                 content: sourceCode.content
75             }, forceSaveAs);
76         });
77     });
78
79     contextMenu.appendSeparator();
80
81     if (location && (sourceCode instanceof WI.Script || (sourceCode instanceof WI.Resource && sourceCode.type === WI.Resource.Type.Script))) {
82         let existingBreakpoint = WI.debuggerManager.breakpointForSourceCodeLocation(location);
83         if (existingBreakpoint) {
84             contextMenu.appendItem(WI.UIString("Delete Breakpoint"), () => {
85                 WI.debuggerManager.removeBreakpoint(existingBreakpoint);
86             });
87         } else {
88             contextMenu.appendItem(WI.UIString("Add Breakpoint"), () => {
89                 WI.debuggerManager.addBreakpoint(new WI.Breakpoint(location));
90             });
91         }
92
93         contextMenu.appendSeparator();
94     }
95 };
96
97 WI.appendContextMenuItemsForURL = function(contextMenu, url, options = {})
98 {
99     if (!url)
100         return;
101
102     function showResourceWithOptions(options) {
103         if (options.location)
104             WI.showSourceCodeLocation(options.location, options);
105         else if (options.sourceCode)
106             WI.showSourceCode(options.sourceCode, options);
107         else
108             WI.openURL(url, options.frame, options);
109     }
110
111     if (!url.startsWith("javascript:") && !url.startsWith("data:")) {
112         contextMenu.appendItem(WI.UIString("Open in New Tab"), () => {
113             const frame = null;
114             WI.openURL(url, frame, {alwaysOpenExternally: true});
115         });
116     }
117
118     if (WI.networkManager.resourceForURL(url)) {
119         if (WI.settings.experimentalEnableSourcesTab.value) {
120             if (!WI.isShowingSourcesTab()) {
121                 contextMenu.appendItem(WI.UIString("Reveal in Sources Tab"), () => {
122                     showResourceWithOptions({preferredTabType: WI.SourcesTabContentView.Type});
123                 });
124             }
125         } else {
126             if (!WI.isShowingResourcesTab()) {
127                 contextMenu.appendItem(WI.UIString("Reveal in Resources Tab"), () => {
128                     showResourceWithOptions({preferredTabType: WI.ResourcesTabContentView.Type});
129                 });
130             }
131         }
132         if (!WI.isShowingNetworkTab()) {
133             contextMenu.appendItem(WI.UIString("Reveal in Network Tab"), () => {
134                 showResourceWithOptions({preferredTabType: WI.NetworkTabContentView.Type});
135             });
136         }
137     }
138
139     contextMenu.appendSeparator();
140
141     contextMenu.appendItem(WI.UIString("Copy Link"), () => {
142         InspectorFrontendHost.copyText(url);
143     });
144 };
145
146 WI.appendContextMenuItemsForDOMNode = function(contextMenu, domNode, options = {})
147 {
148     console.assert(contextMenu instanceof WI.ContextMenu);
149     if (!(contextMenu instanceof WI.ContextMenu))
150         return;
151
152     console.assert(domNode instanceof WI.DOMNode);
153     if (!(domNode instanceof WI.DOMNode))
154         return;
155
156     let copySubMenu = options.copySubMenu || contextMenu.appendSubMenuItem(WI.UIString("Copy"));
157
158     let isElement = domNode.nodeType() === Node.ELEMENT_NODE;
159     let attached = domNode.attached;
160
161     if (isElement && attached) {
162         copySubMenu.appendItem(WI.UIString("Selector Path"), () => {
163             let cssPath = WI.cssPath(domNode);
164             InspectorFrontendHost.copyText(cssPath);
165         });
166     }
167
168     if (!domNode.isPseudoElement() && attached) {
169         copySubMenu.appendItem(WI.UIString("XPath"), () => {
170             let xpath = WI.xpath(domNode);
171             InspectorFrontendHost.copyText(xpath);
172         });
173     }
174
175     contextMenu.appendSeparator();
176
177     if (domNode.isCustomElement()) {
178         contextMenu.appendItem(WI.UIString("Jump to Definition"), () => {
179             function didGetFunctionDetails(error, response) {
180                 if (error)
181                     return;
182
183                 let location = response.location;
184                 let sourceCode = WI.debuggerManager.scriptForIdentifier(location.scriptId, WI.mainTarget);
185                 if (!sourceCode)
186                     return;
187
188                 let sourceCodeLocation = sourceCode.createSourceCodeLocation(location.lineNumber, location.columnNumber || 0);
189                 WI.showSourceCodeLocation(sourceCodeLocation, {
190                     ignoreNetworkTab: true,
191                     ignoreSearchTab: true,
192                 });
193             }
194
195             WI.RemoteObject.resolveNode(domNode).then((remoteObject) => {
196                 remoteObject.getProperty("constructor", (error, result, wasThrown) => {
197                     if (error)
198                         return;
199                     if (result.type === "function")
200                         remoteObject.target.DebuggerAgent.getFunctionDetails(result.objectId, didGetFunctionDetails);
201                     result.release();
202                 });
203                 remoteObject.release();
204             });
205         });
206
207         contextMenu.appendSeparator();
208     }
209
210     if (WI.cssManager.canForcePseudoClasses() && domNode.attached) {
211         contextMenu.appendSeparator();
212
213         let pseudoSubMenu = contextMenu.appendSubMenuItem(WI.UIString("Forced Pseudo-Classes", "A context menu item to force (override) a DOM node's pseudo-classes"));
214
215         let enabledPseudoClasses = domNode.enabledPseudoClasses;
216         WI.CSSManager.ForceablePseudoClasses.forEach((pseudoClass) => {
217             let enabled = enabledPseudoClasses.includes(pseudoClass);
218             pseudoSubMenu.appendCheckboxItem(pseudoClass.capitalize(), () => {
219                 domNode.setPseudoClassEnabled(pseudoClass, !enabled);
220             }, enabled);
221         });
222     }
223
224     if (WI.domDebuggerManager.supported && isElement && !domNode.isPseudoElement() && attached) {
225         contextMenu.appendSeparator();
226
227         WI.appendContextMenuItemsForDOMNodeBreakpoints(contextMenu, domNode, options);
228     }
229
230     contextMenu.appendSeparator();
231
232     if (!options.excludeLogElement && !domNode.isInUserAgentShadowTree() && !domNode.isPseudoElement()) {
233         let label = isElement ? WI.UIString("Log Element", "Log (print) DOM element to Console") : WI.UIString("Log Node", "Log (print) DOM node to Console");
234         contextMenu.appendItem(label, () => {
235             WI.RemoteObject.resolveNode(domNode, WI.RuntimeManager.ConsoleObjectGroup).then((remoteObject) => {
236                 let text = isElement ? WI.UIString("Selected Element", "Selected DOM element") : WI.UIString("Selected Node", "Selected DOM node");
237                 const addSpecialUserLogClass = true;
238                 WI.consoleLogViewController.appendImmediateExecutionWithResult(text, remoteObject, addSpecialUserLogClass);
239             });
240         });
241     }
242
243     if (!options.excludeRevealElement && window.DOMAgent && attached) {
244         contextMenu.appendItem(WI.repeatedUIString.revealInDOMTree(), () => {
245             WI.domManager.inspectElement(domNode.id);
246         });
247     }
248
249     if (WI.settings.experimentalEnableLayersTab.value && window.LayerTreeAgent && attached) {
250         contextMenu.appendItem(WI.UIString("Reveal in Layers Tab", "Open Layers tab and select the layer corresponding to this node"), () => {
251             WI.showLayersTab({nodeToSelect: domNode});
252         });
253     }
254
255     if (window.PageAgent && attached) {
256         contextMenu.appendItem(WI.UIString("Capture Screenshot", "Capture screenshot of the selected DOM node"), () => {
257             PageAgent.snapshotNode(domNode.id, (error, dataURL) => {
258                 if (error) {
259                     const target = WI.mainTarget;
260                     const source = WI.ConsoleMessage.MessageSource.Other;
261                     const level = WI.ConsoleMessage.MessageLevel.Error;
262                     let consoleMessage = new WI.ConsoleMessage(target, source, level, error);
263                     consoleMessage.shouldRevealConsole = true;
264
265                     WI.consoleLogViewController.appendConsoleMessage(consoleMessage);
266                     return;
267                 }
268
269                 WI.FileUtilities.save({
270                     url: WI.FileUtilities.inspectorURLForFilename(WI.FileUtilities.screenshotString() + ".png"),
271                     content: parseDataURL(dataURL).data,
272                     base64Encoded: true,
273                 });
274             });
275         });
276     }
277
278     if (isElement && attached) {
279         contextMenu.appendItem(WI.UIString("Scroll into View", "Scroll selected DOM node into view on the inspected web page"), () => {
280             domNode.scrollIntoView();
281         });
282     }
283
284     contextMenu.appendSeparator();
285 };
286
287 WI.appendContextMenuItemsForDOMNodeBreakpoints = function(contextMenu, domNode, options = {})
288 {
289     if (contextMenu.__domBreakpointItemsAdded)
290         return;
291
292     contextMenu.__domBreakpointItemsAdded = true;
293
294     let breakpoints = WI.domDebuggerManager.domBreakpointsForNode(domNode);
295
296     contextMenu.appendSeparator();
297
298     let subMenu = contextMenu.appendSubMenuItem(WI.UIString("Break on"));
299
300     for (let type of Object.values(WI.DOMBreakpoint.Type)) {
301         let label = WI.DOMBreakpointTreeElement.displayNameForType(type);
302         let breakpoint = breakpoints.find((breakpoint) => breakpoint.type === type);
303
304         subMenu.appendCheckboxItem(label, function() {
305             if (breakpoint)
306                 WI.domDebuggerManager.removeDOMBreakpoint(breakpoint);
307             else
308                 WI.domDebuggerManager.addDOMBreakpoint(new WI.DOMBreakpoint(domNode, type));
309         }, !!breakpoint);
310     }
311
312     contextMenu.appendSeparator();
313
314     if (breakpoints.length) {
315         let shouldEnable = breakpoints.some((breakpoint) => breakpoint.disabled);
316         contextMenu.appendItem(shouldEnable ? WI.UIString("Enable Breakpoint") : WI.UIString("Disable Breakpoint"), () => {
317             for (let breakpoint of breakpoints)
318                 breakpoint.disabled = !shouldEnable;
319         });
320
321         contextMenu.appendItem(WI.UIString("Delete Breakpoint"), () => {
322             for (let breakpoint of breakpoints)
323                 WI.domDebuggerManager.removeDOMBreakpoint(breakpoint);
324         });
325
326         contextMenu.appendSeparator();
327     }
328
329     let subtreeBreakpoints = WI.domDebuggerManager.domBreakpointsInSubtree(domNode);
330     if (subtreeBreakpoints.length) {
331         if (options.revealDescendantBreakpointsMenuItemHandler)
332             contextMenu.appendItem(WI.UIString("Reveal Descendant Breakpoints"), options.revealDescendantBreakpointsMenuItemHandler);
333
334         let subtreeShouldEnable = subtreeBreakpoints.some((breakpoint) => breakpoint.disabled);
335         contextMenu.appendItem(subtreeShouldEnable ? WI.UIString("Enable Descendant Breakpoints") : WI.UIString("Disable Descendant Breakpoints"), () => {
336             for (let subtreeBreakpoint of subtreeBreakpoints)
337                 subtreeBreakpoint.disabled = !subtreeShouldEnable;
338         });
339
340         contextMenu.appendItem(WI.UIString("Delete Descendant Breakpoints"), () => {
341             for (let subtreeBreakpoint of subtreeBreakpoints)
342                 WI.domDebuggerManager.removeDOMBreakpoint(subtreeBreakpoint);
343         });
344
345         contextMenu.appendSeparator();
346     }
347 };