SVGSVGElement.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/CSS/StyleComputer.h>
  9. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/HTML/Parser/HTMLParser.h>
  14. #include <LibWeb/Layout/SVGSVGBox.h>
  15. #include <LibWeb/SVG/AttributeNames.h>
  16. #include <LibWeb/SVG/SVGAnimatedRect.h>
  17. #include <LibWeb/SVG/SVGSVGElement.h>
  18. namespace Web::SVG {
  19. JS_DEFINE_ALLOCATOR(SVGSVGElement);
  20. SVGSVGElement::SVGSVGElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : SVGGraphicsElement(document, qualified_name)
  22. {
  23. }
  24. void SVGSVGElement::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGSVGElement);
  28. m_view_box_for_bindings = heap().allocate<SVGAnimatedRect>(realm, realm);
  29. }
  30. void SVGSVGElement::visit_edges(Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_view_box_for_bindings);
  34. }
  35. JS::GCPtr<Layout::Node> SVGSVGElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  36. {
  37. return heap().allocate_without_realm<Layout::SVGSVGBox>(document(), *this, move(style));
  38. }
  39. void SVGSVGElement::apply_presentational_hints(CSS::StyleProperties& style) const
  40. {
  41. Base::apply_presentational_hints(style);
  42. auto parsing_context = CSS::Parser::ParsingContext { document(), CSS::Parser::ParsingContext::Mode::SVGPresentationAttribute };
  43. auto x_attribute = attribute(SVG::AttributeNames::x);
  44. if (auto x_value = parse_css_value(parsing_context, x_attribute.value_or(String {}), CSS::PropertyID::X)) {
  45. style.set_property(CSS::PropertyID::X, x_value.release_nonnull());
  46. }
  47. auto y_attribute = attribute(SVG::AttributeNames::y);
  48. if (auto y_value = parse_css_value(parsing_context, y_attribute.value_or(String {}), CSS::PropertyID::Y)) {
  49. style.set_property(CSS::PropertyID::Y, y_value.release_nonnull());
  50. }
  51. auto width_attribute = attribute(SVG::AttributeNames::width);
  52. if (auto width_value = parse_css_value(parsing_context, width_attribute.value_or(String {}), CSS::PropertyID::Width)) {
  53. style.set_property(CSS::PropertyID::Width, width_value.release_nonnull());
  54. } else if (width_attribute == "") {
  55. // If the `width` attribute is an empty string, it defaults to 100%.
  56. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  57. // FIXME: Figure out what to do here.
  58. style.set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  59. }
  60. // Height defaults to 100%
  61. auto height_attribute = attribute(SVG::AttributeNames::height);
  62. if (auto height_value = parse_css_value(parsing_context, height_attribute.value_or(String {}), CSS::PropertyID::Height)) {
  63. style.set_property(CSS::PropertyID::Height, height_value.release_nonnull());
  64. } else if (height_attribute == "") {
  65. // If the `height` attribute is an empty string, it defaults to 100%.
  66. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  67. // FIXME: Figure out what to do here.
  68. style.set_property(CSS::PropertyID::Height, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  69. }
  70. }
  71. void SVGSVGElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  72. {
  73. SVGGraphicsElement::attribute_changed(name, value);
  74. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox)) {
  75. if (!value.has_value()) {
  76. m_view_box_for_bindings->set_nulled(true);
  77. } else {
  78. m_view_box = try_parse_view_box(value.value_or(String {}));
  79. m_view_box_for_bindings->set_nulled(!m_view_box.has_value());
  80. if (m_view_box.has_value()) {
  81. m_view_box_for_bindings->set_base_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
  82. m_view_box_for_bindings->set_anim_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
  83. }
  84. }
  85. }
  86. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::preserveAspectRatio))
  87. m_preserve_aspect_ratio = AttributeParser::parse_preserve_aspect_ratio(value.value_or(String {}));
  88. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::width) || name.equals_ignoring_ascii_case(SVG::AttributeNames::height))
  89. update_fallback_view_box_for_svg_as_image();
  90. }
  91. void SVGSVGElement::update_fallback_view_box_for_svg_as_image()
  92. {
  93. // AD-HOC: This creates a fallback viewBox for SVGs used as images.
  94. // If the <svg> element has width and height, but no viewBox,
  95. // we fall back to a synthetic viewBox="0 0 width height".
  96. Optional<double> width;
  97. Optional<double> height;
  98. auto width_attribute = get_attribute_value(SVG::AttributeNames::width);
  99. auto parsing_context = CSS::Parser::ParsingContext { document() };
  100. if (auto width_value = parse_css_value(parsing_context, width_attribute, CSS::PropertyID::Width)) {
  101. if (width_value->is_length() && width_value->as_length().length().is_absolute())
  102. width = width_value->as_length().length().absolute_length_to_px().to_double();
  103. }
  104. auto height_attribute = get_attribute_value(SVG::AttributeNames::height);
  105. if (auto height_value = parse_css_value(parsing_context, height_attribute, CSS::PropertyID::Height)) {
  106. if (height_value->is_length() && height_value->as_length().length().is_absolute())
  107. height = height_value->as_length().length().absolute_length_to_px().to_double();
  108. }
  109. if (width.has_value() && width.value() > 0 && height.has_value() && height.value() > 0) {
  110. m_fallback_view_box_for_svg_as_image = ViewBox { 0, 0, width.value(), height.value() };
  111. } else {
  112. m_fallback_view_box_for_svg_as_image = {};
  113. }
  114. }
  115. void SVGSVGElement::set_fallback_view_box_for_svg_as_image(Optional<ViewBox> view_box)
  116. {
  117. m_fallback_view_box_for_svg_as_image = view_box;
  118. }
  119. Optional<ViewBox> SVGSVGElement::view_box() const
  120. {
  121. if (m_view_box.has_value())
  122. return m_view_box;
  123. // NOTE: If the parent is a document, we're an <svg> element used as an image.
  124. if (parent() && parent()->is_document() && m_fallback_view_box_for_svg_as_image.has_value())
  125. return m_fallback_view_box_for_svg_as_image;
  126. return {};
  127. }
  128. }