Web Inspector: Styles: missing contextmenu items for links
[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                 sourceCode.generateCURLCommand();
51             });
52         }
53     }
54
55     contextMenu.appendItem(WI.UIString("Save File"), () => {
56         sourceCode.requestContent().then(() => {
57             const forceSaveAs = true;
58             WI.saveDataToFile({
59                 url: sourceCode.url || "",
60                 content: sourceCode.content
61             }, forceSaveAs);
62         });
63     });
64
65     contextMenu.appendSeparator();
66
67     if (location && (sourceCode instanceof WI.Script || (sourceCode instanceof WI.Resource && sourceCode.type === WI.Resource.Type.Script))) {
68         let existingBreakpoint = WI.debuggerManager.breakpointForSourceCodeLocation(location);
69         if (existingBreakpoint) {
70             contextMenu.appendItem(WI.UIString("Delete Breakpoint"), () => {
71                 WI.debuggerManager.removeBreakpoint(existingBreakpoint);
72             });
73         } else {
74             contextMenu.appendItem(WI.UIString("Add Breakpoint"), () => {
75                 WI.debuggerManager.addBreakpoint(new WI.Breakpoint(location));
76             });
77         }
78
79         contextMenu.appendSeparator();
80     }
81 };
82
83 WI.appendContextMenuItemsForURL = function(contextMenu, url, options = {})
84 {
85     if (!url)
86         return;
87
88     function showResourceWithOptions(options) {
89         if (options.location)
90             WI.showSourceCodeLocation(options.location, options);
91         else if (options.sourceCode)
92             WI.showSourceCode(options.sourceCode, options);
93         else
94             WI.openURL(url, options.frame, options);
95     }
96
97     if (!url.startsWith("javascript:") && !url.startsWith("data:")) {
98         contextMenu.appendItem(WI.UIString("Open in New Tab"), () => {
99             const frame = null;
100             WI.openURL(url, frame, {alwaysOpenExternally: true});
101         });
102     }
103
104     if (WI.networkManager.resourceForURL(url)) {
105         if (!WI.isShowingResourcesTab()) {
106             contextMenu.appendItem(WI.UIString("Reveal in Resources Tab"), () => {
107                 showResourceWithOptions({ignoreNetworkTab: true, ignoreSearchTab: true});
108             });
109         }
110         if (!WI.isShowingNetworkTab()) {
111             contextMenu.appendItem(WI.UIString("Reveal in Network Tab"), () => {
112                 showResourceWithOptions({ignoreResourcesTab: true, ignoreDebuggerTab: true, ignoreSearchTab: true});
113             });
114         }
115     }
116
117     contextMenu.appendSeparator();
118
119     contextMenu.appendItem(WI.UIString("Copy Link"), () => {
120         InspectorFrontendHost.copyText(url);
121     });
122 };
123
124 WI.appendContextMenuItemsForDOMNode = function(contextMenu, domNode, options = {})
125 {
126     console.assert(contextMenu instanceof WI.ContextMenu);
127     if (!(contextMenu instanceof WI.ContextMenu))
128         return;
129
130     console.assert(domNode instanceof WI.DOMNode);
131     if (!(domNode instanceof WI.DOMNode))
132         return;
133
134     let copySubMenu = options.copySubMenu || contextMenu.appendSubMenuItem(WI.UIString("Copy"));
135
136     let isElement = domNode.nodeType() === Node.ELEMENT_NODE;
137     if (domNode.ownerDocument && isElement) {
138         copySubMenu.appendItem(WI.UIString("Selector Path"), () => {
139             let cssPath = WI.cssPath(domNode);
140             InspectorFrontendHost.copyText(cssPath);
141         });
142     }
143
144     if (domNode.ownerDocument && !domNode.isPseudoElement()) {
145         copySubMenu.appendItem(WI.UIString("XPath"), () => {
146             let xpath = WI.xpath(domNode);
147             InspectorFrontendHost.copyText(xpath);
148         });
149     }
150
151     contextMenu.appendSeparator();
152
153     if (domNode.isCustomElement()) {
154         contextMenu.appendItem(WI.UIString("Jump to Definition"), () => {
155             function didGetFunctionDetails(error, response) {
156                 if (error)
157                     return;
158
159                 let location = response.location;
160                 let sourceCode = WI.debuggerManager.scriptForIdentifier(location.scriptId, WI.mainTarget);
161                 if (!sourceCode)
162                     return;
163
164                 let sourceCodeLocation = sourceCode.createSourceCodeLocation(location.lineNumber, location.columnNumber || 0);
165                 WI.showSourceCodeLocation(sourceCodeLocation, {
166                     ignoreNetworkTab: true,
167                     ignoreSearchTab: true,
168                 });
169             }
170
171             function didGetProperty(error, result, wasThrown) {
172                 if (error || result.type !== "function")
173                     return;
174
175                 DebuggerAgent.getFunctionDetails(result.objectId, didGetFunctionDetails);
176                 result.release();
177             }
178
179             WI.RemoteObject.resolveNode(domNode).then((remoteObject) => {
180                 remoteObject.getProperty("constructor", didGetProperty);
181                 remoteObject.release();
182             });
183         });
184
185         contextMenu.appendSeparator();
186     }
187
188     if (WI.domDebuggerManager.supported && isElement && !domNode.isPseudoElement() && domNode.ownerDocument) {
189         contextMenu.appendSeparator();
190
191         WI.appendContextMenuItemsForDOMNodeBreakpoints(contextMenu, domNode);
192     }
193
194     contextMenu.appendSeparator();
195
196     if (!options.excludeLogElement && !domNode.isInUserAgentShadowTree() && !domNode.isPseudoElement()) {
197         let label = isElement ? WI.UIString("Log Element") : WI.UIString("Log Node");
198         contextMenu.appendItem(label, () => {
199             WI.RemoteObject.resolveNode(domNode, WI.RuntimeManager.ConsoleObjectGroup).then((remoteObject) => {
200                 let text = isElement ? WI.UIString("Selected Element") : WI.UIString("Selected Node");
201                 const addSpecialUserLogClass = true;
202                 WI.consoleLogViewController.appendImmediateExecutionWithResult(text, remoteObject, addSpecialUserLogClass);
203             });
204         });
205     }
206
207     if (!options.excludeRevealElement && window.DOMAgent && domNode.ownerDocument) {
208         contextMenu.appendItem(WI.UIString("Reveal in DOM Tree"), () => {
209             WI.domManager.inspectElement(domNode.id);
210         });
211     }
212
213     if (!options.excludeRevealLayer && window.LayerTreeAgent && domNode.parentNode) {
214         contextMenu.appendItem(WI.UIString("Reveal in Layers Tab"), () => {
215             WI.showLayersTab({nodeToSelect: domNode});
216         });
217     }
218
219     if (window.PageAgent) {
220         contextMenu.appendItem(WI.UIString("Capture Screenshot"), () => {
221             PageAgent.snapshotNode(domNode.id, (error, dataURL) => {
222                 if (error) {
223                     const target = WI.mainTarget;
224                     const source = WI.ConsoleMessage.MessageSource.Other;
225                     const level = WI.ConsoleMessage.MessageLevel.Error;
226                     let consoleMessage = new WI.ConsoleMessage(target, source, level, error);
227                     consoleMessage.shouldRevealConsole = true;
228
229                     WI.consoleLogViewController.appendConsoleMessage(consoleMessage);
230                     return;
231                 }
232
233                 let date = new Date;
234                 let values = [
235                     date.getFullYear(),
236                     Number.zeroPad(date.getMonth() + 1, 2),
237                     Number.zeroPad(date.getDate(), 2),
238                     Number.zeroPad(date.getHours(), 2),
239                     Number.zeroPad(date.getMinutes(), 2),
240                     Number.zeroPad(date.getSeconds(), 2),
241                 ];
242                 let filename = WI.UIString("Screen Shot %s-%s-%s at %s.%s.%s").format(...values);
243                 WI.saveDataToFile({
244                     url: encodeURI(`web-inspector:///${filename}.png`),
245                     content: parseDataURL(dataURL).data,
246                     base64Encoded: true,
247                 });
248             });
249         });
250     }
251
252     if (isElement) {
253         contextMenu.appendItem(WI.UIString("Scroll Into View"), () => {
254             domNode.scrollIntoView();
255         });
256     }
257
258     contextMenu.appendSeparator();
259 };
260
261 WI.appendContextMenuItemsForDOMNodeBreakpoints = function(contextMenu, domNode, {allowEditing} = {})
262 {
263     if (contextMenu.__domBreakpointItemsAdded)
264         return;
265
266     contextMenu.__domBreakpointItemsAdded = true;
267
268     let subMenu = contextMenu.appendSubMenuItem(WI.UIString("Break on…"));
269
270     let breakpoints = WI.domDebuggerManager.domBreakpointsForNode(domNode);
271     let keyValuePairs = breakpoints.map((breakpoint) => [breakpoint.type, breakpoint]);
272     let breakpointsByType = new Map(keyValuePairs);
273
274     for (let type of Object.values(WI.DOMBreakpoint.Type)) {
275         let label = WI.DOMBreakpointTreeElement.displayNameForType(type);
276         let breakpoint = breakpointsByType.get(type);
277
278         subMenu.appendCheckboxItem(label, function() {
279             if (breakpoint)
280                 WI.domDebuggerManager.removeDOMBreakpoint(breakpoint);
281             else
282                 WI.domDebuggerManager.addDOMBreakpoint(new WI.DOMBreakpoint(domNode, type));
283         }, !!breakpoint, false);
284     }
285
286     if (allowEditing) {
287         contextMenu.appendSeparator();
288
289         let shouldEnable = breakpoints.some((breakpoint) => breakpoint.disabled);
290         let label = shouldEnable ? WI.UIString("Enable Breakpoints") : WI.UIString("Disable Breakpoints");
291         contextMenu.appendItem(label, () => {
292             breakpoints.forEach((breakpoint) => breakpoint.disabled = !shouldEnable);
293         });
294
295         contextMenu.appendItem(WI.UIString("Delete Breakpoints"), function() {
296             WI.domDebuggerManager.removeDOMBreakpointsForNode(domNode);
297         });
298     }
299 };