Skip to content

Commit d69c479

Browse files
FarSeeingForbesLindesay
authored andcommitted
toJSON method should be called on object-like values only (#3006)
1 parent 4b50db0 commit d69c479

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/pug-runtime/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ function pug_attr(key, val, escaped, terse) {
132132
if (val === true) {
133133
return ' ' + (terse ? key : key + '="' + key + '"');
134134
}
135-
if (typeof val.toJSON === 'function') {
135+
var type = typeof val;
136+
if ((type === 'object' || type === 'function') && typeof val.toJSON === 'function') {
136137
val = val.toJSON();
137138
}
138139
if (typeof val !== 'string') {

packages/pug-runtime/test/index.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ function addTest(name, fn) {
1414
}
1515

1616
addTest('attr', function (attr) { // (key, val, escaped, terse)
17+
var stringToJSON = String.prototype.toJSON;
18+
19+
String.prototype.toJSON = function() {
20+
return JSON.stringify(this);
21+
};
22+
1723
// Boolean Attributes
1824
expect(attr('key', true, true, true)).toBe(' key');
1925
expect(attr('key', true, false, true)).toBe(' key');
@@ -63,6 +69,8 @@ addTest('attr', function (attr) { // (key, val, escaped, terse)
6369
expect(attr('key', 'foo>bar', false, true)).toBe(' key="foo>bar"');
6470
expect(attr('key', 'foo>bar', true, false)).toBe(' key="foo>bar"');
6571
expect(attr('key', 'foo>bar', false, false)).toBe(' key="foo>bar"');
72+
73+
String.prototype.toJSON = stringToJSON;
6674
});
6775

6876
addTest('attrs', function (attrs) { // (obj, terse)

0 commit comments

Comments
 (0)