SVGSymbolElement.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(DeprecatedFlyString const& name, DeprecatedString const& value)
  36. {
  37. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  38. m_view_box = try_parse_view_box(value);
  39. }
  40. bool SVGSymbolElement::is_direct_child_of_use_shadow_tree() const
  41. {
  42. auto maybe_shadow_root = parent();
  43. if (!is<DOM::ShadowRoot>(maybe_shadow_root)) {
  44. return false;
  45. }
  46. auto host = static_cast<const DOM::ShadowRoot&>(*maybe_shadow_root).host();
  47. return is<SVGUseElement>(host);
  48. }
  49. JS::GCPtr<Layout::Node> SVGSymbolElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  50. {
  51. return heap().allocate_without_realm<Layout::Box>(document(), this, move(style));
  52. }
  53. }