HTMLImageElement.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/CSS/StyleComputer.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/HTML/EventNames.h>
  12. #include <LibWeb/HTML/HTMLImageElement.h>
  13. #include <LibWeb/Layout/ImageBox.h>
  14. #include <LibWeb/Loader/ResourceLoader.h>
  15. #include <LibWeb/Painting/PaintableBox.h>
  16. namespace Web::HTML {
  17. HTMLImageElement::HTMLImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : FormAssociatedElement(document, move(qualified_name))
  19. , m_image_loader(*this)
  20. {
  21. m_image_loader.on_load = [this] {
  22. set_needs_style_update(true);
  23. this->document().set_needs_layout();
  24. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  25. dispatch_event(DOM::Event::create(EventNames::load));
  26. });
  27. };
  28. m_image_loader.on_fail = [this] {
  29. dbgln("HTMLImageElement: Resource did fail: {}", src());
  30. set_needs_style_update(true);
  31. this->document().set_needs_layout();
  32. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  33. dispatch_event(DOM::Event::create(EventNames::error));
  34. });
  35. };
  36. m_image_loader.on_animate = [this] {
  37. if (layout_node())
  38. layout_node()->set_needs_display();
  39. };
  40. }
  41. HTMLImageElement::~HTMLImageElement()
  42. {
  43. }
  44. void HTMLImageElement::apply_presentational_hints(CSS::StyleProperties& style) const
  45. {
  46. for_each_attribute([&](auto& name, auto& value) {
  47. if (name == HTML::AttributeNames::width) {
  48. if (auto parsed_value = parse_html_length(document(), value)) {
  49. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  50. }
  51. } else if (name == HTML::AttributeNames::height) {
  52. if (auto parsed_value = parse_html_length(document(), value)) {
  53. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  54. }
  55. }
  56. });
  57. }
  58. void HTMLImageElement::parse_attribute(const FlyString& name, const String& value)
  59. {
  60. HTMLElement::parse_attribute(name, value);
  61. if (name == HTML::AttributeNames::src && !value.is_empty())
  62. m_image_loader.load(document().parse_url(value));
  63. }
  64. RefPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  65. {
  66. return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
  67. }
  68. const Gfx::Bitmap* HTMLImageElement::bitmap() const
  69. {
  70. return m_image_loader.bitmap(m_image_loader.current_frame_index());
  71. }
  72. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
  73. unsigned HTMLImageElement::width() const
  74. {
  75. const_cast<DOM::Document&>(document()).update_layout();
  76. // Return the rendered width of the image, in CSS pixels, if the image is being rendered.
  77. if (auto* paint_box = this->paint_box())
  78. return paint_box->content_width();
  79. // ...or else the density-corrected intrinsic width and height of the image, in CSS pixels,
  80. // if the image has intrinsic dimensions and is available but not being rendered.
  81. if (m_image_loader.has_image())
  82. return m_image_loader.width();
  83. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  84. return 0;
  85. }
  86. void HTMLImageElement::set_width(unsigned width)
  87. {
  88. set_attribute(HTML::AttributeNames::width, String::number(width));
  89. }
  90. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-height
  91. unsigned HTMLImageElement::height() const
  92. {
  93. const_cast<DOM::Document&>(document()).update_layout();
  94. // Return the rendered height of the image, in CSS pixels, if the image is being rendered.
  95. if (auto* paint_box = this->paint_box())
  96. return paint_box->content_height();
  97. // ...or else the density-corrected intrinsic height and height of the image, in CSS pixels,
  98. // if the image has intrinsic dimensions and is available but not being rendered.
  99. if (m_image_loader.has_image())
  100. return m_image_loader.height();
  101. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  102. return 0;
  103. }
  104. void HTMLImageElement::set_height(unsigned height)
  105. {
  106. set_attribute(HTML::AttributeNames::height, String::number(height));
  107. }
  108. }