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
Notes: sideshowbarker 2024-07-17 20:58:35 +09:00
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) 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
*/
@ -40,17 +40,8 @@ Length Length::make_px(CSSPixels value)
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
{
VERIFY(!is_calculated());
if (is_auto()) {
dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length.");
return *this;
@ -61,8 +52,6 @@ Length Length::percentage_of(Percentage const& percentage) 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())
return make_px(to_px(layout_node));
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
{
if (is_calculated())
return m_calculated_style->resolve_length(layout_node)->to_px(layout_node);
if (is_absolute())
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
{
if (is_calculated())
return m_calculated_style->to_string();
if (is_auto())
return "auto"_string;
return String::formatted("{}{}", m_value, unit_name());
@ -164,8 +148,6 @@ char const* Length::unit_name() const
return "lh";
case Type::Rlh:
return "rlh";
case Type::Calculated:
return "calculated";
}
VERIFY_NOT_REACHED();
}
@ -211,17 +193,4 @@ Optional<Length::Type> Length::unit_from_name(StringView name)
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
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <LibGfx/Forward.h>
#include <LibWeb/Forward.h>
@ -18,7 +17,6 @@ namespace Web::CSS {
class Length {
public:
enum class Type {
Calculated,
Auto,
Cm,
In,
@ -49,13 +47,11 @@ public:
static Length make_auto();
static Length make_px(CSSPixels value);
static Length make_calculated(NonnullRefPtr<CalculatedStyleValue>);
Length percentage_of(Percentage const&) const;
Length resolved(Layout::Node const& layout_node) const;
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_absolute() const
@ -84,7 +80,6 @@ public:
}
float raw_value() const { return m_value; }
NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
CSSPixels to_px(Layout::Node const&) const;
@ -94,8 +89,6 @@ public:
return 0;
if (is_relative())
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();
}
@ -125,9 +118,10 @@ public:
ErrorOr<String> to_string() const;
// We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
// this file already. To break the cyclic dependency, we must move all method definitions out.
bool operator==(Length const& other) const;
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;
@ -136,8 +130,6 @@ private:
Type m_type;
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: {
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 {
auto dimension_value = parse_dimension_value(value);
if (!dimension_value)
@ -5809,7 +5809,7 @@ RefPtr<StyleValue> Parser::parse_transform_value(Vector<ComponentValue> const& c
}
case TransformFunctionParameterType::LengthPercentage: {
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 {
auto dimension_value = parse_dimension_value(value);
if (!dimension_value)
@ -5824,7 +5824,7 @@ RefPtr<StyleValue> Parser::parse_transform_value(Vector<ComponentValue> const& c
}
case TransformFunctionParameterType::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 {
auto number = parse_numeric_value(value);
if (!number)

View file

@ -1212,17 +1212,14 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
maybe_length = font_size->to_length();
} 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(...)
// 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);
auto px = maybe_length.value().to_px(viewport_rect(), font_metrics, parent_font_size(), root_font_size, parent_line_height, root_line_height).value();
if (px != 0)
font_size_in_px = px;
}
}
if (maybe_length.has_value()) {
auto parent_line_height = parent_or_root_element_line_height(element, pseudo_element);
auto px = maybe_length.value().to_px(viewport_rect(), font_metrics, parent_font_size(), root_font_size, parent_line_height, root_line_height).value();
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())
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();
}

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)
{
if (size.is_length() && size.length().is_calculated()) {
if (size.length().calculated_style_value()->contains_percentage()) {
if (size.is_calculated()) {
if (size.calculated().contains_percentage()) {
if (!available_size.is_definite())
return 0;
auto& calc_value = *size.length().calculated_style_value();
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.calculated().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()) {
return size.length().to_px(box);

View file

@ -263,24 +263,22 @@ void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, Us
return false;
}
if (size.is_length() && size.length().is_calculated()) {
if (size.length().calculated_style_value()->contains_percentage()) {
if (size.is_calculated()) {
if (size.calculated().contains_percentage()) {
if (!containing_block_has_definite_size)
return false;
auto& calc_value = *size.length().calculated_style_value();
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_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;
}
resolved_definite_size = size.length().to_px(node);
resolved_definite_size = size.calculated().resolve_length(node)->to_px(node);
return true;
}
if (size.is_length()) {
VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
VERIFY(!size.length().is_calculated()); // Covered above.
VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
resolved_definite_size = size.length().to_px(node);
return true;
}

View file

@ -603,7 +603,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
auto resolve_border_width = [&]() {
auto value = computed_style.property(width_property);
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())
return value->to_length().to_px(*this).value();
if (value->is_identifier()) {