Skip to content

Commit d30859e

Browse files
committed
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.
1 parent bd56456 commit d30859e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,9 @@ jQuery.extend({
557557

558558
// Cross-browser xml parsing
559559
parseXML: function( data ) {
560+
if ( typeof data !== "string" || !data ) {
561+
return null;
562+
}
560563
var xml, tmp;
561564
try {
562565
if ( window.DOMParser ) { // Standard

test/unit/core.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ test("jQuery.parseJSON", function(){
11131113
}
11141114
});
11151115

1116-
test("jQuery.parseXML", 4, function(){
1116+
test("jQuery.parseXML", 8, function(){
11171117
var xml, tmp;
11181118
try {
11191119
xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
@@ -1131,6 +1131,18 @@ test("jQuery.parseXML", 4, function(){
11311131
} catch( e ) {
11321132
strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
11331133
}
1134+
try {
1135+
xml = jQuery.parseXML( "" );
1136+
strictEqual( xml, null, "empty string => null document" );
1137+
xml = jQuery.parseXML();
1138+
strictEqual( xml, null, "undefined string => null document" );
1139+
xml = jQuery.parseXML( null );
1140+
strictEqual( xml, null, "null string => null document" );
1141+
xml = jQuery.parseXML( true );
1142+
strictEqual( xml, null, "non-string => null document" );
1143+
} catch( e ) {
1144+
ok( false, "empty input throws exception" );
1145+
}
11341146
});
11351147

11361148
test("jQuery.sub() - Static Methods", function(){

0 commit comments

Comments
 (0)