| 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 */ | 11 */ |
| 12 appendObjectPreview(parentElement, preview) { | 12 appendObjectPreview(parentElement, preview) { |
| 13 var description = preview.description; | 13 var description = preview.description; |
| 14 if (preview.type !== 'object' || preview.subtype === 'null') { | 14 if (preview.type !== 'object' || preview.subtype === 'null') { |
| 15 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview
.subtype, description)); | 15 parentElement.appendChild(this.renderPropertyPreview(preview.type, preview
.subtype, description)); |
| 16 return; | 16 return; |
| 17 } | 17 } |
| 18 if (description && preview.subtype !== 'array' && preview.subtype !== 'typed
array') { | 18 var isArray = preview.subtype === 'array' || preview.subtype === 'typedarray
'; |
| 19 if (description && !isArray) { |
| 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 } |
| 23 |
| 24 parentElement.createTextChild(isArray ? '[' : '{'); |
| 22 if (preview.entries) | 25 if (preview.entries) |
| 23 this._appendEntriesPreview(parentElement, preview); | 26 this._appendEntriesPreview(parentElement, preview); |
| 27 else if (isArray) |
| 28 this._appendArrayPropertiesPreview(parentElement, preview); |
| 24 else | 29 else |
| 25 this._appendPropertiesPreview(parentElement, preview); | 30 this._appendObjectPropertiesPreview(parentElement, preview); |
| 31 if (preview.overflow) |
| 32 parentElement.createChild('span').textContent = '\u2026'; |
| 33 parentElement.createTextChild(isArray ? ']' : '}'); |
| 26 } | 34 } |
| 27 | 35 |
| 28 /** | 36 /** |
| 29 * @param {string} description | 37 * @param {string} description |
| 30 * @return {string} | 38 * @return {string} |
| 31 */ | 39 */ |
| 32 _abbreviateFullQualifiedClassName(description) { | 40 _abbreviateFullQualifiedClassName(description) { |
| 33 var abbreviatedDescription = description.split('.'); | 41 var abbreviatedDescription = description.split('.'); |
| 34 for (var i = 0; i < abbreviatedDescription.length - 1; ++i) | 42 for (var i = 0; i < abbreviatedDescription.length - 1; ++i) |
| 35 abbreviatedDescription[i] = abbreviatedDescription[i].trimMiddle(3); | 43 abbreviatedDescription[i] = abbreviatedDescription[i].trimMiddle(3); |
| 36 return abbreviatedDescription.join('.'); | 44 return abbreviatedDescription.join('.'); |
| 37 } | 45 } |
| 38 | 46 |
| 39 /** | 47 /** |
| 40 * @param {!Element} parentElement | 48 * @param {!Element} parentElement |
| 41 * @param {!Protocol.Runtime.ObjectPreview} preview | 49 * @param {!Protocol.Runtime.ObjectPreview} preview |
| 42 */ | 50 */ |
| 43 _appendPropertiesPreview(parentElement, preview) { | 51 _appendObjectPropertiesPreview(parentElement, preview) { |
| 44 var isArray = preview.subtype === 'array' || preview.subtype === 'typedarray
'; | 52 var properties = preview.properties.slice().stableSort(compareFunctionsLast)
; |
| 45 var arrayLength = SDK.RemoteObject.arrayLength(preview); | |
| 46 var properties = preview.properties; | |
| 47 if (isArray) | |
| 48 properties = properties.slice().stableSort(compareIndexesFirst); | |
| 49 else | |
| 50 properties = properties.slice().stableSort(compareFunctionsLast); | |
| 51 | 53 |
| 52 /** | 54 /** |
| 53 * @param {!Protocol.Runtime.PropertyPreview} a | 55 * @param {!Protocol.Runtime.PropertyPreview} a |
| 54 * @param {!Protocol.Runtime.PropertyPreview} b | 56 * @param {!Protocol.Runtime.PropertyPreview} b |
| 55 */ | 57 */ |
| 56 function compareFunctionsLast(a, b) { | 58 function compareFunctionsLast(a, b) { |
| 57 if (a.type !== 'function' && b.type === 'function') | 59 if (a.type !== 'function' && b.type === 'function') |
| 58 return -1; | 60 return -1; |
| 59 if (a.type === 'function' && b.type !== 'function') | 61 if (a.type === 'function' && b.type !== 'function') |
| 60 return 1; | 62 return 1; |
| 61 return 0; | 63 return 0; |
| 62 } | 64 } |
| 63 | 65 |
| 66 for (var i = 0; i < properties.length; ++i) { |
| 67 if (i > 0) |
| 68 parentElement.createTextChild(', '); |
| 69 |
| 70 var property = properties[i]; |
| 71 parentElement.appendChild(this._renderDisplayName(property.name)); |
| 72 parentElement.createTextChild(': '); |
| 73 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property]
)); |
| 74 } |
| 75 } |
| 76 |
| 77 /** |
| 78 * @param {!Element} parentElement |
| 79 * @param {!Protocol.Runtime.ObjectPreview} preview |
| 80 */ |
| 81 _appendArrayPropertiesPreview(parentElement, preview) { |
| 82 var arrayLength = SDK.RemoteObject.arrayLength(preview); |
| 83 var properties = preview.properties; |
| 84 properties = properties.slice().stableSort(compareIndexesFirst); |
| 85 |
| 64 /** | 86 /** |
| 65 * @param {!Protocol.Runtime.PropertyPreview} a | 87 * @param {!Protocol.Runtime.PropertyPreview} a |
| 66 * @param {!Protocol.Runtime.PropertyPreview} b | 88 * @param {!Protocol.Runtime.PropertyPreview} b |
| 67 */ | 89 */ |
| 68 function compareIndexesFirst(a, b) { | 90 function compareIndexesFirst(a, b) { |
| 69 var index1 = toArrayIndex(a.name); | 91 var index1 = toArrayIndex(a.name); |
| 70 var index2 = toArrayIndex(b.name); | 92 var index2 = toArrayIndex(b.name); |
| 71 if (index1 < 0) | 93 if (index1 < 0) |
| 72 return index2 < 0 ? 0 : 1; | 94 return index2 < 0 ? 0 : 1; |
| 73 return index2 < 0 ? -1 : index1 - index2; | 95 return index2 < 0 ? -1 : index1 - index2; |
| 74 } | 96 } |
| 75 | 97 |
| 76 /** | 98 /** |
| 77 * @param {string} name | 99 * @param {string} name |
| 78 * @return {number} | 100 * @return {number} |
| 79 */ | 101 */ |
| 80 function toArrayIndex(name) { | 102 function toArrayIndex(name) { |
| 81 var index = name >>> 0; | 103 var index = name >>> 0; |
| 82 if (String(index) === name && index < arrayLength) | 104 if (String(index) === name && index < arrayLength) |
| 83 return index; | 105 return index; |
| 84 return -1; | 106 return -1; |
| 85 } | 107 } |
| 86 | 108 |
| 87 parentElement.createTextChild(isArray ? '[' : '{'); | |
| 88 for (var i = 0; i < properties.length; ++i) { | 109 for (var i = 0; i < properties.length; ++i) { |
| 89 if (i > 0) | 110 if (i > 0) |
| 90 parentElement.createTextChild(', '); | 111 parentElement.createTextChild(', '); |
| 91 | 112 |
| 92 var property = properties[i]; | 113 var property = properties[i]; |
| 93 var name = property.name; | 114 if (property.name !== String(i) || i >= arrayLength) { |
| 94 if (!isArray || name !== String(i) || i >= arrayLength) { | 115 parentElement.appendChild(this._renderDisplayName(property.name)); |
| 95 if (/^\s|\s$|^$|\n/.test(name)) | |
| 96 parentElement.createChild('span', 'name').createTextChildren('"', name
.replace(/\n/g, '\u21B5'), '"'); | |
| 97 else | |
| 98 parentElement.createChild('span', 'name').textContent = name; | |
| 99 parentElement.createTextChild(': '); | 116 parentElement.createTextChild(': '); |
| 100 } | 117 } |
| 101 | |
| 102 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property]
)); | 118 parentElement.appendChild(this._renderPropertyPreviewOrAccessor([property]
)); |
| 103 } | 119 } |
| 104 if (preview.overflow) | |
| 105 parentElement.createChild('span').textContent = '\u2026'; | |
| 106 parentElement.createTextChild(isArray ? ']' : '}'); | |
| 107 } | 120 } |
| 108 | 121 |
| 109 /** | 122 /** |
| 110 * @param {!Element} parentElement | 123 * @param {!Element} parentElement |
| 111 * @param {!Protocol.Runtime.ObjectPreview} preview | 124 * @param {!Protocol.Runtime.ObjectPreview} preview |
| 112 */ | 125 */ |
| 113 _appendEntriesPreview(parentElement, preview) { | 126 _appendEntriesPreview(parentElement, preview) { |
| 114 parentElement.createTextChild('{'); | |
| 115 for (var i = 0; i < preview.entries.length; ++i) { | 127 for (var i = 0; i < preview.entries.length; ++i) { |
| 116 if (i > 0) | 128 if (i > 0) |
| 117 parentElement.createTextChild(', '); | 129 parentElement.createTextChild(', '); |
| 118 | 130 |
| 119 var entry = preview.entries[i]; | 131 var entry = preview.entries[i]; |
| 120 if (entry.key) { | 132 if (entry.key) { |
| 121 this.appendObjectPreview(parentElement, entry.key); | 133 this.appendObjectPreview(parentElement, entry.key); |
| 122 parentElement.createTextChild(' => '); | 134 parentElement.createTextChild(' => '); |
| 123 } | 135 } |
| 124 this.appendObjectPreview(parentElement, entry.value); | 136 this.appendObjectPreview(parentElement, entry.value); |
| 125 } | 137 } |
| 126 if (preview.overflow) | |
| 127 parentElement.createChild('span').textContent = '\u2026'; | |
| 128 parentElement.createTextChild('}'); | |
| 129 } | 138 } |
| 130 | 139 |
| 131 /** | 140 /** |
| 141 * @param {string} name |
| 142 * @return {!Element} |
| 143 */ |
| 144 _renderDisplayName(name) { |
| 145 var result = createElementWithClass('span', 'name'); |
| 146 var needsQuotes = /^\s|\s$|^$|\n/.test(name); |
| 147 result.textContent = needsQuotes ? '"' + name.replace(/\n/g, '\u21B5') + '"'
: name; |
| 148 return result; |
| 149 } |
| 150 |
| 151 /** |
| 132 * @param {!Array.<!Protocol.Runtime.PropertyPreview>} propertyPath | 152 * @param {!Array.<!Protocol.Runtime.PropertyPreview>} propertyPath |
| 133 * @return {!Element} | 153 * @return {!Element} |
| 134 */ | 154 */ |
| 135 _renderPropertyPreviewOrAccessor(propertyPath) { | 155 _renderPropertyPreviewOrAccessor(propertyPath) { |
| 136 var property = propertyPath.peekLast(); | 156 var property = propertyPath.peekLast(); |
| 137 return this.renderPropertyPreview(property.type, /** @type {string} */ (prop
erty.subtype), property.value); | 157 return this.renderPropertyPreview(property.type, /** @type {string} */ (prop
erty.subtype), property.value); |
| 138 } | 158 } |
| 139 | 159 |
| 140 /** | 160 /** |
| 141 * @param {string} type | 161 * @param {string} type |
| (...skipping 22 matching lines...) Expand all Loading... |
| 164 | 184 |
| 165 if (type === 'object' && !subtype) { | 185 if (type === 'object' && !subtype) { |
| 166 span.textContent = this._abbreviateFullQualifiedClassName(description); | 186 span.textContent = this._abbreviateFullQualifiedClassName(description); |
| 167 return span; | 187 return span; |
| 168 } | 188 } |
| 169 | 189 |
| 170 span.textContent = description; | 190 span.textContent = description; |
| 171 return span; | 191 return span; |
| 172 } | 192 } |
| 173 }; | 193 }; |
| OLD | NEW |