Skip to content

Commit c27f99f

Browse files
AtkinsSJawesomekling
authored andcommitted
LibWeb: Implement and use FlexFlowStyleValue
1 parent ab57d7b commit c27f99f

File tree

4 files changed

+101
-63
lines changed

4 files changed

+101
-63
lines changed

Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,63 @@ RefPtr<StyleValue> Parser::parse_flex_value(ParsingContext const& context, Vecto
19811981
return FlexStyleValue::create(flex_grow.release_nonnull(), flex_shrink.release_nonnull(), flex_basis.release_nonnull());
19821982
}
19831983

1984+
RefPtr<StyleValue> Parser::parse_flex_flow_value(ParsingContext const& context, Vector<StyleComponentValueRule> const& component_values)
1985+
{
1986+
auto is_flex_direction = [](StyleValue const& value) -> bool {
1987+
switch (value.to_identifier()) {
1988+
case ValueID::Row:
1989+
case ValueID::RowReverse:
1990+
case ValueID::Column:
1991+
case ValueID::ColumnReverse:
1992+
return true;
1993+
default:
1994+
return false;
1995+
}
1996+
};
1997+
1998+
auto is_flex_wrap = [](StyleValue const& value) -> bool {
1999+
switch (value.to_identifier()) {
2000+
case ValueID::Wrap:
2001+
case ValueID::Nowrap:
2002+
case ValueID::WrapReverse:
2003+
return true;
2004+
default:
2005+
return false;
2006+
}
2007+
};
2008+
2009+
if (component_values.size() > 2)
2010+
return nullptr;
2011+
2012+
RefPtr<StyleValue> flex_direction;
2013+
RefPtr<StyleValue> flex_wrap;
2014+
2015+
for (auto& part : component_values) {
2016+
auto value = Parser::parse_css_value(context, PropertyID::FlexFlow, part);
2017+
if (!value)
2018+
return nullptr;
2019+
if (is_flex_direction(*value)) {
2020+
if (flex_direction)
2021+
return nullptr;
2022+
flex_direction = value.release_nonnull();
2023+
continue;
2024+
}
2025+
if (is_flex_wrap(*value)) {
2026+
if (flex_wrap)
2027+
return nullptr;
2028+
flex_wrap = value.release_nonnull();
2029+
continue;
2030+
}
2031+
}
2032+
2033+
if (!flex_direction)
2034+
flex_direction = IdentifierStyleValue::create(ValueID::Row);
2035+
if (!flex_wrap)
2036+
flex_wrap = IdentifierStyleValue::create(ValueID::Nowrap);
2037+
2038+
return FlexFlowStyleValue::create(flex_direction.release_nonnull(), flex_wrap.release_nonnull());
2039+
}
2040+
19842041
RefPtr<StyleValue> Parser::parse_font_value(ParsingContext const& context, Vector<StyleComponentValueRule> const& component_values)
19852042
{
19862043
auto is_font_size = [](StyleValue const& value) -> bool {
@@ -2381,6 +2438,10 @@ RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<S
23812438
if (auto parsed_value = parse_flex_value(m_context, component_values))
23822439
return parsed_value;
23832440
break;
2441+
case PropertyID::FlexFlow:
2442+
if (auto parsed_value = parse_flex_flow_value(m_context, component_values))
2443+
return parsed_value;
2444+
break;
23842445
case PropertyID::Font:
23852446
if (auto parsed_value = parse_font_value(m_context, component_values))
23862447
return parsed_value;

Userland/Libraries/LibWeb/CSS/Parser/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class Parser {
178178
static RefPtr<StyleValue> parse_background_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
179179
static RefPtr<StyleValue> parse_box_shadow_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
180180
static RefPtr<StyleValue> parse_flex_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
181+
static RefPtr<StyleValue> parse_flex_flow_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
181182
static RefPtr<StyleValue> parse_font_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
182183
static RefPtr<StyleValue> parse_list_style_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
183184
static RefPtr<StyleValue> parse_text_decoration_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);

Userland/Libraries/LibWeb/CSS/StyleResolver.cpp

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -219,37 +219,6 @@ static inline bool is_color(StyleValue const& value)
219219
return false;
220220
}
221221

222-
static inline bool is_flex_direction(StyleValue const& value)
223-
{
224-
if (value.is_builtin_or_dynamic())
225-
return true;
226-
227-
switch (value.to_identifier()) {
228-
case ValueID::Row:
229-
case ValueID::RowReverse:
230-
case ValueID::Column:
231-
case ValueID::ColumnReverse:
232-
return true;
233-
default:
234-
return false;
235-
}
236-
}
237-
238-
static inline bool is_flex_wrap(StyleValue const& value)
239-
{
240-
if (value.is_builtin_or_dynamic())
241-
return true;
242-
243-
switch (value.to_identifier()) {
244-
case ValueID::Wrap:
245-
case ValueID::Nowrap:
246-
case ValueID::WrapReverse:
247-
return true;
248-
default:
249-
return false;
250-
}
251-
}
252-
253222
static inline bool is_font_family(StyleValue const& value)
254223
{
255224
if (value.is_builtin_or_dynamic())
@@ -793,40 +762,17 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
793762
}
794763

795764
if (property_id == CSS::PropertyID::FlexFlow) {
796-
if (value.is_component_value_list()) {
797-
auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
798-
if (parts.is_empty() || parts.size() > 2)
799-
return;
800-
801-
RefPtr<StyleValue> flex_direction_value;
802-
RefPtr<StyleValue> flex_wrap_value;
803-
804-
for (auto& part : parts) {
805-
auto value = Parser::parse_css_value(context, property_id, part);
806-
if (!value)
807-
return;
808-
if (is_flex_direction(*value)) {
809-
if (flex_direction_value)
810-
return;
811-
flex_direction_value = move(value);
812-
continue;
813-
}
814-
if (is_flex_wrap(*value)) {
815-
if (flex_wrap_value)
816-
return;
817-
flex_wrap_value = move(value);
818-
continue;
819-
}
820-
}
821-
822-
if (flex_direction_value)
823-
style.set_property(CSS::PropertyID::FlexDirection, *flex_direction_value);
824-
if (flex_wrap_value)
825-
style.set_property(CSS::PropertyID::FlexWrap, *flex_wrap_value);
826-
765+
if (value.is_flex_flow()) {
766+
auto& flex_flow = static_cast<FlexFlowStyleValue const&>(value);
767+
style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
768+
style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
769+
return;
770+
}
771+
if (value.is_builtin()) {
772+
style.set_property(CSS::PropertyID::FlexDirection, value);
773+
style.set_property(CSS::PropertyID::FlexWrap, value);
827774
return;
828775
}
829-
830776
return;
831777
}
832778

Userland/Libraries/LibWeb/CSS/StyleValue.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class StyleValue : public RefCounted<StyleValue> {
232232
Background,
233233
BoxShadow,
234234
Flex,
235+
FlexFlow,
235236
Font,
236237
ListStyle,
237238
TextDecoration,
@@ -254,6 +255,7 @@ class StyleValue : public RefCounted<StyleValue> {
254255
bool is_background() const { return type() == Type::Background; }
255256
bool is_box_shadow() const { return type() == Type::BoxShadow; }
256257
bool is_flex() const { return type() == Type::Flex; }
258+
bool is_flex_flow() const { return type() == Type::FlexFlow; }
257259
bool is_font() const { return type() == Type::Font; }
258260
bool is_list_style() const { return type() == Type::ListStyle; }
259261
bool is_text_decoration() const { return type() == Type::TextDecoration; }
@@ -716,6 +718,34 @@ class FlexStyleValue final : public StyleValue {
716718
NonnullRefPtr<StyleValue> m_basis;
717719
};
718720

721+
class FlexFlowStyleValue final : public StyleValue {
722+
public:
723+
static NonnullRefPtr<FlexFlowStyleValue> create(NonnullRefPtr<StyleValue> flex_direction, NonnullRefPtr<StyleValue> flex_wrap)
724+
{
725+
return adopt_ref(*new FlexFlowStyleValue(flex_direction, flex_wrap));
726+
}
727+
virtual ~FlexFlowStyleValue() override { }
728+
729+
NonnullRefPtr<StyleValue> flex_direction() const { return m_flex_direction; }
730+
NonnullRefPtr<StyleValue> flex_wrap() const { return m_flex_wrap; }
731+
732+
virtual String to_string() const override
733+
{
734+
return String::formatted("FlexFlow flex_direction: {}, flex_wrap: {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
735+
}
736+
737+
private:
738+
FlexFlowStyleValue(NonnullRefPtr<StyleValue> flex_direction, NonnullRefPtr<StyleValue> flex_wrap)
739+
: StyleValue(Type::FlexFlow)
740+
, m_flex_direction(flex_direction)
741+
, m_flex_wrap(flex_wrap)
742+
{
743+
}
744+
745+
NonnullRefPtr<StyleValue> m_flex_direction;
746+
NonnullRefPtr<StyleValue> m_flex_wrap;
747+
};
748+
719749
class FontStyleValue final : public StyleValue {
720750
public:
721751
static NonnullRefPtr<FontStyleValue> create(NonnullRefPtr<StyleValue> font_style, NonnullRefPtr<StyleValue> font_weight, NonnullRefPtr<StyleValue> font_size, NonnullRefPtr<StyleValue> line_height, NonnullRefPtrVector<StyleValue>&& font_families) { return adopt_ref(*new FontStyleValue(font_style, font_weight, font_size, line_height, move(font_families))); }

0 commit comments

Comments
 (0)