1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 03:07:39 +00:00

LibWeb: Remove CalculatedStyleValue from Length

This commit is contained in:
Sam Atkins 2023-03-30 15:46:05 +01:00 committed by Andreas Kling
parent 62a8cf2bb8
commit 53a4a31af2
8 changed files with 25 additions and 70 deletions

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -40,17 +40,8 @@ Length Length::make_px(CSSPixels value)
return Length(value.value(), Type::Px); return Length(value.value(), Type::Px);
} }
Length Length::make_calculated(NonnullRefPtr<CalculatedStyleValue> calculated_style_value)
{
Length length { 0, Type::Calculated };
length.m_calculated_style = move(calculated_style_value);
return length;
}
Length Length::percentage_of(Percentage const& percentage) const Length Length::percentage_of(Percentage const& percentage) const
{ {
VERIFY(!is_calculated());
if (is_auto()) { if (is_auto()) {
dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length."); dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length.");
return *this; return *this;
@ -61,8 +52,6 @@ Length Length::percentage_of(Percentage const& percentage) const
Length Length::resolved(Layout::Node const& layout_node) const Length Length::resolved(Layout::Node const& layout_node) const
{ {
if (is_calculated())
return m_calculated_style->resolve_length(layout_node).release_value();
if (is_relative()) if (is_relative())
return make_px(to_px(layout_node)); return make_px(to_px(layout_node));
if (!isfinite(m_value)) if (!isfinite(m_value))
@ -101,9 +90,6 @@ CSSPixels Length::relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::
CSSPixels Length::to_px(Layout::Node const& layout_node) const CSSPixels Length::to_px(Layout::Node const& layout_node) const
{ {
if (is_calculated())
return m_calculated_style->resolve_length(layout_node)->to_px(layout_node);
if (is_absolute()) if (is_absolute())
return absolute_length_to_px(); return absolute_length_to_px();
@ -118,8 +104,6 @@ CSSPixels Length::to_px(Layout::Node const& layout_node) const
ErrorOr<String> Length::to_string() const ErrorOr<String> Length::to_string() const
{ {
if (is_calculated())
return m_calculated_style->to_string();
if (is_auto()) if (is_auto())
return "auto"_string; return "auto"_string;
return String::formatted("{}{}", m_value, unit_name()); return String::formatted("{}{}", m_value, unit_name());
@ -164,8 +148,6 @@ char const* Length::unit_name() const
return "lh"; return "lh";
case Type::Rlh: case Type::Rlh:
return "rlh"; return "rlh";
case Type::Calculated:
return "calculated";
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
@ -211,17 +193,4 @@ Optional<Length::Type> Length::unit_from_name(StringView name)
return {}; return {};
} }
NonnullRefPtr<CalculatedStyleValue> Length::calculated_style_value() const
{
VERIFY(!m_calculated_style.is_null());
return *m_calculated_style;
}
bool Length::operator==(Length const& other) const
{
if (is_calculated())
return m_calculated_style == other.m_calculated_style;
return m_type == other.m_type && m_value == other.m_value;
}
} }

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include <AK/RefPtr.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibGfx/Forward.h> #include <LibGfx/Forward.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
@ -18,7 +17,6 @@ namespace Web::CSS {
class Length { class Length {
public: public:
enum class Type { enum class Type {
Calculated,
Auto, Auto,
Cm, Cm,
In, In,
@ -49,13 +47,11 @@ public:
static Length make_auto(); static Length make_auto();
static Length make_px(CSSPixels value); static Length make_px(CSSPixels value);
static Length make_calculated(NonnullRefPtr<CalculatedStyleValue>);
Length percentage_of(Percentage const&) const; Length percentage_of(Percentage const&) const;
Length resolved(Layout::Node const& layout_node) const; Length resolved(Layout::Node const& layout_node) const;
bool is_auto() const { return m_type == Type::Auto; } bool is_auto() const { return m_type == Type::Auto; }
bool is_calculated() const { return m_type == Type::Calculated; }
bool is_px() const { return m_type == Type::Px; } bool is_px() const { return m_type == Type::Px; }
bool is_absolute() const bool is_absolute() const
@ -84,7 +80,6 @@ public:
} }
float raw_value() const { return m_value; } float raw_value() const { return m_value; }
NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
CSSPixels to_px(Layout::Node const&) const; CSSPixels to_px(Layout::Node const&) const;
@ -94,8 +89,6 @@ public:
return 0; return 0;
if (is_relative()) if (is_relative())
return relative_length_to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); return relative_length_to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
if (is_calculated())
VERIFY_NOT_REACHED(); // We can't resolve a calculated length from here. :^(
return absolute_length_to_px(); return absolute_length_to_px();
} }
@ -125,9 +118,10 @@ public:
ErrorOr<String> to_string() const; ErrorOr<String> to_string() const;
// We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes bool operator==(Length const& other) const
// this file already. To break the cyclic dependency, we must move all method definitions out. {
bool operator==(Length const& other) const; return m_type == other.m_type && m_value == other.m_value;
}
CSSPixels relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const; CSSPixels relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const;
@ -136,8 +130,6 @@ private:
Type m_type; Type m_type;
float m_value { 0 }; float m_value { 0 };
RefPtr<CalculatedStyleValue> m_calculated_style;
}; };
} }

View file

@ -5794,7 +5794,7 @@ RefPtr<StyleValue> Parser::parse_transform_value(Vector<ComponentValue> const& c
} }
case TransformFunctionParameterType::Length: { case TransformFunctionParameterType::Length: {
if (maybe_calc_value && maybe_calc_value->resolves_to_length()) { if (maybe_calc_value && maybe_calc_value->resolves_to_length()) {
values.append(LengthStyleValue::create(Length::make_calculated(maybe_calc_value.release_nonnull()))); values.append(maybe_calc_value.release_nonnull());
} else { } else {
auto dimension_value = parse_dimension_value(value); auto dimension_value = parse_dimension_value(value);
if (!dimension_value) if (!dimension_value)
@ -5809,7 +5809,7 @@ RefPtr<StyleValue> Parser::parse_transform_value(Vector<ComponentValue> const& c
} }
case TransformFunctionParameterType::LengthPercentage: { case TransformFunctionParameterType::LengthPercentage: {
if (maybe_calc_value && maybe_calc_value->resolves_to_length()) { if (maybe_calc_value && maybe_calc_value->resolves_to_length()) {
values.append(LengthStyleValue::create(Length::make_calculated(maybe_calc_value.release_nonnull()))); values.append(maybe_calc_value.release_nonnull());
} else { } else {
auto dimension_value = parse_dimension_value(value); auto dimension_value = parse_dimension_value(value);
if (!dimension_value) if (!dimension_value)
@ -5824,7 +5824,7 @@ RefPtr<StyleValue> Parser::parse_transform_value(Vector<ComponentValue> const& c
} }
case TransformFunctionParameterType::Number: { case TransformFunctionParameterType::Number: {
if (maybe_calc_value && maybe_calc_value->resolves_to_number()) { if (maybe_calc_value && maybe_calc_value->resolves_to_number()) {
values.append(LengthStyleValue::create(Length::make_calculated(maybe_calc_value.release_nonnull()))); values.append(maybe_calc_value.release_nonnull());
} else { } else {
auto number = parse_numeric_value(value); auto number = parse_numeric_value(value);
if (!number) if (!number)

View file

@ -1212,17 +1212,14 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
maybe_length = font_size->to_length(); maybe_length = font_size->to_length();
} else if (font_size->is_calculated()) { } else if (font_size->is_calculated()) {
maybe_length = Length::make_calculated(const_cast<CalculatedStyleValue&>(font_size->as_calculated()));
}
if (maybe_length.has_value()) {
// FIXME: Support font-size: calc(...) // FIXME: Support font-size: calc(...)
// Theoretically we can do this now, but to resolve it we need a layout_node which we might not have. :^( // Theoretically we can do this now, but to resolve it we need a layout_node which we might not have. :^(
if (!maybe_length->is_calculated()) { }
auto parent_line_height = parent_or_root_element_line_height(element, pseudo_element); if (maybe_length.has_value()) {
auto px = maybe_length.value().to_px(viewport_rect(), font_metrics, parent_font_size(), root_font_size, parent_line_height, root_line_height).value(); auto parent_line_height = parent_or_root_element_line_height(element, pseudo_element);
if (px != 0) auto px = maybe_length.value().to_px(viewport_rect(), font_metrics, parent_font_size(), root_font_size, parent_line_height, root_line_height).value();
font_size_in_px = px; if (px != 0)
} font_size_in_px = px;
} }
} }

View file

@ -203,7 +203,7 @@ CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const
} }
if (line_height->is_calculated()) if (line_height->is_calculated())
return CSS::Length::make_calculated(const_cast<CalculatedStyleValue&>(line_height->as_calculated())).to_px(layout_node); return line_height->as_calculated().resolve_length(layout_node)->to_px(layout_node);
return layout_node.font().pixel_metrics().line_spacing(); return layout_node.font().pixel_metrics().line_spacing();
} }

View file

@ -57,14 +57,13 @@ size_t GridFormattingContext::count_of_gap_rows()
CSSPixels GridFormattingContext::resolve_size(CSS::Size const& size, AvailableSize const& available_size, Box const& box) CSSPixels GridFormattingContext::resolve_size(CSS::Size const& size, AvailableSize const& available_size, Box const& box)
{ {
if (size.is_length() && size.length().is_calculated()) { if (size.is_calculated()) {
if (size.length().calculated_style_value()->contains_percentage()) { if (size.calculated().contains_percentage()) {
if (!available_size.is_definite()) if (!available_size.is_definite())
return 0; return 0;
auto& calc_value = *size.length().calculated_style_value(); return size.calculated().resolve_length_percentage(box, CSS::Length::make_px(available_size.to_px())).value_or(CSS::Length::make_auto()).to_px(box);
return calc_value.resolve_length_percentage(box, CSS::Length::make_px(available_size.to_px())).value_or(CSS::Length::make_auto()).to_px(box);
} }
return size.length().to_px(box); return size.calculated().resolve_length(box)->to_px(box);
} }
if (size.is_length()) { if (size.is_length()) {
return size.length().to_px(box); return size.length().to_px(box);

View file

@ -263,24 +263,22 @@ void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, Us
return false; return false;
} }
if (size.is_length() && size.length().is_calculated()) { if (size.is_calculated()) {
if (size.length().calculated_style_value()->contains_percentage()) { if (size.calculated().contains_percentage()) {
if (!containing_block_has_definite_size) if (!containing_block_has_definite_size)
return false; return false;
auto& calc_value = *size.length().calculated_style_value();
auto containing_block_size_as_length = width auto containing_block_size_as_length = width
? CSS::Length::make_px(containing_block_used_values->content_width()) ? CSS::Length::make_px(containing_block_used_values->content_width())
: CSS::Length::make_px(containing_block_used_values->content_height()); : CSS::Length::make_px(containing_block_used_values->content_height());
resolved_definite_size = calc_value.resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node); resolved_definite_size = size.calculated().resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node);
return true; return true;
} }
resolved_definite_size = size.length().to_px(node); resolved_definite_size = size.calculated().resolve_length(node)->to_px(node);
return true; return true;
} }
if (size.is_length()) { if (size.is_length()) {
VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
VERIFY(!size.length().is_calculated()); // Covered above.
resolved_definite_size = size.length().to_px(node); resolved_definite_size = size.length().to_px(node);
return true; return true;
} }

View file

@ -603,7 +603,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
auto resolve_border_width = [&]() { auto resolve_border_width = [&]() {
auto value = computed_style.property(width_property); auto value = computed_style.property(width_property);
if (value->is_calculated()) if (value->is_calculated())
return CSS::Length::make_calculated(const_cast<CSS::CalculatedStyleValue&>(value->as_calculated())).to_px(*this).value(); return value->as_calculated().resolve_length(*this)->to_px(*this).value();
if (value->has_length()) if (value->has_length())
return value->to_length().to_px(*this).value(); return value->to_length().to_px(*this).value();
if (value->is_identifier()) { if (value->is_identifier()) {