SVGSVGElement.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/SVGSVGElement.h>
  17. namespace Web::SVG {
  18. SVGSVGElement::SVGSVGElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : SVGGraphicsElement(document, qualified_name)
  20. {
  21. }
  22. void SVGSVGElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGSVGElementPrototype>(realm, "SVGSVGElement"));
  26. }
  27. JS::GCPtr<Layout::Node> SVGSVGElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  28. {
  29. return heap().allocate_without_realm<Layout::SVGSVGBox>(document(), *this, move(style));
  30. }
  31. void SVGSVGElement::apply_presentational_hints(CSS::StyleProperties& style) const
  32. {
  33. Base::apply_presentational_hints(style);
  34. // NOTE: Hack to ensure SVG unitless widths/heights are parsed even with <!DOCTYPE html>
  35. FIXME::TemporarilyEnableQuirksMode enable_quirks(document());
  36. auto width_attribute = deprecated_attribute(SVG::AttributeNames::width);
  37. auto parsing_context = CSS::Parser::ParsingContext { document() };
  38. if (auto width_value = parse_css_value(parsing_context, deprecated_attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width)) {
  39. style.set_property(CSS::PropertyID::Width, width_value.release_nonnull());
  40. } else if (width_attribute == "") {
  41. // If the `width` attribute is an empty string, it defaults to 100%.
  42. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  43. // FIXME: Figure out what to do here.
  44. style.set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  45. }
  46. // Height defaults to 100%
  47. auto height_attribute = deprecated_attribute(SVG::AttributeNames::height);
  48. if (auto height_value = parse_css_value(parsing_context, deprecated_attribute(Web::HTML::AttributeNames::height), CSS::PropertyID::Height)) {
  49. style.set_property(CSS::PropertyID::Height, height_value.release_nonnull());
  50. } else if (height_attribute == "") {
  51. // If the `height` attribute is an empty string, it defaults to 100%.
  52. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  53. // FIXME: Figure out what to do here.
  54. style.set_property(CSS::PropertyID::Height, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  55. }
  56. }
  57. void SVGSVGElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
  58. {
  59. SVGGraphicsElement::attribute_changed(name, value);
  60. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  61. m_view_box = try_parse_view_box(value);
  62. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::preserveAspectRatio))
  63. m_preserve_aspect_ratio = AttributeParser::parse_preserve_aspect_ratio(value);
  64. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::width) || name.equals_ignoring_ascii_case(SVG::AttributeNames::height))
  65. update_fallback_view_box_for_svg_as_image();
  66. }
  67. void SVGSVGElement::update_fallback_view_box_for_svg_as_image()
  68. {
  69. // AD-HOC: This creates a fallback viewBox for SVGs used as images.
  70. // If the <svg> element has width and height, but no viewBox,
  71. // we fall back to a synthetic viewBox="0 0 width height".
  72. Optional<double> width;
  73. Optional<double> height;
  74. auto width_attribute = deprecated_attribute(SVG::AttributeNames::width);
  75. auto parsing_context = CSS::Parser::ParsingContext { document() };
  76. if (auto width_value = parse_css_value(parsing_context, deprecated_attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width)) {
  77. if (width_value->is_length() && width_value->as_length().length().is_absolute())
  78. width = width_value->as_length().length().absolute_length_to_px().to_double();
  79. }
  80. auto height_attribute = deprecated_attribute(SVG::AttributeNames::height);
  81. if (auto height_value = parse_css_value(parsing_context, deprecated_attribute(Web::HTML::AttributeNames::height), CSS::PropertyID::Height)) {
  82. if (height_value->is_length() && height_value->as_length().length().is_absolute())
  83. height = height_value->as_length().length().absolute_length_to_px().to_double();
  84. }
  85. if (width.has_value() && width.value() > 0 && height.has_value() && height.value() > 0) {
  86. m_fallback_view_box_for_svg_as_image = ViewBox { 0, 0, width.value(), height.value() };
  87. } else {
  88. m_fallback_view_box_for_svg_as_image = {};
  89. }
  90. }
  91. void SVGSVGElement::set_fallback_view_box_for_svg_as_image(Optional<ViewBox> view_box)
  92. {
  93. m_fallback_view_box_for_svg_as_image = view_box;
  94. }
  95. Optional<ViewBox> SVGSVGElement::view_box() const
  96. {
  97. if (m_view_box.has_value())
  98. return m_view_box;
  99. // NOTE: If the parent is a document, we're an <svg> element used as an image.
  100. if (parent() && parent()->is_document() && m_fallback_view_box_for_svg_as_image.has_value())
  101. return m_fallback_view_box_for_svg_as_image;
  102. return {};
  103. }
  104. }