Fix cross browser issues for responseXML

- IE8 doesn't support indexOf, use our own arrayContains
- Most browsers don't allow 'text/html' when using a DOMParser, so we
  won't support it for now.
- Firefox doesn't want to serialize Documents over selenium, so don't
  have them in as our expected, just use instanceof ourselves
This commit is contained in:
slackersoft 2014-11-01 16:26:12 -07:00
parent 4751eaafbb
commit 9681f79ece
3 changed files with 12 additions and 27 deletions

View File

@ -101,7 +101,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
function parseXml(xmlText, contentType) { function parseXml(xmlText, contentType) {
if (global.DOMParser) { if (global.DOMParser) {
return (new global.DOMParser()).parseFromString(xmlText, contentType); return (new global.DOMParser()).parseFromString(xmlText, 'text/xml');
} else { } else {
var xml = new global.ActiveXObject("Microsoft.XMLDOM"); var xml = new global.ActiveXObject("Microsoft.XMLDOM");
xml.async = "false"; xml.async = "false";
@ -110,10 +110,10 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
} }
} }
var xmlParsables = ['text/html', 'text/xml', 'application/xml']; var xmlParsables = ['text/xml', 'application/xml'];
function getResponseXml(responseText, contentType) { function getResponseXml(responseText, contentType) {
if (xmlParsables.indexOf(contentType.toLowerCase()) >= 0) { if (arrayContains(xmlParsables, contentType.toLowerCase())) {
return parseXml(responseText, contentType); return parseXml(responseText, contentType);
} else if (contentType.match(/\+xml$/)) { } else if (contentType.match(/\+xml$/)) {
return parseXml(responseText, 'text/xml'); return parseXml(responseText, 'text/xml');

View File

@ -393,10 +393,10 @@ describe('FakeRequest', function() {
request.response({ status: 200, contentType: 'text/xml', responseText: '<dom><stuff/></dom>' }); request.response({ status: 200, contentType: 'text/xml', responseText: '<dom><stuff/></dom>' });
if (typeof window.Document !== 'undefined') { if (typeof window.Document !== 'undefined') {
expect(request.responseXML).toEqual(jasmine.any(window.Document)); expect(request.responseXML instanceof window.Document).toBe(true);
} else { } else {
// IE 8 // IE 8
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject)); expect(request.responseXML instanceof window.ActiveXObject).toBe(true);
} }
}); });
@ -408,25 +408,10 @@ describe('FakeRequest', function() {
request.response({ status: 200, contentType: 'application/xml', responseText: '<dom><stuff/></dom>' }); request.response({ status: 200, contentType: 'application/xml', responseText: '<dom><stuff/></dom>' });
if (typeof window.Document !== 'undefined') { if (typeof window.Document !== 'undefined') {
expect(request.responseXML).toEqual(jasmine.any(window.Document)); expect(request.responseXML instanceof window.Document).toBe(true);
} else { } else {
// IE 8 // IE 8
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject)); expect(request.responseXML instanceof window.ActiveXObject).toBe(true);
}
});
it('parses a text/html document into responseXML', function() {
var request = new this.FakeRequest();
request.open();
request.send();
request.response({ status: 200, contentType: 'text/html', responseText: '<dom><stuff/></dom>' });
if (typeof window.Document !== 'undefined') {
expect(request.responseXML).toEqual(jasmine.any(window.Document));
} else {
// IE 8
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject));
} }
}); });
@ -438,10 +423,10 @@ describe('FakeRequest', function() {
request.response({ status: 200, contentType: 'application/text+xml', responseText: '<dom><stuff/></dom>' }); request.response({ status: 200, contentType: 'application/text+xml', responseText: '<dom><stuff/></dom>' });
if (typeof window.Document !== 'undefined') { if (typeof window.Document !== 'undefined') {
expect(request.responseXML).toEqual(jasmine.any(window.Document)); expect(request.responseXML instanceof window.Document).toBe(true);
} else { } else {
// IE 8 // IE 8
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject)); expect(request.responseXML instanceof window.ActiveXObject).toBe(true);
} }
}); });
}); });

View File

@ -56,7 +56,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
function parseXml(xmlText, contentType) { function parseXml(xmlText, contentType) {
if (global.DOMParser) { if (global.DOMParser) {
return (new global.DOMParser()).parseFromString(xmlText, contentType); return (new global.DOMParser()).parseFromString(xmlText, 'text/xml');
} else { } else {
var xml = new global.ActiveXObject("Microsoft.XMLDOM"); var xml = new global.ActiveXObject("Microsoft.XMLDOM");
xml.async = "false"; xml.async = "false";
@ -65,10 +65,10 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
} }
} }
var xmlParsables = ['text/html', 'text/xml', 'application/xml']; var xmlParsables = ['text/xml', 'application/xml'];
function getResponseXml(responseText, contentType) { function getResponseXml(responseText, contentType) {
if (xmlParsables.indexOf(contentType.toLowerCase()) >= 0) { if (arrayContains(xmlParsables, contentType.toLowerCase())) {
return parseXml(responseText, contentType); return parseXml(responseText, contentType);
} else if (contentType.match(/\+xml$/)) { } else if (contentType.match(/\+xml$/)) {
return parseXml(responseText, 'text/xml'); return parseXml(responseText, 'text/xml');