Skip to content

Commit 161decf

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

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
@@ -8659,6 +8659,11 @@ bool nsContentUtils::IsJsonMimeType(const nsAString& aMimeType) {
86598659
return StringEndsWith(subtype, u"+json"_ns);
86608660
}
86618661

8662+
// https://html.spec.whatwg.org/#fetch-a-single-module-script, 7.3
8663+
bool nsContentUtils::IsCssMimeType(const nsAString& aMimeType) {
8664+
return aMimeType.LowerCaseEqualsLiteral("text/css");
8665+
}
8666+
86628667
bool nsContentUtils::PrefetchPreloadEnabled(nsIDocShell* aDocShell) {
86638668
//
86648669
// 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
@@ -2740,6 +2740,12 @@ class nsContentUtils {
27402740
*/
27412741
static bool IsJsonMimeType(const nsAString& aMimeType);
27422742

2743+
/**
2744+
* Returns true if the given MIME type string is a valid CSS MIME type,
2745+
* otherwise false.
2746+
*/
2747+
static bool IsCssMimeType(const nsAString& aMimeType);
2748+
27432749
static void SplitMimeType(const nsAString& aValue, nsString& aType,
27442750
nsString& aParams);
27452751

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

@@ -4419,6 +4433,11 @@ static bool MimeTypeMatchesExpectedModuleType(
44194433
return true;
44204434
}
44214435

4436+
if (expectedModuleType == JS::ModuleType::CSS &&
4437+
nsContentUtils::IsCssMimeType(typeString)) {
4438+
return true;
4439+
}
4440+
44224441
return false;
44234442
}
44244443

0 commit comments

Comments
 (0)