SVGSVGElement.cpp 5.7 KB

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