mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-30 14:20:21 +00:00
LibWeb/CSS: Use ErrorReporter for media query parsing
This commit is contained in:
parent
1a599ceb98
commit
3b7aa736e7
Notes:
github-actions[bot]
2025-08-04 09:52:11 +00:00
Author: https://github.com/AtkinsSJ
Commit: 3b7aa736e7
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5576
1 changed files with 17 additions and 13 deletions
|
@ -8,7 +8,6 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Debug.h>
|
|
||||||
#include <LibWeb/CSS/CSSMediaRule.h>
|
#include <LibWeb/CSS/CSSMediaRule.h>
|
||||||
#include <LibWeb/CSS/CSSNestedDeclarations.h>
|
#include <LibWeb/CSS/CSSNestedDeclarations.h>
|
||||||
#include <LibWeb/CSS/CalculatedOr.h>
|
#include <LibWeb/CSS/CalculatedOr.h>
|
||||||
|
@ -83,13 +82,14 @@ NonnullRefPtr<MediaQuery> Parser::parse_media_query(TokenStream<ComponentValue>&
|
||||||
return {};
|
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`
|
// "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
|
// during parsing." - https://www.w3.org/TR/mediaqueries-5/#error-handling
|
||||||
if constexpr (CSS_PARSER_DEBUG) {
|
ErrorReporter::the().report(InvalidQueryError {
|
||||||
dbgln("Invalid media query:");
|
.query_type = "@media"_fly_string,
|
||||||
tokens.dump_all_tokens();
|
.value_string = tokens.dump_string(),
|
||||||
}
|
.description = move(description),
|
||||||
|
});
|
||||||
return MediaQuery::create_not_all();
|
return MediaQuery::create_not_all();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ NonnullRefPtr<MediaQuery> Parser::parse_media_query(TokenStream<ComponentValue>&
|
||||||
if (auto media_condition = parse_media_condition(tokens)) {
|
if (auto media_condition = parse_media_condition(tokens)) {
|
||||||
tokens.discard_whitespace();
|
tokens.discard_whitespace();
|
||||||
if (tokens.has_next_token())
|
if (tokens.has_next_token())
|
||||||
return invalid_media_query();
|
return invalid_media_query("Trailing tokens after <media-condition>"_string);
|
||||||
media_query->m_media_condition = media_condition.release_nonnull();
|
media_query->m_media_condition = media_condition.release_nonnull();
|
||||||
return media_query;
|
return media_query;
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,7 @@ NonnullRefPtr<MediaQuery> Parser::parse_media_query(TokenStream<ComponentValue>&
|
||||||
} else {
|
} else {
|
||||||
// https://drafts.csswg.org/mediaqueries-4/#error-handling
|
// 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.
|
// 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 `<media-query>`"_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tokens.has_next_token())
|
if (!tokens.has_next_token())
|
||||||
|
@ -129,18 +129,18 @@ NonnullRefPtr<MediaQuery> Parser::parse_media_query(TokenStream<ComponentValue>&
|
||||||
if (auto media_condition = parse_media_condition(tokens)) {
|
if (auto media_condition = parse_media_condition(tokens)) {
|
||||||
// "or" is disallowed at the top level
|
// "or" is disallowed at the top level
|
||||||
if (is<BooleanOrExpression>(*media_condition))
|
if (is<BooleanOrExpression>(*media_condition))
|
||||||
return invalid_media_query();
|
return invalid_media_query("Contains top-level `or`"_string);
|
||||||
|
|
||||||
tokens.discard_whitespace();
|
tokens.discard_whitespace();
|
||||||
if (tokens.has_next_token())
|
if (tokens.has_next_token())
|
||||||
return invalid_media_query();
|
return invalid_media_query("Trailing tokens after `<media-condition-without-or>`"_string);
|
||||||
media_query->m_media_condition = move(media_condition);
|
media_query->m_media_condition = move(media_condition);
|
||||||
return media_query;
|
return media_query;
|
||||||
}
|
}
|
||||||
return invalid_media_query();
|
return invalid_media_query("Missing `<media-condition>` after `and`"_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
return invalid_media_query();
|
return invalid_media_query("Trailing tokens after `<media-query>`"_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
// `<media-condition>`, https://www.w3.org/TR/mediaqueries-4/#typedef-media-condition
|
// `<media-condition>`, https://www.w3.org/TR/mediaqueries-4/#typedef-media-condition
|
||||||
|
@ -553,7 +553,11 @@ Optional<MediaFeatureValue> Parser::parse_media_feature_value(MediaFeatureID med
|
||||||
|
|
||||||
if (!unknown_tokens.is_empty()) {
|
if (!unknown_tokens.is_empty()) {
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
dbgln_if(CSS_PARSER_DEBUG, "Creating unknown media value: `{}`", String::join(""sv, unknown_tokens));
|
ErrorReporter::the().report(InvalidValueError {
|
||||||
|
.value_type = "<mf-value>"_fly_string,
|
||||||
|
.value_string = MUST(String::join(""sv, unknown_tokens)),
|
||||||
|
.description = "Unrecognized type"_string,
|
||||||
|
});
|
||||||
return MediaFeatureValue(move(unknown_tokens));
|
return MediaFeatureValue(move(unknown_tokens));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue