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.
31 WebInspector.DOMBreakpointsSidebarPane = function()
33 WebInspector.NativeBreakpointsSidebarPane.call(this, WebInspector.UIString("DOM Breakpoints"));
35 this._breakpointElements = {};
37 this._breakpointTypes = {
42 this._breakpointTypeLabels = [
43 WebInspector.UIString("Subtree Modified"),
44 WebInspector.UIString("Attribute Modified"),
45 WebInspector.UIString("Node Removed")
47 this._contextMenuLabels = [
48 WebInspector.UIString("Break on Subtree Modifications"),
49 WebInspector.UIString("Break on Attributes Modifications"),
50 WebInspector.UIString("Break on Node Removal")
54 WebInspector.DOMBreakpointsSidebarPane.prototype = {
55 setInspectedURL: function(url)
58 this._inspectedURL = url.removeURLFragment();
61 populateNodeContextMenu: function(node, contextMenu)
63 var nodeBreakpoints = {};
64 for (var id in this._breakpointElements) {
65 var element = this._breakpointElements[id];
66 if (element._node === node)
67 nodeBreakpoints[element._type] = true;
70 function toggleBreakpoint(type)
72 if (!nodeBreakpoints[type])
73 this._setBreakpoint(node, type, true);
75 this._removeBreakpoint(node, type);
76 this._saveBreakpoints();
79 for (var type = 0; type < 3; ++type) {
80 var label = this._contextMenuLabels[type];
81 contextMenu.appendCheckboxItem(label, toggleBreakpoint.bind(this, type), nodeBreakpoints[type]);
85 createBreakpointHitStatusMessage: function(eventData, callback)
87 if (eventData.type === this._breakpointTypes.SubtreeModified) {
88 var targetNodeObject = WebInspector.RemoteObject.fromPayload(eventData.targetNode);
89 function didPushNodeToFrontend(targetNodeId)
92 targetNodeObject.release();
93 this._doCreateBreakpointHitStatusMessage(eventData, targetNodeId, callback);
95 targetNodeObject.pushNodeToFrontend(didPushNodeToFrontend.bind(this));
97 this._doCreateBreakpointHitStatusMessage(eventData, null, callback);
100 _doCreateBreakpointHitStatusMessage: function (eventData, targetNodeId, callback)
103 var typeLabel = this._breakpointTypeLabels[eventData.type];
104 var linkifiedNode = WebInspector.panels.elements.linkifyNodeById(eventData.nodeId);
105 var substitutions = [typeLabel, linkifiedNode];
108 targetNode = WebInspector.panels.elements.linkifyNodeById(targetNodeId);
110 if (eventData.type === this._breakpointTypes.SubtreeModified) {
111 if (eventData.insertion) {
112 if (targetNodeId !== eventData.nodeId) {
113 message = "Paused on a \"%s\" breakpoint set on %s, because a new child was added to its descendant %s.";
114 substitutions.push(targetNode);
116 message = "Paused on a \"%s\" breakpoint set on %s, because a new child was added to that node.";
118 message = "Paused on a \"%s\" breakpoint set on %s, because its descendant %s was removed.";
119 substitutions.push(targetNode);
122 message = "Paused on a \"%s\" breakpoint set on %s.";
124 var element = document.createElement("span");
126 s: function(substitution)
131 function append(a, b)
133 if (typeof b === "string")
134 b = document.createTextNode(b);
135 element.appendChild(b);
137 WebInspector.formatLocalized(message, substitutions, formatters, "", append);
142 nodeRemoved: function(node)
144 this._removeBreakpointsForNode(node);
147 for (var i = 0; i < node.children.length; ++i)
148 this._removeBreakpointsForNode(node.children[i]);
149 this._saveBreakpoints();
152 _removeBreakpointsForNode: function(node)
154 for (var id in this._breakpointElements) {
155 var element = this._breakpointElements[id];
156 if (element._node === node)
157 this._removeBreakpoint(element._node, element._type);
161 _setBreakpoint: function(node, type, enabled)
163 var breakpointId = this._createBreakpointId(node.id, type);
164 if (breakpointId in this._breakpointElements)
167 var element = document.createElement("li");
168 element._node = node;
169 element._type = type;
170 element.addEventListener("contextmenu", this._contextMenu.bind(this, node, type), true);
172 var checkboxElement = document.createElement("input");
173 checkboxElement.className = "checkbox-elem";
174 checkboxElement.type = "checkbox";
175 checkboxElement.checked = enabled;
176 checkboxElement.addEventListener("click", this._checkboxClicked.bind(this, node, type), false);
177 element._checkboxElement = checkboxElement;
178 element.appendChild(checkboxElement);
180 var labelElement = document.createElement("span");
181 element.appendChild(labelElement);
183 var linkifiedNode = WebInspector.panels.elements.linkifyNodeById(node.id);
184 linkifiedNode.addStyleClass("monospace");
185 labelElement.appendChild(linkifiedNode);
187 var description = document.createElement("div");
188 description.className = "source-text";
189 description.textContent = this._breakpointTypeLabels[type];
190 labelElement.appendChild(description);
192 var currentElement = this.listElement.firstChild;
193 while (currentElement) {
194 if (currentElement._type && currentElement._type < element._type)
196 currentElement = currentElement.nextSibling;
198 this._addListElement(element, currentElement);
199 this._breakpointElements[breakpointId] = element;
201 BrowserDebuggerAgent.setDOMBreakpoint(node.id, type);
204 _removeBreakpoint: function(node, type)
206 var breakpointId = this._createBreakpointId(node.id, type);
207 var element = this._breakpointElements[breakpointId];
211 this._removeListElement(element);
212 delete this._breakpointElements[breakpointId];
213 if (element._checkboxElement.checked)
214 BrowserDebuggerAgent.removeDOMBreakpoint(node.id, type);
217 _contextMenu: function(node, type, event)
219 var contextMenu = new WebInspector.ContextMenu();
220 function removeBreakpoint()
222 this._removeBreakpoint(node, type);
223 this._saveBreakpoints();
225 contextMenu.appendItem(WebInspector.UIString("Remove Breakpoint"), removeBreakpoint.bind(this));
226 contextMenu.show(event);
229 _checkboxClicked: function(node, type, event)
231 if (event.target.checked)
232 BrowserDebuggerAgent.setDOMBreakpoint(node.id, type);
234 BrowserDebuggerAgent.removeDOMBreakpoint(node.id, type);
235 this._saveBreakpoints();
238 highlightBreakpoint: function(eventData)
240 var breakpointId = this._createBreakpointId(eventData.nodeId, eventData.type);
241 var element = this._breakpointElements[breakpointId];
244 this.expanded = true;
245 element.addStyleClass("breakpoint-hit");
246 this._highlightedElement = element;
249 clearBreakpointHighlight: function()
251 if (this._highlightedElement) {
252 this._highlightedElement.removeStyleClass("breakpoint-hit");
253 delete this._highlightedElement;
257 _createBreakpointId: function(nodeId, type)
259 return nodeId + ":" + type;
262 _saveBreakpoints: function()
264 var breakpoints = [];
265 var storedBreakpoints = WebInspector.settings.domBreakpoints;
266 for (var i = 0; i < storedBreakpoints.length; ++i) {
267 var breakpoint = storedBreakpoints[i];
268 if (breakpoint.url !== this._inspectedURL)
269 breakpoints.push(breakpoint);
271 for (var id in this._breakpointElements) {
272 var element = this._breakpointElements[id];
273 breakpoints.push({ url: this._inspectedURL, path: element._node.path(), type: element._type, enabled: element._checkboxElement.checked });
275 WebInspector.settings.domBreakpoints = breakpoints;
278 restoreBreakpoints: function()
280 var pathToBreakpoints = {};
282 function didPushNodeByPathToFrontend(path, nodeId)
284 var node = WebInspector.domAgent.nodeForId(nodeId);
288 var breakpoints = pathToBreakpoints[path];
289 for (var i = 0; i < breakpoints.length; ++i)
290 this._setBreakpoint(node, breakpoints[i].type, breakpoints[i].enabled);
293 var breakpoints = WebInspector.settings.domBreakpoints;
294 for (var i = 0; i < breakpoints.length; ++i) {
295 var breakpoint = breakpoints[i];
296 if (breakpoint.url !== this._inspectedURL)
298 var path = breakpoint.path;
299 if (!pathToBreakpoints[path]) {
300 pathToBreakpoints[path] = [];
301 WebInspector.domAgent.pushNodeByPathToFrontend(path, didPushNodeByPathToFrontend.bind(this, path));
303 pathToBreakpoints[path].push(breakpoint);
308 WebInspector.DOMBreakpointsSidebarPane.prototype.__proto__ = WebInspector.NativeBreakpointsSidebarPane.prototype;