NavigableContainerViewport.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/Layout/NavigableContainerViewport.h>
  9. #include <LibWeb/Layout/Viewport.h>
  10. #include <LibWeb/Painting/NavigableContainerViewportPaintable.h>
  11. #include <LibWeb/SVG/SVGSVGElement.h>
  12. namespace Web::Layout {
  13. GC_DEFINE_ALLOCATOR(NavigableContainerViewport);
  14. NavigableContainerViewport::NavigableContainerViewport(DOM::Document& document, HTML::NavigableContainer& element, CSS::StyleProperties style)
  15. : ReplacedBox(document, element, move(style))
  16. {
  17. }
  18. NavigableContainerViewport::~NavigableContainerViewport() = default;
  19. void NavigableContainerViewport::prepare_for_replaced_layout()
  20. {
  21. if (auto const* content_document = dom_node().content_document_without_origin_check()) {
  22. if (auto const* root_element = content_document->document_element(); root_element && root_element->is_svg_svg_element()) {
  23. auto natural_metrics = SVG::SVGSVGElement::negotiate_natural_metrics(static_cast<SVG::SVGSVGElement const&>(*root_element));
  24. set_natural_width(natural_metrics.width);
  25. set_natural_height(natural_metrics.height);
  26. set_natural_aspect_ratio(natural_metrics.aspect_ratio);
  27. return;
  28. }
  29. }
  30. // FIXME: Do proper error checking, etc.
  31. set_natural_width(dom_node().get_attribute_value(HTML::AttributeNames::width).to_number<int>().value_or(300));
  32. set_natural_height(dom_node().get_attribute_value(HTML::AttributeNames::height).to_number<int>().value_or(150));
  33. }
  34. void NavigableContainerViewport::did_set_content_size()
  35. {
  36. ReplacedBox::did_set_content_size();
  37. if (dom_node().content_navigable())
  38. dom_node().content_navigable()->set_viewport_size(paintable_box()->content_size());
  39. }
  40. GC::Ptr<Painting::Paintable> NavigableContainerViewport::create_paintable() const
  41. {
  42. return Painting::NavigableContainerViewportPaintable::create(*this);
  43. }
  44. }