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( ...@@ -107,9 +107,10 @@ void HTMLScriptElement::ParseAttribute(
Node::InsertionNotificationRequest HTMLScriptElement::InsertedInto( Node::InsertionNotificationRequest HTMLScriptElement::InsertedInto(
ContainerNode& insertion_point) { ContainerNode& insertion_point) {
if (insertion_point.isConnected() && HasSourceAttribute() && if (insertion_point.isConnected() && HasSourceAttribute() &&
!ScriptLoader::IsValidScriptTypeAndLanguage( ScriptLoader::GetScriptTypeAtPrepare(
TypeAttributeValue(), LanguageAttributeValue(), TypeAttributeValue(), LanguageAttributeValue(),
ScriptLoader::kDisallowLegacyTypeInTypeAttribute)) { ScriptLoader::kDisallowLegacyTypeInTypeAttribute) ==
ScriptLoader::ScriptTypeAtPrepare::kInvalid) {
UseCounter::Count(GetDocument(), UseCounter::Count(GetDocument(),
WebFeature::kScriptElementWithInvalidTypeHasSrc); WebFeature::kScriptElementWithInvalidTypeHasSrc);
} }
......
...@@ -669,21 +669,24 @@ class TokenPreloadScanner::StartTagScanner { ...@@ -669,21 +669,24 @@ class TokenPreloadScanner::StartTagScanner {
if (Match(tag_impl_, html_names::kInputTag) && !input_is_image_) if (Match(tag_impl_, html_names::kInputTag) && !input_is_image_)
return false; return false;
if (Match(tag_impl_, html_names::kScriptTag)) { if (Match(tag_impl_, html_names::kScriptTag)) {
mojom::blink::ScriptType script_type = mojom::blink::ScriptType::kClassic; ScriptLoader::ScriptTypeAtPrepare script_type =
bool is_import_map = false; ScriptLoader::GetScriptTypeAtPrepare(
if (!ScriptLoader::IsValidScriptTypeAndLanguage(
type_attribute_value_, language_attribute_value_, type_attribute_value_, language_attribute_value_,
ScriptLoader::kAllowLegacyTypeInTypeAttribute, &script_type, ScriptLoader::kAllowLegacyTypeInTypeAttribute);
&is_import_map)) { switch (script_type) {
return false; 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:
return false; // TODO(crbug.com/922212): External import maps are not yet supported.
} return false;
if (ScriptLoader::BlockForNoModule(script_type,
nomodule_attribute_value_)) { case ScriptLoader::ScriptTypeAtPrepare::kClassic:
return false; case ScriptLoader::ScriptTypeAtPrepare::kModule:
if (ScriptLoader::BlockForNoModule(script_type,
nomodule_attribute_value_)) {
return false;
}
} }
} }
return true; return true;
......
...@@ -171,9 +171,10 @@ HTMLTreeBuilderSimulator::SimulatedToken HTMLTreeBuilderSimulator::Simulate( ...@@ -171,9 +171,10 @@ HTMLTreeBuilderSimulator::SimulatedToken HTMLTreeBuilderSimulator::Simulate(
language_attribute_value = item->Value(); language_attribute_value = item->Value();
} }
if (ScriptLoader::IsValidScriptTypeAndLanguage( if (ScriptLoader::GetScriptTypeAtPrepare(
type_attribute_value, language_attribute_value, type_attribute_value, language_attribute_value,
ScriptLoader::kAllowLegacyTypeInTypeAttribute)) { ScriptLoader::kAllowLegacyTypeInTypeAttribute) !=
ScriptLoader::ScriptTypeAtPrepare::kInvalid) {
simulated_token = kValidScriptStart; simulated_token = kValidScriptStart;
} }
} else if (ThreadSafeMatch(tag_name, html_names::kLinkTag)) { } else if (ThreadSafeMatch(tag_name, html_names::kLinkTag)) {
......
...@@ -61,19 +61,17 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>, ...@@ -61,19 +61,17 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>,
kAllowLegacyTypeInTypeAttribute kAllowLegacyTypeInTypeAttribute
}; };
// |out_is_import_map| is set separately from |out_script_type| in order // Script type at the time of #prepare-a-script. Import maps are included here
// to avoid adding import maps as a mojom::blink::ScriptType enum, because // but not in `mojom::blink::ScriptType` because import maps are handled
// import maps are processed quite differently from classic/module scripts. // differently from ordinal scripts after PrepareScript().
// enum class ScriptTypeAtPrepare { kClassic, kModule, kImportMap, kInvalid };
// TODO(hiroshige, kouhei): Make the method signature simpler.
static bool IsValidScriptTypeAndLanguage( static ScriptTypeAtPrepare GetScriptTypeAtPrepare(
const String& type_attribute_value, const String& type_attribute_value,
const String& language_attribute_value, const String& language_attribute_value,
LegacyTypeSupport support_legacy_types, LegacyTypeSupport support_legacy_types);
mojom::blink::ScriptType* out_script_type = nullptr,
bool* out_is_import_map = nullptr);
static bool BlockForNoModule(mojom::blink::ScriptType, bool nomodule); static bool BlockForNoModule(ScriptTypeAtPrepare, bool nomodule);
static network::mojom::CredentialsMode ModuleScriptCredentialsMode( static network::mojom::CredentialsMode ModuleScriptCredentialsMode(
CrossOriginAttributeValue); CrossOriginAttributeValue);
...@@ -97,7 +95,7 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>, ...@@ -97,7 +95,7 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>,
bool IsParserInserted() const { return parser_inserted_; } bool IsParserInserted() const { return parser_inserted_; }
bool AlreadyStarted() const { return already_started_; } bool AlreadyStarted() const { return already_started_; }
bool IsNonBlocking() const { return non_blocking_; } 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. // Helper functions used by our parent classes.
void DidNotifySubtreeInsertionsToDocument(); void DidNotifySubtreeInsertionsToDocument();
...@@ -178,7 +176,7 @@ class CORE_EXPORT ScriptLoader final : public GarbageCollected<ScriptLoader>, ...@@ -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 // <spec href="https://html.spec.whatwg.org/C/#concept-script-type">... It is
// determined when the script is prepared, ...</spec> // 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"> // <spec href="https://html.spec.whatwg.org/C/#concept-script-external">
// ... It is determined when the script is prepared, ...</spec> // ... It is determined when the script is prepared, ...</spec>
......
...@@ -77,7 +77,8 @@ void XMLParserScriptRunner::ProcessScriptElement( ...@@ -77,7 +77,8 @@ void XMLParserScriptRunner::ProcessScriptElement(
bool success = script_loader->PrepareScript( bool success = script_loader->PrepareScript(
script_start_position, ScriptLoader::kAllowLegacyTypeInTypeAttribute); 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. // XMLDocumentParser does not support a module script, and thus ignores it.
success = false; success = false;
document.AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>( 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