1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WI.ObjectTreePropertyTreeElement = class ObjectTreePropertyTreeElement extends WI.ObjectTreeBaseTreeElement
{
constructor(property, propertyPath, mode, prototypeName)
{
super(property, propertyPath, property);
this._mode = mode || WI.ObjectTreeView.Mode.Properties;
this._prototypeName = prototypeName;
this.mainTitle = this._titleFragment();
this.addClassName("object-tree-property");
if (this.property.hasValue()) {
this.addClassName(this.property.value.type);
if (this.property.value.subtype)
this.addClassName(this.property.value.subtype);
} else
this.addClassName("accessor");
if (this.property.wasThrown)
this.addClassName("had-error");
if (this.property.name === "__proto__")
this.addClassName("prototype-property");
this._updateTooltips();
this._updateHasChildren();
}
// Protected
onpopulate()
{
this._updateChildren();
}
onexpand()
{
if (this._previewView)
this._previewView.showTitle();
}
oncollapse()
{
if (this._previewView)
this._previewView.showPreview();
}
invokedGetter()
{
this.mainTitle = this._titleFragment();
var resolvedValue = this.resolvedValue();
this.addClassName(resolvedValue.type);
if (resolvedValue.subtype)
this.addClassName(resolvedValue.subtype);
if (this.hadError())
this.addClassName("had-error");
this.removeClassName("accessor");
this._updateHasChildren();
}
// Private
_updateHasChildren()
{
var resolvedValue = this.resolvedValue();
var valueHasChildren = (resolvedValue && resolvedValue.hasChildren);
var wasThrown = this.hadError();
if (this._mode === WI.ObjectTreeView.Mode.Properties)
this.hasChildren = !wasThrown && valueHasChildren;
else
this.hasChildren = !wasThrown && valueHasChildren && (this.property.name === "__proto__" || this._alwaysDisplayAsProperty());
}
_updateTooltips()
{
var attributes = [];
if (this.property.configurable)
attributes.push("configurable");
if (this.property.enumerable)
attributes.push("enumerable");
if (this.property.writable)
attributes.push("writable");
this.iconElement.title = attributes.join(" ");
}
_titleFragment()
{
if (this.property.name === "__proto__")
return this._createTitlePrototype();
if (this._mode === WI.ObjectTreeView.Mode.Properties)
return this._createTitlePropertyStyle();
else
return this._createTitleAPIStyle();
}
_createTitlePrototype()
{
console.assert(this.property.hasValue());
console.assert(this.property.name === "__proto__");
var nameElement = document.createElement("span");
nameElement.className = "prototype-name";
nameElement.textContent = WI.UIString("%s Prototype").format(this._sanitizedPrototypeString(this.property.value));
nameElement.title = this.propertyPathString(this.thisPropertyPath());
return nameElement;
}
_createTitlePropertyStyle()
{
var container = document.createDocumentFragment();
// Property name.
var nameElement = document.createElement("span");
nameElement.className = "property-name";
nameElement.textContent = this.property.name + ": ";
nameElement.title = this.propertyPathString(this.thisPropertyPath());
// Property attributes.
if (this._mode === WI.ObjectTreeView.Mode.Properties) {
if (!this.property.enumerable)
nameElement.classList.add("not-enumerable");
}
// Value / Getter Value / Getter.
var valueOrGetterElement;
var resolvedValue = this.resolvedValue();
if (resolvedValue) {
if (resolvedValue.preview) {
this._previewView = new WI.ObjectPreviewView(resolvedValue.preview);
valueOrGetterElement = this._previewView.element;
} else {
this._loadPreviewLazilyIfNeeded();
valueOrGetterElement = WI.FormattedValue.createElementForRemoteObject(resolvedValue, this.hadError());
// Special case a function property string.
if (resolvedValue.type === "function")
valueOrGetterElement.textContent = this._functionPropertyString();
}
} else {
valueOrGetterElement = document.createElement("span");
if (this.property.hasGetter())
valueOrGetterElement.appendChild(this.createGetterElement(this._mode !== WI.ObjectTreeView.Mode.ClassAPI));
if (this.property.hasSetter())
valueOrGetterElement.appendChild(this.createSetterElement());
}
valueOrGetterElement.classList.add("value");
if (this.hadError())
valueOrGetterElement.classList.add("error");
container.appendChild(nameElement);
container.appendChild(valueOrGetterElement);
return container;
}
_createTitleAPIStyle()
{
// Fixed values and special properties display like a property.
if (this._alwaysDisplayAsProperty())
return this._createTitlePropertyStyle();
// No API to display.
var isFunction = this.property.hasValue() && this.property.value.type === "function";
if (!isFunction && !this.property.hasGetter() && !this.property.hasSetter())
return null;
var container = document.createDocumentFragment();
// Function / Getter / Setter.
var nameElement = document.createElement("span");
nameElement.className = "property-name";
nameElement.textContent = this.property.name;
nameElement.title = this.propertyPathString(this.thisPropertyPath());
container.appendChild(nameElement);
if (isFunction) {
var paramElement = document.createElement("span");
paramElement.className = "function-parameters";
paramElement.textContent = this._functionParameterString();
container.appendChild(paramElement);
} else {
var spacer = container.appendChild(document.createElement("span"));
spacer.className = "spacer";
if (this.property.hasGetter())
container.appendChild(this.createGetterElement(this._mode !== WI.ObjectTreeView.Mode.ClassAPI));
if (this.property.hasSetter())
container.appendChild(this.createSetterElement());
}
return container;
}
_loadPreviewLazilyIfNeeded()
{
let resolvedValue = this.resolvedValue();
if (!resolvedValue.canLoadPreview())
return;
resolvedValue.updatePreview((preview) => {
if (preview) {
this.mainTitle = this._titleFragment();
if (this.expanded)
this._previewView.showTitle();
}
});
}
_alwaysDisplayAsProperty()
{
// Constructor, though a function, is often better treated as an expandable object.
if (this.property.name === "constructor")
return true;
// Non-function objects are often better treated as properties.
if (this.property.hasValue() && this.property.value.type !== "function")
return true;
// Fetched getter value.
if (this._getterValue)
return true;
return false;
}
_functionPropertyString()
{
return "function" + this._functionParameterString();
}
_functionParameterString()
{
var resolvedValue = this.resolvedValue();
console.assert(resolvedValue.type === "function");
// For Native methods, the toString is poor. We try to provide good function parameter strings.
if (isFunctionStringNativeCode(resolvedValue.description)) {
// Native function on a prototype, likely "Foo.prototype.method".
if (this._prototypeName) {
if (WI.NativePrototypeFunctionParameters[this._prototypeName]) {
var params = WI.NativePrototypeFunctionParameters[this._prototypeName][this._property.name];
return params ? "(" + params + ")" : "()";
}
}
var parentDescription = this._propertyPath.object.description;
// Native function property on a native function is likely a "Foo.method".
if (isFunctionStringNativeCode(parentDescription)) {
var match = parentDescription.match(/^function\s+([^)]+?)\(/);
if (match) {
var name = match[1];
if (WI.NativeConstructorFunctionParameters[name]) {
var params = WI.NativeConstructorFunctionParameters[name][this._property.name];
return params ? "(" + params + ")" : "()";
}
}
}
// Native DOM constructor or on native objects that are not functions.
if (parentDescription.endsWith("Constructor") || ["Math", "JSON", "Reflect", "Console"].includes(parentDescription)) {
var name = parentDescription;
if (WI.NativeConstructorFunctionParameters[name]) {
var params = WI.NativeConstructorFunctionParameters[name][this._property.name];
return params ? "(" + params + ")" : "()";
}
}
}
var match = resolvedValue.functionDescription.match(/^function.*?(\([^)]*?\))/);
return match ? match[1] : "()";
}
_sanitizedPrototypeString(value)
{
// FIXME: <https://webkit.org/b/141610> For many X, X.prototype is an X when it must be a plain object
if (value.type === "function")
return "Function";
if (value.subtype === "date")
return "Date";
if (value.subtype === "regexp")
return "RegExp";
return value.description.replace(/Prototype$/, "");
}
_updateChildren()
{
if (this.children.length && !this.shouldRefreshChildren)
return;
var resolvedValue = this.resolvedValue();
if (resolvedValue.isCollectionType() && this._mode === WI.ObjectTreeView.Mode.Properties)
resolvedValue.getCollectionEntries(0, 100, this._updateChildrenInternal.bind(this, this._updateEntries, this._mode));
else if (this._mode === WI.ObjectTreeView.Mode.ClassAPI || this._mode === WI.ObjectTreeView.Mode.PureAPI)
resolvedValue.getOwnPropertyDescriptors(this._updateChildrenInternal.bind(this, this._updateProperties, WI.ObjectTreeView.Mode.ClassAPI));
else if (this.property.name === "__proto__")
resolvedValue.getOwnPropertyDescriptors(this._updateChildrenInternal.bind(this, this._updateProperties, WI.ObjectTreeView.Mode.PrototypeAPI));
else
resolvedValue.getDisplayablePropertyDescriptors(this._updateChildrenInternal.bind(this, this._updateProperties, this._mode));
}
_updateChildrenInternal(handler, mode, list)
{
this.removeChildren();
if (!list) {
var errorMessageElement = WI.ObjectTreeView.createEmptyMessageElement(WI.UIString("Could not fetch properties. Object may no longer exist."));
this.appendChild(new WI.TreeElement(errorMessageElement, null, false));
return;
}
handler.call(this, list, this.resolvedValuePropertyPath(), mode);
}
_updateEntries(entries, propertyPath, mode)
{
for (var entry of entries) {
if (entry.key) {
this.appendChild(new WI.ObjectTreeMapKeyTreeElement(entry.key, propertyPath));
this.appendChild(new WI.ObjectTreeMapValueTreeElement(entry.value, propertyPath, entry.key));
} else
this.appendChild(new WI.ObjectTreeSetIndexTreeElement(entry.value, propertyPath));
}
if (!this.children.length) {
var emptyMessageElement = WI.ObjectTreeView.createEmptyMessageElement(WI.UIString("No Entries"));
this.appendChild(new WI.TreeElement(emptyMessageElement, null, false));
}
// Show the prototype so users can see the API.
var resolvedValue = this.resolvedValue();
resolvedValue.getOwnPropertyDescriptor("__proto__", (propertyDescriptor) => {
if (propertyDescriptor)
this.appendChild(new WI.ObjectTreePropertyTreeElement(propertyDescriptor, propertyPath, mode));
});
}
_updateProperties(properties, propertyPath, mode)
{
properties.sort(WI.ObjectTreeView.comparePropertyDescriptors);
var resolvedValue = this.resolvedValue();
var isArray = resolvedValue.isArray();
var isPropertyMode = mode === WI.ObjectTreeView.Mode.Properties || this._getterValue;
var isAPI = mode !== WI.ObjectTreeView.Mode.Properties;
var prototypeName;
if (this.property.name === "__proto__") {
if (resolvedValue.description)
prototypeName = this._sanitizedPrototypeString(resolvedValue);
}
var hadProto = false;
for (var propertyDescriptor of properties) {
// FIXME: If this is a pure API ObjectTree, we should show the native getters.
// For now, just skip native binding getters in API mode, since we likely
// already showed them in the Properties section.
if (isAPI && propertyDescriptor.nativeGetter)
continue;
// COMPATIBILITY (iOS 8): Sometimes __proto__ is not a value, but a get/set property.
// In those cases it is actually not useful to show.
if (propertyDescriptor.name === "__proto__" && !propertyDescriptor.hasValue())
continue;
if (isArray && isPropertyMode) {
if (propertyDescriptor.isIndexProperty())
this.appendChild(new WI.ObjectTreeArrayIndexTreeElement(propertyDescriptor, propertyPath));
else if (propertyDescriptor.name === "__proto__")
this.appendChild(new WI.ObjectTreePropertyTreeElement(propertyDescriptor, propertyPath, mode, prototypeName));
} else
this.appendChild(new WI.ObjectTreePropertyTreeElement(propertyDescriptor, propertyPath, mode, prototypeName));
if (propertyDescriptor.name === "__proto__")
hadProto = true;
}
if (!this.children.length || (hadProto && this.children.length === 1)) {
var emptyMessageElement = WI.ObjectTreeView.createEmptyMessageElement(WI.UIString("No Properties"));
this.insertChild(new WI.TreeElement(emptyMessageElement, null, false), 0);
}
}
};
|