SVGSVGElement.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. JS::ThrowCompletionOr<void> SVGSVGElement::initialize(JS::Realm& realm)
  22. {
  23. MUST_OR_THROW_OOM(Base::initialize(realm));
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGSVGElementPrototype>(realm, "SVGSVGElement"));
  25. return {};
  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. auto width_attribute = attribute(SVG::AttributeNames::width);
  34. if (auto width_value = HTML::parse_dimension_value(width_attribute)) {
  35. style.set_property(CSS::PropertyID::Width, width_value.release_nonnull());
  36. } else if (width_attribute == "") {
  37. // If the `width` attribute is an empty string, it defaults to 100%.
  38. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  39. // FIXME: Figure out what to do here.
  40. style.set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  41. }
  42. // Height defaults to 100%
  43. auto height_attribute = attribute(SVG::AttributeNames::height);
  44. if (auto height_value = HTML::parse_dimension_value(height_attribute)) {
  45. style.set_property(CSS::PropertyID::Height, height_value.release_nonnull());
  46. } else if (height_attribute == "") {
  47. // If the `height` attribute is an empty string, it defaults to 100%.
  48. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  49. // FIXME: Figure out what to do here.
  50. style.set_property(CSS::PropertyID::Height, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  51. }
  52. }
  53. void SVGSVGElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  54. {
  55. SVGGraphicsElement::parse_attribute(name, value);
  56. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  57. m_view_box = try_parse_view_box(value);
  58. }
  59. }