mirror of
https://github.com/RGBCube/serenity
synced 2025-05-21 20:15:07 +00:00
LibWeb: Use new StyleValue parsing for flex
To make this work, we also add `none` as a valid identifier for `flex`. (This is correct, we just didn't need it before.)
This commit is contained in:
parent
021fd15434
commit
91c9d10a78
2 changed files with 38 additions and 39 deletions
|
@ -5370,34 +5370,37 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_filter_value_list_value(Vector<Compone
|
||||||
|
|
||||||
ErrorOr<RefPtr<StyleValue>> Parser::parse_flex_value(Vector<ComponentValue> const& component_values)
|
ErrorOr<RefPtr<StyleValue>> Parser::parse_flex_value(Vector<ComponentValue> const& component_values)
|
||||||
{
|
{
|
||||||
|
auto tokens = TokenStream { component_values };
|
||||||
|
|
||||||
if (component_values.size() == 1) {
|
if (component_values.size() == 1) {
|
||||||
// One-value syntax: <flex-grow> | <flex-basis> | none
|
// One-value syntax: <flex-grow> | <flex-basis> | none
|
||||||
auto value = TRY(parse_css_value(component_values[0]));
|
auto properties = Array { PropertyID::FlexGrow, PropertyID::FlexBasis, PropertyID::Flex };
|
||||||
if (!value)
|
auto property_and_value = TRY(parse_css_value_for_properties(properties, tokens));
|
||||||
|
if (!property_and_value.style_value)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
if (property_accepts_value(PropertyID::FlexGrow, *value)) {
|
auto& value = property_and_value.style_value;
|
||||||
|
switch (property_and_value.property) {
|
||||||
|
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
|
||||||
auto zero_percent = TRY(NumericStyleValue::create_integer(0));
|
auto zero_percent = TRY(NumericStyleValue::create_integer(0));
|
||||||
auto one = TRY(NumericStyleValue::create_integer(1));
|
auto one = TRY(NumericStyleValue::create_integer(1));
|
||||||
return FlexStyleValue::create(*value, one, zero_percent);
|
return FlexStyleValue::create(*value, one, zero_percent);
|
||||||
}
|
}
|
||||||
|
case PropertyID::FlexBasis: {
|
||||||
if (property_accepts_value(PropertyID::FlexBasis, *value)) {
|
|
||||||
auto one = TRY(NumericStyleValue::create_integer(1));
|
auto one = TRY(NumericStyleValue::create_integer(1));
|
||||||
return FlexStyleValue::create(one, one, *value);
|
return FlexStyleValue::create(one, one, *value);
|
||||||
}
|
}
|
||||||
|
case PropertyID::Flex: {
|
||||||
if (value->is_identifier() && property_accepts_value(PropertyID::Flex, *value)) {
|
if (value->is_identifier() && value->to_identifier() == ValueID::None) {
|
||||||
switch (value->to_identifier()) {
|
|
||||||
case ValueID::None: {
|
|
||||||
auto zero = TRY(NumericStyleValue::create_integer(0));
|
auto zero = TRY(NumericStyleValue::create_integer(0));
|
||||||
return FlexStyleValue::create(zero, zero, TRY(IdentifierStyleValue::create(ValueID::Auto)));
|
return FlexStyleValue::create(zero, zero, TRY(IdentifierStyleValue::create(ValueID::Auto)));
|
||||||
}
|
}
|
||||||
default:
|
break;
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -5407,43 +5410,36 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_flex_value(Vector<ComponentValue> cons
|
||||||
RefPtr<StyleValue> flex_shrink;
|
RefPtr<StyleValue> flex_shrink;
|
||||||
RefPtr<StyleValue> flex_basis;
|
RefPtr<StyleValue> flex_basis;
|
||||||
|
|
||||||
for (size_t i = 0; i < component_values.size(); ++i) {
|
// NOTE: FlexGrow has to be before FlexBasis. `0` is a valid FlexBasis, but only
|
||||||
auto value = TRY(parse_css_value(component_values[i]));
|
// if FlexGrow (along with optional FlexShrink) have already been specified.
|
||||||
if (!value)
|
auto remaining_longhands = Vector { PropertyID::FlexGrow, PropertyID::FlexBasis };
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
// Zero is a valid value for basis, but only if grow and shrink are already specified.
|
while (tokens.has_next_token()) {
|
||||||
if (value->has_number() && value->to_number() == 0) {
|
auto property_and_value = TRY(parse_css_value_for_properties(remaining_longhands, tokens));
|
||||||
if (flex_grow && flex_shrink && !flex_basis) {
|
if (!property_and_value.style_value)
|
||||||
flex_basis = TRY(LengthStyleValue::create(Length::make_px(0)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (property_accepts_value(PropertyID::FlexGrow, *value)) {
|
|
||||||
if (flex_grow)
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
auto& value = property_and_value.style_value;
|
||||||
|
remove_property(remaining_longhands, property_and_value.property);
|
||||||
|
|
||||||
|
switch (property_and_value.property) {
|
||||||
|
case PropertyID::FlexGrow: {
|
||||||
|
VERIFY(!flex_grow);
|
||||||
flex_grow = value.release_nonnull();
|
flex_grow = value.release_nonnull();
|
||||||
|
|
||||||
// Flex-shrink may optionally follow directly after.
|
// Flex-shrink may optionally follow directly after.
|
||||||
if (i + 1 < component_values.size()) {
|
auto maybe_flex_shrink = TRY(parse_css_value_for_property(PropertyID::FlexShrink, tokens));
|
||||||
auto second_value = TRY(parse_css_value(component_values[i + 1]));
|
if (maybe_flex_shrink)
|
||||||
if (second_value && property_accepts_value(PropertyID::FlexShrink, *second_value)) {
|
flex_shrink = maybe_flex_shrink.release_nonnull();
|
||||||
flex_shrink = second_value.release_nonnull();
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
case PropertyID::FlexBasis: {
|
||||||
if (property_accepts_value(PropertyID::FlexBasis, *value)) {
|
VERIFY(!flex_basis);
|
||||||
if (flex_basis)
|
|
||||||
return nullptr;
|
|
||||||
flex_basis = value.release_nonnull();
|
flex_basis = value.release_nonnull();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
return nullptr;
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!flex_grow)
|
if (!flex_grow)
|
||||||
|
|
|
@ -647,6 +647,9 @@
|
||||||
"flex": {
|
"flex": {
|
||||||
"inherited": false,
|
"inherited": false,
|
||||||
"initial": "0 1 auto",
|
"initial": "0 1 auto",
|
||||||
|
"valid-identifiers": [
|
||||||
|
"none"
|
||||||
|
],
|
||||||
"longhands": [
|
"longhands": [
|
||||||
"flex-grow",
|
"flex-grow",
|
||||||
"flex-shrink",
|
"flex-shrink",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue