Skip to content

Commit cebdcd9

Browse files
committed
LibWeb/CSS: Use ErrorReporter for value-parsing errors
Also remove some redundant reporting for `<urange>` parsing errors.
1 parent 3b7aa73 commit cebdcd9

File tree

5 files changed

+239
-61
lines changed

5 files changed

+239
-61
lines changed

Libraries/LibWeb/CSS/Parser/Parser.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <LibWeb/CSS/CSSStyleSheet.h>
2121
#include <LibWeb/CSS/FontFace.h>
2222
#include <LibWeb/CSS/MediaList.h>
23+
#include <LibWeb/CSS/Parser/ErrorReporter.h>
2324
#include <LibWeb/CSS/Parser/Parser.h>
2425
#include <LibWeb/CSS/PropertyName.h>
2526
#include <LibWeb/CSS/Sizing.h>
@@ -1750,7 +1751,11 @@ LengthOrCalculated Parser::parse_as_sizes_attribute(DOM::Element const& element,
17501751
remove_all_consecutive_whitespace_tokens_from_the_end_of(unparsed_size);
17511752
if (unparsed_size.is_empty()) {
17521753
log_parse_error();
1753-
dbgln_if(CSS_PARSER_DEBUG, "-> Failed in step 3.1; all whitespace");
1754+
ErrorReporter::the().report(InvalidValueError {
1755+
.value_type = "sizes attribute"_fly_string,
1756+
.value_string = m_token_stream.dump_string(),
1757+
.description = "Failed in step 3.1; all whitespace"_string,
1758+
});
17541759
continue;
17551760
}
17561761

@@ -1764,7 +1769,11 @@ LengthOrCalculated Parser::parse_as_sizes_attribute(DOM::Element const& element,
17641769
unparsed_size.take_last();
17651770
} else {
17661771
log_parse_error();
1767-
dbgln_if(CSS_PARSER_DEBUG, "-> Failed in step 3.2; couldn't parse {} as a <source-size-value>", unparsed_size.last().to_debug_string());
1772+
ErrorReporter::the().report(InvalidValueError {
1773+
.value_type = "sizes attribute"_fly_string,
1774+
.value_string = m_token_stream.dump_string(),
1775+
.description = "Failed in step 3.2; couldn't parse {} as a <source-size-value>"_string,
1776+
});
17681777
continue;
17691778
}
17701779

@@ -1789,7 +1798,11 @@ LengthOrCalculated Parser::parse_as_sizes_attribute(DOM::Element const& element,
17891798
// 1. If this was not the last item in unparsed sizes list, that is a parse error.
17901799
if (i != unparsed_sizes_list.size() - 1) {
17911800
log_parse_error();
1792-
dbgln_if(CSS_PARSER_DEBUG, "-> Failed in step 3.4.1; is unparsed size #{}, count {}", i, unparsed_sizes_list.size());
1801+
ErrorReporter::the().report(InvalidValueError {
1802+
.value_type = "sizes attribute"_fly_string,
1803+
.value_string = m_token_stream.dump_string(),
1804+
.description = MUST(String::formatted("Failed in step 3.4.1; is unparsed size #{}, count {}", i, unparsed_sizes_list.size())),
1805+
});
17931806
}
17941807

17951808
// 2. If size is not auto, then return size. Otherwise, continue.

Libraries/LibWeb/CSS/Parser/PropertyParsing.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <AK/QuickSort.h>
1818
#include <LibWeb/CSS/CSSStyleValue.h>
1919
#include <LibWeb/CSS/CharacterTypes.h>
20+
#include <LibWeb/CSS/Parser/ErrorReporter.h>
2021
#include <LibWeb/CSS/Parser/Parser.h>
2122
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
2223
#include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
@@ -2412,7 +2413,11 @@ RefPtr<CSSStyleValue const> Parser::parse_display_value(TokenStream<ComponentVal
24122413
}
24132414

24142415
// Not a display value, abort.
2415-
dbgln_if(CSS_PARSER_DEBUG, "Unrecognized display value: `{}`", tokens.next_token().to_string());
2416+
ErrorReporter::the().report(InvalidValueError {
2417+
.value_type = "<display>"_fly_string,
2418+
.value_string = tokens.next_token().to_string(),
2419+
.description = "Unrecognized value"_string,
2420+
});
24162421
return {};
24172422
}
24182423

@@ -3924,12 +3929,20 @@ RefPtr<CSSStyleValue const> Parser::parse_transform_value(TokenStream<ComponentV
39243929
auto arguments = parse_a_comma_separated_list_of_component_values(function_tokens);
39253930

39263931
if (arguments.size() > function_metadata.parameters.size()) {
3927-
dbgln_if(CSS_PARSER_DEBUG, "Too many arguments to {}. max: {}", part.function().name, function_metadata.parameters.size());
3932+
ErrorReporter::the().report(InvalidValueError {
3933+
.value_type = "<transform-function>"_fly_string,
3934+
.value_string = part.function().original_source_text(),
3935+
.description = MUST(String::formatted("Too many arguments to {}. max: {}", part.function().name, function_metadata.parameters.size())),
3936+
});
39283937
return nullptr;
39293938
}
39303939

39313940
if (arguments.size() < function_metadata.parameters.size() && function_metadata.parameters[arguments.size()].required) {
3932-
dbgln_if(CSS_PARSER_DEBUG, "Required parameter at position {} is missing", arguments.size());
3941+
ErrorReporter::the().report(InvalidValueError {
3942+
.value_type = "<transform-function>"_fly_string,
3943+
.value_string = part.function().original_source_text(),
3944+
.description = MUST(String::formatted("Required parameter at position {} is missing", arguments.size())),
3945+
});
39333946
return nullptr;
39343947
}
39353948

Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <AK/GenericShorthands.h>
88
#include <LibWeb/CSS/Parser/ArbitrarySubstitutionFunctions.h>
9+
#include <LibWeb/CSS/Parser/ErrorReporter.h>
910
#include <LibWeb/CSS/Parser/Parser.h>
1011
#include <LibWeb/CSS/Parser/Syntax.h>
1112
#include <LibWeb/CSS/Parser/SyntaxParsing.h>
@@ -251,7 +252,11 @@ RefPtr<CSSStyleValue const> Parser::parse_according_to_syntax_node(TokenStream<C
251252
return nullptr;
252253
}
253254

254-
dbgln_if(CSS_PARSER_DEBUG, "Couldn't parse `<{}>` because we don't know what it is.", type_name);
255+
ErrorReporter::the().report(InvalidValueError {
256+
.value_type = MUST(String::formatted("<{}>", type_name)),
257+
.value_string = tokens.dump_string(),
258+
.description = "Unknown type in <syntax>."_string,
259+
});
255260
return nullptr;
256261
}
257262
case SyntaxNode::NodeType::Multiplier: {

0 commit comments

Comments
 (0)