From d30859eb6bd5af9aafa9db003e17835ff594d019 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Wed, 7 Mar 2012 17:37:14 +0100 Subject: [PATCH] Makes parseXML act like parseJSON when given an empty or non-string input: now returns null rather than throwing an exception. Incidently fixes #10527. Unit tests added. --- src/core.js | 3 +++ test/unit/core.js | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index 9a551446b..9c8314c91 100644 --- a/src/core.js +++ b/src/core.js @@ -557,6 +557,9 @@ jQuery.extend({ // Cross-browser xml parsing parseXML: function( data ) { + if ( typeof data !== "string" || !data ) { + return null; + } var xml, tmp; try { if ( window.DOMParser ) { // Standard diff --git a/test/unit/core.js b/test/unit/core.js index 23642e03d..e87c10664 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -1113,7 +1113,7 @@ test("jQuery.parseJSON", function(){ } }); -test("jQuery.parseXML", 4, function(){ +test("jQuery.parseXML", 8, function(){ var xml, tmp; try { xml = jQuery.parseXML( "

A well-formed xml string

" ); @@ -1131,6 +1131,18 @@ test("jQuery.parseXML", 4, function(){ } catch( e ) { strictEqual( e.message, "Invalid XML:

Not a <well-formed xml string

", "invalid xml detected" ); } + try { + xml = jQuery.parseXML( "" ); + strictEqual( xml, null, "empty string => null document" ); + xml = jQuery.parseXML(); + strictEqual( xml, null, "undefined string => null document" ); + xml = jQuery.parseXML( null ); + strictEqual( xml, null, "null string => null document" ); + xml = jQuery.parseXML( true ); + strictEqual( xml, null, "non-string => null document" ); + } catch( e ) { + ok( false, "empty input throws exception" ); + } }); test("jQuery.sub() - Static Methods", function(){