SVGUseElement.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2023, Preston Taylor <95388976+PrestonLTaylor@users.noreply.github.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/ElementFactory.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/SVGGraphicsBox.h>
  13. #include <LibWeb/Namespace.h>
  14. #include <LibWeb/SVG/AttributeNames.h>
  15. #include <LibWeb/SVG/SVGSVGElement.h>
  16. #include <LibWeb/SVG/SVGUseElement.h>
  17. namespace Web::SVG {
  18. JS_DEFINE_ALLOCATOR(SVGUseElement);
  19. SVGUseElement::SVGUseElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  20. : SVGGraphicsElement(document, qualified_name)
  21. {
  22. }
  23. void SVGUseElement::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGUseElement);
  27. // The shadow tree is open (inspectable by script), but read-only.
  28. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm, document(), *this, Bindings::ShadowRootMode::Open);
  29. // The user agent must create a use-element shadow tree whose host is the ‘use’ element itself
  30. set_shadow_root(shadow_root);
  31. m_document_observer = realm.heap().allocate<DOM::DocumentObserver>(realm, realm, document());
  32. m_document_observer->set_document_completely_loaded([this]() {
  33. clone_element_tree_as_our_shadow_tree(referenced_element());
  34. });
  35. }
  36. void SVGUseElement::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. SVGURIReferenceMixin::visit_edges(visitor);
  40. visitor.visit(m_document_observer);
  41. }
  42. void SVGUseElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  43. {
  44. Base::attribute_changed(name, value);
  45. // https://svgwg.org/svg2-draft/struct.html#UseLayout
  46. if (name == SVG::AttributeNames::x) {
  47. m_x = AttributeParser::parse_coordinate(value.value_or(String {}));
  48. } else if (name == SVG::AttributeNames::y) {
  49. m_y = AttributeParser::parse_coordinate(value.value_or(String {}));
  50. } else if (name == SVG::AttributeNames::href) {
  51. // FIXME: Support the xlink:href attribute as a fallback
  52. m_referenced_id = parse_id_from_href(value.value_or(String {}));
  53. clone_element_tree_as_our_shadow_tree(referenced_element());
  54. }
  55. }
  56. Optional<FlyString> SVGUseElement::parse_id_from_href(StringView href)
  57. {
  58. auto id_seperator = href.find('#');
  59. if (!id_seperator.has_value()) {
  60. return {};
  61. }
  62. auto id = href.substring_view(id_seperator.value() + 1);
  63. return MUST(FlyString::from_utf8(id));
  64. }
  65. Gfx::AffineTransform SVGUseElement::element_transform() const
  66. {
  67. // The x and y properties define an additional transformation (translate(x,y), where x and y represent the computed value of the corresponding property)
  68. // to be applied to the ‘use’ element, after any transformations specified with other properties
  69. return Base::element_transform().translate(m_x.value_or(0), m_y.value_or(0));
  70. }
  71. void SVGUseElement::inserted()
  72. {
  73. Base::inserted();
  74. }
  75. void SVGUseElement::svg_element_changed(SVGElement& svg_element)
  76. {
  77. auto to_clone = referenced_element();
  78. if (!to_clone) {
  79. return;
  80. }
  81. // NOTE: We need to check the ancestor because attribute_changed of a child doesn't call children_changed on the parent(s)
  82. if (to_clone == &svg_element || to_clone->is_ancestor_of(svg_element)) {
  83. clone_element_tree_as_our_shadow_tree(to_clone);
  84. }
  85. }
  86. void SVGUseElement::svg_element_removed(SVGElement& svg_element)
  87. {
  88. if (!m_referenced_id.has_value()) {
  89. return;
  90. }
  91. if (AK::StringUtils::matches(svg_element.get_attribute_value("id"_fly_string), m_referenced_id.value())) {
  92. shadow_root()->remove_all_children();
  93. }
  94. }
  95. JS::GCPtr<DOM::Element> SVGUseElement::referenced_element()
  96. {
  97. if (!m_referenced_id.has_value()) {
  98. return nullptr;
  99. }
  100. // FIXME: Support loading of external svg documents
  101. return document().get_element_by_id(m_referenced_id.value());
  102. }
  103. // https://svgwg.org/svg2-draft/struct.html#UseShadowTree
  104. void SVGUseElement::clone_element_tree_as_our_shadow_tree(Element* to_clone) const
  105. {
  106. shadow_root()->remove_all_children();
  107. if (to_clone && is_valid_reference_element(to_clone)) {
  108. // The ‘use’ element references another element, a copy of which is rendered in place of the ‘use’ in the document.
  109. auto cloned_reference_node = to_clone->clone_node(nullptr, true);
  110. shadow_root()->append_child(cloned_reference_node).release_value_but_fixme_should_propagate_errors();
  111. }
  112. }
  113. bool SVGUseElement::is_valid_reference_element(Element* reference_element) const
  114. {
  115. // If the referenced element that results from resolving the URL is not an SVG element, then the reference is invalid and the ‘use’ element is in error.
  116. // If the referenced element is a (shadow-including) ancestor of the ‘use’ element, then this is an invalid circular reference and the ‘use’ element is in error.
  117. return reference_element->is_svg_element() && !reference_element->is_ancestor_of(*this);
  118. }
  119. // https://www.w3.org/TR/SVG11/shapes.html#RectElementXAttribute
  120. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::x() const
  121. {
  122. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  123. // FIXME: Create a proper animated value when animations are supported.
  124. auto base_length = SVGLength::create(realm(), 0, m_x.value_or(0));
  125. auto anim_length = SVGLength::create(realm(), 0, m_x.value_or(0));
  126. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  127. }
  128. // https://www.w3.org/TR/SVG11/shapes.html#RectElementYAttribute
  129. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::y() const
  130. {
  131. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  132. // FIXME: Create a proper animated value when animations are supported.
  133. auto base_length = SVGLength::create(realm(), 0, m_y.value_or(0));
  134. auto anim_length = SVGLength::create(realm(), 0, m_y.value_or(0));
  135. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  136. }
  137. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::width() const
  138. {
  139. // FIXME: Implement this properly.
  140. return SVGAnimatedLength::create(realm(), SVGLength::create(realm(), 0, 0), SVGLength::create(realm(), 0, 0));
  141. }
  142. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::height() const
  143. {
  144. // FIXME: Implement this properly.
  145. return SVGAnimatedLength::create(realm(), SVGLength::create(realm(), 0, 0), SVGLength::create(realm(), 0, 0));
  146. }
  147. // https://svgwg.org/svg2-draft/struct.html#TermInstanceRoot
  148. JS::GCPtr<SVGElement> SVGUseElement::instance_root() const
  149. {
  150. return shadow_root()->first_child_of_type<SVGElement>();
  151. }
  152. JS::GCPtr<SVGElement> SVGUseElement::animated_instance_root() const
  153. {
  154. return instance_root();
  155. }
  156. JS::GCPtr<Layout::Node> SVGUseElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  157. {
  158. return heap().allocate_without_realm<Layout::SVGGraphicsBox>(document(), *this, move(style));
  159. }
  160. }