Commit f9fbe4e1 authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Chromium LUCI CQ

Refactor ScriptLoader::IsValidScriptTypeAndLanguage()

Previously, it returns

- `mojom::blink::ScriptType`,
- `bool is_import_map` and
- a `bool` as a return value indicating whether the type is valid or not.

To unify these, this CL introduces `ScriptTypeAtPrepare`
that includes `kImportMap` and uses it throughout ScriptLoader.

Change-Id: I6573bb2c07b81c4161d7ad51c425ac6499e2bc3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2593737
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838780}
parent 779ea6f9
......@@ -107,9 +107,10 @@ void HTMLScriptElement::ParseAttribute(
Node::InsertionNotificationRequest HTMLScriptElement::InsertedInto(
ContainerNode& insertion_point) {
if (insertion_point.isConnected() && HasSourceAttribute() &&
!ScriptLoader::IsValidScriptTypeAndLanguage(
ScriptLoader::GetScriptTypeAtPrepare(
TypeAttributeValue(), LanguageAttributeValue(),
ScriptLoader::kDisallowLegacyTypeInTypeAttribute)) {
ScriptLoader::kDisallowLegacyTypeInTypeAttribute) ==
ScriptLoader::ScriptTypeAtPrepare::kInvalid) {
UseCounter::Count(GetDocument(),
WebFeature::kScriptElementWithInvalidTypeHasSrc);
}
......
......@@ -669,23 +669,26 @@ class TokenPreloadScanner::StartTagScanner {
if (Match(tag_impl_, html_names::kInputTag) && !input_is_image_)
return false;
if (Match(tag_impl_, html_names::kScriptTag)) {
mojom::blink::ScriptType script_type = mojom::blink::ScriptType::kClassic;
bool is_import_map = false;
if (!ScriptLoader::IsValidScriptTypeAndLanguage(
ScriptLoader::ScriptTypeAtPrepare script_type =
ScriptLoader::GetScriptTypeAtPrepare(
type_attribute_value_, language_attribute_value_,
ScriptLoader::kAllowLegacyTypeInTypeAttribute, &script_type,
&is_import_map)) {
ScriptLoader::kAllowLegacyTypeInTypeAttribute);
switch (script_type) {
case ScriptLoader::ScriptTypeAtPrepare::kInvalid:
return false;
}
if (is_import_map) {
// External import maps are not yet supported. https://crbug.com/922212
case ScriptLoader::ScriptTypeAtPrepare::kImportMap:
// TODO(crbug.com/922212): External import maps are not yet supported.
return false;
}
case ScriptLoader::ScriptTypeAtPrepare::kClassic:
case ScriptLoader::ScriptTypeAtPrepare::kModule:
if (ScriptLoader::BlockForNoModule(script_type,
nomodule_attribute_value_)) {
return false;
}
}
}
return true;
}
......
......@@ -171,9 +171,10 @@ HTMLTreeBuilderSimulator::SimulatedToken HTMLTreeBuilderSimulator::Simulate(
language_attribute_value = item->Value();
}
if (ScriptLoader::IsValidScriptTypeAndLanguage(
if (ScriptLoader::GetScriptTypeAtPrepare(
type_attribute_value, language_attribute_value,
ScriptLoader::kAllowLegacyTypeInTypeAttribute)) {
ScriptLoader::kAllowLegacyTypeInTypeAttribute) !=
ScriptLoader::ScriptTypeAtPrepare::kInvalid) {
simulated_token = kValidScriptStart;
}
} else if (ThreadSafeMatch(tag_name, html_names::kLinkTag)) {
......
......@@ -61,19 +61,17 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>,
kAllowLegacyTypeInTypeAttribute
};
// |out_is_import_map| is set separately from |out_script_type| in order
// to avoid adding import maps as a mojom::blink::ScriptType enum, because
// import maps are processed quite differently from classic/module scripts.
//
// TODO(hiroshige, kouhei): Make the method signature simpler.
static bool IsValidScriptTypeAndLanguage(
// Script type at the time of #prepare-a-script. Import maps are included here
// but not in `mojom::blink::ScriptType` because import maps are handled
// differently from ordinal scripts after PrepareScript().
enum class ScriptTypeAtPrepare { kClassic, kModule, kImportMap, kInvalid };
static ScriptTypeAtPrepare GetScriptTypeAtPrepare(
const String& type_attribute_value,
const String& language_attribute_value,
LegacyTypeSupport support_legacy_types,
mojom::blink::ScriptType* out_script_type = nullptr,
bool* out_is_import_map = nullptr);
LegacyTypeSupport support_legacy_types);
static bool BlockForNoModule(mojom::blink::ScriptType, bool nomodule);
static bool BlockForNoModule(ScriptTypeAtPrepare, bool nomodule);
static network::mojom::CredentialsMode ModuleScriptCredentialsMode(
CrossOriginAttributeValue);
......@@ -97,7 +95,7 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>,
bool IsParserInserted() const { return parser_inserted_; }
bool AlreadyStarted() const { return already_started_; }
bool IsNonBlocking() const { return non_blocking_; }
mojom::blink::ScriptType GetScriptType() const { return script_type_; }
ScriptTypeAtPrepare GetScriptType() const { return script_type_; }
// Helper functions used by our parent classes.
void DidNotifySubtreeInsertionsToDocument();
......@@ -178,7 +176,7 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>,
// <spec href="https://html.spec.whatwg.org/C/#concept-script-type">... It is
// determined when the script is prepared, ...</spec>
mojom::blink::ScriptType script_type_ = mojom::blink::ScriptType::kClassic;
ScriptTypeAtPrepare script_type_ = ScriptTypeAtPrepare::kInvalid;
// <spec href="https://html.spec.whatwg.org/C/#concept-script-external">
// ... It is determined when the script is prepared, ...</spec>
......
......@@ -77,7 +77,8 @@ void XMLParserScriptRunner::ProcessScriptElement(
bool success = script_loader->PrepareScript(
script_start_position, ScriptLoader::kAllowLegacyTypeInTypeAttribute);
if (script_loader->GetScriptType() != mojom::blink::ScriptType::kClassic) {
if (script_loader->GetScriptType() ==
ScriptLoader::ScriptTypeAtPrepare::kModule) {
// XMLDocumentParser does not support a module script, and thus ignores it.
success = false;
document.AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment