Closed Bug 1466526 Opened 6 years ago Closed 6 years ago

XMLHttpRequest response for local .html file is XMLDocument instead of HTMLDocument

Categories

(Firefox :: Untriaged, defect)

60 Branch
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 1037762

People

(Reporter: danny0838, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
Build ID: 20180516032328

Steps to reproduce:

Open a local HTML file (i.e. using file: protocol) and run below javascript in it: 


var xhr = new XMLHttpRequest();

xhr.onload = function (event) {
  if (xhr.status == 200 || xhr.status == 0) {
    var doc = xhr.response;
    console.warn(doc);
    console.warn(doc.body);
  }
};

xhr.responseType = 'document';
xhr.open("GET", 'file.html', true);
xhr.send();


Actual results:

xhr.response is XMLDocument


Expected results:

xhr.response should be HTMLDocument
This is the spec-defined behavior, as far as I can tell last I looked at the spec.

You probably want to overrideMimeType if you know you want HTML.
Status: UNCONFIRMED → RESOLVED
Closed: 6 years ago
Resolution: --- → DUPLICATE
Could you provide the spec if it is really defined?
See https://xhr.spec.whatwg.org/#response-mime-type which uses https://fetch.spec.whatwg.org/#concept-header-extract-mime-type

In the case of a file:// load there is no Content-Type header, so concept-extract-mime-type returns an empty byte sequence and then step 2 of the "response MIME type" steps sets the type to "text/xml".

If you don't overrideMimeType, then https://xhr.spec.whatwg.org/#final-mime-type ends up as "text/xml" as well.

Then in https://xhr.spec.whatwg.org/#document-response the final mime type is "text/xml", so you get an XMLDocument.
Though in theory, the weasel-hole in https://fetch.spec.whatwg.org/#scheme-fetch for file:// _could_ allow returning a Response that has a Content-Type header.  But in practice that's not what browsers do.  Instead, in some contexts they apply <https://mimesniff.spec.whatwg.org/>.  But XHR is explicitly defined to not do that.
You need to log in before you can comment on or make changes to this bug.