SVGURIReference.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/SVG/AttributeNames.h>
  8. #include <LibWeb/SVG/SVGAnimatedString.h>
  9. namespace Web::SVG {
  10. enum class SupportsXLinkHref {
  11. No,
  12. Yes,
  13. };
  14. // https://svgwg.org/svg2-draft/types.html#InterfaceSVGURIReference
  15. template<SupportsXLinkHref supports_xlink_href>
  16. class SVGURIReferenceMixin {
  17. public:
  18. virtual ~SVGURIReferenceMixin() = default;
  19. JS::NonnullGCPtr<SVGAnimatedString> href()
  20. {
  21. // The href IDL attribute represents the value of the ‘href’ attribute, and, on elements that are defined to support
  22. // it, the deprecated ‘xlink:href’ attribute. On getting href, an SVGAnimatedString object is returned that:
  23. // - reflects the ‘href’ attribute, and
  24. // - if the element is defined to support the deprecated ‘xlink:href’ attribute, additionally reflects that
  25. // deprecated attribute.
  26. if (!m_href_animated_string) {
  27. auto* this_svg_element = dynamic_cast<SVGElement*>(this);
  28. VERIFY(this_svg_element);
  29. m_href_animated_string = SVGAnimatedString::create(this_svg_element->realm(), *this_svg_element, AttributeNames::href, supports_xlink_href == SupportsXLinkHref::Yes ? Optional<FlyString> { AttributeNames::xlink_href } : OptionalNone {});
  30. }
  31. return *m_href_animated_string;
  32. }
  33. protected:
  34. void visit_edges(JS::Cell::Visitor& visitor)
  35. {
  36. visitor.visit(m_href_animated_string);
  37. }
  38. private:
  39. JS::GCPtr<SVGAnimatedString> m_href_animated_string;
  40. };
  41. }