Skip to content

Commit 1fb38a1

Browse files
committed
Bug 1720570 - Support fetching css module scripts; r=dom-core,smaug
Differential Revision: https://phabricator.services.mozilla.com/D263415
1 parent 3fe727e commit 1fb38a1

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

dom/base/nsContentUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8660,6 +8660,11 @@ bool nsContentUtils::IsJsonMimeType(const nsAString& aMimeType) {
86608660
return StringEndsWith(subtype, u"+json"_ns);
86618661
}
86628662

8663+
// https://html.spec.whatwg.org/#fetch-a-single-module-script, 7.3
8664+
bool nsContentUtils::IsCssMimeType(const nsAString& aMimeType) {
8665+
return aMimeType.LowerCaseEqualsLiteral("text/css");
8666+
}
8667+
86638668
bool nsContentUtils::PrefetchPreloadEnabled(nsIDocShell* aDocShell) {
86648669
//
86658670
// SECURITY CHECK: disable prefetching and preloading from mailnews!

dom/base/nsContentUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,12 @@ class nsContentUtils {
27432743
*/
27442744
static bool IsJsonMimeType(const nsAString& aMimeType);
27452745

2746+
/**
2747+
* Returns true if the given MIME type string is a valid CSS MIME type,
2748+
* otherwise false.
2749+
*/
2750+
static bool IsCssMimeType(const nsAString& aMimeType);
2751+
27462752
static void SplitMimeType(const nsAString& aValue, nsString& aType,
27472753
nsString& aParams);
27482754

dom/script/ScriptLoader.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,32 @@ nsContentPolicyType ScriptLoadRequestToContentPolicyType(
430430
ScriptLoadRequest* aRequest) {
431431
if (aRequest->GetScriptLoadContext()->IsPreload()) {
432432
if (aRequest->IsModuleRequest()) {
433-
return aRequest->AsModuleRequest()->mModuleType ==
434-
JS::ModuleType::JavaScript
435-
? nsIContentPolicy::TYPE_INTERNAL_MODULE_PRELOAD
436-
: nsIContentPolicy::TYPE_INTERNAL_JSON_PRELOAD;
433+
switch (aRequest->AsModuleRequest()->mModuleType) {
434+
case JS::ModuleType::JavaScript:
435+
return nsIContentPolicy::TYPE_INTERNAL_MODULE_PRELOAD;
436+
case JS::ModuleType::JSON:
437+
return nsIContentPolicy::TYPE_INTERNAL_JSON_PRELOAD;
438+
case JS::ModuleType::CSS:
439+
return nsIContentPolicy::TYPE_INTERNAL_STYLESHEET_PRELOAD;
440+
case JS::ModuleType::Unknown:
441+
MOZ_ASSERT_UNREACHABLE("Unknown module type");
442+
}
437443
}
438444

439445
return nsIContentPolicy::TYPE_INTERNAL_SCRIPT_PRELOAD;
440446
}
441447

442448
if (aRequest->IsModuleRequest()) {
443-
return aRequest->AsModuleRequest()->mModuleType ==
444-
JS::ModuleType::JavaScript
445-
? nsIContentPolicy::TYPE_INTERNAL_MODULE
446-
: nsIContentPolicy::TYPE_JSON;
449+
switch (aRequest->AsModuleRequest()->mModuleType) {
450+
case JS::ModuleType::Unknown:
451+
MOZ_CRASH("Unexpected module type");
452+
case JS::ModuleType::JavaScript:
453+
return nsIContentPolicy::TYPE_INTERNAL_MODULE;
454+
case JS::ModuleType::JSON:
455+
return nsIContentPolicy::TYPE_JSON;
456+
case JS::ModuleType::CSS:
457+
return nsIContentPolicy::TYPE_STYLESHEET;
458+
}
447459
}
448460

449461
return nsIContentPolicy::TYPE_INTERNAL_SCRIPT;
@@ -1963,10 +1975,12 @@ nsresult ScriptLoader::AttemptOffThreadScriptCompile(
19631975
return NS_OK;
19641976
}
19651977

1966-
// Don't off-thread compile JSON modules.
1967-
// https://bugzilla.mozilla.org/show_bug.cgi?id=1912112
1978+
// Don't off-thread compile JSON or CSS modules.
1979+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1912112 (JSON)
1980+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1987143 (CSS)
19681981
if (aRequest->IsModuleRequest() &&
1969-
aRequest->AsModuleRequest()->mModuleType == JS::ModuleType::JSON) {
1982+
(aRequest->AsModuleRequest()->mModuleType == JS::ModuleType::JSON ||
1983+
aRequest->AsModuleRequest()->mModuleType == JS::ModuleType::CSS)) {
19701984
return NS_OK;
19711985
}
19721986

@@ -4431,6 +4445,11 @@ static bool MimeTypeMatchesExpectedModuleType(
44314445
return true;
44324446
}
44334447

4448+
if (expectedModuleType == JS::ModuleType::CSS &&
4449+
nsContentUtils::IsCssMimeType(typeString)) {
4450+
return true;
4451+
}
4452+
44344453
return false;
44354454
}
44364455

0 commit comments

Comments
 (0)