HTMLImageElement.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/ARIA/Roles.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/StyleComputer.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Event.h>
  12. #include <LibWeb/HTML/EventNames.h>
  13. #include <LibWeb/HTML/HTMLImageElement.h>
  14. #include <LibWeb/HTML/Parser/HTMLParser.h>
  15. #include <LibWeb/Layout/ImageBox.h>
  16. #include <LibWeb/Loader/ResourceLoader.h>
  17. #include <LibWeb/Painting/PaintableBox.h>
  18. namespace Web::HTML {
  19. HTMLImageElement::HTMLImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  20. : HTMLElement(document, move(qualified_name))
  21. , m_image_loader(*this)
  22. {
  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->realm(), EventNames::load).release_value_but_fixme_should_propagate_errors());
  28. m_load_event_delayer.clear();
  29. });
  30. };
  31. m_image_loader.on_fail = [this] {
  32. dbgln("HTMLImageElement: Resource did fail: {}", src());
  33. set_needs_style_update(true);
  34. this->document().set_needs_layout();
  35. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  36. dispatch_event(DOM::Event::create(this->realm(), EventNames::error).release_value_but_fixme_should_propagate_errors());
  37. m_load_event_delayer.clear();
  38. });
  39. };
  40. m_image_loader.on_animate = [this] {
  41. if (layout_node())
  42. layout_node()->set_needs_display();
  43. };
  44. }
  45. HTMLImageElement::~HTMLImageElement() = default;
  46. JS::ThrowCompletionOr<void> HTMLImageElement::initialize(JS::Realm& realm)
  47. {
  48. MUST_OR_THROW_OOM(Base::initialize(realm));
  49. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLImageElementPrototype>(realm, "HTMLImageElement"));
  50. return {};
  51. }
  52. void HTMLImageElement::apply_presentational_hints(CSS::StyleProperties& style) const
  53. {
  54. for_each_attribute([&](auto& name, auto& value) {
  55. if (name == HTML::AttributeNames::width) {
  56. if (auto parsed_value = parse_dimension_value(value))
  57. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  58. } else if (name == HTML::AttributeNames::height) {
  59. if (auto parsed_value = parse_dimension_value(value))
  60. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  61. } else if (name == HTML::AttributeNames::hspace) {
  62. if (auto parsed_value = parse_dimension_value(value)) {
  63. style.set_property(CSS::PropertyID::MarginLeft, *parsed_value);
  64. style.set_property(CSS::PropertyID::MarginRight, *parsed_value);
  65. }
  66. } else if (name == HTML::AttributeNames::vspace) {
  67. if (auto parsed_value = parse_dimension_value(value)) {
  68. style.set_property(CSS::PropertyID::MarginTop, *parsed_value);
  69. style.set_property(CSS::PropertyID::MarginBottom, *parsed_value);
  70. }
  71. }
  72. });
  73. }
  74. void HTMLImageElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  75. {
  76. HTMLElement::parse_attribute(name, value);
  77. if (name == HTML::AttributeNames::src && !value.is_empty()) {
  78. m_image_loader.load(document().parse_url(value));
  79. m_load_event_delayer.emplace(document());
  80. }
  81. if (name == HTML::AttributeNames::alt) {
  82. if (layout_node())
  83. verify_cast<Layout::ImageBox>(*layout_node()).dom_node_did_update_alt_text({});
  84. }
  85. }
  86. JS::GCPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  87. {
  88. return heap().allocate_without_realm<Layout::ImageBox>(document(), *this, move(style), m_image_loader);
  89. }
  90. Gfx::Bitmap const* HTMLImageElement::bitmap() const
  91. {
  92. return m_image_loader.bitmap(m_image_loader.current_frame_index());
  93. }
  94. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
  95. unsigned HTMLImageElement::width() const
  96. {
  97. const_cast<DOM::Document&>(document()).update_layout();
  98. // Return the rendered width of the image, in CSS pixels, if the image is being rendered.
  99. if (auto* paint_box = this->paint_box())
  100. return paint_box->content_width().value();
  101. // NOTE: This step seems to not be in the spec, but all browsers do it.
  102. auto width_attr = get_attribute(HTML::AttributeNames::width);
  103. if (auto converted = width_attr.to_uint(); converted.has_value())
  104. return *converted;
  105. // ...or else the density-corrected intrinsic width and height of the image, in CSS pixels,
  106. // if the image has intrinsic dimensions and is available but not being rendered.
  107. if (m_image_loader.has_image())
  108. return m_image_loader.width();
  109. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  110. return 0;
  111. }
  112. void HTMLImageElement::set_width(unsigned width)
  113. {
  114. MUST(set_attribute(HTML::AttributeNames::width, DeprecatedString::number(width)));
  115. }
  116. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-height
  117. unsigned HTMLImageElement::height() const
  118. {
  119. const_cast<DOM::Document&>(document()).update_layout();
  120. // Return the rendered height of the image, in CSS pixels, if the image is being rendered.
  121. if (auto* paint_box = this->paint_box())
  122. return paint_box->content_height().value();
  123. // NOTE: This step seems to not be in the spec, but all browsers do it.
  124. auto height_attr = get_attribute(HTML::AttributeNames::height);
  125. if (auto converted = height_attr.to_uint(); converted.has_value())
  126. return *converted;
  127. // ...or else the density-corrected intrinsic height and height of the image, in CSS pixels,
  128. // if the image has intrinsic dimensions and is available but not being rendered.
  129. if (m_image_loader.has_image())
  130. return m_image_loader.height();
  131. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  132. return 0;
  133. }
  134. void HTMLImageElement::set_height(unsigned height)
  135. {
  136. MUST(set_attribute(HTML::AttributeNames::height, DeprecatedString::number(height)));
  137. }
  138. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalwidth
  139. unsigned HTMLImageElement::natural_width() const
  140. {
  141. // Return the density-corrected intrinsic width 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.width();
  145. // ...or else 0.
  146. return 0;
  147. }
  148. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalheight
  149. unsigned HTMLImageElement::natural_height() const
  150. {
  151. // Return the density-corrected intrinsic height of the image, in CSS pixels,
  152. // if the image has intrinsic dimensions and is available.
  153. if (m_image_loader.has_image())
  154. return m_image_loader.height();
  155. // ...or else 0.
  156. return 0;
  157. }
  158. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
  159. bool HTMLImageElement::complete() const
  160. {
  161. // The IDL attribute complete must return true if any of the following conditions is true:
  162. // - Both the src attribute and the srcset attribute are omitted.
  163. if (!has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::srcset))
  164. return true;
  165. // - The srcset attribute is omitted and the src attribute's value is the empty string.
  166. if (!has_attribute(HTML::AttributeNames::srcset) && attribute(HTML::AttributeNames::src) == ""sv)
  167. return true;
  168. // - The img element's current request's state is completely available and its pending request is null.
  169. // - The img element's current request's state is broken and its pending request is null.
  170. // FIXME: This is ad-hoc and should be updated once we are loading images via the Fetch mechanism.
  171. if (m_image_loader.has_loaded_or_failed())
  172. return true;
  173. return false;
  174. }
  175. Optional<ARIA::Role> HTMLImageElement::default_role() const
  176. {
  177. // https://www.w3.org/TR/html-aria/#el-img
  178. // https://www.w3.org/TR/html-aria/#el-img-no-alt
  179. if (alt().is_null() || !alt().is_empty())
  180. return ARIA::Role::img;
  181. // https://www.w3.org/TR/html-aria/#el-img-empty-alt
  182. return ARIA::Role::presentation;
  183. }
  184. }