|
128 | 128 | wrap.className = "sandbox-output-" + type; |
129 | 129 | for (var i = 0; i < args.length; ++i) { |
130 | 130 | var arg = args[i]; |
| 131 | + if (i) wrap.appendChild(document.createTextNode(" ")); |
131 | 132 | if (typeof arg == "string") |
132 | 133 | wrap.appendChild(document.createTextNode(arg)); |
133 | 134 | else |
134 | | - wrap.appendChild(represent(arg, [50])); |
| 135 | + wrap.appendChild(represent(arg, 53)); |
135 | 136 | } |
136 | 137 | this.div.appendChild(wrap); |
137 | 138 | } |
138 | 139 | }; |
139 | 140 |
|
140 | | - function span(type, text, space) { |
141 | | - if (space) space[0] -= text.length; |
| 141 | + function span(type, text) { |
142 | 142 | var sp = document.createElement("span"); |
143 | 143 | sp.className = "sandbox-output-" + type; |
144 | 144 | sp.appendChild(document.createTextNode(text)); |
145 | 145 | return sp; |
146 | 146 | } |
147 | 147 |
|
| 148 | + function eltSize(elt) { |
| 149 | + return elt.textContent.length; |
| 150 | + } |
| 151 | + |
148 | 152 | function represent(val, space) { |
149 | | - if (typeof val == "boolean") return span("bool", String(val), space); |
150 | | - if (typeof val == "number") return span("number", String(val), space); |
151 | | - if (typeof val == "string") return representString(val, space); |
152 | | - if (val == null) return span("null", String(val), space); |
| 153 | + if (typeof val == "boolean") return span("bool", String(val)); |
| 154 | + if (typeof val == "number") return span("number", String(val)); |
| 155 | + if (typeof val == "string") return span("string", JSON.stringify(val)); |
| 156 | + if (val == null) return span("null", String(val)); |
153 | 157 | if (Array.isArray(val)) return representArray(val, space); |
154 | 158 | else return representObj(val, space); |
155 | 159 | } |
156 | 160 |
|
157 | | - function representString(val, space) { |
158 | | - var json = JSON.stringify(val); |
159 | | - if (json.length < space[0] + 3) return span("string", json, space); |
160 | | - var wrap = span("string", json.slice(0, Math.max(1, space[0])), space); |
161 | | - wrap.appendChild(span("etc", "…", space)).addEventListener("click", function(e) { |
162 | | - wrap.innerHTML = ""; |
163 | | - wrap.appendChild(document.createTextNode(json)); |
164 | | - }); |
165 | | - wrap.appendChild(document.createTextNode("\"")); |
166 | | - return wrap; |
167 | | - } |
168 | | - |
169 | 161 | function representArray(val, space) { |
170 | | - space[0] -= 2; |
| 162 | + space -= 2; |
171 | 163 | var wrap = document.createElement("span"); |
172 | 164 | wrap.appendChild(document.createTextNode("[")); |
173 | 165 | for (var i = 0; i < val.length; ++i) { |
174 | 166 | if (i) { |
175 | 167 | wrap.appendChild(document.createTextNode(", ")); |
176 | | - space[0] -= 2; |
| 168 | + space -= 2; |
177 | 169 | } |
178 | | - if (space[0] <= 0 && i < val.length - 2) { |
179 | | - wrap.appendChild(span("etc", "…", space)).addEventListener("click", function(e) { |
180 | | - replaceEtc(e.target, "array", val, i); |
| 170 | + var next = space > 0 && represent(val[i], space); |
| 171 | + var nextSize = next ? eltSize(next) : 0; |
| 172 | + if (space - nextSize <= 0) { |
| 173 | + wrap.appendChild(span("etc", "…")).addEventListener("click", function(e) { |
| 174 | + expandObj(wrap, "array", val); |
181 | 175 | }); |
182 | 176 | break; |
183 | 177 | } |
184 | | - wrap.appendChild(represent(val[i], space)); |
| 178 | + space -= nextSize; |
| 179 | + wrap.appendChild(next); |
185 | 180 | } |
186 | 181 | wrap.appendChild(document.createTextNode("]")); |
187 | 182 | return wrap; |
188 | 183 | } |
189 | 184 |
|
190 | 185 | function representObj(val, space) { |
191 | | - space[0] -= 2; |
| 186 | + var string = val.toString(), m; |
| 187 | + if (/^\[object .*\]$/.test(string)) |
| 188 | + return representSimpleObj(val, space); |
| 189 | + if (val.call && (m = string.match(/^\s*(function[^(]*\([^)]*\))/))) |
| 190 | + string = m[1] + "{…}"; |
| 191 | + var elt = span("etc", string); |
| 192 | + elt.addEventListener("click", function(e) { |
| 193 | + expandObj(elt, "obj", val); |
| 194 | + }); |
| 195 | + return elt; |
| 196 | + } |
| 197 | + |
| 198 | + function representSimpleObj(val, space) { |
| 199 | + space -= 2; |
192 | 200 | var wrap = document.createElement("span"); |
193 | 201 | wrap.appendChild(document.createTextNode("{")); |
194 | 202 | try { |
|
197 | 205 | if (first) { |
198 | 206 | first = false; |
199 | 207 | } else { |
200 | | - space[0] -= 2; |
| 208 | + space -= 2; |
201 | 209 | wrap.appendChild(document.createTextNode(", ")); |
202 | 210 | } |
203 | | - if (space[0] <= 0) { |
204 | | - wrap.appendChild(span("etc", "…", space)).addEventListener("click", function(e) { |
205 | | - replaceEtc(e.target, "obj", val, prop); |
| 211 | + var next = space > 0 && represent(val[prop], space); |
| 212 | + var nextSize = next ? prop.length + 2 + eltSize(next) : 0; |
| 213 | + if (space - nextSize <= 0) { |
| 214 | + wrap.appendChild(span("etc", "…")).addEventListener("click", function(e) { |
| 215 | + expandObj(wrap, "obj", val); |
206 | 216 | }); |
207 | 217 | break; |
208 | 218 | } |
209 | | - space[0] -= prop.length + 2; |
| 219 | + space -= nextSize; |
210 | 220 | wrap.appendChild(document.createTextNode(prop + ": ")); |
211 | | - wrap.appendChild(represent(val[prop], space)); |
| 221 | + wrap.appendChild(next); |
212 | 222 | } |
213 | 223 | } catch (e) { |
214 | 224 | wrap.appendChild(document.createTextNode("…")); |
|
217 | 227 | return wrap; |
218 | 228 | } |
219 | 229 |
|
220 | | - function replaceEtc(node, type, val, from) { |
221 | | - var block = document.createElement("div"); |
| 230 | + function expandObj(node, type, val) { |
| 231 | + var wrap = document.createElement("span"); |
| 232 | + wrap.appendChild(document.createTextNode(type == "array" ? "[" : "{")); |
| 233 | + var block = wrap.appendChild(document.createElement("div")); |
222 | 234 | block.className = "sandbox-output-etc-block"; |
223 | 235 | var table = block.appendChild(document.createElement("table")); |
224 | 236 | function addProp(name) { |
225 | 237 | var row = table.appendChild(document.createElement("tr")); |
226 | 238 | row.appendChild(document.createElement("td")).appendChild(document.createTextNode(name + ":")); |
227 | | - row.appendChild(document.createElement("td")).appendChild(represent(val[name], [30])); |
| 239 | + row.appendChild(document.createElement("td")).appendChild(represent(val[name], 40)); |
228 | 240 | } |
229 | 241 | if (type == "array") { |
230 | | - for (var i = from; i < val.length; ++i) addProp(i); |
| 242 | + for (var i = 0; i < val.length; ++i) addProp(i); |
231 | 243 | } else { |
232 | | - var showing = false; |
233 | | - for (var prop in val) |
234 | | - if (showing = showing || prop == from) addProp(prop); |
| 244 | + for (var prop in val) if (Object.hasOwnProperty.call(val, prop)) addProp(prop); |
235 | 245 | } |
236 | | - node.parentNode.replaceChild(block, node); |
| 246 | + wrap.appendChild(document.createTextNode(type == "array" ? "]" : "}")); |
| 247 | + node.parentNode.replaceChild(wrap, node); |
237 | 248 | } |
238 | 249 | })(); |
0 commit comments