Web Inspector: Provide UIString descriptions to improve localizations
[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.domDebuggerManager.supported && isElement && !domNode.isPseudoElement() && attached) {
211         contextMenu.appendSeparator();
212
213         WI.appendContextMenuItemsForDOMNodeBreakpoints(contextMenu, domNode);
214     }
215
216     contextMenu.appendSeparator();
217
218     if (!options.excludeLogElement && !domNode.isInUserAgentShadowTree() && !domNode.isPseudoElement()) {
219         let label = isElement ? WI.UIString("Log Element", "Log (print) DOM element to Console") : WI.UIString("Log Node", "Log (print) DOM node to Console");
220         contextMenu.appendItem(label, () => {
221             WI.RemoteObject.resolveNode(domNode, WI.RuntimeManager.ConsoleObjectGroup).then((remoteObject) => {
222                 let text = isElement ? WI.UIString("Selected Element", "Selected DOM element") : WI.UIString("Selected Node", "Selected DOM node");
223                 const addSpecialUserLogClass = true;
224                 WI.consoleLogViewController.appendImmediateExecutionWithResult(text, remoteObject, addSpecialUserLogClass);
225             });
226         });
227     }
228
229     if (!options.excludeRevealElement && window.DOMAgent && attached) {
230         contextMenu.appendItem(WI.repeatedUIString.revealInDOMTree(), () => {
231             WI.domManager.inspectElement(domNode.id);
232         });
233     }
234
235     if (WI.settings.experimentalEnableLayersTab.value && window.LayerTreeAgent && attached) {
236         contextMenu.appendItem(WI.UIString("Reveal in Layers Tab", "Open Layers tab and select the layer corresponding to this node"), () => {
237             WI.showLayersTab({nodeToSelect: domNode});
238         });
239     }
240
241     if (window.PageAgent && attached) {
242         contextMenu.appendItem(WI.UIString("Capture Screenshot", "Capture screenshot of the selected DOM node"), () => {
243             PageAgent.snapshotNode(domNode.id, (error, dataURL) => {
244                 if (error) {
245                     const target = WI.mainTarget;
246                     const source = WI.ConsoleMessage.MessageSource.Other;
247                     const level = WI.ConsoleMessage.MessageLevel.Error;
248                     let consoleMessage = new WI.ConsoleMessage(target, source, level, error);
249                     consoleMessage.shouldRevealConsole = true;
250
251                     WI.consoleLogViewController.appendConsoleMessage(consoleMessage);
252                     return;
253                 }
254
255                 WI.FileUtilities.save({
256                     url: WI.FileUtilities.inspectorURLForFilename(WI.FileUtilities.screenshotString() + ".png"),
257                     content: parseDataURL(dataURL).data,
258                     base64Encoded: true,
259                 });
260             });
261         });
262     }
263
264     if (isElement && attached) {
265         contextMenu.appendItem(WI.UIString("Scroll into View", "Scroll selected DOM node into view on the inspected web page"), () => {
266             domNode.scrollIntoView();
267         });
268     }
269
270     contextMenu.appendSeparator();
271 };
272
273 WI.appendContextMenuItemsForDOMNodeBreakpoints = function(contextMenu, domNode, {allowEditing} = {})
274 {
275     if (contextMenu.__domBreakpointItemsAdded)
276         return;
277
278     contextMenu.__domBreakpointItemsAdded = true;
279
280     let subMenu = contextMenu.appendSubMenuItem(WI.UIString("Break on\u2026"));
281
282     let breakpoints = WI.domDebuggerManager.domBreakpointsForNode(domNode);
283     let keyValuePairs = breakpoints.map((breakpoint) => [breakpoint.type, breakpoint]);
284     let breakpointsByType = new Map(keyValuePairs);
285
286     for (let type of Object.values(WI.DOMBreakpoint.Type)) {
287         let label = WI.DOMBreakpointTreeElement.displayNameForType(type);
288         let breakpoint = breakpointsByType.get(type);
289
290         subMenu.appendCheckboxItem(label, function() {
291             if (breakpoint)
292                 WI.domDebuggerManager.removeDOMBreakpoint(breakpoint);
293             else
294                 WI.domDebuggerManager.addDOMBreakpoint(new WI.DOMBreakpoint(domNode, type));
295         }, !!breakpoint, false);
296     }
297
298     if (allowEditing) {
299         contextMenu.appendSeparator();
300
301         let shouldEnable = breakpoints.some((breakpoint) => breakpoint.disabled);
302         let label = shouldEnable ? WI.UIString("Enable Breakpoints") : WI.UIString("Disable Breakpoints");
303         contextMenu.appendItem(label, () => {
304             breakpoints.forEach((breakpoint) => breakpoint.disabled = !shouldEnable);
305         });
306
307         contextMenu.appendItem(WI.UIString("Delete Breakpoints"), function() {
308             WI.domDebuggerManager.removeDOMBreakpointsForNode(domNode);
309             WI.domManager.removeEventListenerBreakpointsForNode(domNode);
310         });
311     }
312 };