Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/components/RemoteObjectPreviewFormatter.js

Issue 2566443004: DevTools: merge array formatting logic (Closed)
Patch Set: ac Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 /** 4 /**
5 * @unrestricted 5 * @unrestricted
6 */ 6 */
7 Components.RemoteObjectPreviewFormatter = class { 7 Components.RemoteObjectPreviewFormatter = class {
8 /** 8 /**
9 * @param {!Protocol.Runtime.PropertyPreview} a
10 * @param {!Protocol.Runtime.PropertyPreview} b
11 * @return {number}
12 */
13 static objectPropertyComparator(a, b) {
lushnikov 2016/12/12 22:10:43 should it be private?
luoe 2016/12/13 03:57:35 Done.
14 if (a.type !== 'function' && b.type === 'function')
15 return -1;
16 if (a.type === 'function' && b.type !== 'function')
17 return 1;
18 return 0;
19 }
20
21 /**
9 * @param {!Element} parentElement 22 * @param {!Element} parentElement
10 * @param {!Protocol.Runtime.ObjectPreview} preview 23 * @param {!Protocol.Runtime.ObjectPreview} preview
11 */ 24 */
12 appendObjectPreview(parentElement, preview) { 25 appendObjectPreview(parentElement, preview) {
13 var description = preview.description; 26 var description = preview.description;
14 if (preview.type !== 'object' || preview.subtype === 'null') { 27 if (preview.type !== 'object' || preview.subtype === 'null') {
15 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview .subtype, description)); 28 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview .subtype, description));
16 return; 29 return;
17 } 30 }
18 if (description && preview.subtype !== 'array') { 31 if (description && preview.subtype !== 'array') {
(...skipping 23 matching lines...) Expand all
42 for (var i = 0; i < abbreviatedDescription.length - 1; ++i) 55 for (var i = 0; i < abbreviatedDescription.length - 1; ++i)
43 abbreviatedDescription[i] = abbreviatedDescription[i].trimMiddle(3); 56 abbreviatedDescription[i] = abbreviatedDescription[i].trimMiddle(3);
44 return abbreviatedDescription.join('.'); 57 return abbreviatedDescription.join('.');
45 } 58 }
46 59
47 /** 60 /**
48 * @param {!Element} parentElement 61 * @param {!Element} parentElement
49 * @param {!Protocol.Runtime.ObjectPreview} preview 62 * @param {!Protocol.Runtime.ObjectPreview} preview
50 */ 63 */
51 _appendObjectPropertiesPreview(parentElement, preview) { 64 _appendObjectPropertiesPreview(parentElement, preview) {
52 var properties = preview.properties.filter(p => p.type !== 'accessor').stabl eSort(compareFunctionsLast); 65 var properties = preview.properties.filter(p => p.type !== 'accessor')
53 66 .stableSort(Components.RemoteObjectPreviewFormatter.obj ectPropertyComparator);
54 /**
55 * @param {!Protocol.Runtime.PropertyPreview} a
56 * @param {!Protocol.Runtime.PropertyPreview} b
57 */
58 function compareFunctionsLast(a, b) {
59 if (a.type !== 'function' && b.type === 'function')
60 return -1;
61 if (a.type === 'function' && b.type !== 'function')
62 return 1;
63 return 0;
64 }
65
66 for (var i = 0; i < properties.length; ++i) { 67 for (var i = 0; i < properties.length; ++i) {
67 if (i > 0) 68 if (i > 0)
68 parentElement.createTextChild(', '); 69 parentElement.createTextChild(', ');
69 70
70 var property = properties[i]; 71 var property = properties[i];
71 parentElement.appendChild(this._renderDisplayName(property.name)); 72 parentElement.appendChild(this._renderDisplayName(property.name));
72 parentElement.createTextChild(': '); 73 parentElement.createTextChild(': ');
73 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] )); 74 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] ));
74 } 75 }
75 } 76 }
76 77
77 /** 78 /**
78 * @param {!Element} parentElement 79 * @param {!Element} parentElement
79 * @param {!Protocol.Runtime.ObjectPreview} preview 80 * @param {!Protocol.Runtime.ObjectPreview} preview
80 */ 81 */
81 _appendArrayPropertiesPreview(parentElement, preview) { 82 _appendArrayPropertiesPreview(parentElement, preview) {
82 var arrayLength = SDK.RemoteObject.arrayLength(preview); 83 var arrayLength = SDK.RemoteObject.arrayLength(preview);
83 var properties = preview.properties; 84 var indexProperties = preview.properties.filter(p => toArrayIndex(p.name) != = -1).stableSort();
lushnikov 2016/12/12 22:10:43 don't you need to pass in comparator here as well?
luoe 2016/12/13 03:57:35 Done.
84 properties = properties.slice().stableSort(compareIndexesFirst); 85 var otherProperties = preview.properties.filter(p => toArrayIndex(p.name) == = -1)
85 86 .stableSort(Components.RemoteObjectPreviewFormatte r.objectPropertyComparator);
86 /**
87 * @param {!Protocol.Runtime.PropertyPreview} a
88 * @param {!Protocol.Runtime.PropertyPreview} b
89 */
90 function compareIndexesFirst(a, b) {
91 var index1 = toArrayIndex(a.name);
92 var index2 = toArrayIndex(b.name);
93 if (index1 < 0)
94 return index2 < 0 ? 0 : 1;
95 return index2 < 0 ? -1 : index1 - index2;
96 }
97 87
98 /** 88 /**
99 * @param {string} name 89 * @param {string} name
100 * @return {number} 90 * @return {number}
101 */ 91 */
102 function toArrayIndex(name) { 92 function toArrayIndex(name) {
103 var index = name >>> 0; 93 var index = name >>> 0;
104 if (String(index) === name && index < arrayLength) 94 if (String(index) === name && index < arrayLength)
105 return index; 95 return index;
106 return -1; 96 return -1;
107 } 97 }
108 98
109 for (var i = 0; i < properties.length; ++i) { 99 var lastNonEmptyArrayIndex = -1;
100 var property;
101 for (var i = 0; i < indexProperties.length; ++i) {
110 if (i > 0) 102 if (i > 0)
111 parentElement.createTextChild(', '); 103 parentElement.createTextChild(', ');
104 property = indexProperties[i];
105 var index = toArrayIndex(property.name);
106 if (index - lastNonEmptyArrayIndex > 1) {
107 appendUndefined(index);
108 parentElement.createTextChild(', ');
109 }
110 lastNonEmptyArrayIndex = index;
111 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] ));
112 }
112 113
113 var property = properties[i]; 114 var elementsAdded = indexProperties.length > 0;
114 if (property.name !== String(i) || i >= arrayLength) { 115 if (arrayLength - lastNonEmptyArrayIndex > 1) {
115 parentElement.appendChild(this._renderDisplayName(property.name)); 116 if (lastNonEmptyArrayIndex > -1)
116 parentElement.createTextChild(': '); 117 parentElement.createTextChild(', ');
117 } 118 appendUndefined(arrayLength);
119 elementsAdded = true;
120 }
121
122 for (var i = 0; i < otherProperties.length; ++i) {
123 if (elementsAdded || i > 0)
124 parentElement.createTextChild(', ');
125 property = otherProperties[i];
126 parentElement.appendChild(this._renderDisplayName(property.name));
127 parentElement.createTextChild(': ');
118 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] )); 128 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] ));
119 } 129 }
130
131 /**
132 * @param {number} index
133 */
134 function appendUndefined(index) {
135 var span = parentElement.createChild('span', 'object-value-undefined');
136 span.textContent = Common.UIString('undefined × %d', index - lastNonEmptyA rrayIndex - 1);
137 }
120 } 138 }
121 139
122 /** 140 /**
123 * @param {!Element} parentElement 141 * @param {!Element} parentElement
124 * @param {!Protocol.Runtime.ObjectPreview} preview 142 * @param {!Protocol.Runtime.ObjectPreview} preview
125 */ 143 */
126 _appendEntriesPreview(parentElement, preview) { 144 _appendEntriesPreview(parentElement, preview) {
127 for (var i = 0; i < preview.entries.length; ++i) { 145 for (var i = 0; i < preview.entries.length; ++i) {
128 if (i > 0) 146 if (i > 0)
129 parentElement.createTextChild(', '); 147 parentElement.createTextChild(', ');
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 208
191 if (type === 'object' && !subtype) { 209 if (type === 'object' && !subtype) {
192 span.textContent = this._abbreviateFullQualifiedClassName(description); 210 span.textContent = this._abbreviateFullQualifiedClassName(description);
193 return span; 211 return span;
194 } 212 }
195 213
196 span.textContent = description; 214 span.textContent = description;
197 return span; 215 return span;
198 } 216 }
199 }; 217 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698