SVGSVGElement.cpp 3.6 KB

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