SVGSymbolElement.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/SVG/SVGSymbolElement.h>
  13. #include <LibWeb/SVG/SVGUseElement.h>
  14. namespace Web::SVG {
  15. SVGSymbolElement::SVGSymbolElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : SVGGraphicsElement(document, qualified_name)
  17. {
  18. }
  19. JS::ThrowCompletionOr<void> SVGSymbolElement::initialize(JS::Realm& realm)
  20. {
  21. MUST_OR_THROW_OOM(Base::initialize(realm));
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGSymbolElementPrototype>(realm, "SVGSymbolElement"));
  23. return {};
  24. }
  25. // https://svgwg.org/svg2-draft/struct.html#SymbolNotes
  26. void SVGSymbolElement::apply_presentational_hints(CSS::StyleProperties& style) const
  27. {
  28. // The user agent style sheet sets the overflow property for ‘symbol’ elements to hidden.
  29. auto hidden = CSS::IdentifierStyleValue::create(CSS::ValueID::Hidden).release_value_but_fixme_should_propagate_errors();
  30. style.set_property(CSS::PropertyID::Overflow, CSS::OverflowStyleValue::create(hidden, hidden).release_value_but_fixme_should_propagate_errors());
  31. if (is_direct_child_of_use_shadow_tree()) {
  32. // 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.
  33. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Inline)).release_value_but_fixme_should_propagate_errors());
  34. } else {
  35. // FIXME: When we have a DefaultSVG.css then use https://svgwg.org/svg2-draft/styling.html#UAStyleSheet instead.
  36. // The user agent must set the display property on the ‘symbol’ element to none, as part of the user agent style sheet,
  37. // and this declaration must have importance over any other CSS rule or presentation attribute.
  38. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)).release_value_but_fixme_should_propagate_errors());
  39. }
  40. // TODO: Parse viewBox and apply it in SVGGraphicsElement/SVGGraphicsPaintable
  41. }
  42. bool SVGSymbolElement::is_direct_child_of_use_shadow_tree() const
  43. {
  44. auto maybe_shadow_root = parent();
  45. if (!is<DOM::ShadowRoot>(maybe_shadow_root)) {
  46. return false;
  47. }
  48. auto host = static_cast<const DOM::ShadowRoot&>(*maybe_shadow_root).host();
  49. return is<SVGUseElement>(host);
  50. }
  51. }