1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/widgets/VariablesView.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,4050 @@ 1.4 +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 +"use strict"; 1.10 + 1.11 +const Ci = Components.interfaces; 1.12 +const Cu = Components.utils; 1.13 + 1.14 +const DBG_STRINGS_URI = "chrome://browser/locale/devtools/debugger.properties"; 1.15 +const LAZY_EMPTY_DELAY = 150; // ms 1.16 +const LAZY_EXPAND_DELAY = 50; // ms 1.17 +const SCROLL_PAGE_SIZE_DEFAULT = 0; 1.18 +const APPEND_PAGE_SIZE_DEFAULT = 500; 1.19 +const PAGE_SIZE_SCROLL_HEIGHT_RATIO = 100; 1.20 +const PAGE_SIZE_MAX_JUMPS = 30; 1.21 +const SEARCH_ACTION_MAX_DELAY = 300; // ms 1.22 +const ITEM_FLASH_DURATION = 300 // ms 1.23 + 1.24 +Cu.import("resource://gre/modules/Services.jsm"); 1.25 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.26 +Cu.import("resource:///modules/devtools/ViewHelpers.jsm"); 1.27 +Cu.import("resource://gre/modules/devtools/event-emitter.js"); 1.28 +Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm"); 1.29 +Cu.import("resource://gre/modules/Task.jsm"); 1.30 +let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); 1.31 + 1.32 +XPCOMUtils.defineLazyModuleGetter(this, "devtools", 1.33 + "resource://gre/modules/devtools/Loader.jsm"); 1.34 + 1.35 +XPCOMUtils.defineLazyModuleGetter(this, "PluralForm", 1.36 + "resource://gre/modules/PluralForm.jsm"); 1.37 + 1.38 +XPCOMUtils.defineLazyServiceGetter(this, "clipboardHelper", 1.39 + "@mozilla.org/widget/clipboardhelper;1", 1.40 + "nsIClipboardHelper"); 1.41 + 1.42 +Object.defineProperty(this, "WebConsoleUtils", { 1.43 + get: function() { 1.44 + return devtools.require("devtools/toolkit/webconsole/utils").Utils; 1.45 + }, 1.46 + configurable: true, 1.47 + enumerable: true 1.48 +}); 1.49 + 1.50 +Object.defineProperty(this, "NetworkHelper", { 1.51 + get: function() { 1.52 + return devtools.require("devtools/toolkit/webconsole/network-helper"); 1.53 + }, 1.54 + configurable: true, 1.55 + enumerable: true 1.56 +}); 1.57 + 1.58 +this.EXPORTED_SYMBOLS = ["VariablesView", "escapeHTML"]; 1.59 + 1.60 +/** 1.61 + * Debugger localization strings. 1.62 + */ 1.63 +const STR = Services.strings.createBundle(DBG_STRINGS_URI); 1.64 + 1.65 +/** 1.66 + * A tree view for inspecting scopes, objects and properties. 1.67 + * Iterable via "for (let [id, scope] of instance) { }". 1.68 + * Requires the devtools common.css and debugger.css skin stylesheets. 1.69 + * 1.70 + * To allow replacing variable or property values in this view, provide an 1.71 + * "eval" function property. To allow replacing variable or property names, 1.72 + * provide a "switch" function. To handle deleting variables or properties, 1.73 + * provide a "delete" function. 1.74 + * 1.75 + * @param nsIDOMNode aParentNode 1.76 + * The parent node to hold this view. 1.77 + * @param object aFlags [optional] 1.78 + * An object contaning initialization options for this view. 1.79 + * e.g. { lazyEmpty: true, searchEnabled: true ... } 1.80 + */ 1.81 +this.VariablesView = function VariablesView(aParentNode, aFlags = {}) { 1.82 + this._store = []; // Can't use a Map because Scope names needn't be unique. 1.83 + this._itemsByElement = new WeakMap(); 1.84 + this._prevHierarchy = new Map(); 1.85 + this._currHierarchy = new Map(); 1.86 + 1.87 + this._parent = aParentNode; 1.88 + this._parent.classList.add("variables-view-container"); 1.89 + this._parent.classList.add("theme-body"); 1.90 + this._appendEmptyNotice(); 1.91 + 1.92 + this._onSearchboxInput = this._onSearchboxInput.bind(this); 1.93 + this._onSearchboxKeyPress = this._onSearchboxKeyPress.bind(this); 1.94 + this._onViewKeyPress = this._onViewKeyPress.bind(this); 1.95 + this._onViewKeyDown = this._onViewKeyDown.bind(this); 1.96 + 1.97 + // Create an internal scrollbox container. 1.98 + this._list = this.document.createElement("scrollbox"); 1.99 + this._list.setAttribute("orient", "vertical"); 1.100 + this._list.addEventListener("keypress", this._onViewKeyPress, false); 1.101 + this._list.addEventListener("keydown", this._onViewKeyDown, false); 1.102 + this._parent.appendChild(this._list); 1.103 + 1.104 + for (let name in aFlags) { 1.105 + this[name] = aFlags[name]; 1.106 + } 1.107 + 1.108 + EventEmitter.decorate(this); 1.109 +}; 1.110 + 1.111 +VariablesView.prototype = { 1.112 + /** 1.113 + * Helper setter for populating this container with a raw object. 1.114 + * 1.115 + * @param object aObject 1.116 + * The raw object to display. You can only provide this object 1.117 + * if you want the variables view to work in sync mode. 1.118 + */ 1.119 + set rawObject(aObject) { 1.120 + this.empty(); 1.121 + this.addScope() 1.122 + .addItem("", { enumerable: true }) 1.123 + .populate(aObject, { sorted: true }); 1.124 + }, 1.125 + 1.126 + /** 1.127 + * Adds a scope to contain any inspected variables. 1.128 + * 1.129 + * This new scope will be considered the parent of any other scope 1.130 + * added afterwards. 1.131 + * 1.132 + * @param string aName 1.133 + * The scope's name (e.g. "Local", "Global" etc.). 1.134 + * @return Scope 1.135 + * The newly created Scope instance. 1.136 + */ 1.137 + addScope: function(aName = "") { 1.138 + this._removeEmptyNotice(); 1.139 + this._toggleSearchVisibility(true); 1.140 + 1.141 + let scope = new Scope(this, aName); 1.142 + this._store.push(scope); 1.143 + this._itemsByElement.set(scope._target, scope); 1.144 + this._currHierarchy.set(aName, scope); 1.145 + scope.header = !!aName; 1.146 + 1.147 + return scope; 1.148 + }, 1.149 + 1.150 + /** 1.151 + * Removes all items from this container. 1.152 + * 1.153 + * @param number aTimeout [optional] 1.154 + * The number of milliseconds to delay the operation if 1.155 + * lazy emptying of this container is enabled. 1.156 + */ 1.157 + empty: function(aTimeout = this.lazyEmptyDelay) { 1.158 + // If there are no items in this container, emptying is useless. 1.159 + if (!this._store.length) { 1.160 + return; 1.161 + } 1.162 + 1.163 + this._store.length = 0; 1.164 + this._itemsByElement.clear(); 1.165 + this._prevHierarchy = this._currHierarchy; 1.166 + this._currHierarchy = new Map(); // Don't clear, this is just simple swapping. 1.167 + 1.168 + // Check if this empty operation may be executed lazily. 1.169 + if (this.lazyEmpty && aTimeout > 0) { 1.170 + this._emptySoon(aTimeout); 1.171 + return; 1.172 + } 1.173 + 1.174 + while (this._list.hasChildNodes()) { 1.175 + this._list.firstChild.remove(); 1.176 + } 1.177 + 1.178 + this._appendEmptyNotice(); 1.179 + this._toggleSearchVisibility(false); 1.180 + }, 1.181 + 1.182 + /** 1.183 + * Emptying this container and rebuilding it immediately afterwards would 1.184 + * result in a brief redraw flicker, because the previously expanded nodes 1.185 + * may get asynchronously re-expanded, after fetching the prototype and 1.186 + * properties from a server. 1.187 + * 1.188 + * To avoid such behaviour, a normal container list is rebuild, but not 1.189 + * immediately attached to the parent container. The old container list 1.190 + * is kept around for a short period of time, hopefully accounting for the 1.191 + * data fetching delay. In the meantime, any operations can be executed 1.192 + * normally. 1.193 + * 1.194 + * @see VariablesView.empty 1.195 + * @see VariablesView.commitHierarchy 1.196 + */ 1.197 + _emptySoon: function(aTimeout) { 1.198 + let prevList = this._list; 1.199 + let currList = this._list = this.document.createElement("scrollbox"); 1.200 + 1.201 + this.window.setTimeout(() => { 1.202 + prevList.removeEventListener("keypress", this._onViewKeyPress, false); 1.203 + prevList.removeEventListener("keydown", this._onViewKeyDown, false); 1.204 + currList.addEventListener("keypress", this._onViewKeyPress, false); 1.205 + currList.addEventListener("keydown", this._onViewKeyDown, false); 1.206 + currList.setAttribute("orient", "vertical"); 1.207 + 1.208 + this._parent.removeChild(prevList); 1.209 + this._parent.appendChild(currList); 1.210 + 1.211 + if (!this._store.length) { 1.212 + this._appendEmptyNotice(); 1.213 + this._toggleSearchVisibility(false); 1.214 + } 1.215 + }, aTimeout); 1.216 + }, 1.217 + 1.218 + /** 1.219 + * Optional DevTools toolbox containing this VariablesView. Used to 1.220 + * communicate with the inspector and highlighter. 1.221 + */ 1.222 + toolbox: null, 1.223 + 1.224 + /** 1.225 + * The controller for this VariablesView, if it has one. 1.226 + */ 1.227 + controller: null, 1.228 + 1.229 + /** 1.230 + * The amount of time (in milliseconds) it takes to empty this view lazily. 1.231 + */ 1.232 + lazyEmptyDelay: LAZY_EMPTY_DELAY, 1.233 + 1.234 + /** 1.235 + * Specifies if this view may be emptied lazily. 1.236 + * @see VariablesView.prototype.empty 1.237 + */ 1.238 + lazyEmpty: false, 1.239 + 1.240 + /** 1.241 + * Specifies if nodes in this view may be searched lazily. 1.242 + */ 1.243 + lazySearch: true, 1.244 + 1.245 + /** 1.246 + * The number of elements in this container to jump when Page Up or Page Down 1.247 + * keys are pressed. If falsy, then the page size will be based on the 1.248 + * container height. 1.249 + */ 1.250 + scrollPageSize: SCROLL_PAGE_SIZE_DEFAULT, 1.251 + 1.252 + /** 1.253 + * The maximum number of elements allowed in a scope, variable or property 1.254 + * that allows pagination when appending children. 1.255 + */ 1.256 + appendPageSize: APPEND_PAGE_SIZE_DEFAULT, 1.257 + 1.258 + /** 1.259 + * Function called each time a variable or property's value is changed via 1.260 + * user interaction. If null, then value changes are disabled. 1.261 + * 1.262 + * This property is applied recursively onto each scope in this view and 1.263 + * affects only the child nodes when they're created. 1.264 + */ 1.265 + eval: null, 1.266 + 1.267 + /** 1.268 + * Function called each time a variable or property's name is changed via 1.269 + * user interaction. If null, then name changes are disabled. 1.270 + * 1.271 + * This property is applied recursively onto each scope in this view and 1.272 + * affects only the child nodes when they're created. 1.273 + */ 1.274 + switch: null, 1.275 + 1.276 + /** 1.277 + * Function called each time a variable or property is deleted via 1.278 + * user interaction. If null, then deletions are disabled. 1.279 + * 1.280 + * This property is applied recursively onto each scope in this view and 1.281 + * affects only the child nodes when they're created. 1.282 + */ 1.283 + delete: null, 1.284 + 1.285 + /** 1.286 + * Function called each time a property is added via user interaction. If 1.287 + * null, then property additions are disabled. 1.288 + * 1.289 + * This property is applied recursively onto each scope in this view and 1.290 + * affects only the child nodes when they're created. 1.291 + */ 1.292 + new: null, 1.293 + 1.294 + /** 1.295 + * Specifies if after an eval or switch operation, the variable or property 1.296 + * which has been edited should be disabled. 1.297 + */ 1.298 + preventDisableOnChange: false, 1.299 + 1.300 + /** 1.301 + * Specifies if, whenever a variable or property descriptor is available, 1.302 + * configurable, enumerable, writable, frozen, sealed and extensible 1.303 + * attributes should not affect presentation. 1.304 + * 1.305 + * This flag is applied recursively onto each scope in this view and 1.306 + * affects only the child nodes when they're created. 1.307 + */ 1.308 + preventDescriptorModifiers: false, 1.309 + 1.310 + /** 1.311 + * The tooltip text shown on a variable or property's value if an |eval| 1.312 + * function is provided, in order to change the variable or property's value. 1.313 + * 1.314 + * This flag is applied recursively onto each scope in this view and 1.315 + * affects only the child nodes when they're created. 1.316 + */ 1.317 + editableValueTooltip: STR.GetStringFromName("variablesEditableValueTooltip"), 1.318 + 1.319 + /** 1.320 + * The tooltip text shown on a variable or property's name if a |switch| 1.321 + * function is provided, in order to change the variable or property's name. 1.322 + * 1.323 + * This flag is applied recursively onto each scope in this view and 1.324 + * affects only the child nodes when they're created. 1.325 + */ 1.326 + editableNameTooltip: STR.GetStringFromName("variablesEditableNameTooltip"), 1.327 + 1.328 + /** 1.329 + * The tooltip text shown on a variable or property's edit button if an 1.330 + * |eval| function is provided and a getter/setter descriptor is present, 1.331 + * in order to change the variable or property to a plain value. 1.332 + * 1.333 + * This flag is applied recursively onto each scope in this view and 1.334 + * affects only the child nodes when they're created. 1.335 + */ 1.336 + editButtonTooltip: STR.GetStringFromName("variablesEditButtonTooltip"), 1.337 + 1.338 + /** 1.339 + * The tooltip text shown on a variable or property's value if that value is 1.340 + * a DOMNode that can be highlighted and selected in the inspector. 1.341 + * 1.342 + * This flag is applied recursively onto each scope in this view and 1.343 + * affects only the child nodes when they're created. 1.344 + */ 1.345 + domNodeValueTooltip: STR.GetStringFromName("variablesDomNodeValueTooltip"), 1.346 + 1.347 + /** 1.348 + * The tooltip text shown on a variable or property's delete button if a 1.349 + * |delete| function is provided, in order to delete the variable or property. 1.350 + * 1.351 + * This flag is applied recursively onto each scope in this view and 1.352 + * affects only the child nodes when they're created. 1.353 + */ 1.354 + deleteButtonTooltip: STR.GetStringFromName("variablesCloseButtonTooltip"), 1.355 + 1.356 + /** 1.357 + * Specifies the context menu attribute set on variables and properties. 1.358 + * 1.359 + * This flag is applied recursively onto each scope in this view and 1.360 + * affects only the child nodes when they're created. 1.361 + */ 1.362 + contextMenuId: "", 1.363 + 1.364 + /** 1.365 + * The separator label between the variables or properties name and value. 1.366 + * 1.367 + * This flag is applied recursively onto each scope in this view and 1.368 + * affects only the child nodes when they're created. 1.369 + */ 1.370 + separatorStr: STR.GetStringFromName("variablesSeparatorLabel"), 1.371 + 1.372 + /** 1.373 + * Specifies if enumerable properties and variables should be displayed. 1.374 + * These variables and properties are visible by default. 1.375 + * @param boolean aFlag 1.376 + */ 1.377 + set enumVisible(aFlag) { 1.378 + this._enumVisible = aFlag; 1.379 + 1.380 + for (let scope of this._store) { 1.381 + scope._enumVisible = aFlag; 1.382 + } 1.383 + }, 1.384 + 1.385 + /** 1.386 + * Specifies if non-enumerable properties and variables should be displayed. 1.387 + * These variables and properties are visible by default. 1.388 + * @param boolean aFlag 1.389 + */ 1.390 + set nonEnumVisible(aFlag) { 1.391 + this._nonEnumVisible = aFlag; 1.392 + 1.393 + for (let scope of this._store) { 1.394 + scope._nonEnumVisible = aFlag; 1.395 + } 1.396 + }, 1.397 + 1.398 + /** 1.399 + * Specifies if only enumerable properties and variables should be displayed. 1.400 + * Both types of these variables and properties are visible by default. 1.401 + * @param boolean aFlag 1.402 + */ 1.403 + set onlyEnumVisible(aFlag) { 1.404 + if (aFlag) { 1.405 + this.enumVisible = true; 1.406 + this.nonEnumVisible = false; 1.407 + } else { 1.408 + this.enumVisible = true; 1.409 + this.nonEnumVisible = true; 1.410 + } 1.411 + }, 1.412 + 1.413 + /** 1.414 + * Sets if the variable and property searching is enabled. 1.415 + * @param boolean aFlag 1.416 + */ 1.417 + set searchEnabled(aFlag) aFlag ? this._enableSearch() : this._disableSearch(), 1.418 + 1.419 + /** 1.420 + * Gets if the variable and property searching is enabled. 1.421 + * @return boolean 1.422 + */ 1.423 + get searchEnabled() !!this._searchboxContainer, 1.424 + 1.425 + /** 1.426 + * Sets the text displayed for the searchbox in this container. 1.427 + * @param string aValue 1.428 + */ 1.429 + set searchPlaceholder(aValue) { 1.430 + if (this._searchboxNode) { 1.431 + this._searchboxNode.setAttribute("placeholder", aValue); 1.432 + } 1.433 + this._searchboxPlaceholder = aValue; 1.434 + }, 1.435 + 1.436 + /** 1.437 + * Gets the text displayed for the searchbox in this container. 1.438 + * @return string 1.439 + */ 1.440 + get searchPlaceholder() this._searchboxPlaceholder, 1.441 + 1.442 + /** 1.443 + * Enables variable and property searching in this view. 1.444 + * Use the "searchEnabled" setter to enable searching. 1.445 + */ 1.446 + _enableSearch: function() { 1.447 + // If searching was already enabled, no need to re-enable it again. 1.448 + if (this._searchboxContainer) { 1.449 + return; 1.450 + } 1.451 + let document = this.document; 1.452 + let ownerNode = this._parent.parentNode; 1.453 + 1.454 + let container = this._searchboxContainer = document.createElement("hbox"); 1.455 + container.className = "devtools-toolbar"; 1.456 + 1.457 + // Hide the variables searchbox container if there are no variables or 1.458 + // properties to display. 1.459 + container.hidden = !this._store.length; 1.460 + 1.461 + let searchbox = this._searchboxNode = document.createElement("textbox"); 1.462 + searchbox.className = "variables-view-searchinput devtools-searchinput"; 1.463 + searchbox.setAttribute("placeholder", this._searchboxPlaceholder); 1.464 + searchbox.setAttribute("type", "search"); 1.465 + searchbox.setAttribute("flex", "1"); 1.466 + searchbox.addEventListener("input", this._onSearchboxInput, false); 1.467 + searchbox.addEventListener("keypress", this._onSearchboxKeyPress, false); 1.468 + 1.469 + container.appendChild(searchbox); 1.470 + ownerNode.insertBefore(container, this._parent); 1.471 + }, 1.472 + 1.473 + /** 1.474 + * Disables variable and property searching in this view. 1.475 + * Use the "searchEnabled" setter to disable searching. 1.476 + */ 1.477 + _disableSearch: function() { 1.478 + // If searching was already disabled, no need to re-disable it again. 1.479 + if (!this._searchboxContainer) { 1.480 + return; 1.481 + } 1.482 + this._searchboxContainer.remove(); 1.483 + this._searchboxNode.removeEventListener("input", this._onSearchboxInput, false); 1.484 + this._searchboxNode.removeEventListener("keypress", this._onSearchboxKeyPress, false); 1.485 + 1.486 + this._searchboxContainer = null; 1.487 + this._searchboxNode = null; 1.488 + }, 1.489 + 1.490 + /** 1.491 + * Sets the variables searchbox container hidden or visible. 1.492 + * It's hidden by default. 1.493 + * 1.494 + * @param boolean aVisibleFlag 1.495 + * Specifies the intended visibility. 1.496 + */ 1.497 + _toggleSearchVisibility: function(aVisibleFlag) { 1.498 + // If searching was already disabled, there's no need to hide it. 1.499 + if (!this._searchboxContainer) { 1.500 + return; 1.501 + } 1.502 + this._searchboxContainer.hidden = !aVisibleFlag; 1.503 + }, 1.504 + 1.505 + /** 1.506 + * Listener handling the searchbox input event. 1.507 + */ 1.508 + _onSearchboxInput: function() { 1.509 + this.scheduleSearch(this._searchboxNode.value); 1.510 + }, 1.511 + 1.512 + /** 1.513 + * Listener handling the searchbox key press event. 1.514 + */ 1.515 + _onSearchboxKeyPress: function(e) { 1.516 + switch(e.keyCode) { 1.517 + case e.DOM_VK_RETURN: 1.518 + this._onSearchboxInput(); 1.519 + return; 1.520 + case e.DOM_VK_ESCAPE: 1.521 + this._searchboxNode.value = ""; 1.522 + this._onSearchboxInput(); 1.523 + return; 1.524 + } 1.525 + }, 1.526 + 1.527 + /** 1.528 + * Schedules searching for variables or properties matching the query. 1.529 + * 1.530 + * @param string aToken 1.531 + * The variable or property to search for. 1.532 + * @param number aWait 1.533 + * The amount of milliseconds to wait until draining. 1.534 + */ 1.535 + scheduleSearch: function(aToken, aWait) { 1.536 + // Check if this search operation may not be executed lazily. 1.537 + if (!this.lazySearch) { 1.538 + this._doSearch(aToken); 1.539 + return; 1.540 + } 1.541 + 1.542 + // The amount of time to wait for the requests to settle. 1.543 + let maxDelay = SEARCH_ACTION_MAX_DELAY; 1.544 + let delay = aWait === undefined ? maxDelay / aToken.length : aWait; 1.545 + 1.546 + // Allow requests to settle down first. 1.547 + setNamedTimeout("vview-search", delay, () => this._doSearch(aToken)); 1.548 + }, 1.549 + 1.550 + /** 1.551 + * Performs a case insensitive search for variables or properties matching 1.552 + * the query, and hides non-matched items. 1.553 + * 1.554 + * If aToken is falsy, then all the scopes are unhidden and expanded, 1.555 + * while the available variables and properties inside those scopes are 1.556 + * just unhidden. 1.557 + * 1.558 + * @param string aToken 1.559 + * The variable or property to search for. 1.560 + */ 1.561 + _doSearch: function(aToken) { 1.562 + for (let scope of this._store) { 1.563 + switch (aToken) { 1.564 + case "": 1.565 + case null: 1.566 + case undefined: 1.567 + scope.expand(); 1.568 + scope._performSearch(""); 1.569 + break; 1.570 + default: 1.571 + scope._performSearch(aToken.toLowerCase()); 1.572 + break; 1.573 + } 1.574 + } 1.575 + }, 1.576 + 1.577 + /** 1.578 + * Find the first item in the tree of visible items in this container that 1.579 + * matches the predicate. Searches in visual order (the order seen by the 1.580 + * user). Descends into each scope to check the scope and its children. 1.581 + * 1.582 + * @param function aPredicate 1.583 + * A function that returns true when a match is found. 1.584 + * @return Scope | Variable | Property 1.585 + * The first visible scope, variable or property, or null if nothing 1.586 + * is found. 1.587 + */ 1.588 + _findInVisibleItems: function(aPredicate) { 1.589 + for (let scope of this._store) { 1.590 + let result = scope._findInVisibleItems(aPredicate); 1.591 + if (result) { 1.592 + return result; 1.593 + } 1.594 + } 1.595 + return null; 1.596 + }, 1.597 + 1.598 + /** 1.599 + * Find the last item in the tree of visible items in this container that 1.600 + * matches the predicate. Searches in reverse visual order (opposite of the 1.601 + * order seen by the user). Descends into each scope to check the scope and 1.602 + * its children. 1.603 + * 1.604 + * @param function aPredicate 1.605 + * A function that returns true when a match is found. 1.606 + * @return Scope | Variable | Property 1.607 + * The last visible scope, variable or property, or null if nothing 1.608 + * is found. 1.609 + */ 1.610 + _findInVisibleItemsReverse: function(aPredicate) { 1.611 + for (let i = this._store.length - 1; i >= 0; i--) { 1.612 + let scope = this._store[i]; 1.613 + let result = scope._findInVisibleItemsReverse(aPredicate); 1.614 + if (result) { 1.615 + return result; 1.616 + } 1.617 + } 1.618 + return null; 1.619 + }, 1.620 + 1.621 + /** 1.622 + * Gets the scope at the specified index. 1.623 + * 1.624 + * @param number aIndex 1.625 + * The scope's index. 1.626 + * @return Scope 1.627 + * The scope if found, undefined if not. 1.628 + */ 1.629 + getScopeAtIndex: function(aIndex) { 1.630 + return this._store[aIndex]; 1.631 + }, 1.632 + 1.633 + /** 1.634 + * Recursively searches this container for the scope, variable or property 1.635 + * displayed by the specified node. 1.636 + * 1.637 + * @param nsIDOMNode aNode 1.638 + * The node to search for. 1.639 + * @return Scope | Variable | Property 1.640 + * The matched scope, variable or property, or null if nothing is found. 1.641 + */ 1.642 + getItemForNode: function(aNode) { 1.643 + return this._itemsByElement.get(aNode); 1.644 + }, 1.645 + 1.646 + /** 1.647 + * Gets the scope owning a Variable or Property. 1.648 + * 1.649 + * @param Variable | Property 1.650 + * The variable or property to retrieven the owner scope for. 1.651 + * @return Scope 1.652 + * The owner scope. 1.653 + */ 1.654 + getOwnerScopeForVariableOrProperty: function(aItem) { 1.655 + if (!aItem) { 1.656 + return null; 1.657 + } 1.658 + // If this is a Scope, return it. 1.659 + if (!(aItem instanceof Variable)) { 1.660 + return aItem; 1.661 + } 1.662 + // If this is a Variable or Property, find its owner scope. 1.663 + if (aItem instanceof Variable && aItem.ownerView) { 1.664 + return this.getOwnerScopeForVariableOrProperty(aItem.ownerView); 1.665 + } 1.666 + return null; 1.667 + }, 1.668 + 1.669 + /** 1.670 + * Gets the parent scopes for a specified Variable or Property. 1.671 + * The returned list will not include the owner scope. 1.672 + * 1.673 + * @param Variable | Property 1.674 + * The variable or property for which to find the parent scopes. 1.675 + * @return array 1.676 + * A list of parent Scopes. 1.677 + */ 1.678 + getParentScopesForVariableOrProperty: function(aItem) { 1.679 + let scope = this.getOwnerScopeForVariableOrProperty(aItem); 1.680 + return this._store.slice(0, Math.max(this._store.indexOf(scope), 0)); 1.681 + }, 1.682 + 1.683 + /** 1.684 + * Gets the currently focused scope, variable or property in this view. 1.685 + * 1.686 + * @return Scope | Variable | Property 1.687 + * The focused scope, variable or property, or null if nothing is found. 1.688 + */ 1.689 + getFocusedItem: function() { 1.690 + let focused = this.document.commandDispatcher.focusedElement; 1.691 + return this.getItemForNode(focused); 1.692 + }, 1.693 + 1.694 + /** 1.695 + * Focuses the first visible scope, variable, or property in this container. 1.696 + */ 1.697 + focusFirstVisibleItem: function() { 1.698 + let focusableItem = this._findInVisibleItems(item => item.focusable); 1.699 + if (focusableItem) { 1.700 + this._focusItem(focusableItem); 1.701 + } 1.702 + this._parent.scrollTop = 0; 1.703 + this._parent.scrollLeft = 0; 1.704 + }, 1.705 + 1.706 + /** 1.707 + * Focuses the last visible scope, variable, or property in this container. 1.708 + */ 1.709 + focusLastVisibleItem: function() { 1.710 + let focusableItem = this._findInVisibleItemsReverse(item => item.focusable); 1.711 + if (focusableItem) { 1.712 + this._focusItem(focusableItem); 1.713 + } 1.714 + this._parent.scrollTop = this._parent.scrollHeight; 1.715 + this._parent.scrollLeft = 0; 1.716 + }, 1.717 + 1.718 + /** 1.719 + * Focuses the next scope, variable or property in this view. 1.720 + */ 1.721 + focusNextItem: function() { 1.722 + this.focusItemAtDelta(+1); 1.723 + }, 1.724 + 1.725 + /** 1.726 + * Focuses the previous scope, variable or property in this view. 1.727 + */ 1.728 + focusPrevItem: function() { 1.729 + this.focusItemAtDelta(-1); 1.730 + }, 1.731 + 1.732 + /** 1.733 + * Focuses another scope, variable or property in this view, based on 1.734 + * the index distance from the currently focused item. 1.735 + * 1.736 + * @param number aDelta 1.737 + * A scalar specifying by how many items should the selection change. 1.738 + */ 1.739 + focusItemAtDelta: function(aDelta) { 1.740 + let direction = aDelta > 0 ? "advanceFocus" : "rewindFocus"; 1.741 + let distance = Math.abs(Math[aDelta > 0 ? "ceil" : "floor"](aDelta)); 1.742 + while (distance--) { 1.743 + if (!this._focusChange(direction)) { 1.744 + break; // Out of bounds. 1.745 + } 1.746 + } 1.747 + }, 1.748 + 1.749 + /** 1.750 + * Focuses the next or previous scope, variable or property in this view. 1.751 + * 1.752 + * @param string aDirection 1.753 + * Either "advanceFocus" or "rewindFocus". 1.754 + * @return boolean 1.755 + * False if the focus went out of bounds and the first or last element 1.756 + * in this view was focused instead. 1.757 + */ 1.758 + _focusChange: function(aDirection) { 1.759 + let commandDispatcher = this.document.commandDispatcher; 1.760 + let prevFocusedElement = commandDispatcher.focusedElement; 1.761 + let currFocusedItem = null; 1.762 + 1.763 + do { 1.764 + commandDispatcher.suppressFocusScroll = true; 1.765 + commandDispatcher[aDirection](); 1.766 + 1.767 + // Make sure the newly focused item is a part of this view. 1.768 + // If the focus goes out of bounds, revert the previously focused item. 1.769 + if (!(currFocusedItem = this.getFocusedItem())) { 1.770 + prevFocusedElement.focus(); 1.771 + return false; 1.772 + } 1.773 + } while (!currFocusedItem.focusable); 1.774 + 1.775 + // Focus remained within bounds. 1.776 + return true; 1.777 + }, 1.778 + 1.779 + /** 1.780 + * Focuses a scope, variable or property and makes sure it's visible. 1.781 + * 1.782 + * @param aItem Scope | Variable | Property 1.783 + * The item to focus. 1.784 + * @param boolean aCollapseFlag 1.785 + * True if the focused item should also be collapsed. 1.786 + * @return boolean 1.787 + * True if the item was successfully focused. 1.788 + */ 1.789 + _focusItem: function(aItem, aCollapseFlag) { 1.790 + if (!aItem.focusable) { 1.791 + return false; 1.792 + } 1.793 + if (aCollapseFlag) { 1.794 + aItem.collapse(); 1.795 + } 1.796 + aItem._target.focus(); 1.797 + this.boxObject.ensureElementIsVisible(aItem._arrow); 1.798 + return true; 1.799 + }, 1.800 + 1.801 + /** 1.802 + * Listener handling a key press event on the view. 1.803 + */ 1.804 + _onViewKeyPress: function(e) { 1.805 + let item = this.getFocusedItem(); 1.806 + 1.807 + // Prevent scrolling when pressing navigation keys. 1.808 + ViewHelpers.preventScrolling(e); 1.809 + 1.810 + switch (e.keyCode) { 1.811 + case e.DOM_VK_UP: 1.812 + // Always rewind focus. 1.813 + this.focusPrevItem(true); 1.814 + return; 1.815 + 1.816 + case e.DOM_VK_DOWN: 1.817 + // Always advance focus. 1.818 + this.focusNextItem(true); 1.819 + return; 1.820 + 1.821 + case e.DOM_VK_LEFT: 1.822 + // Collapse scopes, variables and properties before rewinding focus. 1.823 + if (item._isExpanded && item._isArrowVisible) { 1.824 + item.collapse(); 1.825 + } else { 1.826 + this._focusItem(item.ownerView); 1.827 + } 1.828 + return; 1.829 + 1.830 + case e.DOM_VK_RIGHT: 1.831 + // Nothing to do here if this item never expands. 1.832 + if (!item._isArrowVisible) { 1.833 + return; 1.834 + } 1.835 + // Expand scopes, variables and properties before advancing focus. 1.836 + if (!item._isExpanded) { 1.837 + item.expand(); 1.838 + } else { 1.839 + this.focusNextItem(true); 1.840 + } 1.841 + return; 1.842 + 1.843 + case e.DOM_VK_PAGE_UP: 1.844 + // Rewind a certain number of elements based on the container height. 1.845 + this.focusItemAtDelta(-(this.scrollPageSize || Math.min(Math.floor(this._list.scrollHeight / 1.846 + PAGE_SIZE_SCROLL_HEIGHT_RATIO), 1.847 + PAGE_SIZE_MAX_JUMPS))); 1.848 + return; 1.849 + 1.850 + case e.DOM_VK_PAGE_DOWN: 1.851 + // Advance a certain number of elements based on the container height. 1.852 + this.focusItemAtDelta(+(this.scrollPageSize || Math.min(Math.floor(this._list.scrollHeight / 1.853 + PAGE_SIZE_SCROLL_HEIGHT_RATIO), 1.854 + PAGE_SIZE_MAX_JUMPS))); 1.855 + return; 1.856 + 1.857 + case e.DOM_VK_HOME: 1.858 + this.focusFirstVisibleItem(); 1.859 + return; 1.860 + 1.861 + case e.DOM_VK_END: 1.862 + this.focusLastVisibleItem(); 1.863 + return; 1.864 + 1.865 + case e.DOM_VK_RETURN: 1.866 + // Start editing the value or name of the Variable or Property. 1.867 + if (item instanceof Variable) { 1.868 + if (e.metaKey || e.altKey || e.shiftKey) { 1.869 + item._activateNameInput(); 1.870 + } else { 1.871 + item._activateValueInput(); 1.872 + } 1.873 + } 1.874 + return; 1.875 + 1.876 + case e.DOM_VK_DELETE: 1.877 + case e.DOM_VK_BACK_SPACE: 1.878 + // Delete the Variable or Property if allowed. 1.879 + if (item instanceof Variable) { 1.880 + item._onDelete(e); 1.881 + } 1.882 + return; 1.883 + 1.884 + case e.DOM_VK_INSERT: 1.885 + item._onAddProperty(e); 1.886 + return; 1.887 + } 1.888 + }, 1.889 + 1.890 + /** 1.891 + * Listener handling a key down event on the view. 1.892 + */ 1.893 + _onViewKeyDown: function(e) { 1.894 + if (e.keyCode == e.DOM_VK_C) { 1.895 + // Copy current selection to clipboard. 1.896 + if (e.ctrlKey || e.metaKey) { 1.897 + let item = this.getFocusedItem(); 1.898 + clipboardHelper.copyString( 1.899 + item._nameString + item.separatorStr + item._valueString 1.900 + ); 1.901 + } 1.902 + } 1.903 + }, 1.904 + 1.905 + /** 1.906 + * Sets the text displayed in this container when there are no available items. 1.907 + * @param string aValue 1.908 + */ 1.909 + set emptyText(aValue) { 1.910 + if (this._emptyTextNode) { 1.911 + this._emptyTextNode.setAttribute("value", aValue); 1.912 + } 1.913 + this._emptyTextValue = aValue; 1.914 + this._appendEmptyNotice(); 1.915 + }, 1.916 + 1.917 + /** 1.918 + * Creates and appends a label signaling that this container is empty. 1.919 + */ 1.920 + _appendEmptyNotice: function() { 1.921 + if (this._emptyTextNode || !this._emptyTextValue) { 1.922 + return; 1.923 + } 1.924 + 1.925 + let label = this.document.createElement("label"); 1.926 + label.className = "variables-view-empty-notice"; 1.927 + label.setAttribute("value", this._emptyTextValue); 1.928 + 1.929 + this._parent.appendChild(label); 1.930 + this._emptyTextNode = label; 1.931 + }, 1.932 + 1.933 + /** 1.934 + * Removes the label signaling that this container is empty. 1.935 + */ 1.936 + _removeEmptyNotice: function() { 1.937 + if (!this._emptyTextNode) { 1.938 + return; 1.939 + } 1.940 + 1.941 + this._parent.removeChild(this._emptyTextNode); 1.942 + this._emptyTextNode = null; 1.943 + }, 1.944 + 1.945 + /** 1.946 + * Gets if all values should be aligned together. 1.947 + * @return boolean 1.948 + */ 1.949 + get alignedValues() { 1.950 + return this._alignedValues; 1.951 + }, 1.952 + 1.953 + /** 1.954 + * Sets if all values should be aligned together. 1.955 + * @param boolean aFlag 1.956 + */ 1.957 + set alignedValues(aFlag) { 1.958 + this._alignedValues = aFlag; 1.959 + if (aFlag) { 1.960 + this._parent.setAttribute("aligned-values", ""); 1.961 + } else { 1.962 + this._parent.removeAttribute("aligned-values"); 1.963 + } 1.964 + }, 1.965 + 1.966 + /** 1.967 + * Gets if action buttons (like delete) should be placed at the beginning or 1.968 + * end of a line. 1.969 + * @return boolean 1.970 + */ 1.971 + get actionsFirst() { 1.972 + return this._actionsFirst; 1.973 + }, 1.974 + 1.975 + /** 1.976 + * Sets if action buttons (like delete) should be placed at the beginning or 1.977 + * end of a line. 1.978 + * @param boolean aFlag 1.979 + */ 1.980 + set actionsFirst(aFlag) { 1.981 + this._actionsFirst = aFlag; 1.982 + if (aFlag) { 1.983 + this._parent.setAttribute("actions-first", ""); 1.984 + } else { 1.985 + this._parent.removeAttribute("actions-first"); 1.986 + } 1.987 + }, 1.988 + 1.989 + /** 1.990 + * Gets the parent node holding this view. 1.991 + * @return nsIDOMNode 1.992 + */ 1.993 + get boxObject() this._list.boxObject.QueryInterface(Ci.nsIScrollBoxObject), 1.994 + 1.995 + /** 1.996 + * Gets the parent node holding this view. 1.997 + * @return nsIDOMNode 1.998 + */ 1.999 + get parentNode() this._parent, 1.1000 + 1.1001 + /** 1.1002 + * Gets the owner document holding this view. 1.1003 + * @return nsIHTMLDocument 1.1004 + */ 1.1005 + get document() this._document || (this._document = this._parent.ownerDocument), 1.1006 + 1.1007 + /** 1.1008 + * Gets the default window holding this view. 1.1009 + * @return nsIDOMWindow 1.1010 + */ 1.1011 + get window() this._window || (this._window = this.document.defaultView), 1.1012 + 1.1013 + _document: null, 1.1014 + _window: null, 1.1015 + 1.1016 + _store: null, 1.1017 + _itemsByElement: null, 1.1018 + _prevHierarchy: null, 1.1019 + _currHierarchy: null, 1.1020 + 1.1021 + _enumVisible: true, 1.1022 + _nonEnumVisible: true, 1.1023 + _alignedValues: false, 1.1024 + _actionsFirst: false, 1.1025 + 1.1026 + _parent: null, 1.1027 + _list: null, 1.1028 + _searchboxNode: null, 1.1029 + _searchboxContainer: null, 1.1030 + _searchboxPlaceholder: "", 1.1031 + _emptyTextNode: null, 1.1032 + _emptyTextValue: "" 1.1033 +}; 1.1034 + 1.1035 +VariablesView.NON_SORTABLE_CLASSES = [ 1.1036 + "Array", 1.1037 + "Int8Array", 1.1038 + "Uint8Array", 1.1039 + "Uint8ClampedArray", 1.1040 + "Int16Array", 1.1041 + "Uint16Array", 1.1042 + "Int32Array", 1.1043 + "Uint32Array", 1.1044 + "Float32Array", 1.1045 + "Float64Array" 1.1046 +]; 1.1047 + 1.1048 +/** 1.1049 + * Determine whether an object's properties should be sorted based on its class. 1.1050 + * 1.1051 + * @param string aClassName 1.1052 + * The class of the object. 1.1053 + */ 1.1054 +VariablesView.isSortable = function(aClassName) { 1.1055 + return VariablesView.NON_SORTABLE_CLASSES.indexOf(aClassName) == -1; 1.1056 +}; 1.1057 + 1.1058 +/** 1.1059 + * Generates the string evaluated when performing simple value changes. 1.1060 + * 1.1061 + * @param Variable | Property aItem 1.1062 + * The current variable or property. 1.1063 + * @param string aCurrentString 1.1064 + * The trimmed user inputted string. 1.1065 + * @param string aPrefix [optional] 1.1066 + * Prefix for the symbolic name. 1.1067 + * @return string 1.1068 + * The string to be evaluated. 1.1069 + */ 1.1070 +VariablesView.simpleValueEvalMacro = function(aItem, aCurrentString, aPrefix = "") { 1.1071 + return aPrefix + aItem._symbolicName + "=" + aCurrentString; 1.1072 +}; 1.1073 + 1.1074 +/** 1.1075 + * Generates the string evaluated when overriding getters and setters with 1.1076 + * plain values. 1.1077 + * 1.1078 + * @param Property aItem 1.1079 + * The current getter or setter property. 1.1080 + * @param string aCurrentString 1.1081 + * The trimmed user inputted string. 1.1082 + * @param string aPrefix [optional] 1.1083 + * Prefix for the symbolic name. 1.1084 + * @return string 1.1085 + * The string to be evaluated. 1.1086 + */ 1.1087 +VariablesView.overrideValueEvalMacro = function(aItem, aCurrentString, aPrefix = "") { 1.1088 + let property = "\"" + aItem._nameString + "\""; 1.1089 + let parent = aPrefix + aItem.ownerView._symbolicName || "this"; 1.1090 + 1.1091 + return "Object.defineProperty(" + parent + "," + property + "," + 1.1092 + "{ value: " + aCurrentString + 1.1093 + ", enumerable: " + parent + ".propertyIsEnumerable(" + property + ")" + 1.1094 + ", configurable: true" + 1.1095 + ", writable: true" + 1.1096 + "})"; 1.1097 +}; 1.1098 + 1.1099 +/** 1.1100 + * Generates the string evaluated when performing getters and setters changes. 1.1101 + * 1.1102 + * @param Property aItem 1.1103 + * The current getter or setter property. 1.1104 + * @param string aCurrentString 1.1105 + * The trimmed user inputted string. 1.1106 + * @param string aPrefix [optional] 1.1107 + * Prefix for the symbolic name. 1.1108 + * @return string 1.1109 + * The string to be evaluated. 1.1110 + */ 1.1111 +VariablesView.getterOrSetterEvalMacro = function(aItem, aCurrentString, aPrefix = "") { 1.1112 + let type = aItem._nameString; 1.1113 + let propertyObject = aItem.ownerView; 1.1114 + let parentObject = propertyObject.ownerView; 1.1115 + let property = "\"" + propertyObject._nameString + "\""; 1.1116 + let parent = aPrefix + parentObject._symbolicName || "this"; 1.1117 + 1.1118 + switch (aCurrentString) { 1.1119 + case "": 1.1120 + case "null": 1.1121 + case "undefined": 1.1122 + let mirrorType = type == "get" ? "set" : "get"; 1.1123 + let mirrorLookup = type == "get" ? "__lookupSetter__" : "__lookupGetter__"; 1.1124 + 1.1125 + // If the parent object will end up without any getter or setter, 1.1126 + // morph it into a plain value. 1.1127 + if ((type == "set" && propertyObject.getter.type == "undefined") || 1.1128 + (type == "get" && propertyObject.setter.type == "undefined")) { 1.1129 + // Make sure the right getter/setter to value override macro is applied 1.1130 + // to the target object. 1.1131 + return propertyObject.evaluationMacro(propertyObject, "undefined", aPrefix); 1.1132 + } 1.1133 + 1.1134 + // Construct and return the getter/setter removal evaluation string. 1.1135 + // e.g: Object.defineProperty(foo, "bar", { 1.1136 + // get: foo.__lookupGetter__("bar"), 1.1137 + // set: undefined, 1.1138 + // enumerable: true, 1.1139 + // configurable: true 1.1140 + // }) 1.1141 + return "Object.defineProperty(" + parent + "," + property + "," + 1.1142 + "{" + mirrorType + ":" + parent + "." + mirrorLookup + "(" + property + ")" + 1.1143 + "," + type + ":" + undefined + 1.1144 + ", enumerable: " + parent + ".propertyIsEnumerable(" + property + ")" + 1.1145 + ", configurable: true" + 1.1146 + "})"; 1.1147 + 1.1148 + default: 1.1149 + // Wrap statements inside a function declaration if not already wrapped. 1.1150 + if (!aCurrentString.startsWith("function")) { 1.1151 + let header = "function(" + (type == "set" ? "value" : "") + ")"; 1.1152 + let body = ""; 1.1153 + // If there's a return statement explicitly written, always use the 1.1154 + // standard function definition syntax 1.1155 + if (aCurrentString.contains("return ")) { 1.1156 + body = "{" + aCurrentString + "}"; 1.1157 + } 1.1158 + // If block syntax is used, use the whole string as the function body. 1.1159 + else if (aCurrentString.startsWith("{")) { 1.1160 + body = aCurrentString; 1.1161 + } 1.1162 + // Prefer an expression closure. 1.1163 + else { 1.1164 + body = "(" + aCurrentString + ")"; 1.1165 + } 1.1166 + aCurrentString = header + body; 1.1167 + } 1.1168 + 1.1169 + // Determine if a new getter or setter should be defined. 1.1170 + let defineType = type == "get" ? "__defineGetter__" : "__defineSetter__"; 1.1171 + 1.1172 + // Make sure all quotes are escaped in the expression's syntax, 1.1173 + let defineFunc = "eval(\"(" + aCurrentString.replace(/"/g, "\\$&") + ")\")"; 1.1174 + 1.1175 + // Construct and return the getter/setter evaluation string. 1.1176 + // e.g: foo.__defineGetter__("bar", eval("(function() { return 42; })")) 1.1177 + return parent + "." + defineType + "(" + property + "," + defineFunc + ")"; 1.1178 + } 1.1179 +}; 1.1180 + 1.1181 +/** 1.1182 + * Function invoked when a getter or setter is deleted. 1.1183 + * 1.1184 + * @param Property aItem 1.1185 + * The current getter or setter property. 1.1186 + */ 1.1187 +VariablesView.getterOrSetterDeleteCallback = function(aItem) { 1.1188 + aItem._disable(); 1.1189 + 1.1190 + // Make sure the right getter/setter to value override macro is applied 1.1191 + // to the target object. 1.1192 + aItem.ownerView.eval(aItem, ""); 1.1193 + 1.1194 + return true; // Don't hide the element. 1.1195 +}; 1.1196 + 1.1197 + 1.1198 +/** 1.1199 + * A Scope is an object holding Variable instances. 1.1200 + * Iterable via "for (let [name, variable] of instance) { }". 1.1201 + * 1.1202 + * @param VariablesView aView 1.1203 + * The view to contain this scope. 1.1204 + * @param string aName 1.1205 + * The scope's name. 1.1206 + * @param object aFlags [optional] 1.1207 + * Additional options or flags for this scope. 1.1208 + */ 1.1209 +function Scope(aView, aName, aFlags = {}) { 1.1210 + this.ownerView = aView; 1.1211 + 1.1212 + this._onClick = this._onClick.bind(this); 1.1213 + this._openEnum = this._openEnum.bind(this); 1.1214 + this._openNonEnum = this._openNonEnum.bind(this); 1.1215 + 1.1216 + // Inherit properties and flags from the parent view. You can override 1.1217 + // each of these directly onto any scope, variable or property instance. 1.1218 + this.scrollPageSize = aView.scrollPageSize; 1.1219 + this.appendPageSize = aView.appendPageSize; 1.1220 + this.eval = aView.eval; 1.1221 + this.switch = aView.switch; 1.1222 + this.delete = aView.delete; 1.1223 + this.new = aView.new; 1.1224 + this.preventDisableOnChange = aView.preventDisableOnChange; 1.1225 + this.preventDescriptorModifiers = aView.preventDescriptorModifiers; 1.1226 + this.editableNameTooltip = aView.editableNameTooltip; 1.1227 + this.editableValueTooltip = aView.editableValueTooltip; 1.1228 + this.editButtonTooltip = aView.editButtonTooltip; 1.1229 + this.deleteButtonTooltip = aView.deleteButtonTooltip; 1.1230 + this.domNodeValueTooltip = aView.domNodeValueTooltip; 1.1231 + this.contextMenuId = aView.contextMenuId; 1.1232 + this.separatorStr = aView.separatorStr; 1.1233 + 1.1234 + this._init(aName.trim(), aFlags); 1.1235 +} 1.1236 + 1.1237 +Scope.prototype = { 1.1238 + /** 1.1239 + * Whether this Scope should be prefetched when it is remoted. 1.1240 + */ 1.1241 + shouldPrefetch: true, 1.1242 + 1.1243 + /** 1.1244 + * Whether this Scope should paginate its contents. 1.1245 + */ 1.1246 + allowPaginate: false, 1.1247 + 1.1248 + /** 1.1249 + * The class name applied to this scope's target element. 1.1250 + */ 1.1251 + targetClassName: "variables-view-scope", 1.1252 + 1.1253 + /** 1.1254 + * Create a new Variable that is a child of this Scope. 1.1255 + * 1.1256 + * @param string aName 1.1257 + * The name of the new Property. 1.1258 + * @param object aDescriptor 1.1259 + * The variable's descriptor. 1.1260 + * @return Variable 1.1261 + * The newly created child Variable. 1.1262 + */ 1.1263 + _createChild: function(aName, aDescriptor) { 1.1264 + return new Variable(this, aName, aDescriptor); 1.1265 + }, 1.1266 + 1.1267 + /** 1.1268 + * Adds a child to contain any inspected properties. 1.1269 + * 1.1270 + * @param string aName 1.1271 + * The child's name. 1.1272 + * @param object aDescriptor 1.1273 + * Specifies the value and/or type & class of the child, 1.1274 + * or 'get' & 'set' accessor properties. If the type is implicit, 1.1275 + * it will be inferred from the value. If this parameter is omitted, 1.1276 + * a property without a value will be added (useful for branch nodes). 1.1277 + * e.g. - { value: 42 } 1.1278 + * - { value: true } 1.1279 + * - { value: "nasu" } 1.1280 + * - { value: { type: "undefined" } } 1.1281 + * - { value: { type: "null" } } 1.1282 + * - { value: { type: "object", class: "Object" } } 1.1283 + * - { get: { type: "object", class: "Function" }, 1.1284 + * set: { type: "undefined" } } 1.1285 + * @param boolean aRelaxed [optional] 1.1286 + * Pass true if name duplicates should be allowed. 1.1287 + * You probably shouldn't do it. Use this with caution. 1.1288 + * @return Variable 1.1289 + * The newly created Variable instance, null if it already exists. 1.1290 + */ 1.1291 + addItem: function(aName = "", aDescriptor = {}, aRelaxed = false) { 1.1292 + if (this._store.has(aName) && !aRelaxed) { 1.1293 + return null; 1.1294 + } 1.1295 + 1.1296 + let child = this._createChild(aName, aDescriptor); 1.1297 + this._store.set(aName, child); 1.1298 + this._variablesView._itemsByElement.set(child._target, child); 1.1299 + this._variablesView._currHierarchy.set(child._absoluteName, child); 1.1300 + child.header = !!aName; 1.1301 + 1.1302 + return child; 1.1303 + }, 1.1304 + 1.1305 + /** 1.1306 + * Adds items for this variable. 1.1307 + * 1.1308 + * @param object aItems 1.1309 + * An object containing some { name: descriptor } data properties, 1.1310 + * specifying the value and/or type & class of the variable, 1.1311 + * or 'get' & 'set' accessor properties. If the type is implicit, 1.1312 + * it will be inferred from the value. 1.1313 + * e.g. - { someProp0: { value: 42 }, 1.1314 + * someProp1: { value: true }, 1.1315 + * someProp2: { value: "nasu" }, 1.1316 + * someProp3: { value: { type: "undefined" } }, 1.1317 + * someProp4: { value: { type: "null" } }, 1.1318 + * someProp5: { value: { type: "object", class: "Object" } }, 1.1319 + * someProp6: { get: { type: "object", class: "Function" }, 1.1320 + * set: { type: "undefined" } } } 1.1321 + * @param object aOptions [optional] 1.1322 + * Additional options for adding the properties. Supported options: 1.1323 + * - sorted: true to sort all the properties before adding them 1.1324 + * - callback: function invoked after each item is added 1.1325 + * @param string aKeysType [optional] 1.1326 + * Helper argument in the case of paginated items. Can be either 1.1327 + * "just-strings" or "just-numbers". Humans shouldn't use this argument. 1.1328 + */ 1.1329 + addItems: function(aItems, aOptions = {}, aKeysType = "") { 1.1330 + let names = Object.keys(aItems); 1.1331 + 1.1332 + // Building the view when inspecting an object with a very large number of 1.1333 + // properties may take a long time. To avoid blocking the UI, group 1.1334 + // the items into several lazily populated pseudo-items. 1.1335 + let exceedsThreshold = names.length >= this.appendPageSize; 1.1336 + let shouldPaginate = exceedsThreshold && aKeysType != "just-strings"; 1.1337 + if (shouldPaginate && this.allowPaginate) { 1.1338 + // Group the items to append into two separate arrays, one containing 1.1339 + // number-like keys, the other one containing string keys. 1.1340 + if (aKeysType == "just-numbers") { 1.1341 + var numberKeys = names; 1.1342 + var stringKeys = []; 1.1343 + } else { 1.1344 + var numberKeys = []; 1.1345 + var stringKeys = []; 1.1346 + for (let name of names) { 1.1347 + // Be very careful. Avoid Infinity, NaN and non Natural number keys. 1.1348 + let coerced = +name; 1.1349 + if (Number.isInteger(coerced) && coerced > -1) { 1.1350 + numberKeys.push(name); 1.1351 + } else { 1.1352 + stringKeys.push(name); 1.1353 + } 1.1354 + } 1.1355 + } 1.1356 + 1.1357 + // This object contains a very large number of properties, but they're 1.1358 + // almost all strings that can't be coerced to numbers. Don't paginate. 1.1359 + if (numberKeys.length < this.appendPageSize) { 1.1360 + this.addItems(aItems, aOptions, "just-strings"); 1.1361 + return; 1.1362 + } 1.1363 + 1.1364 + // Slices a section of the { name: descriptor } data properties. 1.1365 + let paginate = (aArray, aBegin = 0, aEnd = aArray.length) => { 1.1366 + let store = {} 1.1367 + for (let i = aBegin; i < aEnd; i++) { 1.1368 + let name = aArray[i]; 1.1369 + store[name] = aItems[name]; 1.1370 + } 1.1371 + return store; 1.1372 + }; 1.1373 + 1.1374 + // Creates a pseudo-item that populates itself with the data properties 1.1375 + // from the corresponding page range. 1.1376 + let createRangeExpander = (aArray, aBegin, aEnd, aOptions, aKeyTypes) => { 1.1377 + let rangeVar = this.addItem(aArray[aBegin] + Scope.ellipsis + aArray[aEnd - 1]); 1.1378 + rangeVar.onexpand = () => { 1.1379 + let pageItems = paginate(aArray, aBegin, aEnd); 1.1380 + rangeVar.addItems(pageItems, aOptions, aKeyTypes); 1.1381 + } 1.1382 + rangeVar.showArrow(); 1.1383 + rangeVar.target.setAttribute("pseudo-item", ""); 1.1384 + }; 1.1385 + 1.1386 + // Divide the number keys into quarters. 1.1387 + let page = +Math.round(numberKeys.length / 4).toPrecision(1); 1.1388 + createRangeExpander(numberKeys, 0, page, aOptions, "just-numbers"); 1.1389 + createRangeExpander(numberKeys, page, page * 2, aOptions, "just-numbers"); 1.1390 + createRangeExpander(numberKeys, page * 2, page * 3, aOptions, "just-numbers"); 1.1391 + createRangeExpander(numberKeys, page * 3, numberKeys.length, aOptions, "just-numbers"); 1.1392 + 1.1393 + // Append all the string keys together. 1.1394 + this.addItems(paginate(stringKeys), aOptions, "just-strings"); 1.1395 + return; 1.1396 + } 1.1397 + 1.1398 + // Sort all of the properties before adding them, if preferred. 1.1399 + if (aOptions.sorted && aKeysType != "just-numbers") { 1.1400 + names.sort(); 1.1401 + } 1.1402 + 1.1403 + // Add the properties to the current scope. 1.1404 + for (let name of names) { 1.1405 + let descriptor = aItems[name]; 1.1406 + let item = this.addItem(name, descriptor); 1.1407 + 1.1408 + if (aOptions.callback) { 1.1409 + aOptions.callback(item, descriptor.value); 1.1410 + } 1.1411 + } 1.1412 + }, 1.1413 + 1.1414 + /** 1.1415 + * Remove this Scope from its parent and remove all children recursively. 1.1416 + */ 1.1417 + remove: function() { 1.1418 + let view = this._variablesView; 1.1419 + view._store.splice(view._store.indexOf(this), 1); 1.1420 + view._itemsByElement.delete(this._target); 1.1421 + view._currHierarchy.delete(this._nameString); 1.1422 + 1.1423 + this._target.remove(); 1.1424 + 1.1425 + for (let variable of this._store.values()) { 1.1426 + variable.remove(); 1.1427 + } 1.1428 + }, 1.1429 + 1.1430 + /** 1.1431 + * Gets the variable in this container having the specified name. 1.1432 + * 1.1433 + * @param string aName 1.1434 + * The name of the variable to get. 1.1435 + * @return Variable 1.1436 + * The matched variable, or null if nothing is found. 1.1437 + */ 1.1438 + get: function(aName) { 1.1439 + return this._store.get(aName); 1.1440 + }, 1.1441 + 1.1442 + /** 1.1443 + * Recursively searches for the variable or property in this container 1.1444 + * displayed by the specified node. 1.1445 + * 1.1446 + * @param nsIDOMNode aNode 1.1447 + * The node to search for. 1.1448 + * @return Variable | Property 1.1449 + * The matched variable or property, or null if nothing is found. 1.1450 + */ 1.1451 + find: function(aNode) { 1.1452 + for (let [, variable] of this._store) { 1.1453 + let match; 1.1454 + if (variable._target == aNode) { 1.1455 + match = variable; 1.1456 + } else { 1.1457 + match = variable.find(aNode); 1.1458 + } 1.1459 + if (match) { 1.1460 + return match; 1.1461 + } 1.1462 + } 1.1463 + return null; 1.1464 + }, 1.1465 + 1.1466 + /** 1.1467 + * Determines if this scope is a direct child of a parent variables view, 1.1468 + * scope, variable or property. 1.1469 + * 1.1470 + * @param VariablesView | Scope | Variable | Property 1.1471 + * The parent to check. 1.1472 + * @return boolean 1.1473 + * True if the specified item is a direct child, false otherwise. 1.1474 + */ 1.1475 + isChildOf: function(aParent) { 1.1476 + return this.ownerView == aParent; 1.1477 + }, 1.1478 + 1.1479 + /** 1.1480 + * Determines if this scope is a descendant of a parent variables view, 1.1481 + * scope, variable or property. 1.1482 + * 1.1483 + * @param VariablesView | Scope | Variable | Property 1.1484 + * The parent to check. 1.1485 + * @return boolean 1.1486 + * True if the specified item is a descendant, false otherwise. 1.1487 + */ 1.1488 + isDescendantOf: function(aParent) { 1.1489 + if (this.isChildOf(aParent)) { 1.1490 + return true; 1.1491 + } 1.1492 + 1.1493 + // Recurse to parent if it is a Scope, Variable, or Property. 1.1494 + if (this.ownerView instanceof Scope) { 1.1495 + return this.ownerView.isDescendantOf(aParent); 1.1496 + } 1.1497 + 1.1498 + return false; 1.1499 + }, 1.1500 + 1.1501 + /** 1.1502 + * Shows the scope. 1.1503 + */ 1.1504 + show: function() { 1.1505 + this._target.hidden = false; 1.1506 + this._isContentVisible = true; 1.1507 + 1.1508 + if (this.onshow) { 1.1509 + this.onshow(this); 1.1510 + } 1.1511 + }, 1.1512 + 1.1513 + /** 1.1514 + * Hides the scope. 1.1515 + */ 1.1516 + hide: function() { 1.1517 + this._target.hidden = true; 1.1518 + this._isContentVisible = false; 1.1519 + 1.1520 + if (this.onhide) { 1.1521 + this.onhide(this); 1.1522 + } 1.1523 + }, 1.1524 + 1.1525 + /** 1.1526 + * Expands the scope, showing all the added details. 1.1527 + */ 1.1528 + expand: function() { 1.1529 + if (this._isExpanded || this._isLocked) { 1.1530 + return; 1.1531 + } 1.1532 + if (this._variablesView._enumVisible) { 1.1533 + this._openEnum(); 1.1534 + } 1.1535 + if (this._variablesView._nonEnumVisible) { 1.1536 + Services.tm.currentThread.dispatch({ run: this._openNonEnum }, 0); 1.1537 + } 1.1538 + this._isExpanded = true; 1.1539 + 1.1540 + if (this.onexpand) { 1.1541 + this.onexpand(this); 1.1542 + } 1.1543 + }, 1.1544 + 1.1545 + /** 1.1546 + * Collapses the scope, hiding all the added details. 1.1547 + */ 1.1548 + collapse: function() { 1.1549 + if (!this._isExpanded || this._isLocked) { 1.1550 + return; 1.1551 + } 1.1552 + this._arrow.removeAttribute("open"); 1.1553 + this._enum.removeAttribute("open"); 1.1554 + this._nonenum.removeAttribute("open"); 1.1555 + this._isExpanded = false; 1.1556 + 1.1557 + if (this.oncollapse) { 1.1558 + this.oncollapse(this); 1.1559 + } 1.1560 + }, 1.1561 + 1.1562 + /** 1.1563 + * Toggles between the scope's collapsed and expanded state. 1.1564 + */ 1.1565 + toggle: function(e) { 1.1566 + if (e && e.button != 0) { 1.1567 + // Only allow left-click to trigger this event. 1.1568 + return; 1.1569 + } 1.1570 + this.expanded ^= 1; 1.1571 + 1.1572 + // Make sure the scope and its contents are visibile. 1.1573 + for (let [, variable] of this._store) { 1.1574 + variable.header = true; 1.1575 + variable._matched = true; 1.1576 + } 1.1577 + if (this.ontoggle) { 1.1578 + this.ontoggle(this); 1.1579 + } 1.1580 + }, 1.1581 + 1.1582 + /** 1.1583 + * Shows the scope's title header. 1.1584 + */ 1.1585 + showHeader: function() { 1.1586 + if (this._isHeaderVisible || !this._nameString) { 1.1587 + return; 1.1588 + } 1.1589 + this._target.removeAttribute("untitled"); 1.1590 + this._isHeaderVisible = true; 1.1591 + }, 1.1592 + 1.1593 + /** 1.1594 + * Hides the scope's title header. 1.1595 + * This action will automatically expand the scope. 1.1596 + */ 1.1597 + hideHeader: function() { 1.1598 + if (!this._isHeaderVisible) { 1.1599 + return; 1.1600 + } 1.1601 + this.expand(); 1.1602 + this._target.setAttribute("untitled", ""); 1.1603 + this._isHeaderVisible = false; 1.1604 + }, 1.1605 + 1.1606 + /** 1.1607 + * Shows the scope's expand/collapse arrow. 1.1608 + */ 1.1609 + showArrow: function() { 1.1610 + if (this._isArrowVisible) { 1.1611 + return; 1.1612 + } 1.1613 + this._arrow.removeAttribute("invisible"); 1.1614 + this._isArrowVisible = true; 1.1615 + }, 1.1616 + 1.1617 + /** 1.1618 + * Hides the scope's expand/collapse arrow. 1.1619 + */ 1.1620 + hideArrow: function() { 1.1621 + if (!this._isArrowVisible) { 1.1622 + return; 1.1623 + } 1.1624 + this._arrow.setAttribute("invisible", ""); 1.1625 + this._isArrowVisible = false; 1.1626 + }, 1.1627 + 1.1628 + /** 1.1629 + * Gets the visibility state. 1.1630 + * @return boolean 1.1631 + */ 1.1632 + get visible() this._isContentVisible, 1.1633 + 1.1634 + /** 1.1635 + * Gets the expanded state. 1.1636 + * @return boolean 1.1637 + */ 1.1638 + get expanded() this._isExpanded, 1.1639 + 1.1640 + /** 1.1641 + * Gets the header visibility state. 1.1642 + * @return boolean 1.1643 + */ 1.1644 + get header() this._isHeaderVisible, 1.1645 + 1.1646 + /** 1.1647 + * Gets the twisty visibility state. 1.1648 + * @return boolean 1.1649 + */ 1.1650 + get twisty() this._isArrowVisible, 1.1651 + 1.1652 + /** 1.1653 + * Gets the expand lock state. 1.1654 + * @return boolean 1.1655 + */ 1.1656 + get locked() this._isLocked, 1.1657 + 1.1658 + /** 1.1659 + * Sets the visibility state. 1.1660 + * @param boolean aFlag 1.1661 + */ 1.1662 + set visible(aFlag) aFlag ? this.show() : this.hide(), 1.1663 + 1.1664 + /** 1.1665 + * Sets the expanded state. 1.1666 + * @param boolean aFlag 1.1667 + */ 1.1668 + set expanded(aFlag) aFlag ? this.expand() : this.collapse(), 1.1669 + 1.1670 + /** 1.1671 + * Sets the header visibility state. 1.1672 + * @param boolean aFlag 1.1673 + */ 1.1674 + set header(aFlag) aFlag ? this.showHeader() : this.hideHeader(), 1.1675 + 1.1676 + /** 1.1677 + * Sets the twisty visibility state. 1.1678 + * @param boolean aFlag 1.1679 + */ 1.1680 + set twisty(aFlag) aFlag ? this.showArrow() : this.hideArrow(), 1.1681 + 1.1682 + /** 1.1683 + * Sets the expand lock state. 1.1684 + * @param boolean aFlag 1.1685 + */ 1.1686 + set locked(aFlag) this._isLocked = aFlag, 1.1687 + 1.1688 + /** 1.1689 + * Specifies if this target node may be focused. 1.1690 + * @return boolean 1.1691 + */ 1.1692 + get focusable() { 1.1693 + // Check if this target node is actually visibile. 1.1694 + if (!this._nameString || 1.1695 + !this._isContentVisible || 1.1696 + !this._isHeaderVisible || 1.1697 + !this._isMatch) { 1.1698 + return false; 1.1699 + } 1.1700 + // Check if all parent objects are expanded. 1.1701 + let item = this; 1.1702 + 1.1703 + // Recurse while parent is a Scope, Variable, or Property 1.1704 + while ((item = item.ownerView) && item instanceof Scope) { 1.1705 + if (!item._isExpanded) { 1.1706 + return false; 1.1707 + } 1.1708 + } 1.1709 + return true; 1.1710 + }, 1.1711 + 1.1712 + /** 1.1713 + * Focus this scope. 1.1714 + */ 1.1715 + focus: function() { 1.1716 + this._variablesView._focusItem(this); 1.1717 + }, 1.1718 + 1.1719 + /** 1.1720 + * Adds an event listener for a certain event on this scope's title. 1.1721 + * @param string aName 1.1722 + * @param function aCallback 1.1723 + * @param boolean aCapture 1.1724 + */ 1.1725 + addEventListener: function(aName, aCallback, aCapture) { 1.1726 + this._title.addEventListener(aName, aCallback, aCapture); 1.1727 + }, 1.1728 + 1.1729 + /** 1.1730 + * Removes an event listener for a certain event on this scope's title. 1.1731 + * @param string aName 1.1732 + * @param function aCallback 1.1733 + * @param boolean aCapture 1.1734 + */ 1.1735 + removeEventListener: function(aName, aCallback, aCapture) { 1.1736 + this._title.removeEventListener(aName, aCallback, aCapture); 1.1737 + }, 1.1738 + 1.1739 + /** 1.1740 + * Gets the id associated with this item. 1.1741 + * @return string 1.1742 + */ 1.1743 + get id() this._idString, 1.1744 + 1.1745 + /** 1.1746 + * Gets the name associated with this item. 1.1747 + * @return string 1.1748 + */ 1.1749 + get name() this._nameString, 1.1750 + 1.1751 + /** 1.1752 + * Gets the displayed value for this item. 1.1753 + * @return string 1.1754 + */ 1.1755 + get displayValue() this._valueString, 1.1756 + 1.1757 + /** 1.1758 + * Gets the class names used for the displayed value. 1.1759 + * @return string 1.1760 + */ 1.1761 + get displayValueClassName() this._valueClassName, 1.1762 + 1.1763 + /** 1.1764 + * Gets the element associated with this item. 1.1765 + * @return nsIDOMNode 1.1766 + */ 1.1767 + get target() this._target, 1.1768 + 1.1769 + /** 1.1770 + * Initializes this scope's id, view and binds event listeners. 1.1771 + * 1.1772 + * @param string aName 1.1773 + * The scope's name. 1.1774 + * @param object aFlags [optional] 1.1775 + * Additional options or flags for this scope. 1.1776 + */ 1.1777 + _init: function(aName, aFlags) { 1.1778 + this._idString = generateId(this._nameString = aName); 1.1779 + this._displayScope(aName, this.targetClassName, "devtools-toolbar"); 1.1780 + this._addEventListeners(); 1.1781 + this.parentNode.appendChild(this._target); 1.1782 + }, 1.1783 + 1.1784 + /** 1.1785 + * Creates the necessary nodes for this scope. 1.1786 + * 1.1787 + * @param string aName 1.1788 + * The scope's name. 1.1789 + * @param string aTargetClassName 1.1790 + * A custom class name for this scope's target element. 1.1791 + * @param string aTitleClassName [optional] 1.1792 + * A custom class name for this scope's title element. 1.1793 + */ 1.1794 + _displayScope: function(aName, aTargetClassName, aTitleClassName = "") { 1.1795 + let document = this.document; 1.1796 + 1.1797 + let element = this._target = document.createElement("vbox"); 1.1798 + element.id = this._idString; 1.1799 + element.className = aTargetClassName; 1.1800 + 1.1801 + let arrow = this._arrow = document.createElement("hbox"); 1.1802 + arrow.className = "arrow"; 1.1803 + 1.1804 + let name = this._name = document.createElement("label"); 1.1805 + name.className = "plain name"; 1.1806 + name.setAttribute("value", aName); 1.1807 + 1.1808 + let title = this._title = document.createElement("hbox"); 1.1809 + title.className = "title " + aTitleClassName; 1.1810 + title.setAttribute("align", "center"); 1.1811 + 1.1812 + let enumerable = this._enum = document.createElement("vbox"); 1.1813 + let nonenum = this._nonenum = document.createElement("vbox"); 1.1814 + enumerable.className = "variables-view-element-details enum"; 1.1815 + nonenum.className = "variables-view-element-details nonenum"; 1.1816 + 1.1817 + title.appendChild(arrow); 1.1818 + title.appendChild(name); 1.1819 + 1.1820 + element.appendChild(title); 1.1821 + element.appendChild(enumerable); 1.1822 + element.appendChild(nonenum); 1.1823 + }, 1.1824 + 1.1825 + /** 1.1826 + * Adds the necessary event listeners for this scope. 1.1827 + */ 1.1828 + _addEventListeners: function() { 1.1829 + this._title.addEventListener("mousedown", this._onClick, false); 1.1830 + }, 1.1831 + 1.1832 + /** 1.1833 + * The click listener for this scope's title. 1.1834 + */ 1.1835 + _onClick: function(e) { 1.1836 + if (this.editing || 1.1837 + e.button != 0 || 1.1838 + e.target == this._editNode || 1.1839 + e.target == this._deleteNode || 1.1840 + e.target == this._addPropertyNode) { 1.1841 + return; 1.1842 + } 1.1843 + this.toggle(); 1.1844 + this.focus(); 1.1845 + }, 1.1846 + 1.1847 + /** 1.1848 + * Opens the enumerable items container. 1.1849 + */ 1.1850 + _openEnum: function() { 1.1851 + this._arrow.setAttribute("open", ""); 1.1852 + this._enum.setAttribute("open", ""); 1.1853 + }, 1.1854 + 1.1855 + /** 1.1856 + * Opens the non-enumerable items container. 1.1857 + */ 1.1858 + _openNonEnum: function() { 1.1859 + this._nonenum.setAttribute("open", ""); 1.1860 + }, 1.1861 + 1.1862 + /** 1.1863 + * Specifies if enumerable properties and variables should be displayed. 1.1864 + * @param boolean aFlag 1.1865 + */ 1.1866 + set _enumVisible(aFlag) { 1.1867 + for (let [, variable] of this._store) { 1.1868 + variable._enumVisible = aFlag; 1.1869 + 1.1870 + if (!this._isExpanded) { 1.1871 + continue; 1.1872 + } 1.1873 + if (aFlag) { 1.1874 + this._enum.setAttribute("open", ""); 1.1875 + } else { 1.1876 + this._enum.removeAttribute("open"); 1.1877 + } 1.1878 + } 1.1879 + }, 1.1880 + 1.1881 + /** 1.1882 + * Specifies if non-enumerable properties and variables should be displayed. 1.1883 + * @param boolean aFlag 1.1884 + */ 1.1885 + set _nonEnumVisible(aFlag) { 1.1886 + for (let [, variable] of this._store) { 1.1887 + variable._nonEnumVisible = aFlag; 1.1888 + 1.1889 + if (!this._isExpanded) { 1.1890 + continue; 1.1891 + } 1.1892 + if (aFlag) { 1.1893 + this._nonenum.setAttribute("open", ""); 1.1894 + } else { 1.1895 + this._nonenum.removeAttribute("open"); 1.1896 + } 1.1897 + } 1.1898 + }, 1.1899 + 1.1900 + /** 1.1901 + * Performs a case insensitive search for variables or properties matching 1.1902 + * the query, and hides non-matched items. 1.1903 + * 1.1904 + * @param string aLowerCaseQuery 1.1905 + * The lowercased name of the variable or property to search for. 1.1906 + */ 1.1907 + _performSearch: function(aLowerCaseQuery) { 1.1908 + for (let [, variable] of this._store) { 1.1909 + let currentObject = variable; 1.1910 + let lowerCaseName = variable._nameString.toLowerCase(); 1.1911 + let lowerCaseValue = variable._valueString.toLowerCase(); 1.1912 + 1.1913 + // Non-matched variables or properties require a corresponding attribute. 1.1914 + if (!lowerCaseName.contains(aLowerCaseQuery) && 1.1915 + !lowerCaseValue.contains(aLowerCaseQuery)) { 1.1916 + variable._matched = false; 1.1917 + } 1.1918 + // Variable or property is matched. 1.1919 + else { 1.1920 + variable._matched = true; 1.1921 + 1.1922 + // If the variable was ever expanded, there's a possibility it may 1.1923 + // contain some matched properties, so make sure they're visible 1.1924 + // ("expand downwards"). 1.1925 + if (variable._store.size) { 1.1926 + variable.expand(); 1.1927 + } 1.1928 + 1.1929 + // If the variable is contained in another Scope, Variable, or Property, 1.1930 + // the parent may not be a match, thus hidden. It should be visible 1.1931 + // ("expand upwards"). 1.1932 + while ((variable = variable.ownerView) && variable instanceof Scope) { 1.1933 + variable._matched = true; 1.1934 + variable.expand(); 1.1935 + } 1.1936 + } 1.1937 + 1.1938 + // Proceed with the search recursively inside this variable or property. 1.1939 + if (currentObject._store.size || currentObject.getter || currentObject.setter) { 1.1940 + currentObject._performSearch(aLowerCaseQuery); 1.1941 + } 1.1942 + } 1.1943 + }, 1.1944 + 1.1945 + /** 1.1946 + * Sets if this object instance is a matched or non-matched item. 1.1947 + * @param boolean aStatus 1.1948 + */ 1.1949 + set _matched(aStatus) { 1.1950 + if (this._isMatch == aStatus) { 1.1951 + return; 1.1952 + } 1.1953 + if (aStatus) { 1.1954 + this._isMatch = true; 1.1955 + this.target.removeAttribute("unmatched"); 1.1956 + } else { 1.1957 + this._isMatch = false; 1.1958 + this.target.setAttribute("unmatched", ""); 1.1959 + } 1.1960 + }, 1.1961 + 1.1962 + /** 1.1963 + * Find the first item in the tree of visible items in this item that matches 1.1964 + * the predicate. Searches in visual order (the order seen by the user). 1.1965 + * Tests itself, then descends into first the enumerable children and then 1.1966 + * the non-enumerable children (since they are presented in separate groups). 1.1967 + * 1.1968 + * @param function aPredicate 1.1969 + * A function that returns true when a match is found. 1.1970 + * @return Scope | Variable | Property 1.1971 + * The first visible scope, variable or property, or null if nothing 1.1972 + * is found. 1.1973 + */ 1.1974 + _findInVisibleItems: function(aPredicate) { 1.1975 + if (aPredicate(this)) { 1.1976 + return this; 1.1977 + } 1.1978 + 1.1979 + if (this._isExpanded) { 1.1980 + if (this._variablesView._enumVisible) { 1.1981 + for (let item of this._enumItems) { 1.1982 + let result = item._findInVisibleItems(aPredicate); 1.1983 + if (result) { 1.1984 + return result; 1.1985 + } 1.1986 + } 1.1987 + } 1.1988 + 1.1989 + if (this._variablesView._nonEnumVisible) { 1.1990 + for (let item of this._nonEnumItems) { 1.1991 + let result = item._findInVisibleItems(aPredicate); 1.1992 + if (result) { 1.1993 + return result; 1.1994 + } 1.1995 + } 1.1996 + } 1.1997 + } 1.1998 + 1.1999 + return null; 1.2000 + }, 1.2001 + 1.2002 + /** 1.2003 + * Find the last item in the tree of visible items in this item that matches 1.2004 + * the predicate. Searches in reverse visual order (opposite of the order 1.2005 + * seen by the user). Descends into first the non-enumerable children, then 1.2006 + * the enumerable children (since they are presented in separate groups), and 1.2007 + * finally tests itself. 1.2008 + * 1.2009 + * @param function aPredicate 1.2010 + * A function that returns true when a match is found. 1.2011 + * @return Scope | Variable | Property 1.2012 + * The last visible scope, variable or property, or null if nothing 1.2013 + * is found. 1.2014 + */ 1.2015 + _findInVisibleItemsReverse: function(aPredicate) { 1.2016 + if (this._isExpanded) { 1.2017 + if (this._variablesView._nonEnumVisible) { 1.2018 + for (let i = this._nonEnumItems.length - 1; i >= 0; i--) { 1.2019 + let item = this._nonEnumItems[i]; 1.2020 + let result = item._findInVisibleItemsReverse(aPredicate); 1.2021 + if (result) { 1.2022 + return result; 1.2023 + } 1.2024 + } 1.2025 + } 1.2026 + 1.2027 + if (this._variablesView._enumVisible) { 1.2028 + for (let i = this._enumItems.length - 1; i >= 0; i--) { 1.2029 + let item = this._enumItems[i]; 1.2030 + let result = item._findInVisibleItemsReverse(aPredicate); 1.2031 + if (result) { 1.2032 + return result; 1.2033 + } 1.2034 + } 1.2035 + } 1.2036 + } 1.2037 + 1.2038 + if (aPredicate(this)) { 1.2039 + return this; 1.2040 + } 1.2041 + 1.2042 + return null; 1.2043 + }, 1.2044 + 1.2045 + /** 1.2046 + * Gets top level variables view instance. 1.2047 + * @return VariablesView 1.2048 + */ 1.2049 + get _variablesView() this._topView || (this._topView = (function(self) { 1.2050 + let parentView = self.ownerView; 1.2051 + let topView; 1.2052 + 1.2053 + while (topView = parentView.ownerView) { 1.2054 + parentView = topView; 1.2055 + } 1.2056 + return parentView; 1.2057 + })(this)), 1.2058 + 1.2059 + /** 1.2060 + * Gets the parent node holding this scope. 1.2061 + * @return nsIDOMNode 1.2062 + */ 1.2063 + get parentNode() this.ownerView._list, 1.2064 + 1.2065 + /** 1.2066 + * Gets the owner document holding this scope. 1.2067 + * @return nsIHTMLDocument 1.2068 + */ 1.2069 + get document() this._document || (this._document = this.ownerView.document), 1.2070 + 1.2071 + /** 1.2072 + * Gets the default window holding this scope. 1.2073 + * @return nsIDOMWindow 1.2074 + */ 1.2075 + get window() this._window || (this._window = this.ownerView.window), 1.2076 + 1.2077 + _topView: null, 1.2078 + _document: null, 1.2079 + _window: null, 1.2080 + 1.2081 + ownerView: null, 1.2082 + eval: null, 1.2083 + switch: null, 1.2084 + delete: null, 1.2085 + new: null, 1.2086 + preventDisableOnChange: false, 1.2087 + preventDescriptorModifiers: false, 1.2088 + editing: false, 1.2089 + editableNameTooltip: "", 1.2090 + editableValueTooltip: "", 1.2091 + editButtonTooltip: "", 1.2092 + deleteButtonTooltip: "", 1.2093 + domNodeValueTooltip: "", 1.2094 + contextMenuId: "", 1.2095 + separatorStr: "", 1.2096 + 1.2097 + _store: null, 1.2098 + _enumItems: null, 1.2099 + _nonEnumItems: null, 1.2100 + _fetched: false, 1.2101 + _committed: false, 1.2102 + _isLocked: false, 1.2103 + _isExpanded: false, 1.2104 + _isContentVisible: true, 1.2105 + _isHeaderVisible: true, 1.2106 + _isArrowVisible: true, 1.2107 + _isMatch: true, 1.2108 + _idString: "", 1.2109 + _nameString: "", 1.2110 + _target: null, 1.2111 + _arrow: null, 1.2112 + _name: null, 1.2113 + _title: null, 1.2114 + _enum: null, 1.2115 + _nonenum: null, 1.2116 +}; 1.2117 + 1.2118 +// Creating maps and arrays thousands of times for variables or properties 1.2119 +// with a large number of children fills up a lot of memory. Make sure 1.2120 +// these are instantiated only if needed. 1.2121 +DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_store", Map); 1.2122 +DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_enumItems", Array); 1.2123 +DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_nonEnumItems", Array); 1.2124 + 1.2125 +// An ellipsis symbol (usually "ā¦") used for localization. 1.2126 +XPCOMUtils.defineLazyGetter(Scope, "ellipsis", () => 1.2127 + Services.prefs.getComplexValue("intl.ellipsis", Ci.nsIPrefLocalizedString).data); 1.2128 + 1.2129 +/** 1.2130 + * A Variable is a Scope holding Property instances. 1.2131 + * Iterable via "for (let [name, property] of instance) { }". 1.2132 + * 1.2133 + * @param Scope aScope 1.2134 + * The scope to contain this variable. 1.2135 + * @param string aName 1.2136 + * The variable's name. 1.2137 + * @param object aDescriptor 1.2138 + * The variable's descriptor. 1.2139 + */ 1.2140 +function Variable(aScope, aName, aDescriptor) { 1.2141 + this._setTooltips = this._setTooltips.bind(this); 1.2142 + this._activateNameInput = this._activateNameInput.bind(this); 1.2143 + this._activateValueInput = this._activateValueInput.bind(this); 1.2144 + this.openNodeInInspector = this.openNodeInInspector.bind(this); 1.2145 + this.highlightDomNode = this.highlightDomNode.bind(this); 1.2146 + this.unhighlightDomNode = this.unhighlightDomNode.bind(this); 1.2147 + 1.2148 + // Treat safe getter descriptors as descriptors with a value. 1.2149 + if ("getterValue" in aDescriptor) { 1.2150 + aDescriptor.value = aDescriptor.getterValue; 1.2151 + delete aDescriptor.get; 1.2152 + delete aDescriptor.set; 1.2153 + } 1.2154 + 1.2155 + Scope.call(this, aScope, aName, this._initialDescriptor = aDescriptor); 1.2156 + this.setGrip(aDescriptor.value); 1.2157 + this._symbolicName = aName; 1.2158 + this._absoluteName = aScope.name + "[\"" + aName + "\"]"; 1.2159 +} 1.2160 + 1.2161 +Variable.prototype = Heritage.extend(Scope.prototype, { 1.2162 + /** 1.2163 + * Whether this Variable should be prefetched when it is remoted. 1.2164 + */ 1.2165 + get shouldPrefetch() { 1.2166 + return this.name == "window" || this.name == "this"; 1.2167 + }, 1.2168 + 1.2169 + /** 1.2170 + * Whether this Variable should paginate its contents. 1.2171 + */ 1.2172 + get allowPaginate() { 1.2173 + return this.name != "window" && this.name != "this"; 1.2174 + }, 1.2175 + 1.2176 + /** 1.2177 + * The class name applied to this variable's target element. 1.2178 + */ 1.2179 + targetClassName: "variables-view-variable variable-or-property", 1.2180 + 1.2181 + /** 1.2182 + * Create a new Property that is a child of Variable. 1.2183 + * 1.2184 + * @param string aName 1.2185 + * The name of the new Property. 1.2186 + * @param object aDescriptor 1.2187 + * The property's descriptor. 1.2188 + * @return Property 1.2189 + * The newly created child Property. 1.2190 + */ 1.2191 + _createChild: function(aName, aDescriptor) { 1.2192 + return new Property(this, aName, aDescriptor); 1.2193 + }, 1.2194 + 1.2195 + /** 1.2196 + * Remove this Variable from its parent and remove all children recursively. 1.2197 + */ 1.2198 + remove: function() { 1.2199 + if (this._linkedToInspector) { 1.2200 + this.unhighlightDomNode(); 1.2201 + this._valueLabel.removeEventListener("mouseover", this.highlightDomNode, false); 1.2202 + this._valueLabel.removeEventListener("mouseout", this.unhighlightDomNode, false); 1.2203 + this._openInspectorNode.removeEventListener("mousedown", this.openNodeInInspector, false); 1.2204 + } 1.2205 + 1.2206 + this.ownerView._store.delete(this._nameString); 1.2207 + this._variablesView._itemsByElement.delete(this._target); 1.2208 + this._variablesView._currHierarchy.delete(this._absoluteName); 1.2209 + 1.2210 + this._target.remove(); 1.2211 + 1.2212 + for (let property of this._store.values()) { 1.2213 + property.remove(); 1.2214 + } 1.2215 + }, 1.2216 + 1.2217 + /** 1.2218 + * Populates this variable to contain all the properties of an object. 1.2219 + * 1.2220 + * @param object aObject 1.2221 + * The raw object you want to display. 1.2222 + * @param object aOptions [optional] 1.2223 + * Additional options for adding the properties. Supported options: 1.2224 + * - sorted: true to sort all the properties before adding them 1.2225 + * - expanded: true to expand all the properties after adding them 1.2226 + */ 1.2227 + populate: function(aObject, aOptions = {}) { 1.2228 + // Retrieve the properties only once. 1.2229 + if (this._fetched) { 1.2230 + return; 1.2231 + } 1.2232 + this._fetched = true; 1.2233 + 1.2234 + let propertyNames = Object.getOwnPropertyNames(aObject); 1.2235 + let prototype = Object.getPrototypeOf(aObject); 1.2236 + 1.2237 + // Sort all of the properties before adding them, if preferred. 1.2238 + if (aOptions.sorted) { 1.2239 + propertyNames.sort(); 1.2240 + } 1.2241 + // Add all the variable properties. 1.2242 + for (let name of propertyNames) { 1.2243 + let descriptor = Object.getOwnPropertyDescriptor(aObject, name); 1.2244 + if (descriptor.get || descriptor.set) { 1.2245 + let prop = this._addRawNonValueProperty(name, descriptor); 1.2246 + if (aOptions.expanded) { 1.2247 + prop.expanded = true; 1.2248 + } 1.2249 + } else { 1.2250 + let prop = this._addRawValueProperty(name, descriptor, aObject[name]); 1.2251 + if (aOptions.expanded) { 1.2252 + prop.expanded = true; 1.2253 + } 1.2254 + } 1.2255 + } 1.2256 + // Add the variable's __proto__. 1.2257 + if (prototype) { 1.2258 + this._addRawValueProperty("__proto__", {}, prototype); 1.2259 + } 1.2260 + }, 1.2261 + 1.2262 + /** 1.2263 + * Populates a specific variable or property instance to contain all the 1.2264 + * properties of an object 1.2265 + * 1.2266 + * @param Variable | Property aVar 1.2267 + * The target variable to populate. 1.2268 + * @param object aObject [optional] 1.2269 + * The raw object you want to display. If unspecified, the object is 1.2270 + * assumed to be defined in a _sourceValue property on the target. 1.2271 + */ 1.2272 + _populateTarget: function(aVar, aObject = aVar._sourceValue) { 1.2273 + aVar.populate(aObject); 1.2274 + }, 1.2275 + 1.2276 + /** 1.2277 + * Adds a property for this variable based on a raw value descriptor. 1.2278 + * 1.2279 + * @param string aName 1.2280 + * The property's name. 1.2281 + * @param object aDescriptor 1.2282 + * Specifies the exact property descriptor as returned by a call to 1.2283 + * Object.getOwnPropertyDescriptor. 1.2284 + * @param object aValue 1.2285 + * The raw property value you want to display. 1.2286 + * @return Property 1.2287 + * The newly added property instance. 1.2288 + */ 1.2289 + _addRawValueProperty: function(aName, aDescriptor, aValue) { 1.2290 + let descriptor = Object.create(aDescriptor); 1.2291 + descriptor.value = VariablesView.getGrip(aValue); 1.2292 + 1.2293 + let propertyItem = this.addItem(aName, descriptor); 1.2294 + propertyItem._sourceValue = aValue; 1.2295 + 1.2296 + // Add an 'onexpand' callback for the property, lazily handling 1.2297 + // the addition of new child properties. 1.2298 + if (!VariablesView.isPrimitive(descriptor)) { 1.2299 + propertyItem.onexpand = this._populateTarget; 1.2300 + } 1.2301 + return propertyItem; 1.2302 + }, 1.2303 + 1.2304 + /** 1.2305 + * Adds a property for this variable based on a getter/setter descriptor. 1.2306 + * 1.2307 + * @param string aName 1.2308 + * The property's name. 1.2309 + * @param object aDescriptor 1.2310 + * Specifies the exact property descriptor as returned by a call to 1.2311 + * Object.getOwnPropertyDescriptor. 1.2312 + * @return Property 1.2313 + * The newly added property instance. 1.2314 + */ 1.2315 + _addRawNonValueProperty: function(aName, aDescriptor) { 1.2316 + let descriptor = Object.create(aDescriptor); 1.2317 + descriptor.get = VariablesView.getGrip(aDescriptor.get); 1.2318 + descriptor.set = VariablesView.getGrip(aDescriptor.set); 1.2319 + 1.2320 + return this.addItem(aName, descriptor); 1.2321 + }, 1.2322 + 1.2323 + /** 1.2324 + * Gets this variable's path to the topmost scope in the form of a string 1.2325 + * meant for use via eval() or a similar approach. 1.2326 + * For example, a symbolic name may look like "arguments['0']['foo']['bar']". 1.2327 + * @return string 1.2328 + */ 1.2329 + get symbolicName() this._symbolicName, 1.2330 + 1.2331 + /** 1.2332 + * Gets this variable's symbolic path to the topmost scope. 1.2333 + * @return array 1.2334 + * @see Variable._buildSymbolicPath 1.2335 + */ 1.2336 + get symbolicPath() { 1.2337 + if (this._symbolicPath) { 1.2338 + return this._symbolicPath; 1.2339 + } 1.2340 + this._symbolicPath = this._buildSymbolicPath(); 1.2341 + return this._symbolicPath; 1.2342 + }, 1.2343 + 1.2344 + /** 1.2345 + * Build this variable's path to the topmost scope in form of an array of 1.2346 + * strings, one for each segment of the path. 1.2347 + * For example, a symbolic path may look like ["0", "foo", "bar"]. 1.2348 + * @return array 1.2349 + */ 1.2350 + _buildSymbolicPath: function(path = []) { 1.2351 + if (this.name) { 1.2352 + path.unshift(this.name); 1.2353 + if (this.ownerView instanceof Variable) { 1.2354 + return this.ownerView._buildSymbolicPath(path); 1.2355 + } 1.2356 + } 1.2357 + return path; 1.2358 + }, 1.2359 + 1.2360 + /** 1.2361 + * Returns this variable's value from the descriptor if available. 1.2362 + * @return any 1.2363 + */ 1.2364 + get value() this._initialDescriptor.value, 1.2365 + 1.2366 + /** 1.2367 + * Returns this variable's getter from the descriptor if available. 1.2368 + * @return object 1.2369 + */ 1.2370 + get getter() this._initialDescriptor.get, 1.2371 + 1.2372 + /** 1.2373 + * Returns this variable's getter from the descriptor if available. 1.2374 + * @return object 1.2375 + */ 1.2376 + get setter() this._initialDescriptor.set, 1.2377 + 1.2378 + /** 1.2379 + * Sets the specific grip for this variable (applies the text content and 1.2380 + * class name to the value label). 1.2381 + * 1.2382 + * The grip should contain the value or the type & class, as defined in the 1.2383 + * remote debugger protocol. For convenience, undefined and null are 1.2384 + * both considered types. 1.2385 + * 1.2386 + * @param any aGrip 1.2387 + * Specifies the value and/or type & class of the variable. 1.2388 + * e.g. - 42 1.2389 + * - true 1.2390 + * - "nasu" 1.2391 + * - { type: "undefined" } 1.2392 + * - { type: "null" } 1.2393 + * - { type: "object", class: "Object" } 1.2394 + */ 1.2395 + setGrip: function(aGrip) { 1.2396 + // Don't allow displaying grip information if there's no name available 1.2397 + // or the grip is malformed. 1.2398 + if (!this._nameString || aGrip === undefined || aGrip === null) { 1.2399 + return; 1.2400 + } 1.2401 + // Getters and setters should display grip information in sub-properties. 1.2402 + if (this.getter || this.setter) { 1.2403 + return; 1.2404 + } 1.2405 + 1.2406 + let prevGrip = this._valueGrip; 1.2407 + if (prevGrip) { 1.2408 + this._valueLabel.classList.remove(VariablesView.getClass(prevGrip)); 1.2409 + } 1.2410 + this._valueGrip = aGrip; 1.2411 + this._valueString = VariablesView.getString(aGrip, { 1.2412 + concise: true, 1.2413 + noEllipsis: true, 1.2414 + }); 1.2415 + this._valueClassName = VariablesView.getClass(aGrip); 1.2416 + 1.2417 + this._valueLabel.classList.add(this._valueClassName); 1.2418 + this._valueLabel.setAttribute("value", this._valueString); 1.2419 + this._separatorLabel.hidden = false; 1.2420 + 1.2421 + // DOMNodes get special treatment since they can be linked to the inspector 1.2422 + if (this._valueGrip.preview && this._valueGrip.preview.kind === "DOMNode") { 1.2423 + this._linkToInspector(); 1.2424 + } 1.2425 + }, 1.2426 + 1.2427 + /** 1.2428 + * Marks this variable as overridden. 1.2429 + * 1.2430 + * @param boolean aFlag 1.2431 + * Whether this variable is overridden or not. 1.2432 + */ 1.2433 + setOverridden: function(aFlag) { 1.2434 + if (aFlag) { 1.2435 + this._target.setAttribute("overridden", ""); 1.2436 + } else { 1.2437 + this._target.removeAttribute("overridden"); 1.2438 + } 1.2439 + }, 1.2440 + 1.2441 + /** 1.2442 + * Briefly flashes this variable. 1.2443 + * 1.2444 + * @param number aDuration [optional] 1.2445 + * An optional flash animation duration. 1.2446 + */ 1.2447 + flash: function(aDuration = ITEM_FLASH_DURATION) { 1.2448 + let fadeInDelay = this._variablesView.lazyEmptyDelay + 1; 1.2449 + let fadeOutDelay = fadeInDelay + aDuration; 1.2450 + 1.2451 + setNamedTimeout("vview-flash-in" + this._absoluteName, 1.2452 + fadeInDelay, () => this._target.setAttribute("changed", "")); 1.2453 + 1.2454 + setNamedTimeout("vview-flash-out" + this._absoluteName, 1.2455 + fadeOutDelay, () => this._target.removeAttribute("changed")); 1.2456 + }, 1.2457 + 1.2458 + /** 1.2459 + * Initializes this variable's id, view and binds event listeners. 1.2460 + * 1.2461 + * @param string aName 1.2462 + * The variable's name. 1.2463 + * @param object aDescriptor 1.2464 + * The variable's descriptor. 1.2465 + */ 1.2466 + _init: function(aName, aDescriptor) { 1.2467 + this._idString = generateId(this._nameString = aName); 1.2468 + this._displayScope(aName, this.targetClassName); 1.2469 + this._displayVariable(); 1.2470 + this._customizeVariable(); 1.2471 + this._prepareTooltips(); 1.2472 + this._setAttributes(); 1.2473 + this._addEventListeners(); 1.2474 + 1.2475 + if (this._initialDescriptor.enumerable || 1.2476 + this._nameString == "this" || 1.2477 + this._nameString == "<return>" || 1.2478 + this._nameString == "<exception>") { 1.2479 + this.ownerView._enum.appendChild(this._target); 1.2480 + this.ownerView._enumItems.push(this); 1.2481 + } else { 1.2482 + this.ownerView._nonenum.appendChild(this._target); 1.2483 + this.ownerView._nonEnumItems.push(this); 1.2484 + } 1.2485 + }, 1.2486 + 1.2487 + /** 1.2488 + * Creates the necessary nodes for this variable. 1.2489 + */ 1.2490 + _displayVariable: function() { 1.2491 + let document = this.document; 1.2492 + let descriptor = this._initialDescriptor; 1.2493 + 1.2494 + let separatorLabel = this._separatorLabel = document.createElement("label"); 1.2495 + separatorLabel.className = "plain separator"; 1.2496 + separatorLabel.setAttribute("value", this.separatorStr + " "); 1.2497 + 1.2498 + let valueLabel = this._valueLabel = document.createElement("label"); 1.2499 + valueLabel.className = "plain value"; 1.2500 + valueLabel.setAttribute("flex", "1"); 1.2501 + valueLabel.setAttribute("crop", "center"); 1.2502 + 1.2503 + this._title.appendChild(separatorLabel); 1.2504 + this._title.appendChild(valueLabel); 1.2505 + 1.2506 + if (VariablesView.isPrimitive(descriptor)) { 1.2507 + this.hideArrow(); 1.2508 + } 1.2509 + 1.2510 + // If no value will be displayed, we don't need the separator. 1.2511 + if (!descriptor.get && !descriptor.set && !("value" in descriptor)) { 1.2512 + separatorLabel.hidden = true; 1.2513 + } 1.2514 + 1.2515 + // If this is a getter/setter property, create two child pseudo-properties 1.2516 + // called "get" and "set" that display the corresponding functions. 1.2517 + if (descriptor.get || descriptor.set) { 1.2518 + separatorLabel.hidden = true; 1.2519 + valueLabel.hidden = true; 1.2520 + 1.2521 + // Changing getter/setter names is never allowed. 1.2522 + this.switch = null; 1.2523 + 1.2524 + // Getter/setter properties require special handling when it comes to 1.2525 + // evaluation and deletion. 1.2526 + if (this.ownerView.eval) { 1.2527 + this.delete = VariablesView.getterOrSetterDeleteCallback; 1.2528 + this.evaluationMacro = VariablesView.overrideValueEvalMacro; 1.2529 + } 1.2530 + // Deleting getters and setters individually is not allowed if no 1.2531 + // evaluation method is provided. 1.2532 + else { 1.2533 + this.delete = null; 1.2534 + this.evaluationMacro = null; 1.2535 + } 1.2536 + 1.2537 + let getter = this.addItem("get", { value: descriptor.get }); 1.2538 + let setter = this.addItem("set", { value: descriptor.set }); 1.2539 + getter.evaluationMacro = VariablesView.getterOrSetterEvalMacro; 1.2540 + setter.evaluationMacro = VariablesView.getterOrSetterEvalMacro; 1.2541 + 1.2542 + getter.hideArrow(); 1.2543 + setter.hideArrow(); 1.2544 + this.expand(); 1.2545 + } 1.2546 + }, 1.2547 + 1.2548 + /** 1.2549 + * Adds specific nodes for this variable based on custom flags. 1.2550 + */ 1.2551 + _customizeVariable: function() { 1.2552 + let ownerView = this.ownerView; 1.2553 + let descriptor = this._initialDescriptor; 1.2554 + 1.2555 + if (ownerView.eval && this.getter || this.setter) { 1.2556 + let editNode = this._editNode = this.document.createElement("toolbarbutton"); 1.2557 + editNode.className = "plain variables-view-edit"; 1.2558 + editNode.addEventListener("mousedown", this._onEdit.bind(this), false); 1.2559 + this._title.insertBefore(editNode, this._spacer); 1.2560 + } 1.2561 + 1.2562 + if (ownerView.delete) { 1.2563 + let deleteNode = this._deleteNode = this.document.createElement("toolbarbutton"); 1.2564 + deleteNode.className = "plain variables-view-delete"; 1.2565 + deleteNode.addEventListener("click", this._onDelete.bind(this), false); 1.2566 + this._title.appendChild(deleteNode); 1.2567 + } 1.2568 + 1.2569 + if (ownerView.new) { 1.2570 + let addPropertyNode = this._addPropertyNode = this.document.createElement("toolbarbutton"); 1.2571 + addPropertyNode.className = "plain variables-view-add-property"; 1.2572 + addPropertyNode.addEventListener("mousedown", this._onAddProperty.bind(this), false); 1.2573 + this._title.appendChild(addPropertyNode); 1.2574 + 1.2575 + // Can't add properties to primitive values, hide the node in those cases. 1.2576 + if (VariablesView.isPrimitive(descriptor)) { 1.2577 + addPropertyNode.setAttribute("invisible", ""); 1.2578 + } 1.2579 + } 1.2580 + 1.2581 + if (ownerView.contextMenuId) { 1.2582 + this._title.setAttribute("context", ownerView.contextMenuId); 1.2583 + } 1.2584 + 1.2585 + if (ownerView.preventDescriptorModifiers) { 1.2586 + return; 1.2587 + } 1.2588 + 1.2589 + if (!descriptor.writable && !ownerView.getter && !ownerView.setter) { 1.2590 + let nonWritableIcon = this.document.createElement("hbox"); 1.2591 + nonWritableIcon.className = "plain variable-or-property-non-writable-icon"; 1.2592 + nonWritableIcon.setAttribute("optional-visibility", ""); 1.2593 + this._title.appendChild(nonWritableIcon); 1.2594 + } 1.2595 + if (descriptor.value && typeof descriptor.value == "object") { 1.2596 + if (descriptor.value.frozen) { 1.2597 + let frozenLabel = this.document.createElement("label"); 1.2598 + frozenLabel.className = "plain variable-or-property-frozen-label"; 1.2599 + frozenLabel.setAttribute("optional-visibility", ""); 1.2600 + frozenLabel.setAttribute("value", "F"); 1.2601 + this._title.appendChild(frozenLabel); 1.2602 + } 1.2603 + if (descriptor.value.sealed) { 1.2604 + let sealedLabel = this.document.createElement("label"); 1.2605 + sealedLabel.className = "plain variable-or-property-sealed-label"; 1.2606 + sealedLabel.setAttribute("optional-visibility", ""); 1.2607 + sealedLabel.setAttribute("value", "S"); 1.2608 + this._title.appendChild(sealedLabel); 1.2609 + } 1.2610 + if (!descriptor.value.extensible) { 1.2611 + let nonExtensibleLabel = this.document.createElement("label"); 1.2612 + nonExtensibleLabel.className = "plain variable-or-property-non-extensible-label"; 1.2613 + nonExtensibleLabel.setAttribute("optional-visibility", ""); 1.2614 + nonExtensibleLabel.setAttribute("value", "N"); 1.2615 + this._title.appendChild(nonExtensibleLabel); 1.2616 + } 1.2617 + } 1.2618 + }, 1.2619 + 1.2620 + /** 1.2621 + * Prepares all tooltips for this variable. 1.2622 + */ 1.2623 + _prepareTooltips: function() { 1.2624 + this._target.addEventListener("mouseover", this._setTooltips, false); 1.2625 + }, 1.2626 + 1.2627 + /** 1.2628 + * Sets all tooltips for this variable. 1.2629 + */ 1.2630 + _setTooltips: function() { 1.2631 + this._target.removeEventListener("mouseover", this._setTooltips, false); 1.2632 + 1.2633 + let ownerView = this.ownerView; 1.2634 + if (ownerView.preventDescriptorModifiers) { 1.2635 + return; 1.2636 + } 1.2637 + 1.2638 + let tooltip = this.document.createElement("tooltip"); 1.2639 + tooltip.id = "tooltip-" + this._idString; 1.2640 + tooltip.setAttribute("orient", "horizontal"); 1.2641 + 1.2642 + let labels = [ 1.2643 + "configurable", "enumerable", "writable", 1.2644 + "frozen", "sealed", "extensible", "overridden", "WebIDL"]; 1.2645 + 1.2646 + for (let type of labels) { 1.2647 + let labelElement = this.document.createElement("label"); 1.2648 + labelElement.className = type; 1.2649 + labelElement.setAttribute("value", STR.GetStringFromName(type + "Tooltip")); 1.2650 + tooltip.appendChild(labelElement); 1.2651 + } 1.2652 + 1.2653 + this._target.appendChild(tooltip); 1.2654 + this._target.setAttribute("tooltip", tooltip.id); 1.2655 + 1.2656 + if (this._editNode && ownerView.eval) { 1.2657 + this._editNode.setAttribute("tooltiptext", ownerView.editButtonTooltip); 1.2658 + } 1.2659 + if (this._openInspectorNode && this._linkedToInspector) { 1.2660 + this._openInspectorNode.setAttribute("tooltiptext", this.ownerView.domNodeValueTooltip); 1.2661 + } 1.2662 + if (this._valueLabel && ownerView.eval) { 1.2663 + this._valueLabel.setAttribute("tooltiptext", ownerView.editableValueTooltip); 1.2664 + } 1.2665 + if (this._name && ownerView.switch) { 1.2666 + this._name.setAttribute("tooltiptext", ownerView.editableNameTooltip); 1.2667 + } 1.2668 + if (this._deleteNode && ownerView.delete) { 1.2669 + this._deleteNode.setAttribute("tooltiptext", ownerView.deleteButtonTooltip); 1.2670 + } 1.2671 + }, 1.2672 + 1.2673 + /** 1.2674 + * Get the parent variablesview toolbox, if any. 1.2675 + */ 1.2676 + get toolbox() { 1.2677 + return this._variablesView.toolbox; 1.2678 + }, 1.2679 + 1.2680 + /** 1.2681 + * Checks if this variable is a DOMNode and is part of a variablesview that 1.2682 + * has been linked to the toolbox, so that highlighting and jumping to the 1.2683 + * inspector can be done. 1.2684 + */ 1.2685 + _isLinkableToInspector: function() { 1.2686 + let isDomNode = this._valueGrip && this._valueGrip.preview.kind === "DOMNode"; 1.2687 + let hasBeenLinked = this._linkedToInspector; 1.2688 + let hasToolbox = !!this.toolbox; 1.2689 + 1.2690 + return isDomNode && !hasBeenLinked && hasToolbox; 1.2691 + }, 1.2692 + 1.2693 + /** 1.2694 + * If the variable is a DOMNode, and if a toolbox is set, then link it to the 1.2695 + * inspector (highlight on hover, and jump to markup-view on click) 1.2696 + */ 1.2697 + _linkToInspector: function() { 1.2698 + if (!this._isLinkableToInspector()) { 1.2699 + return; 1.2700 + } 1.2701 + 1.2702 + // Listen to value mouseover/click events to highlight and jump 1.2703 + this._valueLabel.addEventListener("mouseover", this.highlightDomNode, false); 1.2704 + this._valueLabel.addEventListener("mouseout", this.unhighlightDomNode, false); 1.2705 + 1.2706 + // Add a button to open the node in the inspector 1.2707 + this._openInspectorNode = this.document.createElement("toolbarbutton"); 1.2708 + this._openInspectorNode.className = "plain variables-view-open-inspector"; 1.2709 + this._openInspectorNode.addEventListener("mousedown", this.openNodeInInspector, false); 1.2710 + this._title.insertBefore(this._openInspectorNode, this._title.querySelector("toolbarbutton")); 1.2711 + 1.2712 + this._linkedToInspector = true; 1.2713 + }, 1.2714 + 1.2715 + /** 1.2716 + * In case this variable is a DOMNode and part of a variablesview that has been 1.2717 + * linked to the toolbox's inspector, then select the corresponding node in 1.2718 + * the inspector, and switch the inspector tool in the toolbox 1.2719 + * @return a promise that resolves when the node is selected and the inspector 1.2720 + * has been switched to and is ready 1.2721 + */ 1.2722 + openNodeInInspector: function(event) { 1.2723 + if (!this.toolbox) { 1.2724 + return promise.reject(new Error("Toolbox not available")); 1.2725 + } 1.2726 + 1.2727 + event && event.stopPropagation(); 1.2728 + 1.2729 + return Task.spawn(function*() { 1.2730 + yield this.toolbox.initInspector(); 1.2731 + 1.2732 + let nodeFront = this._nodeFront; 1.2733 + if (!nodeFront) { 1.2734 + nodeFront = yield this.toolbox.walker.getNodeActorFromObjectActor(this._valueGrip.actor); 1.2735 + } 1.2736 + 1.2737 + if (nodeFront) { 1.2738 + yield this.toolbox.selectTool("inspector"); 1.2739 + 1.2740 + let inspectorReady = promise.defer(); 1.2741 + this.toolbox.getPanel("inspector").once("inspector-updated", inspectorReady.resolve); 1.2742 + yield this.toolbox.selection.setNodeFront(nodeFront, "variables-view"); 1.2743 + yield inspectorReady.promise; 1.2744 + } 1.2745 + }.bind(this)); 1.2746 + }, 1.2747 + 1.2748 + /** 1.2749 + * In case this variable is a DOMNode and part of a variablesview that has been 1.2750 + * linked to the toolbox's inspector, then highlight the corresponding node 1.2751 + */ 1.2752 + highlightDomNode: function() { 1.2753 + if (this.toolbox) { 1.2754 + if (this._nodeFront) { 1.2755 + // If the nodeFront has been retrieved before, no need to ask the server 1.2756 + // again for it 1.2757 + this.toolbox.highlighterUtils.highlightNodeFront(this._nodeFront); 1.2758 + return; 1.2759 + } 1.2760 + 1.2761 + this.toolbox.highlighterUtils.highlightDomValueGrip(this._valueGrip).then(front => { 1.2762 + this._nodeFront = front; 1.2763 + }); 1.2764 + } 1.2765 + }, 1.2766 + 1.2767 + /** 1.2768 + * Unhighlight a previously highlit node 1.2769 + * @see highlightDomNode 1.2770 + */ 1.2771 + unhighlightDomNode: function() { 1.2772 + if (this.toolbox) { 1.2773 + this.toolbox.highlighterUtils.unhighlight(); 1.2774 + } 1.2775 + }, 1.2776 + 1.2777 + /** 1.2778 + * Sets a variable's configurable, enumerable and writable attributes, 1.2779 + * and specifies if it's a 'this', '<exception>', '<return>' or '__proto__' 1.2780 + * reference. 1.2781 + */ 1.2782 + _setAttributes: function() { 1.2783 + let ownerView = this.ownerView; 1.2784 + if (ownerView.preventDescriptorModifiers) { 1.2785 + return; 1.2786 + } 1.2787 + 1.2788 + let descriptor = this._initialDescriptor; 1.2789 + let target = this._target; 1.2790 + let name = this._nameString; 1.2791 + 1.2792 + if (ownerView.eval) { 1.2793 + target.setAttribute("editable", ""); 1.2794 + } 1.2795 + 1.2796 + if (!descriptor.configurable) { 1.2797 + target.setAttribute("non-configurable", ""); 1.2798 + } 1.2799 + if (!descriptor.enumerable) { 1.2800 + target.setAttribute("non-enumerable", ""); 1.2801 + } 1.2802 + if (!descriptor.writable && !ownerView.getter && !ownerView.setter) { 1.2803 + target.setAttribute("non-writable", ""); 1.2804 + } 1.2805 + 1.2806 + if (descriptor.value && typeof descriptor.value == "object") { 1.2807 + if (descriptor.value.frozen) { 1.2808 + target.setAttribute("frozen", ""); 1.2809 + } 1.2810 + if (descriptor.value.sealed) { 1.2811 + target.setAttribute("sealed", ""); 1.2812 + } 1.2813 + if (!descriptor.value.extensible) { 1.2814 + target.setAttribute("non-extensible", ""); 1.2815 + } 1.2816 + } 1.2817 + 1.2818 + if (descriptor && "getterValue" in descriptor) { 1.2819 + target.setAttribute("safe-getter", ""); 1.2820 + } 1.2821 + 1.2822 + if (name == "this") { 1.2823 + target.setAttribute("self", ""); 1.2824 + } 1.2825 + else if (name == "<exception>") { 1.2826 + target.setAttribute("exception", ""); 1.2827 + target.setAttribute("pseudo-item", ""); 1.2828 + } 1.2829 + else if (name == "<return>") { 1.2830 + target.setAttribute("return", ""); 1.2831 + target.setAttribute("pseudo-item", ""); 1.2832 + } 1.2833 + else if (name == "__proto__") { 1.2834 + target.setAttribute("proto", ""); 1.2835 + target.setAttribute("pseudo-item", ""); 1.2836 + } 1.2837 + 1.2838 + if (Object.keys(descriptor).length == 0) { 1.2839 + target.setAttribute("pseudo-item", ""); 1.2840 + } 1.2841 + }, 1.2842 + 1.2843 + /** 1.2844 + * Adds the necessary event listeners for this variable. 1.2845 + */ 1.2846 + _addEventListeners: function() { 1.2847 + this._name.addEventListener("dblclick", this._activateNameInput, false); 1.2848 + this._valueLabel.addEventListener("mousedown", this._activateValueInput, false); 1.2849 + this._title.addEventListener("mousedown", this._onClick, false); 1.2850 + }, 1.2851 + 1.2852 + /** 1.2853 + * Makes this variable's name editable. 1.2854 + */ 1.2855 + _activateNameInput: function(e) { 1.2856 + if (!this._variablesView.alignedValues) { 1.2857 + this._separatorLabel.hidden = true; 1.2858 + this._valueLabel.hidden = true; 1.2859 + } 1.2860 + 1.2861 + EditableName.create(this, { 1.2862 + onSave: aKey => { 1.2863 + if (!this._variablesView.preventDisableOnChange) { 1.2864 + this._disable(); 1.2865 + } 1.2866 + this.ownerView.switch(this, aKey); 1.2867 + }, 1.2868 + onCleanup: () => { 1.2869 + if (!this._variablesView.alignedValues) { 1.2870 + this._separatorLabel.hidden = false; 1.2871 + this._valueLabel.hidden = false; 1.2872 + } 1.2873 + } 1.2874 + }, e); 1.2875 + }, 1.2876 + 1.2877 + /** 1.2878 + * Makes this variable's value editable. 1.2879 + */ 1.2880 + _activateValueInput: function(e) { 1.2881 + EditableValue.create(this, { 1.2882 + onSave: aString => { 1.2883 + if (this._linkedToInspector) { 1.2884 + this.unhighlightDomNode(); 1.2885 + } 1.2886 + if (!this._variablesView.preventDisableOnChange) { 1.2887 + this._disable(); 1.2888 + } 1.2889 + this.ownerView.eval(this, aString); 1.2890 + } 1.2891 + }, e); 1.2892 + }, 1.2893 + 1.2894 + /** 1.2895 + * Disables this variable prior to a new name switch or value evaluation. 1.2896 + */ 1.2897 + _disable: function() { 1.2898 + // Prevent the variable from being collapsed or expanded. 1.2899 + this.hideArrow(); 1.2900 + 1.2901 + // Hide any nodes that may offer information about the variable. 1.2902 + for (let node of this._title.childNodes) { 1.2903 + node.hidden = node != this._arrow && node != this._name; 1.2904 + } 1.2905 + this._enum.hidden = true; 1.2906 + this._nonenum.hidden = true; 1.2907 + }, 1.2908 + 1.2909 + /** 1.2910 + * The current macro used to generate the string evaluated when performing 1.2911 + * a variable or property value change. 1.2912 + */ 1.2913 + evaluationMacro: VariablesView.simpleValueEvalMacro, 1.2914 + 1.2915 + /** 1.2916 + * The click listener for the edit button. 1.2917 + */ 1.2918 + _onEdit: function(e) { 1.2919 + if (e.button != 0) { 1.2920 + return; 1.2921 + } 1.2922 + 1.2923 + e.preventDefault(); 1.2924 + e.stopPropagation(); 1.2925 + this._activateValueInput(); 1.2926 + }, 1.2927 + 1.2928 + /** 1.2929 + * The click listener for the delete button. 1.2930 + */ 1.2931 + _onDelete: function(e) { 1.2932 + if ("button" in e && e.button != 0) { 1.2933 + return; 1.2934 + } 1.2935 + 1.2936 + e.preventDefault(); 1.2937 + e.stopPropagation(); 1.2938 + 1.2939 + if (this.ownerView.delete) { 1.2940 + if (!this.ownerView.delete(this)) { 1.2941 + this.hide(); 1.2942 + } 1.2943 + } 1.2944 + }, 1.2945 + 1.2946 + /** 1.2947 + * The click listener for the add property button. 1.2948 + */ 1.2949 + _onAddProperty: function(e) { 1.2950 + if ("button" in e && e.button != 0) { 1.2951 + return; 1.2952 + } 1.2953 + 1.2954 + e.preventDefault(); 1.2955 + e.stopPropagation(); 1.2956 + 1.2957 + this.expanded = true; 1.2958 + 1.2959 + let item = this.addItem(" ", { 1.2960 + value: undefined, 1.2961 + configurable: true, 1.2962 + enumerable: true, 1.2963 + writable: true 1.2964 + }, true); 1.2965 + 1.2966 + // Force showing the separator. 1.2967 + item._separatorLabel.hidden = false; 1.2968 + 1.2969 + EditableNameAndValue.create(item, { 1.2970 + onSave: ([aKey, aValue]) => { 1.2971 + if (!this._variablesView.preventDisableOnChange) { 1.2972 + this._disable(); 1.2973 + } 1.2974 + this.ownerView.new(this, aKey, aValue); 1.2975 + } 1.2976 + }, e); 1.2977 + }, 1.2978 + 1.2979 + _symbolicName: "", 1.2980 + _symbolicPath: null, 1.2981 + _absoluteName: "", 1.2982 + _initialDescriptor: null, 1.2983 + _separatorLabel: null, 1.2984 + _valueLabel: null, 1.2985 + _spacer: null, 1.2986 + _editNode: null, 1.2987 + _deleteNode: null, 1.2988 + _addPropertyNode: null, 1.2989 + _tooltip: null, 1.2990 + _valueGrip: null, 1.2991 + _valueString: "", 1.2992 + _valueClassName: "", 1.2993 + _prevExpandable: false, 1.2994 + _prevExpanded: false 1.2995 +}); 1.2996 + 1.2997 +/** 1.2998 + * A Property is a Variable holding additional child Property instances. 1.2999 + * Iterable via "for (let [name, property] of instance) { }". 1.3000 + * 1.3001 + * @param Variable aVar 1.3002 + * The variable to contain this property. 1.3003 + * @param string aName 1.3004 + * The property's name. 1.3005 + * @param object aDescriptor 1.3006 + * The property's descriptor. 1.3007 + */ 1.3008 +function Property(aVar, aName, aDescriptor) { 1.3009 + Variable.call(this, aVar, aName, aDescriptor); 1.3010 + this._symbolicName = aVar._symbolicName + "[\"" + aName + "\"]"; 1.3011 + this._absoluteName = aVar._absoluteName + "[\"" + aName + "\"]"; 1.3012 +} 1.3013 + 1.3014 +Property.prototype = Heritage.extend(Variable.prototype, { 1.3015 + /** 1.3016 + * The class name applied to this property's target element. 1.3017 + */ 1.3018 + targetClassName: "variables-view-property variable-or-property" 1.3019 +}); 1.3020 + 1.3021 +/** 1.3022 + * A generator-iterator over the VariablesView, Scopes, Variables and Properties. 1.3023 + */ 1.3024 +VariablesView.prototype["@@iterator"] = 1.3025 +Scope.prototype["@@iterator"] = 1.3026 +Variable.prototype["@@iterator"] = 1.3027 +Property.prototype["@@iterator"] = function*() { 1.3028 + yield* this._store; 1.3029 +}; 1.3030 + 1.3031 +/** 1.3032 + * Forget everything recorded about added scopes, variables or properties. 1.3033 + * @see VariablesView.commitHierarchy 1.3034 + */ 1.3035 +VariablesView.prototype.clearHierarchy = function() { 1.3036 + this._prevHierarchy.clear(); 1.3037 + this._currHierarchy.clear(); 1.3038 +}; 1.3039 + 1.3040 +/** 1.3041 + * Perform operations on all the VariablesView Scopes, Variables and Properties 1.3042 + * after you've added all the items you wanted. 1.3043 + * 1.3044 + * Calling this method is optional, and does the following: 1.3045 + * - styles the items overridden by other items in parent scopes 1.3046 + * - reopens the items which were previously expanded 1.3047 + * - flashes the items whose values changed 1.3048 + */ 1.3049 +VariablesView.prototype.commitHierarchy = function() { 1.3050 + for (let [, currItem] of this._currHierarchy) { 1.3051 + // Avoid performing expensive operations. 1.3052 + if (this.commitHierarchyIgnoredItems[currItem._nameString]) { 1.3053 + continue; 1.3054 + } 1.3055 + let overridden = this.isOverridden(currItem); 1.3056 + if (overridden) { 1.3057 + currItem.setOverridden(true); 1.3058 + } 1.3059 + let expanded = !currItem._committed && this.wasExpanded(currItem); 1.3060 + if (expanded) { 1.3061 + currItem.expand(); 1.3062 + } 1.3063 + let changed = !currItem._committed && this.hasChanged(currItem); 1.3064 + if (changed) { 1.3065 + currItem.flash(); 1.3066 + } 1.3067 + currItem._committed = true; 1.3068 + } 1.3069 + if (this.oncommit) { 1.3070 + this.oncommit(this); 1.3071 + } 1.3072 +}; 1.3073 + 1.3074 +// Some variables are likely to contain a very large number of properties. 1.3075 +// It would be a bad idea to re-expand them or perform expensive operations. 1.3076 +VariablesView.prototype.commitHierarchyIgnoredItems = Heritage.extend(null, { 1.3077 + "window": true, 1.3078 + "this": true 1.3079 +}); 1.3080 + 1.3081 +/** 1.3082 + * Checks if the an item was previously expanded, if it existed in a 1.3083 + * previous hierarchy. 1.3084 + * 1.3085 + * @param Scope | Variable | Property aItem 1.3086 + * The item to verify. 1.3087 + * @return boolean 1.3088 + * Whether the item was expanded. 1.3089 + */ 1.3090 +VariablesView.prototype.wasExpanded = function(aItem) { 1.3091 + if (!(aItem instanceof Scope)) { 1.3092 + return false; 1.3093 + } 1.3094 + let prevItem = this._prevHierarchy.get(aItem._absoluteName || aItem._nameString); 1.3095 + return prevItem ? prevItem._isExpanded : false; 1.3096 +}; 1.3097 + 1.3098 +/** 1.3099 + * Checks if the an item's displayed value (a representation of the grip) 1.3100 + * has changed, if it existed in a previous hierarchy. 1.3101 + * 1.3102 + * @param Variable | Property aItem 1.3103 + * The item to verify. 1.3104 + * @return boolean 1.3105 + * Whether the item has changed. 1.3106 + */ 1.3107 +VariablesView.prototype.hasChanged = function(aItem) { 1.3108 + // Only analyze Variables and Properties for displayed value changes. 1.3109 + // Scopes are just collections of Variables and Properties and 1.3110 + // don't have a "value", so they can't change. 1.3111 + if (!(aItem instanceof Variable)) { 1.3112 + return false; 1.3113 + } 1.3114 + let prevItem = this._prevHierarchy.get(aItem._absoluteName); 1.3115 + return prevItem ? prevItem._valueString != aItem._valueString : false; 1.3116 +}; 1.3117 + 1.3118 +/** 1.3119 + * Checks if the an item was previously expanded, if it existed in a 1.3120 + * previous hierarchy. 1.3121 + * 1.3122 + * @param Scope | Variable | Property aItem 1.3123 + * The item to verify. 1.3124 + * @return boolean 1.3125 + * Whether the item was expanded. 1.3126 + */ 1.3127 +VariablesView.prototype.isOverridden = function(aItem) { 1.3128 + // Only analyze Variables for being overridden in different Scopes. 1.3129 + if (!(aItem instanceof Variable) || aItem instanceof Property) { 1.3130 + return false; 1.3131 + } 1.3132 + let currVariableName = aItem._nameString; 1.3133 + let parentScopes = this.getParentScopesForVariableOrProperty(aItem); 1.3134 + 1.3135 + for (let otherScope of parentScopes) { 1.3136 + for (let [otherVariableName] of otherScope) { 1.3137 + if (otherVariableName == currVariableName) { 1.3138 + return true; 1.3139 + } 1.3140 + } 1.3141 + } 1.3142 + return false; 1.3143 +}; 1.3144 + 1.3145 +/** 1.3146 + * Returns true if the descriptor represents an undefined, null or 1.3147 + * primitive value. 1.3148 + * 1.3149 + * @param object aDescriptor 1.3150 + * The variable's descriptor. 1.3151 + */ 1.3152 +VariablesView.isPrimitive = function(aDescriptor) { 1.3153 + // For accessor property descriptors, the getter and setter need to be 1.3154 + // contained in 'get' and 'set' properties. 1.3155 + let getter = aDescriptor.get; 1.3156 + let setter = aDescriptor.set; 1.3157 + if (getter || setter) { 1.3158 + return false; 1.3159 + } 1.3160 + 1.3161 + // As described in the remote debugger protocol, the value grip 1.3162 + // must be contained in a 'value' property. 1.3163 + let grip = aDescriptor.value; 1.3164 + if (typeof grip != "object") { 1.3165 + return true; 1.3166 + } 1.3167 + 1.3168 + // For convenience, undefined, null, Infinity, -Infinity, NaN, -0, and long 1.3169 + // strings are considered types. 1.3170 + let type = grip.type; 1.3171 + if (type == "undefined" || 1.3172 + type == "null" || 1.3173 + type == "Infinity" || 1.3174 + type == "-Infinity" || 1.3175 + type == "NaN" || 1.3176 + type == "-0" || 1.3177 + type == "longString") { 1.3178 + return true; 1.3179 + } 1.3180 + 1.3181 + return false; 1.3182 +}; 1.3183 + 1.3184 +/** 1.3185 + * Returns true if the descriptor represents an undefined value. 1.3186 + * 1.3187 + * @param object aDescriptor 1.3188 + * The variable's descriptor. 1.3189 + */ 1.3190 +VariablesView.isUndefined = function(aDescriptor) { 1.3191 + // For accessor property descriptors, the getter and setter need to be 1.3192 + // contained in 'get' and 'set' properties. 1.3193 + let getter = aDescriptor.get; 1.3194 + let setter = aDescriptor.set; 1.3195 + if (typeof getter == "object" && getter.type == "undefined" && 1.3196 + typeof setter == "object" && setter.type == "undefined") { 1.3197 + return true; 1.3198 + } 1.3199 + 1.3200 + // As described in the remote debugger protocol, the value grip 1.3201 + // must be contained in a 'value' property. 1.3202 + let grip = aDescriptor.value; 1.3203 + if (typeof grip == "object" && grip.type == "undefined") { 1.3204 + return true; 1.3205 + } 1.3206 + 1.3207 + return false; 1.3208 +}; 1.3209 + 1.3210 +/** 1.3211 + * Returns true if the descriptor represents a falsy value. 1.3212 + * 1.3213 + * @param object aDescriptor 1.3214 + * The variable's descriptor. 1.3215 + */ 1.3216 +VariablesView.isFalsy = function(aDescriptor) { 1.3217 + // As described in the remote debugger protocol, the value grip 1.3218 + // must be contained in a 'value' property. 1.3219 + let grip = aDescriptor.value; 1.3220 + if (typeof grip != "object") { 1.3221 + return !grip; 1.3222 + } 1.3223 + 1.3224 + // For convenience, undefined, null, NaN, and -0 are all considered types. 1.3225 + let type = grip.type; 1.3226 + if (type == "undefined" || 1.3227 + type == "null" || 1.3228 + type == "NaN" || 1.3229 + type == "-0") { 1.3230 + return true; 1.3231 + } 1.3232 + 1.3233 + return false; 1.3234 +}; 1.3235 + 1.3236 +/** 1.3237 + * Returns true if the value is an instance of Variable or Property. 1.3238 + * 1.3239 + * @param any aValue 1.3240 + * The value to test. 1.3241 + */ 1.3242 +VariablesView.isVariable = function(aValue) { 1.3243 + return aValue instanceof Variable; 1.3244 +}; 1.3245 + 1.3246 +/** 1.3247 + * Returns a standard grip for a value. 1.3248 + * 1.3249 + * @param any aValue 1.3250 + * The raw value to get a grip for. 1.3251 + * @return any 1.3252 + * The value's grip. 1.3253 + */ 1.3254 +VariablesView.getGrip = function(aValue) { 1.3255 + switch (typeof aValue) { 1.3256 + case "boolean": 1.3257 + case "string": 1.3258 + return aValue; 1.3259 + case "number": 1.3260 + if (aValue === Infinity) { 1.3261 + return { type: "Infinity" }; 1.3262 + } else if (aValue === -Infinity) { 1.3263 + return { type: "-Infinity" }; 1.3264 + } else if (Number.isNaN(aValue)) { 1.3265 + return { type: "NaN" }; 1.3266 + } else if (1 / aValue === -Infinity) { 1.3267 + return { type: "-0" }; 1.3268 + } 1.3269 + return aValue; 1.3270 + case "undefined": 1.3271 + // document.all is also "undefined" 1.3272 + if (aValue === undefined) { 1.3273 + return { type: "undefined" }; 1.3274 + } 1.3275 + case "object": 1.3276 + if (aValue === null) { 1.3277 + return { type: "null" }; 1.3278 + } 1.3279 + case "function": 1.3280 + return { type: "object", 1.3281 + class: WebConsoleUtils.getObjectClassName(aValue) }; 1.3282 + default: 1.3283 + Cu.reportError("Failed to provide a grip for value of " + typeof value + 1.3284 + ": " + aValue); 1.3285 + return null; 1.3286 + } 1.3287 +}; 1.3288 + 1.3289 +/** 1.3290 + * Returns a custom formatted property string for a grip. 1.3291 + * 1.3292 + * @param any aGrip 1.3293 + * @see Variable.setGrip 1.3294 + * @param object aOptions 1.3295 + * Options: 1.3296 + * - concise: boolean that tells you want a concisely formatted string. 1.3297 + * - noStringQuotes: boolean that tells to not quote strings. 1.3298 + * - noEllipsis: boolean that tells to not add an ellipsis after the 1.3299 + * initial text of a longString. 1.3300 + * @return string 1.3301 + * The formatted property string. 1.3302 + */ 1.3303 +VariablesView.getString = function(aGrip, aOptions = {}) { 1.3304 + if (aGrip && typeof aGrip == "object") { 1.3305 + switch (aGrip.type) { 1.3306 + case "undefined": 1.3307 + case "null": 1.3308 + case "NaN": 1.3309 + case "Infinity": 1.3310 + case "-Infinity": 1.3311 + case "-0": 1.3312 + return aGrip.type; 1.3313 + default: 1.3314 + let stringifier = VariablesView.stringifiers.byType[aGrip.type]; 1.3315 + if (stringifier) { 1.3316 + let result = stringifier(aGrip, aOptions); 1.3317 + if (result != null) { 1.3318 + return result; 1.3319 + } 1.3320 + } 1.3321 + 1.3322 + if (aGrip.displayString) { 1.3323 + return VariablesView.getString(aGrip.displayString, aOptions); 1.3324 + } 1.3325 + 1.3326 + if (aGrip.type == "object" && aOptions.concise) { 1.3327 + return aGrip.class; 1.3328 + } 1.3329 + 1.3330 + return "[" + aGrip.type + " " + aGrip.class + "]"; 1.3331 + } 1.3332 + } 1.3333 + 1.3334 + switch (typeof aGrip) { 1.3335 + case "string": 1.3336 + return VariablesView.stringifiers.byType.string(aGrip, aOptions); 1.3337 + case "boolean": 1.3338 + return aGrip ? "true" : "false"; 1.3339 + case "number": 1.3340 + if (!aGrip && 1 / aGrip === -Infinity) { 1.3341 + return "-0"; 1.3342 + } 1.3343 + default: 1.3344 + return aGrip + ""; 1.3345 + } 1.3346 +}; 1.3347 + 1.3348 +/** 1.3349 + * The VariablesView stringifiers are used by VariablesView.getString(). These 1.3350 + * are organized by object type, object class and by object actor preview kind. 1.3351 + * Some objects share identical ways for previews, for example Arrays, Sets and 1.3352 + * NodeLists. 1.3353 + * 1.3354 + * Any stringifier function must return a string. If null is returned, * then 1.3355 + * the default stringifier will be used. When invoked, the stringifier is 1.3356 + * given the same two arguments as those given to VariablesView.getString(). 1.3357 + */ 1.3358 +VariablesView.stringifiers = {}; 1.3359 + 1.3360 +VariablesView.stringifiers.byType = { 1.3361 + string: function(aGrip, {noStringQuotes}) { 1.3362 + if (noStringQuotes) { 1.3363 + return aGrip; 1.3364 + } 1.3365 + return '"' + aGrip + '"'; 1.3366 + }, 1.3367 + 1.3368 + longString: function({initial}, {noStringQuotes, noEllipsis}) { 1.3369 + let ellipsis = noEllipsis ? "" : Scope.ellipsis; 1.3370 + if (noStringQuotes) { 1.3371 + return initial + ellipsis; 1.3372 + } 1.3373 + let result = '"' + initial + '"'; 1.3374 + if (!ellipsis) { 1.3375 + return result; 1.3376 + } 1.3377 + return result.substr(0, result.length - 1) + ellipsis + '"'; 1.3378 + }, 1.3379 + 1.3380 + object: function(aGrip, aOptions) { 1.3381 + let {preview} = aGrip; 1.3382 + let stringifier; 1.3383 + if (preview && preview.kind) { 1.3384 + stringifier = VariablesView.stringifiers.byObjectKind[preview.kind]; 1.3385 + } 1.3386 + if (!stringifier && aGrip.class) { 1.3387 + stringifier = VariablesView.stringifiers.byObjectClass[aGrip.class]; 1.3388 + } 1.3389 + if (stringifier) { 1.3390 + return stringifier(aGrip, aOptions); 1.3391 + } 1.3392 + return null; 1.3393 + }, 1.3394 +}; // VariablesView.stringifiers.byType 1.3395 + 1.3396 +VariablesView.stringifiers.byObjectClass = { 1.3397 + Function: function(aGrip, {concise}) { 1.3398 + // TODO: Bug 948484 - support arrow functions and ES6 generators 1.3399 + 1.3400 + let name = aGrip.userDisplayName || aGrip.displayName || aGrip.name || ""; 1.3401 + name = VariablesView.getString(name, { noStringQuotes: true }); 1.3402 + 1.3403 + // TODO: Bug 948489 - Support functions with destructured parameters and 1.3404 + // rest parameters 1.3405 + let params = aGrip.parameterNames || ""; 1.3406 + if (!concise) { 1.3407 + return "function " + name + "(" + params + ")"; 1.3408 + } 1.3409 + return (name || "function ") + "(" + params + ")"; 1.3410 + }, 1.3411 + 1.3412 + RegExp: function({displayString}) { 1.3413 + return VariablesView.getString(displayString, { noStringQuotes: true }); 1.3414 + }, 1.3415 + 1.3416 + Date: function({preview}) { 1.3417 + if (!preview || !("timestamp" in preview)) { 1.3418 + return null; 1.3419 + } 1.3420 + 1.3421 + if (typeof preview.timestamp != "number") { 1.3422 + return new Date(preview.timestamp).toString(); // invalid date 1.3423 + } 1.3424 + 1.3425 + return "Date " + new Date(preview.timestamp).toISOString(); 1.3426 + }, 1.3427 + 1.3428 + String: function({displayString}) { 1.3429 + if (displayString === undefined) { 1.3430 + return null; 1.3431 + } 1.3432 + return VariablesView.getString(displayString); 1.3433 + }, 1.3434 + 1.3435 + Number: function({preview}) { 1.3436 + if (preview === undefined) { 1.3437 + return null; 1.3438 + } 1.3439 + return VariablesView.getString(preview.value); 1.3440 + }, 1.3441 +}; // VariablesView.stringifiers.byObjectClass 1.3442 + 1.3443 +VariablesView.stringifiers.byObjectClass.Boolean = 1.3444 + VariablesView.stringifiers.byObjectClass.Number; 1.3445 + 1.3446 +VariablesView.stringifiers.byObjectKind = { 1.3447 + ArrayLike: function(aGrip, {concise}) { 1.3448 + let {preview} = aGrip; 1.3449 + if (concise) { 1.3450 + return aGrip.class + "[" + preview.length + "]"; 1.3451 + } 1.3452 + 1.3453 + if (!preview.items) { 1.3454 + return null; 1.3455 + } 1.3456 + 1.3457 + let shown = 0, result = [], lastHole = null; 1.3458 + for (let item of preview.items) { 1.3459 + if (item === null) { 1.3460 + if (lastHole !== null) { 1.3461 + result[lastHole] += ","; 1.3462 + } else { 1.3463 + result.push(""); 1.3464 + } 1.3465 + lastHole = result.length - 1; 1.3466 + } else { 1.3467 + lastHole = null; 1.3468 + result.push(VariablesView.getString(item, { concise: true })); 1.3469 + } 1.3470 + shown++; 1.3471 + } 1.3472 + 1.3473 + if (shown < preview.length) { 1.3474 + let n = preview.length - shown; 1.3475 + result.push(VariablesView.stringifiers._getNMoreString(n)); 1.3476 + } else if (lastHole !== null) { 1.3477 + // make sure we have the right number of commas... 1.3478 + result[lastHole] += ","; 1.3479 + } 1.3480 + 1.3481 + let prefix = aGrip.class == "Array" ? "" : aGrip.class + " "; 1.3482 + return prefix + "[" + result.join(", ") + "]"; 1.3483 + }, 1.3484 + 1.3485 + MapLike: function(aGrip, {concise}) { 1.3486 + let {preview} = aGrip; 1.3487 + if (concise || !preview.entries) { 1.3488 + let size = typeof preview.size == "number" ? 1.3489 + "[" + preview.size + "]" : ""; 1.3490 + return aGrip.class + size; 1.3491 + } 1.3492 + 1.3493 + let entries = []; 1.3494 + for (let [key, value] of preview.entries) { 1.3495 + let keyString = VariablesView.getString(key, { 1.3496 + concise: true, 1.3497 + noStringQuotes: true, 1.3498 + }); 1.3499 + let valueString = VariablesView.getString(value, { concise: true }); 1.3500 + entries.push(keyString + ": " + valueString); 1.3501 + } 1.3502 + 1.3503 + if (typeof preview.size == "number" && preview.size > entries.length) { 1.3504 + let n = preview.size - entries.length; 1.3505 + entries.push(VariablesView.stringifiers._getNMoreString(n)); 1.3506 + } 1.3507 + 1.3508 + return aGrip.class + " {" + entries.join(", ") + "}"; 1.3509 + }, 1.3510 + 1.3511 + ObjectWithText: function(aGrip, {concise}) { 1.3512 + if (concise) { 1.3513 + return aGrip.class; 1.3514 + } 1.3515 + 1.3516 + return aGrip.class + " " + VariablesView.getString(aGrip.preview.text); 1.3517 + }, 1.3518 + 1.3519 + ObjectWithURL: function(aGrip, {concise}) { 1.3520 + let result = aGrip.class; 1.3521 + let url = aGrip.preview.url; 1.3522 + if (!VariablesView.isFalsy({ value: url })) { 1.3523 + result += " \u2192 " + WebConsoleUtils.abbreviateSourceURL(url, 1.3524 + { onlyCropQuery: !concise }); 1.3525 + } 1.3526 + return result; 1.3527 + }, 1.3528 + 1.3529 + // Stringifier for any kind of object. 1.3530 + Object: function(aGrip, {concise}) { 1.3531 + if (concise) { 1.3532 + return aGrip.class; 1.3533 + } 1.3534 + 1.3535 + let {preview} = aGrip; 1.3536 + let props = []; 1.3537 + for (let key of Object.keys(preview.ownProperties || {})) { 1.3538 + let value = preview.ownProperties[key]; 1.3539 + let valueString = ""; 1.3540 + if (value.get) { 1.3541 + valueString = "Getter"; 1.3542 + } else if (value.set) { 1.3543 + valueString = "Setter"; 1.3544 + } else { 1.3545 + valueString = VariablesView.getString(value.value, { concise: true }); 1.3546 + } 1.3547 + props.push(key + ": " + valueString); 1.3548 + } 1.3549 + 1.3550 + for (let key of Object.keys(preview.safeGetterValues || {})) { 1.3551 + let value = preview.safeGetterValues[key]; 1.3552 + let valueString = VariablesView.getString(value.getterValue, 1.3553 + { concise: true }); 1.3554 + props.push(key + ": " + valueString); 1.3555 + } 1.3556 + 1.3557 + if (!props.length) { 1.3558 + return null; 1.3559 + } 1.3560 + 1.3561 + if (preview.ownPropertiesLength) { 1.3562 + let previewLength = Object.keys(preview.ownProperties).length; 1.3563 + let diff = preview.ownPropertiesLength - previewLength; 1.3564 + if (diff > 0) { 1.3565 + props.push(VariablesView.stringifiers._getNMoreString(diff)); 1.3566 + } 1.3567 + } 1.3568 + 1.3569 + let prefix = aGrip.class != "Object" ? aGrip.class + " " : ""; 1.3570 + return prefix + "{" + props.join(", ") + "}"; 1.3571 + }, // Object 1.3572 + 1.3573 + Error: function(aGrip, {concise}) { 1.3574 + let {preview} = aGrip; 1.3575 + let name = VariablesView.getString(preview.name, { noStringQuotes: true }); 1.3576 + if (concise) { 1.3577 + return name || aGrip.class; 1.3578 + } 1.3579 + 1.3580 + let msg = name + ": " + 1.3581 + VariablesView.getString(preview.message, { noStringQuotes: true }); 1.3582 + 1.3583 + if (!VariablesView.isFalsy({ value: preview.stack })) { 1.3584 + msg += "\n" + STR.GetStringFromName("variablesViewErrorStacktrace") + 1.3585 + "\n" + preview.stack; 1.3586 + } 1.3587 + 1.3588 + return msg; 1.3589 + }, 1.3590 + 1.3591 + DOMException: function(aGrip, {concise}) { 1.3592 + let {preview} = aGrip; 1.3593 + if (concise) { 1.3594 + return preview.name || aGrip.class; 1.3595 + } 1.3596 + 1.3597 + let msg = aGrip.class + " [" + preview.name + ": " + 1.3598 + VariablesView.getString(preview.message) + "\n" + 1.3599 + "code: " + preview.code + "\n" + 1.3600 + "nsresult: 0x" + (+preview.result).toString(16); 1.3601 + 1.3602 + if (preview.filename) { 1.3603 + msg += "\nlocation: " + preview.filename; 1.3604 + if (preview.lineNumber) { 1.3605 + msg += ":" + preview.lineNumber; 1.3606 + } 1.3607 + } 1.3608 + 1.3609 + return msg + "]"; 1.3610 + }, 1.3611 + 1.3612 + DOMEvent: function(aGrip, {concise}) { 1.3613 + let {preview} = aGrip; 1.3614 + if (!preview.type) { 1.3615 + return null; 1.3616 + } 1.3617 + 1.3618 + if (concise) { 1.3619 + return aGrip.class + " " + preview.type; 1.3620 + } 1.3621 + 1.3622 + let result = preview.type; 1.3623 + 1.3624 + if (preview.eventKind == "key" && preview.modifiers && 1.3625 + preview.modifiers.length) { 1.3626 + result += " " + preview.modifiers.join("-"); 1.3627 + } 1.3628 + 1.3629 + let props = []; 1.3630 + if (preview.target) { 1.3631 + let target = VariablesView.getString(preview.target, { concise: true }); 1.3632 + props.push("target: " + target); 1.3633 + } 1.3634 + 1.3635 + for (let prop in preview.properties) { 1.3636 + let value = preview.properties[prop]; 1.3637 + props.push(prop + ": " + VariablesView.getString(value, { concise: true })); 1.3638 + } 1.3639 + 1.3640 + return result + " {" + props.join(", ") + "}"; 1.3641 + }, // DOMEvent 1.3642 + 1.3643 + DOMNode: function(aGrip, {concise}) { 1.3644 + let {preview} = aGrip; 1.3645 + 1.3646 + switch (preview.nodeType) { 1.3647 + case Ci.nsIDOMNode.DOCUMENT_NODE: { 1.3648 + let location = WebConsoleUtils.abbreviateSourceURL(preview.location, 1.3649 + { onlyCropQuery: !concise }); 1.3650 + return aGrip.class + " \u2192 " + location; 1.3651 + } 1.3652 + 1.3653 + case Ci.nsIDOMNode.ATTRIBUTE_NODE: { 1.3654 + let value = VariablesView.getString(preview.value, { noStringQuotes: true }); 1.3655 + return preview.nodeName + '="' + escapeHTML(value) + '"'; 1.3656 + } 1.3657 + 1.3658 + case Ci.nsIDOMNode.TEXT_NODE: 1.3659 + return preview.nodeName + " " + 1.3660 + VariablesView.getString(preview.textContent); 1.3661 + 1.3662 + case Ci.nsIDOMNode.COMMENT_NODE: { 1.3663 + let comment = VariablesView.getString(preview.textContent, 1.3664 + { noStringQuotes: true }); 1.3665 + return "<!--" + comment + "-->"; 1.3666 + } 1.3667 + 1.3668 + case Ci.nsIDOMNode.DOCUMENT_FRAGMENT_NODE: { 1.3669 + if (concise || !preview.childNodes) { 1.3670 + return aGrip.class + "[" + preview.childNodesLength + "]"; 1.3671 + } 1.3672 + let nodes = []; 1.3673 + for (let node of preview.childNodes) { 1.3674 + nodes.push(VariablesView.getString(node)); 1.3675 + } 1.3676 + if (nodes.length < preview.childNodesLength) { 1.3677 + let n = preview.childNodesLength - nodes.length; 1.3678 + nodes.push(VariablesView.stringifiers._getNMoreString(n)); 1.3679 + } 1.3680 + return aGrip.class + " [" + nodes.join(", ") + "]"; 1.3681 + } 1.3682 + 1.3683 + case Ci.nsIDOMNode.ELEMENT_NODE: { 1.3684 + let attrs = preview.attributes; 1.3685 + if (!concise) { 1.3686 + let n = 0, result = "<" + preview.nodeName; 1.3687 + for (let name in attrs) { 1.3688 + let value = VariablesView.getString(attrs[name], 1.3689 + { noStringQuotes: true }); 1.3690 + result += " " + name + '="' + escapeHTML(value) + '"'; 1.3691 + n++; 1.3692 + } 1.3693 + if (preview.attributesLength > n) { 1.3694 + result += " " + Scope.ellipsis; 1.3695 + } 1.3696 + return result + ">"; 1.3697 + } 1.3698 + 1.3699 + let result = "<" + preview.nodeName; 1.3700 + if (attrs.id) { 1.3701 + result += "#" + attrs.id; 1.3702 + } 1.3703 + return result + ">"; 1.3704 + } 1.3705 + 1.3706 + default: 1.3707 + return null; 1.3708 + } 1.3709 + }, // DOMNode 1.3710 +}; // VariablesView.stringifiers.byObjectKind 1.3711 + 1.3712 + 1.3713 +/** 1.3714 + * Get the "N moreā¦" formatted string, given an N. This is used for displaying 1.3715 + * how many elements are not displayed in an object preview (eg. an array). 1.3716 + * 1.3717 + * @private 1.3718 + * @param number aNumber 1.3719 + * @return string 1.3720 + */ 1.3721 +VariablesView.stringifiers._getNMoreString = function(aNumber) { 1.3722 + let str = STR.GetStringFromName("variablesViewMoreObjects"); 1.3723 + return PluralForm.get(aNumber, str).replace("#1", aNumber); 1.3724 +}; 1.3725 + 1.3726 +/** 1.3727 + * Returns a custom class style for a grip. 1.3728 + * 1.3729 + * @param any aGrip 1.3730 + * @see Variable.setGrip 1.3731 + * @return string 1.3732 + * The custom class style. 1.3733 + */ 1.3734 +VariablesView.getClass = function(aGrip) { 1.3735 + if (aGrip && typeof aGrip == "object") { 1.3736 + if (aGrip.preview) { 1.3737 + switch (aGrip.preview.kind) { 1.3738 + case "DOMNode": 1.3739 + return "token-domnode"; 1.3740 + } 1.3741 + } 1.3742 + 1.3743 + switch (aGrip.type) { 1.3744 + case "undefined": 1.3745 + return "token-undefined"; 1.3746 + case "null": 1.3747 + return "token-null"; 1.3748 + case "Infinity": 1.3749 + case "-Infinity": 1.3750 + case "NaN": 1.3751 + case "-0": 1.3752 + return "token-number"; 1.3753 + case "longString": 1.3754 + return "token-string"; 1.3755 + } 1.3756 + } 1.3757 + switch (typeof aGrip) { 1.3758 + case "string": 1.3759 + return "token-string"; 1.3760 + case "boolean": 1.3761 + return "token-boolean"; 1.3762 + case "number": 1.3763 + return "token-number"; 1.3764 + default: 1.3765 + return "token-other"; 1.3766 + } 1.3767 +}; 1.3768 + 1.3769 +/** 1.3770 + * A monotonically-increasing counter, that guarantees the uniqueness of scope, 1.3771 + * variables and properties ids. 1.3772 + * 1.3773 + * @param string aName 1.3774 + * An optional string to prefix the id with. 1.3775 + * @return number 1.3776 + * A unique id. 1.3777 + */ 1.3778 +let generateId = (function() { 1.3779 + let count = 0; 1.3780 + return function(aName = "") { 1.3781 + return aName.toLowerCase().trim().replace(/\s+/g, "-") + (++count); 1.3782 + }; 1.3783 +})(); 1.3784 + 1.3785 +/** 1.3786 + * Escape some HTML special characters. We do not need full HTML serialization 1.3787 + * here, we just want to make strings safe to display in HTML attributes, for 1.3788 + * the stringifiers. 1.3789 + * 1.3790 + * @param string aString 1.3791 + * @return string 1.3792 + */ 1.3793 +function escapeHTML(aString) { 1.3794 + return aString.replace(/&/g, "&") 1.3795 + .replace(/"/g, """) 1.3796 + .replace(/</g, "<") 1.3797 + .replace(/>/g, ">"); 1.3798 +} 1.3799 + 1.3800 + 1.3801 +/** 1.3802 + * An Editable encapsulates the UI of an edit box that overlays a label, 1.3803 + * allowing the user to edit the value. 1.3804 + * 1.3805 + * @param Variable aVariable 1.3806 + * The Variable or Property to make editable. 1.3807 + * @param object aOptions 1.3808 + * - onSave 1.3809 + * The callback to call with the value when editing is complete. 1.3810 + * - onCleanup 1.3811 + * The callback to call when the editable is removed for any reason. 1.3812 + */ 1.3813 +function Editable(aVariable, aOptions) { 1.3814 + this._variable = aVariable; 1.3815 + this._onSave = aOptions.onSave; 1.3816 + this._onCleanup = aOptions.onCleanup; 1.3817 +} 1.3818 + 1.3819 +Editable.create = function(aVariable, aOptions, aEvent) { 1.3820 + let editable = new this(aVariable, aOptions); 1.3821 + editable.activate(aEvent); 1.3822 + return editable; 1.3823 +}; 1.3824 + 1.3825 +Editable.prototype = { 1.3826 + /** 1.3827 + * The class name for targeting this Editable type's label element. Overridden 1.3828 + * by inheriting classes. 1.3829 + */ 1.3830 + className: null, 1.3831 + 1.3832 + /** 1.3833 + * Boolean indicating whether this Editable should activate. Overridden by 1.3834 + * inheriting classes. 1.3835 + */ 1.3836 + shouldActivate: null, 1.3837 + 1.3838 + /** 1.3839 + * The label element for this Editable. Overridden by inheriting classes. 1.3840 + */ 1.3841 + label: null, 1.3842 + 1.3843 + /** 1.3844 + * Activate this editable by replacing the input box it overlays and 1.3845 + * initialize the handlers. 1.3846 + * 1.3847 + * @param Event e [optional] 1.3848 + * Optionally, the Event object that was used to activate the Editable. 1.3849 + */ 1.3850 + activate: function(e) { 1.3851 + if (!this.shouldActivate) { 1.3852 + this._onCleanup && this._onCleanup(); 1.3853 + return; 1.3854 + } 1.3855 + 1.3856 + let { label } = this; 1.3857 + let initialString = label.getAttribute("value"); 1.3858 + 1.3859 + if (e) { 1.3860 + e.preventDefault(); 1.3861 + e.stopPropagation(); 1.3862 + } 1.3863 + 1.3864 + // Create a texbox input element which will be shown in the current 1.3865 + // element's specified label location. 1.3866 + let input = this._input = this._variable.document.createElement("textbox"); 1.3867 + input.className = "plain " + this.className; 1.3868 + input.setAttribute("value", initialString); 1.3869 + input.setAttribute("flex", "1"); 1.3870 + 1.3871 + // Replace the specified label with a textbox input element. 1.3872 + label.parentNode.replaceChild(input, label); 1.3873 + this._variable._variablesView.boxObject.ensureElementIsVisible(input); 1.3874 + input.select(); 1.3875 + 1.3876 + // When the value is a string (displayed as "value"), then we probably want 1.3877 + // to change it to another string in the textbox, so to avoid typing the "" 1.3878 + // again, tackle with the selection bounds just a bit. 1.3879 + if (initialString.match(/^".+"$/)) { 1.3880 + input.selectionEnd--; 1.3881 + input.selectionStart++; 1.3882 + } 1.3883 + 1.3884 + this._onKeypress = this._onKeypress.bind(this); 1.3885 + this._onBlur = this._onBlur.bind(this); 1.3886 + input.addEventListener("keypress", this._onKeypress); 1.3887 + input.addEventListener("blur", this._onBlur); 1.3888 + 1.3889 + this._prevExpandable = this._variable.twisty; 1.3890 + this._prevExpanded = this._variable.expanded; 1.3891 + this._variable.collapse(); 1.3892 + this._variable.hideArrow(); 1.3893 + this._variable.locked = true; 1.3894 + this._variable.editing = true; 1.3895 + }, 1.3896 + 1.3897 + /** 1.3898 + * Remove the input box and restore the Variable or Property to its previous 1.3899 + * state. 1.3900 + */ 1.3901 + deactivate: function() { 1.3902 + this._input.removeEventListener("keypress", this._onKeypress); 1.3903 + this._input.removeEventListener("blur", this.deactivate); 1.3904 + this._input.parentNode.replaceChild(this.label, this._input); 1.3905 + this._input = null; 1.3906 + 1.3907 + let { boxObject } = this._variable._variablesView; 1.3908 + boxObject.scrollBy(-this._variable._target, 0); 1.3909 + this._variable.locked = false; 1.3910 + this._variable.twisty = this._prevExpandable; 1.3911 + this._variable.expanded = this._prevExpanded; 1.3912 + this._variable.editing = false; 1.3913 + this._onCleanup && this._onCleanup(); 1.3914 + }, 1.3915 + 1.3916 + /** 1.3917 + * Save the current value and deactivate the Editable. 1.3918 + */ 1.3919 + _save: function() { 1.3920 + let initial = this.label.getAttribute("value"); 1.3921 + let current = this._input.value.trim(); 1.3922 + this.deactivate(); 1.3923 + if (initial != current) { 1.3924 + this._onSave(current); 1.3925 + } 1.3926 + }, 1.3927 + 1.3928 + /** 1.3929 + * Called when tab is pressed, allowing subclasses to link different 1.3930 + * behavior to tabbing if desired. 1.3931 + */ 1.3932 + _next: function() { 1.3933 + this._save(); 1.3934 + }, 1.3935 + 1.3936 + /** 1.3937 + * Called when escape is pressed, indicating a cancelling of editing without 1.3938 + * saving. 1.3939 + */ 1.3940 + _reset: function() { 1.3941 + this.deactivate(); 1.3942 + this._variable.focus(); 1.3943 + }, 1.3944 + 1.3945 + /** 1.3946 + * Event handler for when the input loses focus. 1.3947 + */ 1.3948 + _onBlur: function() { 1.3949 + this.deactivate(); 1.3950 + }, 1.3951 + 1.3952 + /** 1.3953 + * Event handler for when the input receives a key press. 1.3954 + */ 1.3955 + _onKeypress: function(e) { 1.3956 + e.stopPropagation(); 1.3957 + 1.3958 + switch (e.keyCode) { 1.3959 + case e.DOM_VK_TAB: 1.3960 + this._next(); 1.3961 + break; 1.3962 + case e.DOM_VK_RETURN: 1.3963 + this._save(); 1.3964 + break; 1.3965 + case e.DOM_VK_ESCAPE: 1.3966 + this._reset(); 1.3967 + break; 1.3968 + } 1.3969 + }, 1.3970 +}; 1.3971 + 1.3972 + 1.3973 +/** 1.3974 + * An Editable specific to editing the name of a Variable or Property. 1.3975 + */ 1.3976 +function EditableName(aVariable, aOptions) { 1.3977 + Editable.call(this, aVariable, aOptions); 1.3978 +} 1.3979 + 1.3980 +EditableName.create = Editable.create; 1.3981 + 1.3982 +EditableName.prototype = Heritage.extend(Editable.prototype, { 1.3983 + className: "element-name-input", 1.3984 + 1.3985 + get label() { 1.3986 + return this._variable._name; 1.3987 + }, 1.3988 + 1.3989 + get shouldActivate() { 1.3990 + return !!this._variable.ownerView.switch; 1.3991 + }, 1.3992 +}); 1.3993 + 1.3994 + 1.3995 +/** 1.3996 + * An Editable specific to editing the value of a Variable or Property. 1.3997 + */ 1.3998 +function EditableValue(aVariable, aOptions) { 1.3999 + Editable.call(this, aVariable, aOptions); 1.4000 +} 1.4001 + 1.4002 +EditableValue.create = Editable.create; 1.4003 + 1.4004 +EditableValue.prototype = Heritage.extend(Editable.prototype, { 1.4005 + className: "element-value-input", 1.4006 + 1.4007 + get label() { 1.4008 + return this._variable._valueLabel; 1.4009 + }, 1.4010 + 1.4011 + get shouldActivate() { 1.4012 + return !!this._variable.ownerView.eval; 1.4013 + }, 1.4014 +}); 1.4015 + 1.4016 + 1.4017 +/** 1.4018 + * An Editable specific to editing the key and value of a new property. 1.4019 + */ 1.4020 +function EditableNameAndValue(aVariable, aOptions) { 1.4021 + EditableName.call(this, aVariable, aOptions); 1.4022 +} 1.4023 + 1.4024 +EditableNameAndValue.create = Editable.create; 1.4025 + 1.4026 +EditableNameAndValue.prototype = Heritage.extend(EditableName.prototype, { 1.4027 + _reset: function(e) { 1.4028 + // Hide the Variable or Property if the user presses escape. 1.4029 + this._variable.remove(); 1.4030 + this.deactivate(); 1.4031 + }, 1.4032 + 1.4033 + _next: function(e) { 1.4034 + // Override _next so as to set both key and value at the same time. 1.4035 + let key = this._input.value; 1.4036 + this.label.setAttribute("value", key); 1.4037 + 1.4038 + let valueEditable = EditableValue.create(this._variable, { 1.4039 + onSave: aValue => { 1.4040 + this._onSave([key, aValue]); 1.4041 + } 1.4042 + }); 1.4043 + valueEditable._reset = () => { 1.4044 + this._variable.remove(); 1.4045 + valueEditable.deactivate(); 1.4046 + }; 1.4047 + }, 1.4048 + 1.4049 + _save: function(e) { 1.4050 + // Both _save and _next activate the value edit box. 1.4051 + this._next(e); 1.4052 + } 1.4053 +});