Truly independent web browser
/*
* Copyright (c) 2023, MacDue <[email protected]>
* Copyright (c) 2025, Jelle Raaijmakers <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGC/Heap.h>
#include <LibWeb/SVG/AttributeParsing.h>
#include <LibWeb/SVG/SVGAnimatedNumber.h>
namespace Web::SVG {
GC_DEFINE_ALLOCATOR(SVGAnimatedNumber);
GC::Ref<SVGAnimatedNumber> SVGAnimatedNumber::create(
GC::Ref<SVGElement> element,
DOM::QualifiedName reflected_attribute,
float initial_value,
SupportsSecondValue supports_second_value,
ValueRepresented value_represented,
AcceptsPercentage accepts_percentage)
{
return GC::Heap::the().allocate<SVGAnimatedNumber>(element, move(reflected_attribute), initial_value,
supports_second_value, value_represented, accepts_percentage);
}
SVGAnimatedNumber::SVGAnimatedNumber(
GC::Ref<SVGElement> element,
DOM::QualifiedName reflected_attribute,
float initial_value,
SupportsSecondValue supports_second_value,
ValueRepresented value_represented,
AcceptsPercentage accepts_percentage)
: m_element(element)
, m_reflected_attribute(move(reflected_attribute))
, m_initial_value(initial_value)
, m_supports_second_value(supports_second_value)
, m_value_represented(value_represented)
, m_accepts_percentage(accepts_percentage)
{
}
SVGAnimatedNumber::~SVGAnimatedNumber() = default;
// https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__baseVal
float SVGAnimatedNumber::base_val() const
{
// On getting baseVal or animVal, the following steps are run:
return get_base_or_anim_value();
}
// https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__baseVal
void SVGAnimatedNumber::set_base_val(float new_value)
{
// 1. Let value be the value being assigned to baseVal.
auto value = new_value;
// 2. Let new be a list of numbers.
Vector<float, 2> new_;
// 3. If the reflected attribute is defined to take an number followed by an optional second number, then:
if (m_supports_second_value == SupportsSecondValue::Yes) {
// 1. Let current be the value of the reflected attribute (using the attribute's initial value if it is not
// present or invalid).
auto current = parse_number_optional_number(m_element->get_attribute_value(m_reflected_attribute.local_name(), m_reflected_attribute.namespace_()))
.value_or({ m_initial_value, {} });
// 2. Let first be the first number in current.
auto first = current.first;
// 3. Let second be the second number in current if it has been explicitly specified, and if not, the implicit
// value as described in the definition of the attribute.
// NB: All known usages of <number-optional-number> specify that a missing second number defaults to the value
// of the first number.
auto second = current.second.value_or(first);
// 4. If this SVGAnimatedNumber object reflects the first number, then set first to value. Otherwise, set second
// to value.
if (m_value_represented == ValueRepresented::First)
first = value;
else
second = value;
// 5. Append first to new.
new_.unchecked_append(first);
// 6. Append second to new.
new_.unchecked_append(second);
}
// 4. Otherwise, the reflected attribute is defined to take a single number value. Append value to new.
else {
new_.unchecked_append(value);
}
// 5. Set the content attribute to a string consisting of each number in new serialized to an implementation
// specific string that, if parsed as an <number> using CSS syntax, would return the value closest to the number
// (given the implementation's supported Precisionreal number precision), joined and separated by a single U+0020
// SPACE character.
auto new_attribute_value = Utf16String::join(' ', new_);
m_element->set_attribute_value(m_reflected_attribute.local_name(), new_attribute_value, m_reflected_attribute.prefix(), m_reflected_attribute.namespace_());
}
// https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__animVal
float SVGAnimatedNumber::anim_val() const
{
// On getting baseVal or animVal, the following steps are run:
return get_base_or_anim_value();
}
float SVGAnimatedNumber::parse_value_or_initial(Utf16View number_value) const
{
if (m_accepts_percentage == AcceptsPercentage::Yes) {
if (auto value = parse_number_percentage(number_value); value.has_value())
return value->value();
return m_initial_value;
}
return parse_number(number_value).value_or(m_initial_value);
}
// https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedNumber__baseVal
float SVGAnimatedNumber::get_base_or_anim_value() const
{
// 1. Let value be the value of the reflected attribute (using the attribute's initial value if it is not present or
// invalid).
auto value = m_element->get_attribute_value(m_reflected_attribute.local_name(), m_reflected_attribute.namespace_());
// 2. If the reflected attribute is defined to take an number followed by an optional second number, then:
if (m_supports_second_value == SupportsSecondValue::Yes) {
// NB: A value that does not parse as a whole is invalid, so both numbers take the initial value.
auto values = parse_number_optional_number(value);
if (!values.has_value())
return m_initial_value;
// 1. If this SVGAnimatedNumber object reflects the first number, then return the first value in value.
if (m_value_represented == ValueRepresented::First)
return values->first;
// 2. Otherwise, this SVGAnimatedNumber object reflects the second number. Return the second value in value if
// it has been explicitly specified, and if not, return the implicit value as described in the definition of
// the attribute.
// NB: All known usages of <number-optional-number> specify that a missing second number defaults to the value
// of the first number.
VERIFY(m_value_represented == ValueRepresented::Second);
return values->second.value_or(values->first);
}
// 3. Otherwise, the reflected attribute is defined to take a single number value. Return value.
return parse_value_or_initial(value);
}
void SVGAnimatedNumber::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_element);
}
}