3 ms·
If you're going to use the BOM despite it not being in the HTTP spec, why not ignore the spec entirely for encoding? The spec is pretty brain dead in that it sa
by mapgrep 16y ago
If you're going to use the BOM despite it not being in the HTTP spec, why not ignore the spec entirely for encoding? The spec is pretty brain dead in that it says the HTTP header should override explicit declarations within the document itself.
When I've written this sort of code I've found it most practically effective to look at any explicit document declarations (meta tags), then the BOM, then any XML declarations (XHTML was big once upon a time), then the HTTP headers. Far more people have control over their HTML docs than over their HTTP server configuration and I think it's insane to look at the former and ignore the latter. (Actually, there was one case in which I did so: When the BOM and the HTTP headers agreed, I would ignore meta tags, which are downright wrong more often than you would think. For some reason there are a lot of pages out there encoded in UTF-8 but with meta tags declaring Latin-1 [not vice versa like you might think]. This seemed to be correlated with ColdFusion.)
That said, it's impressive that he's thought through different paths for text/plain and application/xml. The last, most comprehensive function I wrote to do this assumed HTML or XML, which obviously isn't comprehensive, and I missed the text/xml wrinkle.