aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2020-12-08 11:22:21 +0100
committerMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2020-12-08 11:34:50 +0100
commit54d98835b6e35e3dcb92a9f30bcac82ad2039487 (patch)
tree91d7c68a937a97b3c4c6f25ab6df3c0d9ab8df98
parent2fadbc0a9839893f434cfca42587593c8794375d (diff)
downloadjquery-54d98835b6e35e3dcb92a9f30bcac82ad2039487.tar.gz
jquery-54d98835b6e35e3dcb92a9f30bcac82ad2039487.zip
Core: Report browser errors in parseXML
Fixes gh-4784 Closes gh-4816 (cherry picked from commit 8969732518470a7f8e654d5bc5be0b0076cb0b87)
-rw-r--r--src/core/parseXML.js17
-rw-r--r--test/unit/core.js27
2 files changed, 36 insertions, 8 deletions
diff --git a/src/core/parseXML.js b/src/core/parseXML.js
index acf7ab259..9b5d29011 100644
--- a/src/core/parseXML.js
+++ b/src/core/parseXML.js
@@ -6,7 +6,7 @@ define( [
// Cross-browser xml parsing
jQuery.parseXML = function( data ) {
- var xml;
+ var xml, parserErrorElem;
if ( !data || typeof data !== "string" ) {
return null;
}
@@ -15,12 +15,17 @@ jQuery.parseXML = function( data ) {
// IE throws on parseFromString with invalid input.
try {
xml = ( new window.DOMParser() ).parseFromString( data, "text/xml" );
- } catch ( e ) {
- xml = undefined;
- }
+ } catch ( e ) {}
- if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
- jQuery.error( "Invalid XML: " + data );
+ parserErrorElem = xml && xml.getElementsByTagName( "parsererror" )[ 0 ];
+ if ( !xml || parserErrorElem ) {
+ jQuery.error( "Invalid XML: " + (
+ parserErrorElem ?
+ jQuery.map( parserErrorElem.childNodes, function( el ) {
+ return el.textContent;
+ } ).join( "\n" ) :
+ data
+ ) );
}
return xml;
};
diff --git a/test/unit/core.js b/test/unit/core.js
index b31ee9e2b..66465e666 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -1411,9 +1411,9 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
}
try {
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 ) {
- 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 {
xml = jQuery.parseXML( "" );
@@ -1429,6 +1429,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(
"Conditional compilation compatibility (#13274)",
"core/cc_on.html",