2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @extends {WebInspector.NativeBreakpointsSidebarPane}
35 WebInspector.DOMBreakpointsSidebarPane = function()
37 WebInspector.NativeBreakpointsSidebarPane.call(this, WebInspector.UIString("DOM Breakpoints"));
39 this._breakpointElements = {};
41 this._breakpointTypes = {
42 SubtreeModified: "subtree-modified",
43 AttributeModified: "attribute-modified",
44 NodeRemoved: "node-removed"
46 this._breakpointTypeLabels = {};
47 this._breakpointTypeLabels[this._breakpointTypes.SubtreeModified] = WebInspector.UIString("Subtree Modified");
48 this._breakpointTypeLabels[this._breakpointTypes.AttributeModified] = WebInspector.UIString("Attribute Modified");
49 this._breakpointTypeLabels[this._breakpointTypes.NodeRemoved] = WebInspector.UIString("Node Removed");
51 this._contextMenuLabels = {};
52 this._contextMenuLabels[this._breakpointTypes.SubtreeModified] = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Subtree modifications" : "Subtree Modifications");
53 this._contextMenuLabels[this._breakpointTypes.AttributeModified] = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Attributes modifications" : "Attributes Modifications");
54 this._contextMenuLabels[this._breakpointTypes.NodeRemoved] = WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Node removal" : "Node Removal");
56 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Events.InspectedURLChanged, this._inspectedURLChanged, this);
57 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspector.DOMModel.Events.NodeRemoved, this._nodeRemoved, this);
60 WebInspector.DOMBreakpointsSidebarPane.prototype = {
61 _inspectedURLChanged: function(event)
63 this._breakpointElements = {};
65 var url = /** @type {string} */ (event.data);
66 this._inspectedURL = url.removeURLFragment();
70 * @param {!WebInspector.DOMNode} node
71 * @param {!WebInspector.ContextMenu} contextMenu
73 populateNodeContextMenu: function(node, contextMenu)
75 if (node.pseudoType())
78 var nodeBreakpoints = {};
79 for (var id in this._breakpointElements) {
80 var element = this._breakpointElements[id];
81 if (element._node === node && element._checkboxElement.checked)
82 nodeBreakpoints[element._type] = true;
86 * @param {string} type
87 * @this {WebInspector.DOMBreakpointsSidebarPane}
89 function toggleBreakpoint(type)
91 if (!nodeBreakpoints[type])
92 this._setBreakpoint(node, type, true);
94 this._removeBreakpoint(node, type);
95 this._saveBreakpoints();
98 var breakPointSubMenu = contextMenu.appendSubMenuItem(WebInspector.UIString("Break on..."));
99 for (var key in this._breakpointTypes) {
100 var type = this._breakpointTypes[key];
101 var label = this._contextMenuLabels[type];
102 breakPointSubMenu.appendCheckboxItem(label, toggleBreakpoint.bind(this, type), nodeBreakpoints[type]);
107 * @param {!WebInspector.DebuggerPausedDetails} details
108 * @param {function(!Element)} callback
110 createBreakpointHitStatusMessage: function(details, callback)
112 var auxData = /** @type {!Object} */ (details.auxData);
113 var domModel = details.target().domModel;
114 if (auxData.type === this._breakpointTypes.SubtreeModified) {
115 var targetNodeObject = details.target().runtimeModel.createRemoteObject(auxData["targetNode"]);
116 targetNodeObject.pushNodeToFrontend(didPushNodeToFrontend.bind(this));
118 this._doCreateBreakpointHitStatusMessage(auxData, domModel.nodeForId(auxData.nodeId), null, callback);
122 * @param {?WebInspector.DOMNode} targetNode
123 * @this {WebInspector.DOMBreakpointsSidebarPane}
125 function didPushNodeToFrontend(targetNode)
128 targetNodeObject.release();
129 this._doCreateBreakpointHitStatusMessage(auxData, domModel.nodeForId(auxData.nodeId), targetNode, callback);
134 * @param {!Object} auxData
135 * @param {?WebInspector.DOMNode} node
136 * @param {?WebInspector.DOMNode} targetNode
137 * @param {function(!Element)} callback
139 _doCreateBreakpointHitStatusMessage: function(auxData, node, targetNode, callback)
142 var typeLabel = this._breakpointTypeLabels[auxData.type];
143 var linkifiedNode = WebInspector.DOMPresentationUtils.linkifyNodeReference(node);
144 var substitutions = [typeLabel, linkifiedNode];
145 var targetNodeLink = "";
147 targetNodeLink = WebInspector.DOMPresentationUtils.linkifyNodeReference(targetNode);
149 if (auxData.type === this._breakpointTypes.SubtreeModified) {
150 if (auxData.insertion) {
151 if (targetNode !== node) {
152 message = "Paused on a \"%s\" breakpoint set on %s, because a new child was added to its descendant %s.";
153 substitutions.push(targetNodeLink);
155 message = "Paused on a \"%s\" breakpoint set on %s, because a new child was added to that node.";
157 message = "Paused on a \"%s\" breakpoint set on %s, because its descendant %s was removed.";
158 substitutions.push(targetNodeLink);
161 message = "Paused on a \"%s\" breakpoint set on %s.";
163 var element = createElement("span");
165 s: function(substitution)
170 function append(a, b)
172 if (typeof b === "string")
173 b = createTextNode(b);
174 element.appendChild(b);
176 WebInspector.formatLocalized(message, substitutions, formatters, "", append);
181 _nodeRemoved: function(event)
183 var node = event.data.node;
184 this._removeBreakpointsForNode(event.data.node);
185 var children = node.children();
188 for (var i = 0; i < children.length; ++i)
189 this._removeBreakpointsForNode(children[i]);
190 this._saveBreakpoints();
194 * @param {!WebInspector.DOMNode} node
196 _removeBreakpointsForNode: function(node)
198 for (var id in this._breakpointElements) {
199 var element = this._breakpointElements[id];
200 if (element._node === node)
201 this._removeBreakpoint(element._node, element._type);
206 * @param {!WebInspector.DOMNode} node
207 * @param {string} type
208 * @param {boolean} enabled
210 _setBreakpoint: function(node, type, enabled)
212 var breakpointId = this._createBreakpointId(node.id, type);
213 var breakpointElement = this._breakpointElements[breakpointId];
214 if (!breakpointElement) {
215 breakpointElement = this._createBreakpointElement(node, type, enabled);
216 this._breakpointElements[breakpointId] = breakpointElement;
218 breakpointElement._checkboxElement.checked = enabled;
221 DOMDebuggerAgent.setDOMBreakpoint(node.id, type);
225 * @param {!WebInspector.DOMNode} node
226 * @param {string} type
227 * @param {boolean} enabled
229 _createBreakpointElement: function(node, type, enabled)
231 var element = createElement("li");
232 element._node = node;
233 element._type = type;
234 element.addEventListener("contextmenu", this._contextMenu.bind(this, node, type), true);
236 var checkboxElement = createElement("input");
237 checkboxElement.className = "checkbox-elem";
238 checkboxElement.type = "checkbox";
239 checkboxElement.checked = enabled;
240 checkboxElement.addEventListener("click", this._checkboxClicked.bind(this, node, type), false);
241 element._checkboxElement = checkboxElement;
242 element.appendChild(checkboxElement);
244 var labelElement = createElement("span");
245 element.appendChild(labelElement);
247 var linkifiedNode = WebInspector.DOMPresentationUtils.linkifyNodeReference(node);
248 linkifiedNode.classList.add("monospace");
249 labelElement.appendChild(linkifiedNode);
251 var description = createElement("div");
252 description.className = "source-text";
253 description.textContent = this._breakpointTypeLabels[type];
254 labelElement.appendChild(description);
256 var currentElement = this.listElement.firstChild;
257 while (currentElement) {
258 if (currentElement._type && currentElement._type < element._type)
260 currentElement = currentElement.nextSibling;
262 this.addListElement(element, currentElement);
266 _removeAllBreakpoints: function()
268 for (var id in this._breakpointElements) {
269 var element = this._breakpointElements[id];
270 this._removeBreakpoint(element._node, element._type);
272 this._saveBreakpoints();
276 * @param {!WebInspector.DOMNode} node
277 * @param {string} type
279 _removeBreakpoint: function(node, type)
281 var breakpointId = this._createBreakpointId(node.id, type);
282 var element = this._breakpointElements[breakpointId];
286 this.removeListElement(element);
287 delete this._breakpointElements[breakpointId];
288 if (element._checkboxElement.checked)
289 DOMDebuggerAgent.removeDOMBreakpoint(node.id, type);
293 * @param {!WebInspector.DOMNode} node
294 * @param {string} type
295 * @param {!Event} event
297 _contextMenu: function(node, type, event)
299 var contextMenu = new WebInspector.ContextMenu(event);
302 * @this {WebInspector.DOMBreakpointsSidebarPane}
304 function removeBreakpoint()
306 this._removeBreakpoint(node, type);
307 this._saveBreakpoints();
309 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Remove breakpoint" : "Remove Breakpoint"), removeBreakpoint.bind(this));
310 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Remove all DOM breakpoints" : "Remove All DOM Breakpoints"), this._removeAllBreakpoints.bind(this));
315 * @param {!WebInspector.DOMNode} node
316 * @param {string} type
317 * @param {!Event} event
319 _checkboxClicked: function(node, type, event)
321 if (event.target.checked)
322 DOMDebuggerAgent.setDOMBreakpoint(node.id, type);
324 DOMDebuggerAgent.removeDOMBreakpoint(node.id, type);
325 this._saveBreakpoints();
328 highlightBreakpoint: function(auxData)
330 var breakpointId = this._createBreakpointId(auxData.nodeId, auxData.type);
331 var element = this._breakpointElements[breakpointId];
335 element.classList.add("breakpoint-hit");
336 this._highlightedElement = element;
339 clearBreakpointHighlight: function()
341 if (this._highlightedElement) {
342 this._highlightedElement.classList.remove("breakpoint-hit");
343 delete this._highlightedElement;
348 * @param {number} nodeId
349 * @param {string} type
351 _createBreakpointId: function(nodeId, type)
353 return nodeId + ":" + type;
356 _saveBreakpoints: function()
358 var breakpoints = [];
359 var storedBreakpoints = WebInspector.settings.domBreakpoints.get();
360 for (var i = 0; i < storedBreakpoints.length; ++i) {
361 var breakpoint = storedBreakpoints[i];
362 if (breakpoint.url !== this._inspectedURL)
363 breakpoints.push(breakpoint);
365 for (var id in this._breakpointElements) {
366 var element = this._breakpointElements[id];
367 breakpoints.push({ url: this._inspectedURL, path: element._node.path(), type: element._type, enabled: element._checkboxElement.checked });
369 WebInspector.settings.domBreakpoints.set(breakpoints);
373 * @param {!WebInspector.Target} target
375 restoreBreakpoints: function(target)
377 var pathToBreakpoints = {};
380 * @param {string} path
381 * @param {?DOMAgent.NodeId} nodeId
382 * @this {WebInspector.DOMBreakpointsSidebarPane}
384 function didPushNodeByPathToFrontend(path, nodeId)
386 var node = nodeId ? target.domModel.nodeForId(nodeId) : null;
390 var breakpoints = pathToBreakpoints[path];
391 for (var i = 0; i < breakpoints.length; ++i)
392 this._setBreakpoint(node, breakpoints[i].type, breakpoints[i].enabled);
395 var breakpoints = WebInspector.settings.domBreakpoints.get();
396 for (var i = 0; i < breakpoints.length; ++i) {
397 var breakpoint = breakpoints[i];
398 if (breakpoint.url !== this._inspectedURL)
400 var path = breakpoint.path;
401 if (!pathToBreakpoints[path]) {
402 pathToBreakpoints[path] = [];
403 target.domModel.pushNodeByPathToFrontend(path, didPushNodeByPathToFrontend.bind(this, path));
405 pathToBreakpoints[path].push(breakpoint);
410 * @param {!WebInspector.Panel} panel
411 * @return {!WebInspector.DOMBreakpointsSidebarPane.Proxy}
413 createProxy: function(panel)
415 var proxy = new WebInspector.DOMBreakpointsSidebarPane.Proxy(this, panel);
418 this._proxies.push(proxy);
422 onContentReady: function()
424 for (var i = 0; i != this._proxies.length; i++)
425 this._proxies[i].onContentReady();
428 __proto__: WebInspector.NativeBreakpointsSidebarPane.prototype
433 * @extends {WebInspector.SidebarPane}
434 * @param {!WebInspector.DOMBreakpointsSidebarPane} pane
435 * @param {!WebInspector.Panel} panel
437 WebInspector.DOMBreakpointsSidebarPane.Proxy = function(pane, panel)
439 WebInspector.View.__assert(!pane.titleElement.firstChild, "Cannot create proxy for a sidebar pane with a toolbar");
441 WebInspector.SidebarPane.call(this, pane.title());
442 this.registerRequiredCSS("components/breakpointsList.css");
444 this._wrappedPane = pane;
447 this.bodyElement.remove();
448 this.bodyElement = this._wrappedPane.bodyElement;
451 WebInspector.DOMBreakpointsSidebarPane.Proxy.prototype = {
454 this._wrappedPane.expand();
457 onContentReady: function()
459 if (this._panel.isShowing())
460 this._reattachBody();
462 WebInspector.SidebarPane.prototype.onContentReady.call(this);
467 WebInspector.SidebarPane.prototype.wasShown.call(this);
468 this._reattachBody();
471 _reattachBody: function()
473 if (this.bodyElement.parentNode !== this.element)
474 this.element.appendChild(this.bodyElement);
477 __proto__: WebInspector.SidebarPane.prototype
481 * @type {!WebInspector.DOMBreakpointsSidebarPane}
483 WebInspector.domBreakpointsSidebarPane;