From b058e24a058829bc6386e3e90458e3ad3515ee4f Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Thu, 15 Nov 2007 19:37:26 +0000 Subject: [PATCH] prototype: Make String#isJSON return false for empty or blank strings. Make Ajax.Response#responseJSON null when Ajax.Response#responseText is empty or blank. --- CHANGELOG | 2 ++ src/ajax.js | 5 +++-- src/string.js | 4 +++- test/unit/ajax.html | 10 +++++++++- test/unit/string.html | 20 ++++++++++++++++---- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5d5020a..74f4cc4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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] diff --git a/src/ajax.js b/src/ajax.js index 1d6f21d..c18c529 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -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) { diff --git a/src/string.js b/src/string.js index ff89433..58ef999 100644 --- a/src/string.js +++ b/src/string.js @@ -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); }, diff --git a/test/unit/ajax.html b/test/unit/ajax.html index c2aa9ba..7e7e98e 100644 --- a/test/unit/ajax.html +++ b/test/unit/ajax.html @@ -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, diff --git a/test/unit/string.html b/test/unit/string.html index 023b5bb..386fe7a 100644 --- a/test/unit/string.html +++ b/test/unit/string.html @@ -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'); // ]]>