HTMLImageElement.cpp 4.3 KB

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