SVGSymbolElement.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/Bindings/SVGSymbolElementPrototype.h>
  8. #include <LibWeb/CSS/StyleProperties.h>
  9. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  10. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  12. #include <LibWeb/DOM/ShadowRoot.h>
  13. #include <LibWeb/Layout/SVGGraphicsBox.h>
  14. #include <LibWeb/SVG/AttributeNames.h>
  15. #include <LibWeb/SVG/SVGAnimatedRect.h>
  16. #include <LibWeb/SVG/SVGSymbolElement.h>
  17. #include <LibWeb/SVG/SVGUseElement.h>
  18. namespace Web::SVG {
  19. GC_DEFINE_ALLOCATOR(SVGSymbolElement);
  20. SVGSymbolElement::SVGSymbolElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : SVGGraphicsElement(document, qualified_name)
  22. {
  23. }
  24. void SVGSymbolElement::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGSymbolElement);
  28. m_view_box_for_bindings = realm.create<SVGAnimatedRect>(realm);
  29. }
  30. void SVGSymbolElement::visit_edges(Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_view_box_for_bindings);
  34. }
  35. // https://svgwg.org/svg2-draft/struct.html#SymbolNotes
  36. void SVGSymbolElement::apply_presentational_hints(CSS::StyleProperties& style) const
  37. {
  38. Base::apply_presentational_hints(style);
  39. if (is_direct_child_of_use_shadow_tree()) {
  40. // The generated instance of a ‘symbol’ that is the direct referenced element of a ‘use’ element must always have a computed value of inline for the display property.
  41. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Inline)));
  42. }
  43. }
  44. void SVGSymbolElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  45. {
  46. Base::attribute_changed(name, old_value, value, namespace_);
  47. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox)) {
  48. m_view_box = try_parse_view_box(value.value_or(String {}));
  49. m_view_box_for_bindings->set_nulled(!m_view_box.has_value());
  50. if (m_view_box.has_value()) {
  51. m_view_box_for_bindings->set_base_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
  52. m_view_box_for_bindings->set_anim_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
  53. }
  54. }
  55. }
  56. bool SVGSymbolElement::is_direct_child_of_use_shadow_tree() const
  57. {
  58. auto maybe_shadow_root = parent();
  59. if (!is<DOM::ShadowRoot>(maybe_shadow_root)) {
  60. return false;
  61. }
  62. auto host = static_cast<const DOM::ShadowRoot&>(*maybe_shadow_root).host();
  63. return is<SVGUseElement>(host);
  64. }
  65. GC::Ptr<Layout::Node> SVGSymbolElement::create_layout_node(CSS::StyleProperties style)
  66. {
  67. return heap().allocate<Layout::SVGGraphicsBox>(document(), *this, move(style));
  68. }
  69. }