SVGSVGElement.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. set_prototype(&Bindings::cached_web_prototype(realm(), "SVGSVGElement"));
  21. }
  22. RefPtr<Layout::Node> SVGSVGElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  23. {
  24. return adopt_ref(*new Layout::SVGSVGBox(document(), *this, move(style)));
  25. }
  26. void SVGSVGElement::apply_presentational_hints(CSS::StyleProperties& style) const
  27. {
  28. auto width_attribute = attribute(SVG::AttributeNames::width);
  29. if (auto width_value = HTML::parse_dimension_value(width_attribute)) {
  30. style.set_property(CSS::PropertyID::Width, width_value.release_nonnull());
  31. } else if (width_attribute == "") {
  32. // If the `width` attribute is an empty string, it defaults to 100%.
  33. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  34. // FIXME: Figure out what to do here.
  35. style.set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  36. }
  37. // Height defaults to 100%
  38. auto height_attribute = attribute(SVG::AttributeNames::height);
  39. if (auto height_value = HTML::parse_dimension_value(height_attribute)) {
  40. style.set_property(CSS::PropertyID::Height, height_value.release_nonnull());
  41. } else if (height_attribute == "") {
  42. // If the `height` 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::Height, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  46. }
  47. }
  48. void SVGSVGElement::parse_attribute(FlyString const& name, String const& value)
  49. {
  50. SVGGraphicsElement::parse_attribute(name, value);
  51. if (name.equals_ignoring_case(SVG::AttributeNames::viewBox))
  52. m_view_box = try_parse_view_box(value);
  53. }
  54. }