DOMBreakpointsSidebarPane.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. /**
  31. * @constructor
  32. * @extends {WebInspector.NativeBreakpointsSidebarPane}
  33. */
  34. WebInspector.DOMBreakpointsSidebarPane = function()
  35. {
  36. WebInspector.NativeBreakpointsSidebarPane.call(this, WebInspector.UIString("DOM Breakpoints"));
  37. this._breakpointElements = {};
  38. this._breakpointTypes = {
  39. SubtreeModified: "subtree-modified",
  40. AttributeModified: "attribute-modified",
  41. NodeRemoved: "node-removed"
  42. };
  43. this._breakpointTypeLabels = {};
  44. this._breakpointTypeLabels[this._breakpointTypes.SubtreeModified] = WebInspector.UIString("Subtree Modified");
  45. this._breakpointTypeLabels[this._breakpointTypes.AttributeModified] = WebInspector.UIString("Attribute Modified");
  46. this._breakpointTypeLabels[this._breakpointTypes.NodeRemoved] = WebInspector.UIString("Node Removed");
  47. this._contextMenuLabels = {};
  48. this._contextMenuLabels[this._breakpointTypes.SubtreeModified] = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Subtree modifications" : "Subtree Modifications");
  49. this._contextMenuLabels[this._breakpointTypes.AttributeModified] = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Attributes modifications" : "Attributes Modifications");
  50. this._contextMenuLabels[this._breakpointTypes.NodeRemoved] = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Node removal" : "Node Removal");
  51. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.InspectedURLChanged, this._inspectedURLChanged, this);
  52. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.NodeRemoved, this._nodeRemoved, this);
  53. }
  54. WebInspector.DOMBreakpointsSidebarPane.prototype = {
  55. _inspectedURLChanged: function(event)
  56. {
  57. this._breakpointElements = {};
  58. this._reset();
  59. var url = event.data;
  60. this._inspectedURL = url.removeURLFragment();
  61. },
  62. populateNodeContextMenu: function(node, contextMenu)
  63. {
  64. var nodeBreakpoints = {};
  65. for (var id in this._breakpointElements) {
  66. var element = this._breakpointElements[id];
  67. if (element._node === node)
  68. nodeBreakpoints[element._type] = true;
  69. }
  70. function toggleBreakpoint(type)
  71. {
  72. if (!nodeBreakpoints[type])
  73. this._setBreakpoint(node, type, true);
  74. else
  75. this._removeBreakpoint(node, type);
  76. this._saveBreakpoints();
  77. }
  78. var breakPointSubMenu = contextMenu.appendSubMenuItem(WebInspector.UIString("Break on..."));
  79. for (var key in this._breakpointTypes) {
  80. var type = this._breakpointTypes[key];
  81. var label = this._contextMenuLabels[type];
  82. breakPointSubMenu.appendCheckboxItem(label, toggleBreakpoint.bind(this, type), nodeBreakpoints[type]);
  83. }
  84. },
  85. createBreakpointHitStatusMessage: function(auxData, callback)
  86. {
  87. if (auxData.type === this._breakpointTypes.SubtreeModified) {
  88. var targetNodeObject = WebInspector.RemoteObject.fromPayload(auxData["targetNode"]);
  89. function didPushNodeToFrontend(targetNodeId)
  90. {
  91. if (targetNodeId)
  92. targetNodeObject.release();
  93. this._doCreateBreakpointHitStatusMessage(auxData, targetNodeId, callback);
  94. }
  95. targetNodeObject.pushNodeToFrontend(didPushNodeToFrontend.bind(this));
  96. } else
  97. this._doCreateBreakpointHitStatusMessage(auxData, null, callback);
  98. },
  99. _doCreateBreakpointHitStatusMessage: function (auxData, targetNodeId, callback)
  100. {
  101. var message;
  102. var typeLabel = this._breakpointTypeLabels[auxData.type];
  103. var linkifiedNode = WebInspector.DOMPresentationUtils.linkifyNodeById(auxData.nodeId);
  104. var substitutions = [typeLabel, linkifiedNode];
  105. var targetNode = "";
  106. if (targetNodeId)
  107. targetNode = WebInspector.DOMPresentationUtils.linkifyNodeById(targetNodeId);
  108. if (auxData.type === this._breakpointTypes.SubtreeModified) {
  109. if (auxData.insertion) {
  110. if (targetNodeId !== auxData.nodeId) {
  111. message = "Paused on a \"%s\" breakpoint set on %s, because a new child was added to its descendant %s.";
  112. substitutions.push(targetNode);
  113. } else
  114. message = "Paused on a \"%s\" breakpoint set on %s, because a new child was added to that node.";
  115. } else {
  116. message = "Paused on a \"%s\" breakpoint set on %s, because its descendant %s was removed.";
  117. substitutions.push(targetNode);
  118. }
  119. } else
  120. message = "Paused on a \"%s\" breakpoint set on %s.";
  121. var element = document.createElement("span");
  122. var formatters = {
  123. s: function(substitution)
  124. {
  125. return substitution;
  126. }
  127. };
  128. function append(a, b)
  129. {
  130. if (typeof b === "string")
  131. b = document.createTextNode(b);
  132. element.appendChild(b);
  133. }
  134. WebInspector.formatLocalized(message, substitutions, formatters, "", append);
  135. callback(element);
  136. },
  137. _nodeRemoved: function(event)
  138. {
  139. var node = event.data.node;
  140. this._removeBreakpointsForNode(event.data.node);
  141. if (!node.children)
  142. return;
  143. for (var i = 0; i < node.children.length; ++i)
  144. this._removeBreakpointsForNode(node.children[i]);
  145. this._saveBreakpoints();
  146. },
  147. _removeBreakpointsForNode: function(node)
  148. {
  149. for (var id in this._breakpointElements) {
  150. var element = this._breakpointElements[id];
  151. if (element._node === node)
  152. this._removeBreakpoint(element._node, element._type);
  153. }
  154. },
  155. _setBreakpoint: function(node, type, enabled)
  156. {
  157. var breakpointId = this._createBreakpointId(node.id, type);
  158. if (breakpointId in this._breakpointElements)
  159. return;
  160. var element = document.createElement("li");
  161. element._node = node;
  162. element._type = type;
  163. element.addEventListener("contextmenu", this._contextMenu.bind(this, node, type), true);
  164. var checkboxElement = document.createElement("input");
  165. checkboxElement.className = "checkbox-elem";
  166. checkboxElement.type = "checkbox";
  167. checkboxElement.checked = enabled;
  168. checkboxElement.addEventListener("click", this._checkboxClicked.bind(this, node, type), false);
  169. element._checkboxElement = checkboxElement;
  170. element.appendChild(checkboxElement);
  171. var labelElement = document.createElement("span");
  172. element.appendChild(labelElement);
  173. var linkifiedNode = WebInspector.DOMPresentationUtils.linkifyNodeById(node.id);
  174. linkifiedNode.addStyleClass("monospace");
  175. labelElement.appendChild(linkifiedNode);
  176. var description = document.createElement("div");
  177. description.className = "source-text";
  178. description.textContent = this._breakpointTypeLabels[type];
  179. labelElement.appendChild(description);
  180. var currentElement = this.listElement.firstChild;
  181. while (currentElement) {
  182. if (currentElement._type && currentElement._type < element._type)
  183. break;
  184. currentElement = currentElement.nextSibling;
  185. }
  186. this._addListElement(element, currentElement);
  187. this._breakpointElements[breakpointId] = element;
  188. if (enabled)
  189. DOMDebuggerAgent.setDOMBreakpoint(node.id, type);
  190. },
  191. _removeAllBreakpoints: function()
  192. {
  193. for (var id in this._breakpointElements) {
  194. var element = this._breakpointElements[id];
  195. this._removeBreakpoint(element._node, element._type);
  196. }
  197. this._saveBreakpoints();
  198. },
  199. _removeBreakpoint: function(node, type)
  200. {
  201. var breakpointId = this._createBreakpointId(node.id, type);
  202. var element = this._breakpointElements[breakpointId];
  203. if (!element)
  204. return;
  205. this._removeListElement(element);
  206. delete this._breakpointElements[breakpointId];
  207. if (element._checkboxElement.checked)
  208. DOMDebuggerAgent.removeDOMBreakpoint(node.id, type);
  209. },
  210. _contextMenu: function(node, type, event)
  211. {
  212. var contextMenu = new WebInspector.ContextMenu(event);
  213. function removeBreakpoint()
  214. {
  215. this._removeBreakpoint(node, type);
  216. this._saveBreakpoints();
  217. }
  218. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Remove breakpoint" : "Remove Breakpoint"), removeBreakpoint.bind(this));
  219. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Remove all DOM breakpoints" : "Remove All DOM Breakpoints"), this._removeAllBreakpoints.bind(this));
  220. contextMenu.show();
  221. },
  222. _checkboxClicked: function(node, type, event)
  223. {
  224. if (event.target.checked)
  225. DOMDebuggerAgent.setDOMBreakpoint(node.id, type);
  226. else
  227. DOMDebuggerAgent.removeDOMBreakpoint(node.id, type);
  228. this._saveBreakpoints();
  229. },
  230. highlightBreakpoint: function(auxData)
  231. {
  232. var breakpointId = this._createBreakpointId(auxData.nodeId, auxData.type);
  233. var element = this._breakpointElements[breakpointId];
  234. if (!element)
  235. return;
  236. this.expand();
  237. element.addStyleClass("breakpoint-hit");
  238. this._highlightedElement = element;
  239. },
  240. clearBreakpointHighlight: function()
  241. {
  242. if (this._highlightedElement) {
  243. this._highlightedElement.removeStyleClass("breakpoint-hit");
  244. delete this._highlightedElement;
  245. }
  246. },
  247. _createBreakpointId: function(nodeId, type)
  248. {
  249. return nodeId + ":" + type;
  250. },
  251. _saveBreakpoints: function()
  252. {
  253. var breakpoints = [];
  254. var storedBreakpoints = WebInspector.settings.domBreakpoints.get();
  255. for (var i = 0; i < storedBreakpoints.length; ++i) {
  256. var breakpoint = storedBreakpoints[i];
  257. if (breakpoint.url !== this._inspectedURL)
  258. breakpoints.push(breakpoint);
  259. }
  260. for (var id in this._breakpointElements) {
  261. var element = this._breakpointElements[id];
  262. breakpoints.push({ url: this._inspectedURL, path: element._node.path(), type: element._type, enabled: element._checkboxElement.checked });
  263. }
  264. WebInspector.settings.domBreakpoints.set(breakpoints);
  265. },
  266. restoreBreakpoints: function()
  267. {
  268. var pathToBreakpoints = {};
  269. function didPushNodeByPathToFrontend(path, nodeId)
  270. {
  271. var node = WebInspector.domAgent.nodeForId(nodeId);
  272. if (!node)
  273. return;
  274. var breakpoints = pathToBreakpoints[path];
  275. for (var i = 0; i < breakpoints.length; ++i)
  276. this._setBreakpoint(node, breakpoints[i].type, breakpoints[i].enabled);
  277. }
  278. var breakpoints = WebInspector.settings.domBreakpoints.get();
  279. for (var i = 0; i < breakpoints.length; ++i) {
  280. var breakpoint = breakpoints[i];
  281. if (breakpoint.url !== this._inspectedURL)
  282. continue;
  283. var path = breakpoint.path;
  284. if (!pathToBreakpoints[path]) {
  285. pathToBreakpoints[path] = [];
  286. WebInspector.domAgent.pushNodeByPathToFrontend(path, didPushNodeByPathToFrontend.bind(this, path));
  287. }
  288. pathToBreakpoints[path].push(breakpoint);
  289. }
  290. },
  291. /**
  292. * @param {WebInspector.Panel} panel
  293. */
  294. createProxy: function(panel)
  295. {
  296. var proxy = new WebInspector.DOMBreakpointsSidebarPane.Proxy(this, panel);
  297. if (!this._proxies)
  298. this._proxies = [];
  299. this._proxies.push(proxy);
  300. return proxy;
  301. },
  302. onContentReady: function()
  303. {
  304. for (var i = 0; i != this._proxies.length; i++)
  305. this._proxies[i].onContentReady();
  306. },
  307. __proto__: WebInspector.NativeBreakpointsSidebarPane.prototype
  308. }
  309. /**
  310. * @constructor
  311. * @extends {WebInspector.SidebarPane}
  312. * @param {WebInspector.DOMBreakpointsSidebarPane} pane
  313. * @param {WebInspector.Panel} panel
  314. */
  315. WebInspector.DOMBreakpointsSidebarPane.Proxy = function(pane, panel)
  316. {
  317. WebInspector.View._assert(!pane.titleElement.firstChild, "Cannot create proxy for a sidebar pane with a toolbar");
  318. WebInspector.SidebarPane.call(this, pane.title());
  319. this.registerRequiredCSS("breakpointsList.css");
  320. this._wrappedPane = pane;
  321. this._panel = panel;
  322. this.bodyElement.removeSelf();
  323. this.bodyElement = this._wrappedPane.bodyElement;
  324. }
  325. WebInspector.DOMBreakpointsSidebarPane.Proxy.prototype = {
  326. expand: function()
  327. {
  328. this._wrappedPane.expand();
  329. },
  330. onContentReady: function()
  331. {
  332. if (!this._panel.isShowing())
  333. return;
  334. this._reattachBody();
  335. WebInspector.SidebarPane.prototype.onContentReady.call(this);
  336. },
  337. wasShown: function()
  338. {
  339. WebInspector.SidebarPane.prototype.wasShown.call(this);
  340. this._reattachBody();
  341. },
  342. _reattachBody: function()
  343. {
  344. if (this.bodyElement.parentNode !== this.element)
  345. this.element.appendChild(this.bodyElement);
  346. },
  347. __proto__: WebInspector.SidebarPane.prototype
  348. }
  349. /**
  350. * @type {?WebInspector.DOMBreakpointsSidebarPane}
  351. */
  352. WebInspector.domBreakpointsSidebarPane = null;