SVGUseElement.cpp 6.5 KB

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