Skip to content

Commit 44089ce

Browse files
shannonboothawesomekling
authored andcommitted
LibWeb: Reuse attribute lookups in HTMLScriptElement::prepare_script
Switch over from deprecated_attribute to attribute, and reuse the result of this lookup for both checking whether that attribute exists - and for retrieving the value of that attribute when we need it.
1 parent f9a7535 commit 44089ce

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void HTMLScriptElement::execute_script()
108108
document->set_current_script({}, nullptr);
109109

110110
if (m_from_an_external_file)
111-
dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running script {}", deprecated_attribute(HTML::AttributeNames::src));
111+
dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running script {}", attribute(HTML::AttributeNames::src).value_or(String {}));
112112
else
113113
dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running inline script");
114114

@@ -177,28 +177,28 @@ void HTMLScriptElement::prepare_script()
177177
// - el has a type attribute whose value is the empty string;
178178
// - el has no type attribute but it has a language attribute and that attribute's value is the empty string; or
179179
// - el has neither a type attribute nor a language attribute
180-
ByteString script_block_type;
181-
bool has_type_attribute = has_attribute(HTML::AttributeNames::type);
182-
bool has_language_attribute = has_attribute(HTML::AttributeNames::language);
183-
if ((has_type_attribute && deprecated_attribute(HTML::AttributeNames::type).is_empty())
184-
|| (!has_type_attribute && has_language_attribute && deprecated_attribute(HTML::AttributeNames::language).is_empty())
185-
|| (!has_type_attribute && !has_language_attribute)) {
180+
String script_block_type;
181+
auto maybe_type_attribute = attribute(HTML::AttributeNames::type);
182+
auto maybe_language_attribute = attribute(HTML::AttributeNames::language);
183+
if ((maybe_type_attribute.has_value() && maybe_type_attribute->is_empty())
184+
|| (!maybe_type_attribute.has_value() && maybe_language_attribute.has_value() && maybe_language_attribute->is_empty())
185+
|| (!maybe_type_attribute.has_value() && !maybe_language_attribute.has_value())) {
186186
// then let the script block's type string for this script element be "text/javascript".
187-
script_block_type = "text/javascript";
187+
script_block_type = "text/javascript"_string;
188188
}
189189
// Otherwise, if el has a type attribute,
190-
else if (has_type_attribute) {
190+
else if (maybe_type_attribute.has_value()) {
191191
// then let the script block's type string be the value of that attribute with leading and trailing ASCII whitespace stripped.
192-
script_block_type = deprecated_attribute(HTML::AttributeNames::type).trim(Infra::ASCII_WHITESPACE);
192+
script_block_type = MUST(maybe_type_attribute->trim(Infra::ASCII_WHITESPACE));
193193
}
194194
// Otherwise, el has a non-empty language attribute;
195-
else if (!deprecated_attribute(HTML::AttributeNames::language).is_empty()) {
195+
else if (maybe_language_attribute.has_value() && !maybe_language_attribute->is_empty()) {
196196
// let the script block's type string be the concatenation of "text/" and the value of el's language attribute.
197-
script_block_type = ByteString::formatted("text/{}", deprecated_attribute(HTML::AttributeNames::language));
197+
script_block_type = MUST(String::formatted("text/{}", maybe_language_attribute.value()));
198198
}
199199

200200
// 9. If the script block's type string is a JavaScript MIME type essence match,
201-
if (MimeSniff::is_javascript_mime_type_essence_match(script_block_type.trim(Infra::ASCII_WHITESPACE))) {
201+
if (MimeSniff::is_javascript_mime_type_essence_match(MUST(script_block_type.trim(Infra::ASCII_WHITESPACE)))) {
202202
// then set el's type to "classic".
203203
m_script_type = ScriptType::Classic;
204204
}

0 commit comments

Comments
 (0)