SVGSVGElement.cpp 2.8 KB

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