SVGSymbolElement.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/AttributeNames.h>
  13. #include <LibWeb/SVG/SVGSymbolElement.h>
  14. #include <LibWeb/SVG/SVGUseElement.h>
  15. namespace Web::SVG {
  16. SVGSymbolElement::SVGSymbolElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  17. : SVGGraphicsElement(document, qualified_name)
  18. {
  19. }
  20. JS::ThrowCompletionOr<void> SVGSymbolElement::initialize(JS::Realm& realm)
  21. {
  22. MUST_OR_THROW_OOM(Base::initialize(realm));
  23. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGSymbolElementPrototype>(realm, "SVGSymbolElement"));
  24. return {};
  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).release_value_but_fixme_should_propagate_errors();
  31. style.set_property(CSS::PropertyID::Overflow, CSS::OverflowStyleValue::create(hidden, hidden).release_value_but_fixme_should_propagate_errors());
  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)).release_value_but_fixme_should_propagate_errors());
  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)).release_value_but_fixme_should_propagate_errors());
  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. }