Permalink
Browse files

Makes parseXML act like parseJSON when given an empty or non-string i…

…nput: now returns null rather than throwing an exception. Incidently fixes #10527. Unit tests added.
  • Loading branch information...
jaubourg committed Mar 7, 2012
1 parent bd56456 commit d30859eb6bd5af9aafa9db003e17835ff594d019
Showing with 16 additions and 1 deletion.
  1. +3 −0 src/core.js
  2. +13 −1 test/unit/core.js
View
@@ -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
View
@@ -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( "<p>A <b>well-formed</b> xml string</p>" );
@@ -1131,6 +1131,18 @@ test("jQuery.parseXML", 4, function(){
} catch( e ) {
strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "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(){

0 comments on commit d30859e

Please sign in to comment.