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