mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:58:11 +00:00
LibWeb: Implement Flex and FlexStyleValue types
This commit is contained in:
parent
f1d7ea67c0
commit
b0317bb3a1
15 changed files with 258 additions and 6 deletions
59
Userland/Libraries/LibWeb/CSS/Flex.cpp
Normal file
59
Userland/Libraries/LibWeb/CSS/Flex.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Flex.h"
|
||||
#include <LibWeb/CSS/Percentage.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
Flex::Flex(double value, Type type)
|
||||
: m_type(type)
|
||||
, m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
Flex Flex::make_fr(double value)
|
||||
{
|
||||
return { value, Type::Fr };
|
||||
}
|
||||
|
||||
Flex Flex::percentage_of(Percentage const& percentage) const
|
||||
{
|
||||
return Flex { percentage.as_fraction() * m_value, m_type };
|
||||
}
|
||||
|
||||
String Flex::to_string() const
|
||||
{
|
||||
return MUST(String::formatted("{}fr", to_fr()));
|
||||
}
|
||||
|
||||
double Flex::to_fr() const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::Fr:
|
||||
return m_value;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
StringView Flex::unit_name() const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::Fr:
|
||||
return "fr"sv;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
Optional<Flex::Type> Flex::unit_from_name(StringView name)
|
||||
{
|
||||
if (name.equals_ignoring_ascii_case("fr"sv))
|
||||
return Type::Fr;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue