prototype: Make String#isJSON return false for empty or blank strings. Make Ajax.Response#responseJSON null when Ajax.Response#responseText is empty or blank.

This commit is contained in:
Tobie Langel 2007-11-15 19:37:26 +00:00
parent f8ffe7b40f
commit b058e24a05
5 changed files with 33 additions and 8 deletions

View File

@ -1,3 +1,5 @@
* Make String#isJSON return false for empty or blank strings. Make Ajax.Response#responseJSON null when Ajax.Response#responseText is empty or blank. [Andrew Dupont, Thomas Fuchs, Tobie Langel]
* Make Ajax.Response#_getResponseJSON use Ajax.Response#responseText. [Tobie Langel]
* Remove the forked declaration of Hash#_each. As we are now systematically cloning the object upon instantiation, preventing iteration of shadowed properties is no longer required. [Tobie Langel]

View File

@ -288,8 +288,9 @@ Ajax.Response = Class.create({
_getResponseJSON: function() {
var options = this.request.options;
if (!options.evalJSON || (options.evalJSON != 'force' &&
!(this.getHeader('Content-type') || '').include('application/json')))
return null;
!(this.getHeader('Content-type') || '').include('application/json')) ||
this.responseText.blank())
return null;
try {
return this.responseText.evalJSON(options.sanitizeJSON);
} catch (e) {

View File

@ -166,7 +166,9 @@ Object.extend(String.prototype, {
},
isJSON: function() {
var str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
var str = this;
if (str.blank()) return false;
str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
},

View File

@ -312,7 +312,15 @@
parameters: Fixtures.json,
onComplete: function(transport) { assertEqual(123, transport.responseJSON.test) }
}));
new Ajax.Request("/response", extendDefault({
parameters: {
'Content-Length': 0,
'Content-Type': 'application/json'
},
onComplete: function(transport) { assertNull(transport.responseJSON) }
}));
new Ajax.Request("/response", extendDefault({
evalJSON: false,
parameters: Fixtures.json,

View File

@ -504,7 +504,9 @@
}},
testIsJSON: function() {with(this) {
assert(''.isJSON());
assert(!''.isJSON());
assert(!' '.isJSON());
assert('""'.isJSON());
assert('"foo"'.isJSON());
assert('{}'.isJSON());
assert('[]'.isJSON());
@ -517,13 +519,13 @@
assert(!'new'.isJSON());
assert(!'\u0028\u0029'.isJSON());
// we use '@' as a placeholder for characters authorized only inside brackets,
// so this tests make sure it is considered as authorized.
// so this tests make sure it is not considered authorized elsewhere.
assert(!'@'.isJSON());
}},
testEvalJSON: function() {with(this) {
var valid = '{test: \n\r"hello world!"}';
var invalid = '{test: "hello world!"';
var valid = '{"test": \n\r"hello world!"}';
var invalid = '{"test": "hello world!"';
var dangerous = '{});attackTarget = "attack succeeded!";({}';
// use smaller huge string size for KHTML
@ -552,6 +554,16 @@
Prototype.JSONFilter = temp;
assertMatch(123, huge.evalJSON(true).last().test);
assertEqual('', '""'.evalJSON());
assertEqual('foo', '"foo"'.evalJSON());
assert('object', typeof '{}'.evalJSON());
assert(Object.isArray('[]'.evalJSON()));
assertNull('null'.evalJSON());
assert(123, '123'.evalJSON());
assertIdentical(true, 'true'.evalJSON());
assertIdentical(false, 'false'.evalJSON());
assertEqual('"', '"\\""'.evalJSON());
}}
}, 'testlog');
// ]]>