From 27291ff06ddb655f90a8d1eada71f7ac61499b12 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Sat, 23 Jul 2011 03:26:36 +0200 Subject: [PATCH] Fixes #9255: xml parsing error in $.parseXML is now properly detected for all browsers. Unit test added. --- src/core.js | 29 ++++++++++++++--------------- test/unit/core.js | 24 ++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/core.js b/src/core.js index ab0d9f7b5..715d73ad3 100644 --- a/src/core.js +++ b/src/core.js @@ -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; }, diff --git a/test/unit/core.js b/test/unit/core.js index 3a1553526..8c285f6dd 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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++; @@ -984,6 +984,26 @@ test("jQuery.parseJSON", function(){ } }); +test("jQuery.parseXML", 4, function(){ + var xml, tmp; + try { + xml = jQuery.parseXML( "

A well-formed xml string

" ); + tmp = xml.getElementsByTagName( "p" )[ 0 ]; + ok( !!tmp, "

present in document" ); + tmp = tmp.getElementsByTagName( "b" )[ 0 ]; + ok( !!tmp, " present in document" ); + strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", " text is as expected" ); + } catch (e) { + strictEqual( e, undefined, "unexpected error" ); + } + try { + xml = jQuery.parseXML( "

Not a <well-formed xml string

" ); + ok( false, "invalid xml not detected" ); + } catch( e ) { + strictEqual( e, "Invalid XML:

Not a <well-formed xml string

", "invalid xml detected" ); + } +}); + test("jQuery.sub() - Static Methods", function(){ expect(18); var Subclass = jQuery.sub(); @@ -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" }; -- 2.39.5