Skip to content

Commit

Permalink
LibWeb: Make javascript mime matching spec compliant
Browse files Browse the repository at this point in the history
When mime essence matching, the spec only asks for a string comparison
ignoring ascii case. The whitespace trimming and parsing of the mime
produces unexpected and wrong results.
Fixes tests on WPT html/semantics/scripting-1/the-script-element :^)
  • Loading branch information
OHermesJunior authored and tcl3 committed Nov 1, 2024
1 parent 437879f commit cdd78be
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void HTMLScriptElement::prepare_script()
}

// 9. If the script block's type string is a JavaScript MIME type essence match,
if (MimeSniff::is_javascript_mime_type_essence_match(MUST(script_block_type.trim(Infra::ASCII_WHITESPACE)))) {
if (MimeSniff::is_javascript_mime_type_essence_match(script_block_type)) {
// then set el's type to "classic".
m_script_type = ScriptType::Classic;
}
Expand Down
45 changes: 6 additions & 39 deletions Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace Web::MimeSniff {
bool is_javascript_mime_type_essence_match(StringView string)
{
// A string is a JavaScript MIME type essence match if it is an ASCII case-insensitive match for one of the JavaScript MIME type essence strings.
// NOTE: The mime type parser automatically lowercases the essence.
auto type = MimeType::parse(string);
if (!type.has_value())
return false;
return type->is_javascript();
for (auto const& javascript_essence : s_javascript_mime_type_essence_strings) {
if (string.equals_ignoring_ascii_case(javascript_essence))
return true;
}
return false;
}

static bool contains_only_http_quoted_string_token_code_points(StringView string)
Expand Down Expand Up @@ -330,40 +330,7 @@ bool MimeType::is_scriptable() const
// https://mimesniff.spec.whatwg.org/#javascript-mime-type
bool MimeType::is_javascript() const
{
// A JavaScript MIME type is any MIME type whose essence is one of the following:
// - application/ecmascript
// - application/javascript
// - application/x-ecmascript
// - application/x-javascript
// - text/ecmascript
// - text/javascript
// - text/javascript1.0
// - text/javascript1.1
// - text/javascript1.2
// - text/javascript1.3
// - text/javascript1.4
// - text/javascript1.5
// - text/jscript
// - text/livescript
// - text/x-ecmascript
// - text/x-javascript
return essence().is_one_of(
"application/ecmascript"sv,
"application/javascript"sv,
"application/x-ecmascript"sv,
"application/x-javascript"sv,
"text/ecmascript"sv,
"text/javascript"sv,
"text/javascript1.0"sv,
"text/javascript1.1"sv,
"text/javascript1.2"sv,
"text/javascript1.3"sv,
"text/javascript1.4"sv,
"text/javascript1.5"sv,
"text/jscript"sv,
"text/livescript"sv,
"text/x-ecmascript"sv,
"text/x-javascript"sv);
return s_javascript_mime_type_essence_strings.contains_slow(essence());
}

// https://mimesniff.spec.whatwg.org/#json-mime-type
Expand Down
21 changes: 21 additions & 0 deletions Userland/Libraries/LibWeb/MimeSniff/MimeType.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ namespace Web::MimeSniff {

bool is_javascript_mime_type_essence_match(StringView);

// https://mimesniff.spec.whatwg.org/#javascript-mime-type
// A JavaScript MIME type is any MIME type whose essence is one of the following:
static constexpr Array s_javascript_mime_type_essence_strings = {
"application/ecmascript"sv,
"application/javascript"sv,
"application/x-ecmascript"sv,
"application/x-javascript"sv,
"text/ecmascript"sv,
"text/javascript"sv,
"text/javascript1.0"sv,
"text/javascript1.1"sv,
"text/javascript1.2"sv,
"text/javascript1.3"sv,
"text/javascript1.4"sv,
"text/javascript1.5"sv,
"text/jscript"sv,
"text/livescript"sv,
"text/x-ecmascript"sv,
"text/x-javascript"sv
};

// https://mimesniff.spec.whatwg.org/#mime-type
class MimeType {
public:
Expand Down

0 comments on commit cdd78be

Please sign in to comment.