Skip to content

Commit aab45da

Browse files
committed
Rewrite inspect function
* Display arrays like `[]` rather then `{length: 0}` * Escape HTML. `b{content:'<div>'}` now shows properly * Use DOM methods rather than string concatenation. It's faster in Chrome. * Use inner function with an accumulator. It's a little bit faster http://jsperf.com/continuation-passing-style/3
1 parent 5501eff commit aab45da

1 file changed

Lines changed: 94 additions & 28 deletions

File tree

docs/parse.js

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
if (!Array.isArray) {
2+
Array.isArray = function(array) {
3+
return {}.toString.call(array) === '[object Array]';
4+
};
5+
}
6+
17
function byId(id) {
28
return document.getElementById(id);
39
}
@@ -61,38 +67,97 @@ function stringifyObjectKey(key) {
6167

6268
/**
6369
* @param {Object} object
64-
* @param {number} [depth]
65-
* @param {Array} [stack]
66-
* @return {string}
70+
* @return {DocumentFragment}
6771
*/
68-
function inspect(object, depth, stack) {
69-
depth ? depth++ : (depth = 1);
70-
stack = stack || (stack = []);
71-
72-
switch (typeof object) {
73-
case 'object':
74-
var level = stack.indexOf(object);
75-
if (level !== -1) {
76-
return buildPath(level);
77-
}
78-
stack = [object].concat(stack);
79-
80-
var properties = [];
81-
var indent = makeIndent(depth);
82-
for (var key in object) {
83-
if (object.hasOwnProperty(key)) {
84-
properties.push(indent + stringifyObjectKey(key) + '<span>: </span>' + inspect(object[key], depth, stack));
72+
function inspect(object) {
73+
74+
var accumulator = document.createDocumentFragment();
75+
_inspect(accumulator, object, 0, []);
76+
return accumulator;
77+
78+
/**
79+
* @param {DocumentFragment} root
80+
* @param {Object} object
81+
* @param {number} depth
82+
* @param {Array} stack
83+
*/
84+
function _inspect(root, object, depth, stack) {
85+
switch (typeof object) {
86+
case 'object':
87+
var level = stack.indexOf(object);
88+
if (level !== -1) {
89+
root.appendChild(document.createTextNode(buildPath(depth - level)));
90+
break;
8591
}
86-
}
87-
var indentInside = makeIndent(depth - 1);
88-
return '<span>{</span>\n' + properties.join('<span>,</span>\n') + '\n' + indentInside + '<span>}</span>';
92+
depth++;
93+
var stackLength = stack.push(object);
94+
95+
var indent = document.createTextNode(makeIndent(depth));
96+
var span = document.createElement('span');
97+
span.textContent = ',\n';
98+
var comma = span;
99+
if (Array.isArray(object)) {
100+
var length = object.length;
101+
if (length === 0) {
102+
span = span.cloneNode(false);
103+
span.textContent = '[]';
104+
root.appendChild(span);
105+
} else {
106+
span = span.cloneNode(false);
107+
span.textContent = '[\n';
108+
root.appendChild(span);
109+
for (var i = 0; i < length; i++) {
110+
root.appendChild(indent.cloneNode(true));
111+
_inspect(root, object[i], depth, stack);
112+
stack.length = stackLength;
113+
if (i < length - 1) {
114+
root.appendChild(comma.cloneNode(true));
115+
}
116+
}
117+
span = span.cloneNode(false);
118+
span.textContent = '\n' + makeIndent(depth - 1) + ']';
119+
root.appendChild(span);
120+
}
121+
} else {
122+
var keys = Object.keys(object);
123+
length = keys.length;
124+
if (length === 0) {
125+
span = span.cloneNode(false);
126+
span.textContent = '{}';
127+
root.appendChild(span);
128+
} else {
129+
span = span.cloneNode(false);
130+
span.textContent = '{\n';
131+
root.appendChild(span);
132+
var colon = span.cloneNode(false);
133+
colon.textContent = ': ';
134+
for (i = 0; i < length; i++) {
135+
var key = keys[i];
136+
root.appendChild(indent.cloneNode(true));
137+
root.appendChild(document.createTextNode(stringifyObjectKey(key)));
138+
root.appendChild(colon.cloneNode(true));
139+
_inspect(root, object[key], depth, stack);
140+
stack.length = stackLength;
141+
if (i < length - 1) {
142+
root.appendChild(comma.cloneNode(true));
143+
}
144+
}
145+
span = span.cloneNode(false);
146+
span.textContent = '\n' + makeIndent(depth - 1) + '}';
147+
root.appendChild(span);
148+
}
149+
}
150+
break;
89151

90-
case 'string':
91-
return '"' + object + '"';
152+
case 'string':
153+
root.appendChild(document.createTextNode(JSON.stringify(object)));
154+
break;
92155

93-
default:
94-
return object.toString();
156+
default:
157+
root.appendChild(document.createTextNode(object.toString()));
158+
}
95159
}
160+
96161
}
97162

98163

@@ -115,7 +180,8 @@ function outputUpdated() {
115180
if (value !== style.prevValue) {
116181
style.prevValue = value;
117182
var css = CSSOM.parse(value);
118-
output.innerHTML = inspect(css);
183+
output.innerHTML = '';
184+
output.appendChild(inspect(css));
119185
serialized.innerHTML = css.toString();
120186
}
121187
}

0 commit comments

Comments
 (0)