2022-02-21 17:49:47 +00:00
|
|
|
/*
|
2025-05-16 20:02:16 +01:00
|
|
|
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
2022-02-21 17:49:47 +00:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-16 20:02:16 +01:00
|
|
|
#include <LibWeb/CSS/Frequency.h>
|
2023-03-30 15:54:38 +01:00
|
|
|
#include <LibWeb/CSS/Percentage.h>
|
2025-08-18 13:04:15 +01:00
|
|
|
#include <LibWeb/CSS/Serialize.h>
|
2024-12-11 15:05:56 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
2022-02-21 17:49:47 +00:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2025-09-02 13:48:49 +01:00
|
|
|
Frequency::Frequency(double value, FrequencyUnit unit)
|
|
|
|
|
: m_unit(unit)
|
2022-02-21 17:49:47 +00:00
|
|
|
, m_value(value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-27 21:10:21 +02:00
|
|
|
Frequency Frequency::make_hertz(double value)
|
2022-02-21 17:49:47 +00:00
|
|
|
{
|
2025-09-02 13:48:49 +01:00
|
|
|
return { value, FrequencyUnit::Hz };
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Frequency Frequency::percentage_of(Percentage const& percentage) const
|
|
|
|
|
{
|
2025-09-02 13:48:49 +01:00
|
|
|
return Frequency { percentage.as_fraction() * m_value, m_unit };
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 14:55:48 +00:00
|
|
|
void Frequency::serialize(StringBuilder& builder, SerializationMode serialization_mode) const
|
2022-02-21 17:49:47 +00:00
|
|
|
{
|
2025-08-18 13:04:15 +01: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 14:55:48 +00:00
|
|
|
return;
|
2025-08-18 13:04:15 +01:00
|
|
|
}
|
|
|
|
|
serialize_a_number(builder, raw_value());
|
|
|
|
|
builder.append(unit_name());
|
2026-01-08 14:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String Frequency::to_string(SerializationMode serialization_mode) const
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
serialize(builder, serialization_mode);
|
2025-08-18 13:04:15 +01:00
|
|
|
return builder.to_string_without_validation();
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-27 21:10:21 +02:00
|
|
|
double Frequency::to_hertz() const
|
2022-02-21 17:49:47 +00:00
|
|
|
{
|
2025-09-04 12:27:20 +01:00
|
|
|
return ratio_between_units(m_unit, FrequencyUnit::Hz) * m_value;
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|