Bug 1718673 - Allow loading text/event-stream in-browser as plain-text, r=smaug,necko-reviewers,kershaw

This patch also updates some documentation which was telling folks supporting new content types to update dead code.

Differential Revision: https://phabricator.services.mozilla.com/D211797
This commit is contained in:
Nika Layzell 2024-06-05 00:05:57 +00:00
Родитель 83e10bcf4c
Коммит 9306cb7c3e
7 изменённых файлов: 87 добавлений и 38 удалений

Просмотреть файл

@ -4632,8 +4632,8 @@ bool nsContentUtils::IsChildOfSameType(Document* aDoc) {
} }
bool nsContentUtils::IsPlainTextType(const nsACString& aContentType) { bool nsContentUtils::IsPlainTextType(const nsACString& aContentType) {
// NOTE: if you add a type here, add it to the CONTENTDLF_CATEGORIES // NOTE: if you add a type here, add it to the content_types array in
// define in nsContentDLF.h as well. // layout/build/components.conf as well.
return aContentType.EqualsLiteral(TEXT_PLAIN) || return aContentType.EqualsLiteral(TEXT_PLAIN) ||
aContentType.EqualsLiteral(TEXT_CSS) || aContentType.EqualsLiteral(TEXT_CSS) ||
aContentType.EqualsLiteral(TEXT_CACHE_MANIFEST) || aContentType.EqualsLiteral(TEXT_CACHE_MANIFEST) ||
@ -4644,7 +4644,8 @@ bool nsContentUtils::IsPlainTextType(const nsACString& aContentType) {
aContentType.EqualsLiteral(APPLICATION_ECMASCRIPT) || aContentType.EqualsLiteral(APPLICATION_ECMASCRIPT) ||
aContentType.EqualsLiteral(TEXT_JAVASCRIPT) || aContentType.EqualsLiteral(TEXT_JAVASCRIPT) ||
aContentType.EqualsLiteral(APPLICATION_JSON) || aContentType.EqualsLiteral(APPLICATION_JSON) ||
aContentType.EqualsLiteral(TEXT_JSON); aContentType.EqualsLiteral(TEXT_JSON) ||
aContentType.EqualsLiteral(TEXT_EVENT_STREAM);
} }
bool nsContentUtils::IsUtf8OnlyPlainTextType(const nsACString& aContentType) { bool nsContentUtils::IsUtf8OnlyPlainTextType(const nsACString& aContentType) {

Просмотреть файл

@ -28,6 +28,7 @@ content_types = [
'text/cache-manifest', 'text/cache-manifest',
'text/css', 'text/css',
'text/ecmascript', 'text/ecmascript',
'text/event-stream',
'text/html', 'text/html',
'text/javascript', 'text/javascript',
'text/json', 'text/json',

Просмотреть файл

@ -55,38 +55,4 @@ class nsContentDLF final : public nsIDocumentLoaderFactory {
nsresult NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult); nsresult NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult);
// clang-format off
#ifdef MOZ_WEBM
#define CONTENTDLF_WEBM_CATEGORIES \
{ "Gecko-Content-Viewers", VIDEO_WEBM, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", AUDIO_WEBM, "@mozilla.org/content/document-loader-factory;1" },
#else
#define CONTENTDLF_WEBM_CATEGORIES
#endif
#define CONTENTDLF_CATEGORIES \
{ "Gecko-Content-Viewers", TEXT_HTML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_PLAIN, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_CACHE_MANIFEST, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_CSS, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_JAVASCRIPT, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_ECMASCRIPT, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_JAVASCRIPT, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_ECMASCRIPT, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_XJAVASCRIPT, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_JSON, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_JSON, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_XHTML_XML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_XML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_XML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_RDF_XML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_RDF, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", VIEWSOURCE_CONTENT_TYPE, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", IMAGE_SVG_XML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_MATHML_XML, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", TEXT_VTT, "@mozilla.org/content/document-loader-factory;1" }, \
{ "Gecko-Content-Viewers", APPLICATION_WAPXHTML_XML, "@mozilla.org/content/document-loader-factory;1" }, \
CONTENTDLF_WEBM_CATEGORIES
// clang-format on
#endif #endif

Просмотреть файл

@ -160,6 +160,9 @@ skip-if = ["os == 'win'"] # Bug 1775761
["browser_cookie_sync_across_tabs.js"] ["browser_cookie_sync_across_tabs.js"]
["browser_display_plaintext_types.js"]
support-files = ["res_hello_h1.sjs"]
["browser_dns_prefetch_link_header.js"] ["browser_dns_prefetch_link_header.js"]
["browser_fetch_lnk.js"] ["browser_fetch_lnk.js"]

Просмотреть файл

@ -0,0 +1,64 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const BASE_URI =
"http://mochi.test:8888/browser/netwerk/test/browser/res_hello_h1.sjs?type=";
async function expectOutcome(aContentType, aBehavior) {
info(`Expecting ${aContentType} to be loaded as ${aBehavior}`);
const url = BASE_URI + aContentType;
await BrowserTestUtils.withNewTab(url, async browser => {
await SpecialPowers.spawn(browser, [url, aBehavior], (url, aBehavior) => {
is(content.location.href, url, "expected url was loaded");
switch (aBehavior) {
case "html":
is(
content.document.querySelector("h1").textContent,
"hello",
"parsed as HTML, so document should contain an <h1> element"
);
break;
case "text":
is(
content.document.body.textContent,
"<h1>hello</h1>",
"parsed as text, so document should contain bare text"
);
break;
case "jsonviewer":
ok(
content.wrappedJSObject.JSONView,
"page has loaded the DevTools JSONViewer"
);
break;
default:
ok(false, "unexpected behavior");
break;
}
});
});
}
add_task(async function test_display_plaintext_type() {
// Make sure that if the data is HTML it loads as HTML.
await expectOutcome("text/html", "html");
// For other text-like types, make sure we load as plain text.
await expectOutcome("text/plain", "text");
await expectOutcome("application/ecmascript", "text");
await expectOutcome("application/javascript", "text");
await expectOutcome("application/x-javascript", "text");
await expectOutcome("text/cache-manifest", "text");
await expectOutcome("text/css", "text");
await expectOutcome("text/ecmascript", "text");
await expectOutcome("text/event-stream", "text");
await expectOutcome("text/javascript", "text");
await expectOutcome("application/json", "jsonviewer");
// NOTE: text/json does not load JSON viewer?
await expectOutcome("text/json", "text");
});

Просмотреть файл

@ -0,0 +1,14 @@
"use strict";
function handleRequest(request, response) {
const query = new URLSearchParams(request.queryString);
// If the
let type = "text/html";
if (query.has("type")) {
type = query.get("type");
}
response.setHeader("Content-Type", type, false);
response.write(`<h1>hello</h1>`);
}

Просмотреть файл

@ -43,4 +43,4 @@ flags passed to the loader service):
For the most part the process ends at step 1 because nsDocShell passes a ``nsDSURIContentListener`` For the most part the process ends at step 1 because nsDocShell passes a ``nsDSURIContentListener``
for the ``nsIURIContentListener`` consulted first and it accepts most of the for the ``nsIURIContentListener`` consulted first and it accepts most of the
`web content types <https://searchfox.org/mozilla-central/search?q=CONTENTDLF_CATEGORIES&redirect=false>`_. `web content types <https://searchfox.org/mozilla-central/source/layout/build/components.conf>`_.