SVGSVGElement.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // NOTE: Hack to ensure SVG unitless widths/heights are parsed even with <!DOCTYPE html>
  34. auto previous_quirks_mode = document().mode();
  35. const_cast<DOM::Document&>(document()).set_quirks_mode(DOM::QuirksMode::Yes);
  36. ScopeGuard reset_quirks_mode = [&] {
  37. const_cast<DOM::Document&>(document()).set_quirks_mode(previous_quirks_mode);
  38. };
  39. auto width_attribute = attribute(SVG::AttributeNames::width);
  40. auto parsing_context = CSS::Parser::ParsingContext { document() };
  41. if (auto width_value = parse_css_value(parsing_context, attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width)) {
  42. style.set_property(CSS::PropertyID::Width, width_value.release_nonnull());
  43. } else if (width_attribute == "") {
  44. // If the `width` attribute is an empty string, it defaults to 100%.
  45. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  46. // FIXME: Figure out what to do here.
  47. style.set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  48. }
  49. // Height defaults to 100%
  50. auto height_attribute = attribute(SVG::AttributeNames::height);
  51. if (auto height_value = parse_css_value(parsing_context, attribute(Web::HTML::AttributeNames::height), CSS::PropertyID::Height)) {
  52. style.set_property(CSS::PropertyID::Height, height_value.release_nonnull());
  53. } else if (height_attribute == "") {
  54. // If the `height` attribute is an empty string, it defaults to 100%.
  55. // This matches WebKit and Blink, but not Firefox. The spec is unclear.
  56. // FIXME: Figure out what to do here.
  57. style.set_property(CSS::PropertyID::Height, CSS::PercentageStyleValue::create(CSS::Percentage { 100 }));
  58. }
  59. }
  60. void SVGSVGElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  61. {
  62. SVGGraphicsElement::parse_attribute(name, value);
  63. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox))
  64. m_view_box = try_parse_view_box(value);
  65. if (name.equals_ignoring_ascii_case(SVG::AttributeNames::preserveAspectRatio))
  66. m_preserve_aspect_ratio = AttributeParser::parse_preserve_aspect_ratio(value);
  67. }
  68. }