SVGSVGElement.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <LibGfx/Painter.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/StyleComputer.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Event.h>
  12. #include <LibWeb/HTML/Parser/HTMLParser.h>
  13. #include <LibWeb/Layout/SVGSVGBox.h>
  14. #include <LibWeb/SVG/AttributeNames.h>
  15. #include <LibWeb/SVG/SVGSVGElement.h>
  16. namespace Web::SVG {
  17. SVGSVGElement::SVGSVGElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : SVGGraphicsElement(document, qualified_name)
  19. {
  20. }
  21. RefPtr<Layout::Node> SVGSVGElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  22. {
  23. return adopt_ref(*new Layout::SVGSVGBox(document(), *this, move(style)));
  24. }
  25. void SVGSVGElement::apply_presentational_hints(CSS::StyleProperties& style) const
  26. {
  27. auto width_attribute = attribute(SVG::AttributeNames::width);
  28. if (auto width_value = HTML::parse_dimension_value(width_attribute)) {
  29. style.set_property(CSS::PropertyID::Width, width_value.release_nonnull());
  30. } else if (width_attribute == "") {
  31. // If the `width` attribute is an empty string, it defaults to 100%.
  32. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  33. // FIXME: Figure out what to do here.
  34. style.set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  35. }
  36. // Height defaults to 100%
  37. auto height_attribute = attribute(SVG::AttributeNames::height);
  38. if (auto height_value = HTML::parse_dimension_value(height_attribute)) {
  39. style.set_property(CSS::PropertyID::Height, height_value.release_nonnull());
  40. } else if (height_attribute == "") {
  41. // If the `height` 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::Height, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  45. }
  46. }
  47. void SVGSVGElement::parse_attribute(FlyString const& name, String const& value)
  48. {
  49. SVGGraphicsElement::parse_attribute(name, value);
  50. if (name.equals_ignoring_case(SVG::AttributeNames::viewBox))
  51. m_view_box = try_parse_view_box(value);
  52. }
  53. }