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

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

Issue 2566443004: DevTools: merge array formatting logic (Closed)
Patch Set: 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 {!Element} parentElement 9 * @param {!Element} parentElement
10 * @param {!Protocol.Runtime.ObjectPreview} preview 10 * @param {!Protocol.Runtime.ObjectPreview} preview
11 * @param {boolean=} formatArrayByIndices
11 */ 12 */
12 appendObjectPreview(parentElement, preview) { 13 appendObjectPreview(parentElement, preview, formatArrayByIndices) {
13 var description = preview.description; 14 var description = preview.description;
14 if (preview.type !== 'object' || preview.subtype === 'null') { 15 if (preview.type !== 'object' || preview.subtype === 'null') {
15 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview .subtype, description)); 16 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview .subtype, description));
16 return; 17 return;
17 } 18 }
18 if (description && preview.subtype !== 'array') { 19 if (description && preview.subtype !== 'array') {
19 var text = preview.subtype ? description : this._abbreviateFullQualifiedCl assName(description); 20 var text = preview.subtype ? description : this._abbreviateFullQualifiedCl assName(description);
20 parentElement.createTextChildren(text, ' '); 21 parentElement.createTextChildren(text, ' ');
21 } 22 }
22 23
23 var isArray = preview.subtype === 'array' || preview.subtype === 'typedarray '; 24 var isArray = preview.subtype === 'array' || preview.subtype === 'typedarray ';
24 parentElement.createTextChild(isArray ? '[' : '{'); 25 parentElement.createTextChild(isArray ? '[' : '{');
25 if (preview.entries) 26 if (preview.entries)
26 this._appendEntriesPreview(parentElement, preview); 27 this._appendEntriesPreview(parentElement, preview);
27 else if (isArray) 28 else if (isArray)
28 this._appendArrayPropertiesPreview(parentElement, preview); 29 this._appendArrayPropertiesPreview(parentElement, preview, formatArrayByIn dices);
luoe 2016/12/08 22:36:45 The plan is to pass preview.entries / preview.prop
29 else 30 else
30 this._appendObjectPropertiesPreview(parentElement, preview); 31 this._appendObjectPropertiesPreview(parentElement, preview);
31 if (preview.overflow) 32 if (preview.overflow)
32 parentElement.createChild('span').textContent = '\u2026'; 33 parentElement.createChild('span').textContent = '\u2026';
33 parentElement.createTextChild(isArray ? ']' : '}'); 34 parentElement.createTextChild(isArray ? ']' : '}');
34 } 35 }
35 36
36 /** 37 /**
37 * @param {string} description 38 * @param {string} description
38 * @return {string} 39 * @return {string}
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
81 * @param {boolean=} formatArrayByIndices
80 */ 82 */
81 _appendArrayPropertiesPreview(parentElement, preview) { 83 _appendArrayPropertiesPreview(parentElement, preview, formatArrayByIndices) {
82 var arrayLength = SDK.RemoteObject.arrayLength(preview); 84 var arrayLength = SDK.RemoteObject.arrayLength(preview);
83 var properties = preview.properties; 85 var properties = preview.properties;
84 properties = properties.slice().stableSort(compareIndexesFirst); 86 if (formatArrayByIndices)
87 properties = properties.filter(p => !isNaN(p.name)).stableSort(compareInde xesFirst);
88 else
89 properties = properties.slice().stableSort(compareIndexesFirst);
85 90
86 /** 91 /**
87 * @param {!Protocol.Runtime.PropertyPreview} a 92 * @param {!Protocol.Runtime.PropertyPreview} a
88 * @param {!Protocol.Runtime.PropertyPreview} b 93 * @param {!Protocol.Runtime.PropertyPreview} b
89 */ 94 */
90 function compareIndexesFirst(a, b) { 95 function compareIndexesFirst(a, b) {
91 var index1 = toArrayIndex(a.name); 96 var index1 = toArrayIndex(a.name);
92 var index2 = toArrayIndex(b.name); 97 var index2 = toArrayIndex(b.name);
93 if (index1 < 0) 98 if (index1 < 0)
94 return index2 < 0 ? 0 : 1; 99 return index2 < 0 ? 0 : 1;
95 return index2 < 0 ? -1 : index1 - index2; 100 return index2 < 0 ? -1 : index1 - index2;
96 } 101 }
97 102
98 /** 103 /**
99 * @param {string} name 104 * @param {string} name
100 * @return {number} 105 * @return {number}
101 */ 106 */
102 function toArrayIndex(name) { 107 function toArrayIndex(name) {
103 var index = name >>> 0; 108 var index = name >>> 0;
104 if (String(index) === name && index < arrayLength) 109 if (String(index) === name && index < arrayLength)
105 return index; 110 return index;
106 return -1; 111 return -1;
107 } 112 }
108 113
114 var lastNonEmptyArrayIndex = -1;
109 for (var i = 0; i < properties.length; ++i) { 115 for (var i = 0; i < properties.length; ++i) {
110 if (i > 0) 116 if (i > 0)
111 parentElement.createTextChild(', '); 117 parentElement.createTextChild(', ');
112 118
113 var property = properties[i]; 119 var property = properties[i];
114 if (property.name !== String(i) || i >= arrayLength) { 120 if (formatArrayByIndices) {
121 var index = parseInt(property.name, 10);
122 if (index - lastNonEmptyArrayIndex > 1) {
123 appendUndefined(index);
124 parentElement.createTextChild(', ');
125 }
126 lastNonEmptyArrayIndex = index;
127 } else if (property.name !== String(i) || i >= arrayLength) {
115 parentElement.appendChild(this._renderDisplayName(property.name)); 128 parentElement.appendChild(this._renderDisplayName(property.name));
116 parentElement.createTextChild(': '); 129 parentElement.createTextChild(': ');
117 } 130 }
118 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] )); 131 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property] ));
119 } 132 }
133 if (formatArrayByIndices && arrayLength - lastNonEmptyArrayIndex > 1) {
134 if (lastNonEmptyArrayIndex > -1)
135 parentElement.createTextChild(', ');
136 appendUndefined(arrayLength);
137 }
138
139 /**
140 * @param {number} index
141 */
142 function appendUndefined(index) {
143 var span = parentElement.createChild('span', 'object-value-undefined');
144 span.textContent = Common.UIString('undefined × %d', index - lastNonEmptyA rrayIndex - 1);
145 }
120 } 146 }
121 147
122 /** 148 /**
123 * @param {!Element} parentElement 149 * @param {!Element} parentElement
124 * @param {!Protocol.Runtime.ObjectPreview} preview 150 * @param {!Protocol.Runtime.ObjectPreview} preview
125 */ 151 */
126 _appendEntriesPreview(parentElement, preview) { 152 _appendEntriesPreview(parentElement, preview) {
127 for (var i = 0; i < preview.entries.length; ++i) { 153 for (var i = 0; i < preview.entries.length; ++i) {
128 if (i > 0) 154 if (i > 0)
129 parentElement.createTextChild(', '); 155 parentElement.createTextChild(', ');
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 216
191 if (type === 'object' && !subtype) { 217 if (type === 'object' && !subtype) {
192 span.textContent = this._abbreviateFullQualifiedClassName(description); 218 span.textContent = this._abbreviateFullQualifiedClassName(description);
193 return span; 219 return span;
194 } 220 }
195 221
196 span.textContent = description; 222 span.textContent = description;
197 return span; 223 return span;
198 } 224 }
199 }; 225 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698