Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8969732

Browse files
authored
Core: Report browser errors in parseXML
Fixesgh-4784Closesgh-4816
1 parentfd42109 commit8969732

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
@@ -2,7 +2,7 @@ import jQuery from "../core.js";
22

33
// Cross-browser xml parsing
44
jQuery.parseXML=function(data){
5-
varxml;
5+
varxml,parserErrorElem;
66
if(!data||typeofdata!=="string"){
77
returnnull;
88
}
@@ -11,12 +11,17 @@ jQuery.parseXML = function( data ) {
1111
// IE throws on parseFromString with invalid input.
1212
try{
1313
xml=(newwindow.DOMParser()).parseFromString(data,"text/xml");
14-
}catch(e){
15-
xml=undefined;
16-
}
14+
}catch(e){}
1715

18-
if(!xml||xml.getElementsByTagName("parsererror").length){
19-
jQuery.error("Invalid XML: "+data);
16+
parserErrorElem=xml&&xml.getElementsByTagName("parsererror")[0];
17+
if(!xml||parserErrorElem){
18+
jQuery.error("Invalid XML: "+(
19+
parserErrorElem ?
20+
jQuery.map(parserErrorElem.childNodes,function(el){
21+
returnel.textContent;
22+
}).join("\n") :
23+
data
24+
));
2025
}
2126
returnxml;
2227
};

‎test/unit/core.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,9 +1418,9 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
14181418
}
14191419
try{
14201420
xml=jQuery.parseXML("<p>Not a <<b>well-formed</b> xml string</p>");
1421-
assert.ok(false,"invalidxml not detected");
1421+
assert.ok(false,"invalidXML not detected");
14221422
}catch(e){
1423-
assert.strictEqual(e.message,"Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>","invalidxml detected");
1423+
assert.ok(e.message.indexOf("Invalid XML:")===0,"invalidXML detected");
14241424
}
14251425
try{
14261426
xml=jQuery.parseXML("");
@@ -1436,6 +1436,29 @@ QUnit.test( "jQuery.parseXML", function( assert ) {
14361436
}
14371437
});
14381438

1439+
// Support: IE 11+
1440+
// IE throws an error when parsing invalid XML instead of reporting the error
1441+
// in a `parsererror` element, skip the test there.
1442+
QUnit.testUnlessIE("jQuery.parseXML - error reporting",function(assert){
1443+
assert.expect(2);
1444+
1445+
varerrorArg,lineMatch,line,columnMatch,column;
1446+
1447+
sinon.stub(jQuery,"error");
1448+
1449+
jQuery.parseXML("<p>Not a <<b>well-formed</b> xml string</p>");
1450+
errorArg=jQuery.error.firstCall.lastArg.toLowerCase();
1451+
console.log("errorArg",errorArg);
1452+
1453+
lineMatch=errorArg.match(/line\s*(?:number)?\s*(\d+)/);
1454+
line=lineMatch&&lineMatch[1];
1455+
columnMatch=errorArg.match(/column\s*(\d+)/);
1456+
column=columnMatch&&columnMatch[1];
1457+
1458+
assert.strictEqual(line,"1","reports error line");
1459+
assert.strictEqual(column,"11","reports error column");
1460+
});
1461+
14391462
testIframe(
14401463
"document ready when jQuery loaded asynchronously (#13655)",
14411464
"core/dynamic_ready.html",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp