From 3b7aa736e73a8889f007cc817af00fb84beff4a8 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 23 Jul 2025 10:53:11 +0100 Subject: [PATCH] LibWeb/CSS: Use ErrorReporter for media query parsing --- Libraries/LibWeb/CSS/Parser/MediaParsing.cpp | 30 +++++++++++--------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Libraries/LibWeb/CSS/Parser/MediaParsing.cpp b/Libraries/LibWeb/CSS/Parser/MediaParsing.cpp index 5fb8764f93d..1b8f275429a 100644 --- a/Libraries/LibWeb/CSS/Parser/MediaParsing.cpp +++ b/Libraries/LibWeb/CSS/Parser/MediaParsing.cpp @@ -8,7 +8,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include @@ -83,13 +82,14 @@ NonnullRefPtr Parser::parse_media_query(TokenStream& return {}; }; - auto invalid_media_query = [&]() { + auto invalid_media_query = [&](String&& description) { // "A media query that does not match the grammar in the previous section must be replaced by `not all` // during parsing." - https://www.w3.org/TR/mediaqueries-5/#error-handling - if constexpr (CSS_PARSER_DEBUG) { - dbgln("Invalid media query:"); - tokens.dump_all_tokens(); - } + ErrorReporter::the().report(InvalidQueryError { + .query_type = "@media"_fly_string, + .value_string = tokens.dump_string(), + .description = move(description), + }); return MediaQuery::create_not_all(); }; @@ -100,7 +100,7 @@ NonnullRefPtr Parser::parse_media_query(TokenStream& if (auto media_condition = parse_media_condition(tokens)) { tokens.discard_whitespace(); if (tokens.has_next_token()) - return invalid_media_query(); + return invalid_media_query("Trailing tokens after "_string); media_query->m_media_condition = media_condition.release_nonnull(); return media_query; } @@ -118,7 +118,7 @@ NonnullRefPtr Parser::parse_media_query(TokenStream& } else { // https://drafts.csswg.org/mediaqueries-4/#error-handling // A media query that does not match the grammar in the previous section must be replaced by not all during parsing. - return invalid_media_query(); + return invalid_media_query("Doesn't match ``"_string); } if (!tokens.has_next_token()) @@ -129,18 +129,18 @@ NonnullRefPtr Parser::parse_media_query(TokenStream& if (auto media_condition = parse_media_condition(tokens)) { // "or" is disallowed at the top level if (is(*media_condition)) - return invalid_media_query(); + return invalid_media_query("Contains top-level `or`"_string); tokens.discard_whitespace(); if (tokens.has_next_token()) - return invalid_media_query(); + return invalid_media_query("Trailing tokens after ``"_string); media_query->m_media_condition = move(media_condition); return media_query; } - return invalid_media_query(); + return invalid_media_query("Missing `` after `and`"_string); } - return invalid_media_query(); + return invalid_media_query("Trailing tokens after ``"_string); } // ``, https://www.w3.org/TR/mediaqueries-4/#typedef-media-condition @@ -553,7 +553,11 @@ Optional Parser::parse_media_feature_value(MediaFeatureID med if (!unknown_tokens.is_empty()) { transaction.commit(); - dbgln_if(CSS_PARSER_DEBUG, "Creating unknown media value: `{}`", String::join(""sv, unknown_tokens)); + ErrorReporter::the().report(InvalidValueError { + .value_type = ""_fly_string, + .value_string = MUST(String::join(""sv, unknown_tokens)), + .description = "Unrecognized type"_string, + }); return MediaFeatureValue(move(unknown_tokens)); }