Skip to content

Commit 0d0972a

Browse files
committed
fix(client): Fix stringify serializing objects
Serialize uses the name of the constructor to serialize objects. When the object was created using Object.create(null) or has a property constructor this may throw an Error
1 parent 51f1b88 commit 0d0972a

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

client/stringify.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ var stringify = function stringify(obj, depth) {
3939
} else if (obj.outerHTML) {
4040
return obj.outerHTML;
4141
} else {
42-
strs.push(obj.constructor.name);
42+
var constructor = 'Object';
43+
if (obj.constructor && typeof obj.constructor === 'function') {
44+
constructor = obj.constructor.name;
45+
}
46+
47+
strs.push(constructor);
4348
strs.push('{');
4449
var first = true;
4550
for (var key in obj) {
46-
if (obj.hasOwnProperty(key)) {
51+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
4752
if (first) {
4853
first = false;
4954
} else {

test/client/stringify.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ describe('stringify', function() {
3232
expect(stringify(['a', 'b', null, true, false])).toBe("['a', 'b', null, true, false]");
3333
});
3434

35+
it('should serialize objects', function () {
36+
var obj;
37+
38+
39+
obj = {a: 'a', b: 'b', c: null, d: true, e: false};
40+
expect(stringify(obj)).toBe('Object{a: \'a\', b: \'b\', c: null, d: true, e: false}');
41+
42+
function MyObj() {
43+
this.a = 'a';
44+
}
45+
46+
obj = new MyObj();
47+
expect(stringify(obj)).toBe('MyObj{a: \'a\'}');
48+
49+
obj = {constructor: null};
50+
expect(stringify(obj)).toBe('Object{constructor: null}');
51+
52+
obj = Object.create(null);
53+
obj.a = 'a';
54+
expect(stringify(obj)).toBe('Object{a: \'a\'}');
55+
});
56+
3557

3658
it('should serialize html', function() {
3759
var div = document.createElement('div');

0 commit comments

Comments
 (0)