Skip to content

Commit

Permalink
Fixes #9255: xml parsing error in $.parseXML is now properly detected…
Browse files Browse the repository at this point in the history
… for all browsers. Unit test added.
  • Loading branch information
jaubourg committed Jul 23, 2011
1 parent 0ed9909 commit 27291ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
29 changes: 14 additions & 15 deletions src/core.js
Expand Up @@ -553,24 +553,23 @@ jQuery.extend({
},

// Cross-browser xml parsing
// (xml & tmp used internally)
parseXML: function( data , xml , tmp ) {

if ( window.DOMParser ) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString( data , "text/xml" );
} else { // IE
xml = new ActiveXObject( "Microsoft.XMLDOM" );
xml.async = "false";
xml.loadXML( data );
parseXML: function( data ) {
var xml, tmp;
try {
if ( window.DOMParser ) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString( data , "text/xml" );
} else { // IE
xml = new ActiveXObject( "Microsoft.XMLDOM" );
xml.async = "false";
xml.loadXML( data );
}
} catch( e ) {
xml = undefined;
}

tmp = xml.documentElement;

if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
jQuery.error( "Invalid XML: " + data );
}

return xml;
},

Expand Down
24 changes: 22 additions & 2 deletions test/unit/core.js
Expand Up @@ -867,7 +867,7 @@ test("jQuery.each(Object,Function)", function() {
f[i] = "baz";
});
equals( "baz", f.foo, "Loop over a function" );

var stylesheet_count = 0;
jQuery.each(document.styleSheets, function(i){
stylesheet_count++;
Expand Down Expand Up @@ -984,6 +984,26 @@ test("jQuery.parseJSON", function(){
}
});

test("jQuery.parseXML", 4, function(){
var xml, tmp;
try {
xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
tmp = xml.getElementsByTagName( "p" )[ 0 ];
ok( !!tmp, "<p> present in document" );
tmp = tmp.getElementsByTagName( "b" )[ 0 ];
ok( !!tmp, "<b> present in document" );
strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", "<b> text is as expected" );
} catch (e) {
strictEqual( e, undefined, "unexpected error" );
}
try {
xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
ok( false, "invalid xml not detected" );
} catch( e ) {
strictEqual( e, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
}
});

test("jQuery.sub() - Static Methods", function(){
expect(18);
var Subclass = jQuery.sub();
Expand Down Expand Up @@ -1108,7 +1128,7 @@ test("jQuery.sub() - .fn Methods", function(){
test("jQuery.camelCase()", function() {

var tests = {
"foo-bar": "fooBar",
"foo-bar": "fooBar",
"foo-bar-baz": "fooBarBaz"
};

Expand Down

0 comments on commit 27291ff

Please sign in to comment.