Core: Report browser errors in parseXML

Fixes gh-4784
Closes gh-4816
This commit is contained in:
Michał Gołębiowski-Owczarek 2020-12-08 11:22:21 +01:00 committed by GitHub
parent fd421097c5
commit 8969732518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 8 deletions

View File

@ -2,7 +2,7 @@ import jQuery from "../core.js";
// Cross-browser xml parsing // Cross-browser xml parsing
jQuery.parseXML = function( data ) { jQuery.parseXML = function( data ) {
var xml; var xml, parserErrorElem;
if ( !data || typeof data !== "string" ) { if ( !data || typeof data !== "string" ) {
return null; return null;
} }
@ -11,12 +11,17 @@ jQuery.parseXML = function( data ) {
// IE throws on parseFromString with invalid input. // IE throws on parseFromString with invalid input.
try { try {
xml = ( new window.DOMParser() ).parseFromString( data, "text/xml" ); xml = ( new window.DOMParser() ).parseFromString( data, "text/xml" );
} catch ( e ) { } catch ( e ) {}
xml = undefined;
}
if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) { parserErrorElem = xml && xml.getElementsByTagName( "parsererror" )[ 0 ];
jQuery.error( "Invalid XML: " + data ); if ( !xml || parserErrorElem ) {
jQuery.error( "Invalid XML: " + (
parserErrorElem ?
jQuery.map( parserErrorElem.childNodes, function( el ) {
return el.textContent;
} ).join( "\n" ) :
data
) );
} }
return xml; return xml;
}; };

View File

@ -1418,9 +1418,9 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
} }
try { try {
xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" ); xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
assert.ok( false, "invalid xml not detected" ); assert.ok( false, "invalid XML not detected" );
} catch ( e ) { } catch ( e ) {
assert.strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" ); assert.ok( e.message.indexOf( "Invalid XML:" ) === 0, "invalid XML detected" );
} }
try { try {
xml = jQuery.parseXML( "" ); xml = jQuery.parseXML( "" );
@ -1436,6 +1436,29 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
} }
} ); } );
// Support: IE 11+
// IE throws an error when parsing invalid XML instead of reporting the error
// in a `parsererror` element, skip the test there.
QUnit.testUnlessIE( "jQuery.parseXML - error reporting", function( assert ) {
assert.expect( 2 );
var errorArg, lineMatch, line, columnMatch, column;
sinon.stub( jQuery, "error" );
jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
errorArg = jQuery.error.firstCall.lastArg.toLowerCase();
console.log( "errorArg", errorArg );
lineMatch = errorArg.match( /line\s*(?:number)?\s*(\d+)/ );
line = lineMatch && lineMatch[ 1 ];
columnMatch = errorArg.match( /column\s*(\d+)/ );
column = columnMatch && columnMatch[ 1 ];
assert.strictEqual( line, "1", "reports error line" );
assert.strictEqual( column, "11", "reports error column" );
} );
testIframe( testIframe(
"document ready when jQuery loaded asynchronously (#13655)", "document ready when jQuery loaded asynchronously (#13655)",
"core/dynamic_ready.html", "core/dynamic_ready.html",