2022-02-21 14:49:47 -03:00
|
|
|
/*
|
2025-05-16 16:02:16 -03:00
|
|
|
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
2022-02-21 14:49:47 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-16 16:02:16 -03:00
|
|
|
#include <LibWeb/CSS/Frequency.h>
|
2023-03-30 11:54:38 -03:00
|
|
|
#include <LibWeb/CSS/Percentage.h>
|
2025-08-18 09:04:15 -03:00
|
|
|
#include <LibWeb/CSS/Serialize.h>
|
2024-12-11 12:05:56 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
2022-02-21 14:49:47 -03:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2025-09-02 09:48:49 -03:00
|
|
|
Frequency::Frequency(double value, FrequencyUnit unit)
|
|
|
|
|
: m_unit(unit)
|
2022-02-21 14:49:47 -03:00
|
|
|
, m_value(value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-27 16:10:21 -03:00
|
|
|
Frequency Frequency::make_hertz(double value)
|
2022-02-21 14:49:47 -03:00
|
|
|
{
|
2025-09-02 09:48:49 -03:00
|
|
|
return { value, FrequencyUnit::Hz };
|
2022-02-21 14:49:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Frequency Frequency::percentage_of(Percentage const& percentage) const
|
|
|
|
|
{
|
2025-09-02 09:48:49 -03:00
|
|
|
return Frequency { percentage.as_fraction() * m_value, m_unit };
|
2022-02-21 14:49:47 -03:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 11:55:48 -03:00
|
|
|
void Frequency::serialize(StringBuilder& builder, SerializationMode serialization_mode) const
|
2022-02-21 14:49:47 -03:00
|
|
|
{
|
2025-08-18 09:04:15 -03:00
|
|
|
// https://drafts.csswg.org/cssom/#serialize-a-css-value
|
|
|
|
|
// -> <frequency>
|
|
|
|
|
// The <number> component serialized as per <number> followed by the unit in its canonical form as defined in its
|
|
|
|
|
// respective specification.
|
|
|
|
|
if (serialization_mode == SerializationMode::ResolvedValue) {
|
|
|
|
|
serialize_a_number(builder, to_hertz());
|
|
|
|
|
builder.append("hz"sv);
|
2026-01-08 11:55:48 -03:00
|
|
|
return;
|
2025-08-18 09:04:15 -03:00
|
|
|
}
|
|
|
|
|
serialize_a_number(builder, raw_value());
|
|
|
|
|
builder.append(unit_name());
|
2026-01-08 11:55:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String Frequency::to_string(SerializationMode serialization_mode) const
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
serialize(builder, serialization_mode);
|
2025-08-18 09:04:15 -03:00
|
|
|
return builder.to_string_without_validation();
|
2022-02-21 14:49:47 -03:00
|
|
|
}
|
|
|
|
|
|
2023-05-27 16:10:21 -03:00
|
|
|
double Frequency::to_hertz() const
|
2022-02-21 14:49:47 -03:00
|
|
|
{
|
2025-09-04 08:27:20 -03:00
|
|
|
return ratio_between_units(m_unit, FrequencyUnit::Hz) * m_value;
|
2022-02-21 14:49:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|