1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:07:34 +00:00

LibWeb: Make parse_css_value_for_properties() return Optional

Signalling failure with a null `PropertyAndValue::style_value` is weird.
This commit is contained in:
Sam Atkins 2023-08-19 15:26:57 +01:00 committed by Andreas Kling
parent 2038cb3c81
commit e177cef8ff
2 changed files with 40 additions and 38 deletions

View file

@ -2804,12 +2804,12 @@ RefPtr<StyleValue> Parser::parse_background_value(Vector<ComponentValue> const&
} }
auto value_and_property = parse_css_value_for_properties(remaining_layer_properties, tokens); auto value_and_property = parse_css_value_for_properties(remaining_layer_properties, tokens);
if (!value_and_property.style_value) if (!value_and_property.has_value())
return nullptr; return nullptr;
auto& value = value_and_property.style_value; auto& value = value_and_property->style_value;
remove_property(remaining_layer_properties, value_and_property.property); remove_property(remaining_layer_properties, value_and_property->property);
switch (value_and_property.property) { switch (value_and_property->property) {
case PropertyID::BackgroundAttachment: case PropertyID::BackgroundAttachment:
VERIFY(!background_attachment); VERIFY(!background_attachment);
background_attachment = value.release_nonnull(); background_attachment = value.release_nonnull();
@ -3270,12 +3270,12 @@ RefPtr<StyleValue> Parser::parse_border_value(Vector<ComponentValue> const& comp
auto tokens = TokenStream { component_values }; auto tokens = TokenStream { component_values };
while (tokens.has_next_token()) { while (tokens.has_next_token()) {
auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens); auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value.property); remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::BorderWidth: { case PropertyID::BorderWidth: {
VERIFY(!border_width); VERIFY(!border_width);
border_width = value.release_nonnull(); border_width = value.release_nonnull();
@ -3939,11 +3939,11 @@ RefPtr<StyleValue> Parser::parse_flex_value(Vector<ComponentValue> const& compon
// One-value syntax: <flex-grow> | <flex-basis> | none // One-value syntax: <flex-grow> | <flex-basis> | none
auto properties = Array { PropertyID::FlexGrow, PropertyID::FlexBasis, PropertyID::Flex }; auto properties = Array { PropertyID::FlexGrow, PropertyID::FlexBasis, PropertyID::Flex };
auto property_and_value = parse_css_value_for_properties(properties, tokens); auto property_and_value = parse_css_value_for_properties(properties, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::FlexGrow: { case PropertyID::FlexGrow: {
// NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%. // NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
// https://github.com/w3c/csswg-drafts/issues/5742 // https://github.com/w3c/csswg-drafts/issues/5742
@ -3979,12 +3979,12 @@ RefPtr<StyleValue> Parser::parse_flex_value(Vector<ComponentValue> const& compon
while (tokens.has_next_token()) { while (tokens.has_next_token()) {
auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens); auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value.property); remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::FlexGrow: { case PropertyID::FlexGrow: {
VERIFY(!flex_grow); VERIFY(!flex_grow);
flex_grow = value.release_nonnull(); flex_grow = value.release_nonnull();
@ -4030,12 +4030,12 @@ RefPtr<StyleValue> Parser::parse_flex_flow_value(Vector<ComponentValue> const& c
auto tokens = TokenStream { component_values }; auto tokens = TokenStream { component_values };
while (tokens.has_next_token()) { while (tokens.has_next_token()) {
auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens); auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value.property); remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::FlexDirection: case PropertyID::FlexDirection:
VERIFY(!flex_direction); VERIFY(!flex_direction);
flex_direction = value.release_nonnull(); flex_direction = value.release_nonnull();
@ -4103,12 +4103,12 @@ RefPtr<StyleValue> Parser::parse_font_value(Vector<ComponentValue> const& compon
} }
auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens); auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value.property); remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::FontSize: { case PropertyID::FontSize: {
VERIFY(!font_size); VERIFY(!font_size);
font_size = value.release_nonnull(); font_size = value.release_nonnull();
@ -4481,12 +4481,12 @@ RefPtr<StyleValue> Parser::parse_list_style_value(Vector<ComponentValue> const&
} }
auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens); auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value.property); remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::ListStylePosition: { case PropertyID::ListStylePosition: {
VERIFY(!list_position); VERIFY(!list_position);
list_position = value.release_nonnull(); list_position = value.release_nonnull();
@ -4630,12 +4630,12 @@ RefPtr<StyleValue> Parser::parse_text_decoration_value(Vector<ComponentValue> co
auto tokens = TokenStream { component_values }; auto tokens = TokenStream { component_values };
while (tokens.has_next_token()) { while (tokens.has_next_token()) {
auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens); auto property_and_value = parse_css_value_for_properties(remaining_longhands, tokens);
if (!property_and_value.style_value) if (!property_and_value.has_value())
return nullptr; return nullptr;
auto& value = property_and_value.style_value; auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value.property); remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value.property) { switch (property_and_value->property) {
case PropertyID::TextDecorationColor: { case PropertyID::TextDecorationColor: {
VERIFY(!decoration_color); VERIFY(!decoration_color);
decoration_color = value.release_nonnull(); decoration_color = value.release_nonnull();
@ -5902,9 +5902,9 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
while (stream.has_next_token() && !unassigned_properties.is_empty()) { while (stream.has_next_token() && !unassigned_properties.is_empty()) {
auto property_and_value = parse_css_value_for_properties(unassigned_properties, stream); auto property_and_value = parse_css_value_for_properties(unassigned_properties, stream);
if (property_and_value.style_value) { if (property_and_value.has_value()) {
auto property = property_and_value.property; auto property = property_and_value->property;
auto value = property_and_value.style_value; auto value = property_and_value->style_value;
auto& values = assigned_values.ensure(to_underlying(property)); auto& values = assigned_values.ensure(to_underlying(property));
if (values.size() + 1 == property_maximum_value_count(property)) { if (values.size() + 1 == property_maximum_value_count(property)) {
// We're done with this property, move on to the next one. // We're done with this property, move on to the next one.
@ -5948,10 +5948,12 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
RefPtr<StyleValue> Parser::parse_css_value_for_property(PropertyID property_id, TokenStream<ComponentValue>& tokens) RefPtr<StyleValue> Parser::parse_css_value_for_property(PropertyID property_id, TokenStream<ComponentValue>& tokens)
{ {
return parse_css_value_for_properties({ &property_id, 1 }, tokens).style_value; return parse_css_value_for_properties({ &property_id, 1 }, tokens)
.map([](auto& it) { return it.style_value; })
.value_or(nullptr);
} }
Parser::PropertyAndValue Parser::parse_css_value_for_properties(ReadonlySpan<PropertyID> property_ids, TokenStream<ComponentValue>& tokens) Optional<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(ReadonlySpan<PropertyID> property_ids, TokenStream<ComponentValue>& tokens)
{ {
auto any_property_accepts_type = [](ReadonlySpan<PropertyID> property_ids, ValueType value_type) -> Optional<PropertyID> { auto any_property_accepts_type = [](ReadonlySpan<PropertyID> property_ids, ValueType value_type) -> Optional<PropertyID> {
for (auto const& property : property_ids) { for (auto const& property : property_ids) {
@ -6167,7 +6169,7 @@ Parser::PropertyAndValue Parser::parse_css_value_for_properties(ReadonlySpan<Pro
return PropertyAndValue { *property, value.release_nonnull() }; return PropertyAndValue { *property, value.release_nonnull() };
} }
return PropertyAndValue { property_ids.first(), nullptr }; return OptionalNone {};
} }
class UnparsedCalculationNode final : public CalculationNode { class UnparsedCalculationNode final : public CalculationNode {

View file

@ -198,7 +198,7 @@ private:
PropertyID property; PropertyID property;
RefPtr<StyleValue> style_value; RefPtr<StyleValue> style_value;
}; };
PropertyAndValue parse_css_value_for_properties(ReadonlySpan<PropertyID>, TokenStream<ComponentValue>&); Optional<PropertyAndValue> parse_css_value_for_properties(ReadonlySpan<PropertyID>, TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_builtin_value(ComponentValue const&); RefPtr<StyleValue> parse_builtin_value(ComponentValue const&);
RefPtr<CalculatedStyleValue> parse_calculated_value(ComponentValue const&); RefPtr<CalculatedStyleValue> parse_calculated_value(ComponentValue const&);
// NOTE: Implemented in generated code. (GenerateCSSMathFunctions.cpp) // NOTE: Implemented in generated code. (GenerateCSSMathFunctions.cpp)