SVGAnimatedString.cpp 3.5 KB

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