SVGAnimatedString.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/SVG/SVGAnimatedString.h>
  10. #include <LibWeb/SVG/SVGElement.h>
  11. namespace Web::SVG {
  12. JS_DEFINE_ALLOCATOR(SVGAnimatedString);
  13. JS::NonnullGCPtr<SVGAnimatedString> SVGAnimatedString::create(JS::Realm& realm, JS::NonnullGCPtr<SVGElement> element, FlyString reflected_attribute, Optional<FlyString> deprecated_reflected_attribute, Optional<FlyString> initial_value)
  14. {
  15. return realm.heap().allocate<SVGAnimatedString>(realm, realm, element, move(reflected_attribute), move(deprecated_reflected_attribute), move(initial_value));
  16. }
  17. SVGAnimatedString::SVGAnimatedString(JS::Realm& realm, JS::NonnullGCPtr<SVGElement> element, FlyString reflected_attribute, Optional<FlyString> deprecated_reflected_attribute, Optional<FlyString> initial_value)
  18. : Bindings::PlatformObject(realm)
  19. , m_element(element)
  20. , m_reflected_attribute(move(reflected_attribute))
  21. , m_deprecated_reflected_attribute(move(deprecated_reflected_attribute))
  22. , m_initial_value(move(initial_value))
  23. {
  24. }
  25. SVGAnimatedString::~SVGAnimatedString() = default;
  26. void SVGAnimatedString::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGAnimatedString);
  30. }
  31. void SVGAnimatedString::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_element);
  35. }
  36. // https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedString__baseVal
  37. String SVGAnimatedString::base_val() const
  38. {
  39. // On getting baseVal or animVal, the following steps are run:
  40. // 1. If the reflected attribute is not present, then:
  41. if (!m_element->has_attribute(m_reflected_attribute)) {
  42. // 1. If the SVGAnimatedString object is defined to additionally reflect a second, deprecated attribute,
  43. // and that attribute is present, then return its value.
  44. if (m_deprecated_reflected_attribute.has_value()) {
  45. if (auto attribute = m_element->get_attribute(m_deprecated_reflected_attribute.value()); attribute.has_value())
  46. return attribute.release_value();
  47. }
  48. // 2. Otherwise, if the reflected attribute has an initial value, then return it.
  49. if (m_initial_value.has_value())
  50. return m_initial_value.value().to_string();
  51. // 3. Otherwise, return the empty string.
  52. return {};
  53. }
  54. // 2. Otherwise, the reflected attribute is present. Return its value.
  55. return m_element->attribute(m_reflected_attribute).value();
  56. }
  57. // https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedString__baseVal
  58. void SVGAnimatedString::set_base_val(String const& base_val)
  59. {
  60. // 1. If the reflected attribute is not present, the SVGAnimatedString object is defined to additionally reflect
  61. // a second, deprecated attribute, and that deprecated attribute is present, then set that deprecated attribute
  62. // to the specified value.
  63. if (!m_element->has_attribute(m_reflected_attribute)
  64. && m_deprecated_reflected_attribute.has_value()
  65. && m_element->has_attribute(m_deprecated_reflected_attribute.value())) {
  66. MUST(m_element->set_attribute(m_deprecated_reflected_attribute.value(), base_val));
  67. return;
  68. }
  69. // 2. Otherwise, set the reflected attribute to the specified value.
  70. MUST(m_element->set_attribute(m_reflected_attribute, base_val));
  71. }
  72. }