2 * Copyright (C) 2014-2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 WebInspector.TypePropertiesSection = function(types, title, subtitle)
28 this.emptyPlaceholder = WebInspector.UIString("No Properties");
30 this._typeSet = WebInspector.TypeSet.fromPayload(this.types);
32 WebInspector.PropertiesSection.call(this, title, subtitle);
35 WebInspector.TypePropertiesSection.prototype = {
36 constructor: WebInspector.TypePropertiesSection,
37 __proto__: WebInspector.PropertiesSection.prototype,
39 onpopulate: function()
41 this.propertiesTreeOutline.removeChildren();
43 var primitiveTypeNames = this._typeSet.primitiveTypeNames;
44 var structures = this.types.structures;
46 for (var struct of structures) {
48 name: struct.constructorName,
52 for (var primitiveName of primitiveTypeNames) {
59 properties.sort(WebInspector.TypePropertiesSection.PropertyComparator);
61 if (this.types.isTruncated)
62 properties.push({name: "\u2026", structure: null});
64 for (var property of properties)
65 this.propertiesTreeOutline.appendChild(new WebInspector.TypePropertyTreeElement(property));
67 if (!this.propertiesTreeOutline.children.length) {
68 var title = document.createElement("div");
69 title.className = "info";
70 title.textContent = this.emptyPlaceholder;
71 var infoElement = new WebInspector.TreeElement(title, null, false);
72 this.propertiesTreeOutline.appendChild(infoElement);
75 this.dispatchEventToListeners(WebInspector.Section.Event.VisibleContentDidChange);
77 if (properties.length === 1)
78 this.propertiesTreeOutline.children[0].expandRecursively();
82 // This is mostly identical to ObjectTreeView.compareProperties.
83 // But this checks for equality because we can have two objects named the same thing.
84 WebInspector.TypePropertiesSection.PropertyComparator = function(propertyA, propertyB)
86 var a = propertyA.name;
87 var b = propertyB.name;
88 if (a.indexOf("__proto__") !== -1)
90 if (b.indexOf("__proto__") !== -1)
96 var chunk = /^\d+|^\D+/;
97 var chunka, chunkb, anum, bnum;
103 chunka = a.match(chunk)[0];
104 chunkb = b.match(chunk)[0];
105 anum = !isNaN(chunka);
106 bnum = !isNaN(chunkb);
112 diff = chunka - chunkb;
113 if (diff === 0 && chunka.length !== chunkb.length) {
114 if (!+chunka && !+chunkb) // chunks are strings of all 0s (special case)
115 return chunka.length - chunkb.length;
117 return chunkb.length - chunka.length;
119 } else if (chunka !== chunkb)
120 return (chunka < chunkb) ? -1 : 1;
121 a = a.substring(chunka.length);
122 b = b.substring(chunkb.length);
128 WebInspector.TypePropertyTreeElement = class TypePropertyTreeElement extends WebInspector.TreeElement
130 constructor(property)
132 super(this.nameElement, null, false);
134 this.property = property;
136 this.nameElement = document.createElement("span");
137 this.nameElement.className = "name";
138 this.nameElement.textContent = this.property.name;
140 this.toggleOnClick = true;
141 this.hasChildren = !!this.property.structure;
146 this.removeChildren();
149 for (var fieldName of this.property.structure.fields) {
156 properties.sort(WebInspector.TypePropertiesSection.PropertyComparator);
158 for (var property of properties)
159 this.appendChild(new WebInspector.TypePropertyTreeElement(property));
162 for (var fieldName of this.property.structure.optionalFields) {
164 name: fieldName + "?",
169 properties.sort(WebInspector.TypePropertiesSection.PropertyComparator);
171 if (this.property.structure.isImprecise)
172 properties.push({name: "\u2026", structure: null});
174 if (this.property.structure.prototypeStructure) {
176 name: this.property.structure.prototypeStructure.constructorName + " (" + WebInspector.UIString("Prototype") + ")",
177 structure: this.property.structure.prototypeStructure
181 for (var property of properties)
182 this.appendChild(new WebInspector.TypePropertyTreeElement(property));