LibWeb: Move CSS::Parser::Dimension class to a separate file

This commit is contained in:
Sam Atkins 2023-08-17 15:43:30 +01:00 committed by Andreas Kling
parent 199a931da1
commit f176e04323
Notes: sideshowbarker 2024-07-17 04:21:32 +09:00
3 changed files with 107 additions and 180 deletions

View file

@ -0,0 +1,105 @@
/*
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Variant.h>
#include <LibWeb/CSS/Angle.h>
#include <LibWeb/CSS/Frequency.h>
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/PercentageOr.h>
#include <LibWeb/CSS/Resolution.h>
#include <LibWeb/CSS/Time.h>
namespace Web::CSS::Parser {
class Dimension {
public:
Dimension(Angle&& value)
: m_value(move(value))
{
}
Dimension(Frequency&& value)
: m_value(move(value))
{
}
Dimension(Length&& value)
: m_value(move(value))
{
}
Dimension(Percentage&& value)
: m_value(move(value))
{
}
Dimension(Resolution&& value)
: m_value(move(value))
{
}
Dimension(Time&& value)
: m_value(move(value))
{
}
bool is_angle() const { return m_value.has<Angle>(); }
Angle angle() const { return m_value.get<Angle>(); }
bool is_angle_percentage() const { return is_angle() || is_percentage(); }
AnglePercentage angle_percentage() const
{
if (is_angle())
return angle();
return percentage();
}
bool is_frequency() const { return m_value.has<Frequency>(); }
Frequency frequency() const { return m_value.get<Frequency>(); }
bool is_frequency_percentage() const { return is_frequency() || is_percentage(); }
FrequencyPercentage frequency_percentage() const
{
if (is_frequency())
return frequency();
return percentage();
}
bool is_length() const { return m_value.has<Length>(); }
Length length() const { return m_value.get<Length>(); }
bool is_length_percentage() const { return is_length() || is_percentage(); }
LengthPercentage length_percentage() const
{
if (is_length())
return length();
return percentage();
}
bool is_percentage() const { return m_value.has<Percentage>(); }
Percentage percentage() const { return m_value.get<Percentage>(); }
bool is_resolution() const { return m_value.has<Resolution>(); }
Resolution resolution() const { return m_value.get<Resolution>(); }
bool is_time() const { return m_value.has<Time>(); }
Time time() const { return m_value.get<Time>(); }
bool is_time_percentage() const { return is_time() || is_percentage(); }
TimePercentage time_percentage() const
{
if (is_time())
return time();
return percentage();
}
private:
Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value;
};
}

View file

@ -1752,7 +1752,7 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
return nullptr;
}
Optional<Parser::Dimension> Parser::parse_dimension(ComponentValue const& component_value)
Optional<Dimension> Parser::parse_dimension(ComponentValue const& component_value)
{
if (component_value.is(Token::Type::Dimension)) {
float numeric_value = component_value.token().dimension_value();
@ -6525,119 +6525,4 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_css_value(Badge<StyleComputer>, Parsin
return result.release_value();
}
bool Parser::Dimension::is_angle() const
{
return m_value.has<Angle>();
}
Angle Parser::Dimension::angle() const
{
return m_value.get<Angle>();
}
bool Parser::Dimension::is_angle_percentage() const
{
return is_angle() || is_percentage();
}
AnglePercentage Parser::Dimension::angle_percentage() const
{
if (is_angle())
return angle();
if (is_percentage())
return percentage();
VERIFY_NOT_REACHED();
}
bool Parser::Dimension::is_frequency() const
{
return m_value.has<Frequency>();
}
Frequency Parser::Dimension::frequency() const
{
return m_value.get<Frequency>();
}
bool Parser::Dimension::is_frequency_percentage() const
{
return is_frequency() || is_percentage();
}
FrequencyPercentage Parser::Dimension::frequency_percentage() const
{
if (is_frequency())
return frequency();
if (is_percentage())
return percentage();
VERIFY_NOT_REACHED();
}
bool Parser::Dimension::is_length() const
{
return m_value.has<Length>();
}
Length Parser::Dimension::length() const
{
return m_value.get<Length>();
}
bool Parser::Dimension::is_length_percentage() const
{
return is_length() || is_percentage();
}
LengthPercentage Parser::Dimension::length_percentage() const
{
if (is_length())
return length();
if (is_percentage())
return percentage();
VERIFY_NOT_REACHED();
}
bool Parser::Dimension::is_percentage() const
{
return m_value.has<Percentage>();
}
Percentage Parser::Dimension::percentage() const
{
return m_value.get<Percentage>();
}
bool Parser::Dimension::is_resolution() const
{
return m_value.has<Resolution>();
}
Resolution Parser::Dimension::resolution() const
{
return m_value.get<Resolution>();
}
bool Parser::Dimension::is_time() const
{
return m_value.has<Time>();
}
Time Parser::Dimension::time() const
{
return m_value.get<Time>();
}
bool Parser::Dimension::is_time_percentage() const
{
return is_time() || is_percentage();
}
TimePercentage Parser::Dimension::time_percentage() const
{
if (is_time())
return time();
if (is_percentage())
return percentage();
VERIFY_NOT_REACHED();
}
}

View file

@ -18,6 +18,7 @@
#include <LibWeb/CSS/Parser/ComponentValue.h>
#include <LibWeb/CSS/Parser/Declaration.h>
#include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
#include <LibWeb/CSS/Parser/Dimension.h>
#include <LibWeb/CSS/Parser/Function.h>
#include <LibWeb/CSS/Parser/Rule.h>
#include <LibWeb/CSS/Parser/TokenStream.h>
@ -190,70 +191,6 @@ private:
PropertyOwningCSSStyleDeclaration* convert_to_style_declaration(Vector<DeclarationOrAtRule> const& declarations);
Optional<StyleProperty> convert_to_style_property(Declaration const&);
class Dimension {
public:
Dimension(Angle&& value)
: m_value(move(value))
{
}
Dimension(Frequency&& value)
: m_value(move(value))
{
}
Dimension(Length&& value)
: m_value(move(value))
{
}
Dimension(Percentage&& value)
: m_value(move(value))
{
}
Dimension(Resolution&& value)
: m_value(move(value))
{
}
Dimension(Time&& value)
: m_value(move(value))
{
}
bool is_angle() const;
Angle angle() const;
bool is_angle_percentage() const;
AnglePercentage angle_percentage() const;
bool is_frequency() const;
Frequency frequency() const;
bool is_frequency_percentage() const;
FrequencyPercentage frequency_percentage() const;
bool is_length() const;
Length length() const;
bool is_length_percentage() const;
LengthPercentage length_percentage() const;
bool is_percentage() const;
Percentage percentage() const;
bool is_resolution() const;
Resolution resolution() const;
bool is_time() const;
Time time() const;
bool is_time_percentage() const;
TimePercentage time_percentage() const;
private:
Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value;
};
Optional<Dimension> parse_dimension(ComponentValue const&);
Optional<Color> parse_rgb_or_hsl_color(StringView function_name, Vector<ComponentValue> const&);
Optional<Color> parse_color(ComponentValue const&);