HTMLImageElement.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/HTML/Parser/HTMLParser.h>
  14. #include <LibWeb/Layout/ImageBox.h>
  15. #include <LibWeb/Loader/ResourceLoader.h>
  16. #include <LibWeb/Painting/PaintableBox.h>
  17. namespace Web::HTML {
  18. HTMLImageElement::HTMLImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : HTMLElement(document, move(qualified_name))
  20. , m_image_loader(*this)
  21. {
  22. set_prototype(&window().cached_web_prototype("HTMLImageElement"));
  23. m_image_loader.on_load = [this] {
  24. set_needs_style_update(true);
  25. this->document().set_needs_layout();
  26. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  27. dispatch_event(*DOM::Event::create(this->document().window(), EventNames::load));
  28. });
  29. };
  30. m_image_loader.on_fail = [this] {
  31. dbgln("HTMLImageElement: Resource did fail: {}", src());
  32. set_needs_style_update(true);
  33. this->document().set_needs_layout();
  34. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  35. dispatch_event(*DOM::Event::create(this->document().window(), EventNames::error));
  36. });
  37. };
  38. m_image_loader.on_animate = [this] {
  39. if (layout_node())
  40. layout_node()->set_needs_display();
  41. };
  42. }
  43. HTMLImageElement::~HTMLImageElement() = default;
  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_dimension_value(value))
  49. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  50. } else if (name == HTML::AttributeNames::height) {
  51. if (auto parsed_value = parse_dimension_value(value))
  52. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  53. } else if (name == HTML::AttributeNames::hspace) {
  54. if (auto parsed_value = parse_dimension_value(value)) {
  55. style.set_property(CSS::PropertyID::MarginLeft, *parsed_value);
  56. style.set_property(CSS::PropertyID::MarginRight, *parsed_value);
  57. }
  58. } else if (name == HTML::AttributeNames::vspace) {
  59. if (auto parsed_value = parse_dimension_value(value)) {
  60. style.set_property(CSS::PropertyID::MarginTop, *parsed_value);
  61. style.set_property(CSS::PropertyID::MarginBottom, *parsed_value);
  62. }
  63. }
  64. });
  65. }
  66. void HTMLImageElement::parse_attribute(FlyString const& name, String const& value)
  67. {
  68. HTMLElement::parse_attribute(name, value);
  69. if (name == HTML::AttributeNames::src && !value.is_empty())
  70. m_image_loader.load(document().parse_url(value));
  71. }
  72. RefPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  73. {
  74. auto display = style->display();
  75. auto layout_node = adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
  76. if (display.is_block_outside())
  77. layout_node->set_inline(false);
  78. return layout_node;
  79. }
  80. Gfx::Bitmap const* HTMLImageElement::bitmap() const
  81. {
  82. return m_image_loader.bitmap(m_image_loader.current_frame_index());
  83. }
  84. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
  85. unsigned HTMLImageElement::width() const
  86. {
  87. const_cast<DOM::Document&>(document()).update_layout();
  88. // Return the rendered width of the image, in CSS pixels, if the image is being rendered.
  89. if (auto* paint_box = this->paint_box())
  90. return paint_box->content_width();
  91. // NOTE: This step seems to not be in the spec, but all browsers do it.
  92. auto width_attr = get_attribute(HTML::AttributeNames::width);
  93. if (auto converted = width_attr.to_uint(); converted.has_value())
  94. return *converted;
  95. // ...or else the density-corrected intrinsic width and height of the image, in CSS pixels,
  96. // if the image has intrinsic dimensions and is available but not being rendered.
  97. if (m_image_loader.has_image())
  98. return m_image_loader.width();
  99. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  100. return 0;
  101. }
  102. void HTMLImageElement::set_width(unsigned width)
  103. {
  104. set_attribute(HTML::AttributeNames::width, String::number(width));
  105. }
  106. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-height
  107. unsigned HTMLImageElement::height() const
  108. {
  109. const_cast<DOM::Document&>(document()).update_layout();
  110. // Return the rendered height of the image, in CSS pixels, if the image is being rendered.
  111. if (auto* paint_box = this->paint_box())
  112. return paint_box->content_height();
  113. // NOTE: This step seems to not be in the spec, but all browsers do it.
  114. auto height_attr = get_attribute(HTML::AttributeNames::height);
  115. if (auto converted = height_attr.to_uint(); converted.has_value())
  116. return *converted;
  117. // ...or else the density-corrected intrinsic height and height of the image, in CSS pixels,
  118. // if the image has intrinsic dimensions and is available but not being rendered.
  119. if (m_image_loader.has_image())
  120. return m_image_loader.height();
  121. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  122. return 0;
  123. }
  124. void HTMLImageElement::set_height(unsigned height)
  125. {
  126. set_attribute(HTML::AttributeNames::height, String::number(height));
  127. }
  128. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalwidth
  129. unsigned HTMLImageElement::natural_width() const
  130. {
  131. // Return the density-corrected intrinsic width of the image, in CSS pixels,
  132. // if the image has intrinsic dimensions and is available.
  133. if (m_image_loader.has_image())
  134. return m_image_loader.width();
  135. // ...or else 0.
  136. return 0;
  137. }
  138. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalheight
  139. unsigned HTMLImageElement::natural_height() const
  140. {
  141. // Return the density-corrected intrinsic height of the image, in CSS pixels,
  142. // if the image has intrinsic dimensions and is available.
  143. if (m_image_loader.has_image())
  144. return m_image_loader.height();
  145. // ...or else 0.
  146. return 0;
  147. }
  148. }