Skip to content

Commit 54d9883

Browse files
committed
Core: Report browser errors in parseXML
Fixes gh-4784 Closes gh-4816 (cherry picked from commit 8969732)
1 parent 2fadbc0 commit 54d9883

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/core/parseXML.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define( [
66

77
// Cross-browser xml parsing
88
jQuery.parseXML = function( data ) {
9-
var xml;
9+
var xml, parserErrorElem;
1010
if ( !data || typeof data !== "string" ) {
1111
return null;
1212
}
@@ -15,12 +15,17 @@ jQuery.parseXML = function( data ) {
1515
// IE throws on parseFromString with invalid input.
1616
try {
1717
xml = ( new window.DOMParser() ).parseFromString( data, "text/xml" );
18-
} catch ( e ) {
19-
xml = undefined;
20-
}
18+
} catch ( e ) {}
2119

22-
if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
23-
jQuery.error( "Invalid XML: " + data );
20+
parserErrorElem = xml && xml.getElementsByTagName( "parsererror" )[ 0 ];
21+
if ( !xml || parserErrorElem ) {
22+
jQuery.error( "Invalid XML: " + (
23+
parserErrorElem ?
24+
jQuery.map( parserErrorElem.childNodes, function( el ) {
25+
return el.textContent;
26+
} ).join( "\n" ) :
27+
data
28+
) );
2429
}
2530
return xml;
2631
};

test/unit/core.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,9 +1411,9 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
14111411
}
14121412
try {
14131413
xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1414-
assert.ok( false, "invalid xml not detected" );
1414+
assert.ok( false, "invalid XML not detected" );
14151415
} catch ( e ) {
1416-
assert.strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
1416+
assert.ok( e.message.indexOf( "Invalid XML:" ) === 0, "invalid XML detected" );
14171417
}
14181418
try {
14191419
xml = jQuery.parseXML( "" );
@@ -1429,6 +1429,29 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
14291429
}
14301430
} );
14311431

1432+
// Support: IE 11+
1433+
// IE throws an error when parsing invalid XML instead of reporting the error
1434+
// in a `parsererror` element, skip the test there.
1435+
QUnit.testUnlessIE( "jQuery.parseXML - error reporting", function( assert ) {
1436+
assert.expect( 2 );
1437+
1438+
var errorArg, lineMatch, line, columnMatch, column;
1439+
1440+
sinon.stub( jQuery, "error" );
1441+
1442+
jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1443+
errorArg = jQuery.error.firstCall.lastArg.toLowerCase();
1444+
console.log( "errorArg", errorArg );
1445+
1446+
lineMatch = errorArg.match( /line\s*(?:number)?\s*(\d+)/ );
1447+
line = lineMatch && lineMatch[ 1 ];
1448+
columnMatch = errorArg.match( /column\s*(\d+)/ );
1449+
column = columnMatch && columnMatch[ 1 ];
1450+
1451+
assert.strictEqual( line, "1", "reports error line" );
1452+
assert.strictEqual( column, "11", "reports error column" );
1453+
} );
1454+
14321455
testIframe(
14331456
"Conditional compilation compatibility (#13274)",
14341457
"core/cc_on.html",

0 commit comments

Comments
 (0)