SVGSymbolElement.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/CSS/StyleProperties.h>
  8. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  11. #include <LibWeb/DOM/ShadowRoot.h>
  12. #include <LibWeb/Layout/Box.h>
  13. #include <LibWeb/SVG/AttributeNames.h>
  14. #include <LibWeb/SVG/SVGSymbolElement.h>
  15. #include <LibWeb/SVG/SVGUseElement.h>
  16. namespace Web::SVG {
  17. SVGSymbolElement::SVGSymbolElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : SVGGraphicsElement(document, qualified_name)
  19. {
  20. }
  21. void SVGSymbolElement::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGSymbolElementPrototype>(realm, "SVGSymbolElement"));
  25. }
  26. // https://svgwg.org/svg2-draft/struct.html#SymbolNotes
  27. void SVGSymbolElement::apply_presentational_hints(CSS::StyleProperties& style) const
  28. {
  29. Base::apply_presentational_hints(style);
  30. if (is_direct_child_of_use_shadow_tree()) {
  31. // 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.
  32. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Inline)));
  33. }
  34. }
  35. void SVGSymbolElement::attribute_changed(FlyString const& name, Optional<DeprecatedString> const& value)
  36. {
  37. Base::attribute_changed(name, value);
  38. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  39. m_view_box = try_parse_view_box(value.value_or(""));
  40. }
  41. bool SVGSymbolElement::is_direct_child_of_use_shadow_tree() const
  42. {
  43. auto maybe_shadow_root = parent();
  44. if (!is<DOM::ShadowRoot>(maybe_shadow_root)) {
  45. return false;
  46. }
  47. auto host = static_cast<const DOM::ShadowRoot&>(*maybe_shadow_root).host();
  48. return is<SVGUseElement>(host);
  49. }
  50. JS::GCPtr<Layout::Node> SVGSymbolElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  51. {
  52. return heap().allocate_without_realm<Layout::Box>(document(), this, move(style));
  53. }
  54. }