| OLD | NEW |
| 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 {!SDK.RemoteObject=} objectForAccessing | 11 * @param {!SDK.RemoteObject=} objectForAccessing |
| 12 */ | 12 */ |
| 13 appendObjectPreview(parentElement, preview, objectForAccessing) { | 13 appendObjectPreview(parentElement, preview, objectForAccessing) { |
| 14 var description = preview.description; | 14 var description = preview.description; |
| 15 if (preview.type !== 'object' || preview.subtype === 'null') { | 15 if (preview.type !== 'object' || preview.subtype === 'null') { |
| 16 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview
.subtype, description)); | 16 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview
.subtype, description)); |
| 17 return; | 17 return; |
| 18 } | 18 } |
| 19 if (description && preview.subtype !== 'array' && preview.subtype !== 'typed
array') { | 19 var isArray = preview.subtype === 'array' || preview.subtype === 'typedarray
'; |
| 20 if (description && !isArray) { |
| 20 var text = preview.subtype ? description : this._abbreviateFullQualifiedCl
assName(description); | 21 var text = preview.subtype ? description : this._abbreviateFullQualifiedCl
assName(description); |
| 21 parentElement.createTextChildren(text, ' '); | 22 parentElement.createTextChildren(text, ' '); |
| 22 } | 23 } |
| 24 |
| 25 parentElement.createTextChild(isArray ? '[' : '{'); |
| 23 if (preview.entries) | 26 if (preview.entries) |
| 24 this._appendEntriesPreview(parentElement, preview); | 27 this._appendEntriesPreview(parentElement, preview); |
| 28 else if (isArray) |
| 29 this._appendArrayPropertiesPreview(parentElement, preview, objectForAccess
ing); |
| 25 else | 30 else |
| 26 this._appendPropertiesPreview(parentElement, preview, objectForAccessing); | 31 this._appendPropertiesPreview(parentElement, preview, objectForAccessing); |
| 32 if (preview.overflow) |
| 33 parentElement.createChild('span').textContent = '\u2026'; |
| 34 parentElement.createTextChild(isArray ? ']' : '}'); |
| 27 } | 35 } |
| 28 | 36 |
| 29 /** | 37 /** |
| 30 * @param {string} description | 38 * @param {string} description |
| 31 * @return {string} | 39 * @return {string} |
| 32 */ | 40 */ |
| 33 _abbreviateFullQualifiedClassName(description) { | 41 _abbreviateFullQualifiedClassName(description) { |
| 34 var abbreviatedDescription = description.split('.'); | 42 var abbreviatedDescription = description.split('.'); |
| 35 for (var i = 0; i < abbreviatedDescription.length - 1; ++i) | 43 for (var i = 0; i < abbreviatedDescription.length - 1; ++i) |
| 36 abbreviatedDescription[i] = abbreviatedDescription[i].trimMiddle(3); | 44 abbreviatedDescription[i] = abbreviatedDescription[i].trimMiddle(3); |
| 37 return abbreviatedDescription.join('.'); | 45 return abbreviatedDescription.join('.'); |
| 38 } | 46 } |
| 39 | 47 |
| 40 /** | 48 /** |
| 41 * @param {!Element} parentElement | 49 * @param {!Element} parentElement |
| 42 * @param {!Protocol.Runtime.ObjectPreview} preview | 50 * @param {!Protocol.Runtime.ObjectPreview} preview |
| 43 * @param {!SDK.RemoteObject=} objectForAccessing | 51 * @param {!SDK.RemoteObject=} objectForAccessing |
| 44 */ | 52 */ |
| 45 _appendPropertiesPreview(parentElement, preview, objectForAccessing) { | 53 _appendPropertiesPreview(parentElement, preview, objectForAccessing) { |
| 46 var isArray = preview.subtype === 'array' || preview.subtype === 'typedarray
'; | 54 var properties = preview.properties.slice().stableSort(compareFunctionsLast)
; |
| 47 var arrayLength = SDK.RemoteObject.arrayLength(preview); | |
| 48 var properties = preview.properties; | |
| 49 if (isArray) | |
| 50 properties = properties.slice().stableSort(compareIndexesFirst); | |
| 51 else | |
| 52 properties = properties.slice().stableSort(compareFunctionsLast); | |
| 53 | 55 |
| 54 /** | 56 /** |
| 55 * @param {!Protocol.Runtime.PropertyPreview} a | 57 * @param {!Protocol.Runtime.PropertyPreview} a |
| 56 * @param {!Protocol.Runtime.PropertyPreview} b | 58 * @param {!Protocol.Runtime.PropertyPreview} b |
| 57 */ | 59 */ |
| 58 function compareFunctionsLast(a, b) { | 60 function compareFunctionsLast(a, b) { |
| 59 if (a.type !== 'function' && b.type === 'function') | 61 if (a.type !== 'function' && b.type === 'function') |
| 60 return -1; | 62 return -1; |
| 61 if (a.type === 'function' && b.type !== 'function') | 63 if (a.type === 'function' && b.type !== 'function') |
| 62 return 1; | 64 return 1; |
| 63 return 0; | 65 return 0; |
| 64 } | 66 } |
| 65 | 67 |
| 68 for (var i = 0; i < properties.length; ++i) { |
| 69 if (i > 0) |
| 70 parentElement.createTextChild(', '); |
| 71 |
| 72 var property = properties[i]; |
| 73 var name = property.name; |
| 74 if (/^\s|\s$|^$|\n/.test(name)) |
| 75 parentElement.createChild('span', 'name').createTextChildren('"', name.r
eplace(/\n/g, '\u21B5'), '"'); |
| 76 else |
| 77 parentElement.createChild('span', 'name').textContent = name; |
| 78 parentElement.createTextChild(': '); |
| 79 parentElement.appendChild(this.renderPropertyPreviewOrAccessor([property],
objectForAccessing)); |
| 80 } |
| 81 } |
| 82 |
| 83 /** |
| 84 * @param {!Element} parentElement |
| 85 * @param {!Protocol.Runtime.ObjectPreview} preview |
| 86 * @param {!SDK.RemoteObject=} objectForAccessing |
| 87 */ |
| 88 _appendArrayPropertiesPreview(parentElement, preview, objectForAccessing) { |
| 89 var arrayLength = SDK.RemoteObject.arrayLength(preview); |
| 90 var properties = preview.properties.slice().stableSort(compareIndexesFirst); |
| 91 var lastNonEmptyArrayIndex = -1; |
| 92 for (var i = 0; i < properties.length; ++i) { |
| 93 var property = properties[i]; |
| 94 var name = property.name; |
| 95 if (isNaN(name)) |
| 96 continue; |
| 97 if (i > 0) |
| 98 parentElement.createTextChild(', '); |
| 99 var index = parseInt(name, 10); |
| 100 if (index - lastNonEmptyArrayIndex > 1) { |
| 101 appendUndefined(index); |
| 102 parentElement.createTextChild(', '); |
| 103 } |
| 104 parentElement.appendChild(this.renderPropertyPreviewOrAccessor([property],
objectForAccessing)); |
| 105 lastNonEmptyArrayIndex = index; |
| 106 } |
| 107 if (lastNonEmptyArrayIndex > -1 && arrayLength - lastNonEmptyArrayIndex > 1)
{ |
| 108 parentElement.createTextChild(', '); |
| 109 appendUndefined(arrayLength); |
| 110 } |
| 111 |
| 112 /** |
| 113 * @param {number} index |
| 114 */ |
| 115 function appendUndefined(index) { |
| 116 var span = parentElement.createChild('span', 'object-value-undefined'); |
| 117 var gapSize = index - lastNonEmptyArrayIndex - 1; |
| 118 span.textContent = gapSize > 1 ? Common.UIString('undefined × %d', gapSize
) : Common.UIString('undefined'); |
| 119 } |
| 120 |
| 66 /** | 121 /** |
| 67 * @param {!Protocol.Runtime.PropertyPreview} a | 122 * @param {!Protocol.Runtime.PropertyPreview} a |
| 68 * @param {!Protocol.Runtime.PropertyPreview} b | 123 * @param {!Protocol.Runtime.PropertyPreview} b |
| 69 */ | 124 */ |
| 70 function compareIndexesFirst(a, b) { | 125 function compareIndexesFirst(a, b) { |
| 71 var index1 = toArrayIndex(a.name); | 126 var index1 = toArrayIndex(a.name); |
| 72 var index2 = toArrayIndex(b.name); | 127 var index2 = toArrayIndex(b.name); |
| 73 if (index1 < 0) | 128 if (index1 < 0) |
| 74 return index2 < 0 ? 0 : 1; | 129 return index2 < 0 ? 0 : 1; |
| 75 return index2 < 0 ? -1 : index1 - index2; | 130 return index2 < 0 ? -1 : index1 - index2; |
| 76 } | 131 } |
| 77 | 132 |
| 78 /** | 133 /** |
| 79 * @param {string} name | 134 * @param {string} name |
| 80 * @return {number} | 135 * @return {number} |
| 81 */ | 136 */ |
| 82 function toArrayIndex(name) { | 137 function toArrayIndex(name) { |
| 83 var index = name >>> 0; | 138 var index = name >>> 0; |
| 84 if (String(index) === name && index < arrayLength) | 139 if (String(index) === name && index < arrayLength) |
| 85 return index; | 140 return index; |
| 86 return -1; | 141 return -1; |
| 87 } | 142 } |
| 88 | |
| 89 parentElement.createTextChild(isArray ? '[' : '{'); | |
| 90 for (var i = 0; i < properties.length; ++i) { | |
| 91 if (i > 0) | |
| 92 parentElement.createTextChild(', '); | |
| 93 | |
| 94 var property = properties[i]; | |
| 95 var name = property.name; | |
| 96 if (!isArray || name !== String(i) || i >= arrayLength) { | |
| 97 if (/^\s|\s$|^$|\n/.test(name)) | |
| 98 parentElement.createChild('span', 'name').createTextChildren('"', name
.replace(/\n/g, '\u21B5'), '"'); | |
| 99 else | |
| 100 parentElement.createChild('span', 'name').textContent = name; | |
| 101 parentElement.createTextChild(': '); | |
| 102 } | |
| 103 | |
| 104 parentElement.appendChild(this.renderPropertyPreviewOrAccessor([property],
objectForAccessing)); | |
| 105 } | |
| 106 if (preview.overflow) | |
| 107 parentElement.createChild('span').textContent = '\u2026'; | |
| 108 parentElement.createTextChild(isArray ? ']' : '}'); | |
| 109 } | 143 } |
| 110 | 144 |
| 111 /** | 145 /** |
| 112 * @param {!Element} parentElement | 146 * @param {!Element} parentElement |
| 113 * @param {!Protocol.Runtime.ObjectPreview} preview | 147 * @param {!Protocol.Runtime.ObjectPreview} preview |
| 114 */ | 148 */ |
| 115 _appendEntriesPreview(parentElement, preview) { | 149 _appendEntriesPreview(parentElement, preview) { |
| 116 parentElement.createTextChild('{'); | |
| 117 for (var i = 0; i < preview.entries.length; ++i) { | 150 for (var i = 0; i < preview.entries.length; ++i) { |
| 118 if (i > 0) | 151 if (i > 0) |
| 119 parentElement.createTextChild(', '); | 152 parentElement.createTextChild(', '); |
| 120 | 153 |
| 121 var entry = preview.entries[i]; | 154 var entry = preview.entries[i]; |
| 122 if (entry.key) { | 155 if (entry.key) { |
| 123 this.appendObjectPreview(parentElement, entry.key); | 156 this.appendObjectPreview(parentElement, entry.key); |
| 124 parentElement.createTextChild(' => '); | 157 parentElement.createTextChild(' => '); |
| 125 } | 158 } |
| 126 this.appendObjectPreview(parentElement, entry.value); | 159 this.appendObjectPreview(parentElement, entry.value); |
| 127 } | 160 } |
| 128 if (preview.overflow) | |
| 129 parentElement.createChild('span').textContent = '\u2026'; | |
| 130 parentElement.createTextChild('}'); | |
| 131 } | 161 } |
| 132 | 162 |
| 133 /** | 163 /** |
| 134 * @param {!Array.<!Protocol.Runtime.PropertyPreview>} propertyPath | 164 * @param {!Array.<!Protocol.Runtime.PropertyPreview>} propertyPath |
| 135 * @param {!SDK.RemoteObject=} objectForAccessing | 165 * @param {!SDK.RemoteObject=} objectForAccessing |
| 136 * @return {!Element} | 166 * @return {!Element} |
| 137 */ | 167 */ |
| 138 renderPropertyPreviewOrAccessor(propertyPath, objectForAccessing) { | 168 renderPropertyPreviewOrAccessor(propertyPath, objectForAccessing) { |
| 139 var property = propertyPath.peekLast(); | 169 var property = propertyPath.peekLast(); |
| 140 if (property.type === 'accessor' && objectForAccessing) | 170 if (property.type === 'accessor' && objectForAccessing) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 else | 240 else |
| 211 description = result.description.trimEnd(maxLength); | 241 description = result.description.trimEnd(maxLength); |
| 212 } | 242 } |
| 213 rootElement.appendChild(this.renderPropertyPreview(type, subtype, descri
ption)); | 243 rootElement.appendChild(this.renderPropertyPreview(type, subtype, descri
ption)); |
| 214 } | 244 } |
| 215 } | 245 } |
| 216 | 246 |
| 217 return rootElement; | 247 return rootElement; |
| 218 } | 248 } |
| 219 }; | 249 }; |
| OLD | NEW |