Bug 1846780: responseXML is populated for a blob with an empty type. r=twisniewski

A blob with an empty content type or any other invalid content type should
return a value for responseXML.

Differential Revision: https://phabricator.services.mozilla.com/D185185
This commit is contained in:
Dan Robertson 2023-08-08 00:34:03 +00:00
parent a117230132
commit 2e71051f28
2 changed files with 52 additions and 3 deletions

View File

@ -2061,9 +2061,9 @@ XMLHttpRequestMainThread::OnStartRequest(nsIRequest* request) {
} else {
mIsHtml = true;
}
} else if (!(type.EqualsLiteral("text/xml") ||
type.EqualsLiteral("application/xml") ||
StringEndsWith(type, "+xml"_ns))) {
} else if (!type.IsEmpty() && (!(type.EqualsLiteral("text/xml") ||
type.EqualsLiteral("application/xml") ||
StringEndsWith(type, "+xml"_ns)))) {
// Follow https://xhr.spec.whatwg.org/
// If final MIME type is not null, text/html, text/xml, application/xml,
// or does not end in +xml, return null.

View File

@ -0,0 +1,49 @@
<!doctype html>
<html>
<head>
<title>XMLHttpRequest: responseXML content-type test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsexml"/>
</head>
<body>
</body>
<script>
function parseBlob(blob) {
return new Promise(resolve => {
let xhr = new XMLHttpRequest();
xhr.open("GET", URL.createObjectURL(blob));
xhr.onload = () => {
resolve(xhr.responseXML);
}
xhr.send();
});
}
promise_test(async function() {
let blob = new Blob(["<x></x>"]);
let responseXML = await parseBlob(blob);
assert_not_equals(responseXML, null);
}, "Empty MIME type should be equivalent to text/xml")
promise_test(async function() {
let blob = new Blob(["<x></x>"], {type: "text/html"});
let responseXML = await parseBlob(blob);
assert_equals(responseXML, null);
}, "HTML content type should return null")
promise_test(async function() {
let blob = new Blob(["<x></x>"], {type: "text/plain"});
let responseXML = await parseBlob(blob);
assert_equals(responseXML, null);
}, "Non XML or HTML content type should return null")
promise_test(async function() {
let blob = new Blob(["<x></x>"], {type: "text/xml"});
let responseXML = await parseBlob(blob);
assert_not_equals(responseXML, null);
}, "XML content type should parse")
</script>
</html>