SVGSymbolElement.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/OverflowStyleValue.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. // The user agent style sheet sets the overflow property for ‘symbol’ elements to hidden.
  30. auto hidden = CSS::IdentifierStyleValue::create(CSS::ValueID::Hidden);
  31. style.set_property(CSS::PropertyID::Overflow, CSS::OverflowStyleValue::create(hidden, hidden));
  32. if (is_direct_child_of_use_shadow_tree()) {
  33. // 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.
  34. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Inline)));
  35. } else {
  36. // FIXME: When we have a DefaultSVG.css then use https://svgwg.org/svg2-draft/styling.html#UAStyleSheet instead.
  37. // The user agent must set the display property on the ‘symbol’ element to none, as part of the user agent style sheet,
  38. // and this declaration must have importance over any other CSS rule or presentation attribute.
  39. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)));
  40. }
  41. }
  42. void SVGSymbolElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
  43. {
  44. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  45. m_view_box = try_parse_view_box(value);
  46. }
  47. bool SVGSymbolElement::is_direct_child_of_use_shadow_tree() const
  48. {
  49. auto maybe_shadow_root = parent();
  50. if (!is<DOM::ShadowRoot>(maybe_shadow_root)) {
  51. return false;
  52. }
  53. auto host = static_cast<const DOM::ShadowRoot&>(*maybe_shadow_root).host();
  54. return is<SVGUseElement>(host);
  55. }
  56. JS::GCPtr<Layout::Node> SVGSymbolElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  57. {
  58. return heap().allocate_without_realm<Layout::Box>(document(), this, move(style));
  59. }
  60. }