SVGUseElement.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(FlyString const& name, Optional<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.value_or(""));
  45. } else if (name == SVG::AttributeNames::y) {
  46. m_y = AttributeParser::parse_coordinate(value.value_or(""));
  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.value_or(""));
  50. clone_element_tree_as_our_shadow_tree(referenced_element());
  51. }
  52. }
  53. Optional<FlyString> 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. auto id = href.substring_view(id_seperator.value() + 1);
  60. return MUST(FlyString::from_utf8(id));
  61. }
  62. Gfx::AffineTransform SVGUseElement::element_transform() const
  63. {
  64. // The x and y properties define an additional transformation (translate(x,y), where x and y represent the computed value of the corresponding property)
  65. // to be applied to the ‘use’ element, after any transformations specified with other properties
  66. return Base::element_transform().translate(m_x.value_or(0), m_y.value_or(0));
  67. }
  68. void SVGUseElement::inserted()
  69. {
  70. Base::inserted();
  71. }
  72. void SVGUseElement::svg_element_changed(SVGElement& svg_element)
  73. {
  74. auto to_clone = referenced_element();
  75. if (!to_clone) {
  76. return;
  77. }
  78. // NOTE: We need to check the ancestor because attribute_changed of a child doesn't call children_changed on the parent(s)
  79. if (to_clone == &svg_element || to_clone->is_ancestor_of(svg_element)) {
  80. clone_element_tree_as_our_shadow_tree(to_clone);
  81. }
  82. }
  83. void SVGUseElement::svg_element_removed(SVGElement& svg_element)
  84. {
  85. if (!m_referenced_id.has_value()) {
  86. return;
  87. }
  88. if (svg_element.deprecated_attribute("id"sv).matches(m_referenced_id.value())) {
  89. shadow_root()->remove_all_children();
  90. }
  91. }
  92. JS::GCPtr<DOM::Element> SVGUseElement::referenced_element()
  93. {
  94. if (!m_referenced_id.has_value()) {
  95. return nullptr;
  96. }
  97. // FIXME: Support loading of external svg documents
  98. return document().get_element_by_id(m_referenced_id.value());
  99. }
  100. // https://svgwg.org/svg2-draft/struct.html#UseShadowTree
  101. void SVGUseElement::clone_element_tree_as_our_shadow_tree(Element* to_clone) const
  102. {
  103. shadow_root()->remove_all_children();
  104. if (to_clone && is_valid_reference_element(to_clone)) {
  105. // The ‘use’ element references another element, a copy of which is rendered in place of the ‘use’ in the document.
  106. auto cloned_reference_node = to_clone->clone_node(nullptr, true);
  107. shadow_root()->append_child(cloned_reference_node).release_value_but_fixme_should_propagate_errors();
  108. }
  109. }
  110. bool SVGUseElement::is_valid_reference_element(Element* reference_element) const
  111. {
  112. // 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.
  113. // 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.
  114. return reference_element->is_svg_element() && !reference_element->is_ancestor_of(*this);
  115. }
  116. // https://www.w3.org/TR/SVG11/shapes.html#RectElementXAttribute
  117. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::x() const
  118. {
  119. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  120. // FIXME: Create a proper animated value when animations are supported.
  121. auto base_length = SVGLength::create(realm(), 0, m_x.value_or(0));
  122. auto anim_length = SVGLength::create(realm(), 0, m_x.value_or(0));
  123. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  124. }
  125. // https://www.w3.org/TR/SVG11/shapes.html#RectElementYAttribute
  126. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::y() const
  127. {
  128. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  129. // FIXME: Create a proper animated value when animations are supported.
  130. auto base_length = SVGLength::create(realm(), 0, m_y.value_or(0));
  131. auto anim_length = SVGLength::create(realm(), 0, m_y.value_or(0));
  132. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  133. }
  134. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::width() const
  135. {
  136. TODO();
  137. }
  138. JS::NonnullGCPtr<SVGAnimatedLength> SVGUseElement::height() const
  139. {
  140. TODO();
  141. }
  142. // https://svgwg.org/svg2-draft/struct.html#TermInstanceRoot
  143. JS::GCPtr<SVGElement> SVGUseElement::instance_root() const
  144. {
  145. return shadow_root()->first_child_of_type<SVGElement>();
  146. }
  147. JS::GCPtr<SVGElement> SVGUseElement::animated_instance_root() const
  148. {
  149. return instance_root();
  150. }
  151. JS::GCPtr<Layout::Node> SVGUseElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  152. {
  153. return heap().allocate_without_realm<Layout::Box>(document(), this, move(style));
  154. }
  155. }