SVGSymbolElement.cpp 2.2 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/ShorthandStyleValue.h>
  11. #include <LibWeb/DOM/ShadowRoot.h>
  12. #include <LibWeb/Layout/SVGGraphicsBox.h>
  13. #include <LibWeb/SVG/AttributeNames.h>
  14. #include <LibWeb/SVG/SVGSymbolElement.h>
  15. #include <LibWeb/SVG/SVGUseElement.h>
  16. namespace Web::SVG {
  17. JS_DEFINE_ALLOCATOR(SVGSymbolElement);
  18. SVGSymbolElement::SVGSymbolElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : SVGGraphicsElement(document, qualified_name)
  20. {
  21. }
  22. void SVGSymbolElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGSymbolElement);
  26. }
  27. // https://svgwg.org/svg2-draft/struct.html#SymbolNotes
  28. void SVGSymbolElement::apply_presentational_hints(CSS::StyleProperties& style) const
  29. {
  30. Base::apply_presentational_hints(style);
  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)));
  34. }
  35. }
  36. void SVGSymbolElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  37. {
  38. Base::attribute_changed(name, value);
  39. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  40. m_view_box = try_parse_view_box(value.value_or(String {}));
  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. JS::GCPtr<Layout::Node> SVGSymbolElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  52. {
  53. return heap().allocate_without_realm<Layout::SVGGraphicsBox>(document(), *this, move(style));
  54. }
  55. }