HTMLImageElement.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/Timer.h>
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibWeb/ARIA/Roles.h>
  9. #include <LibWeb/CSS/Parser/Parser.h>
  10. #include <LibWeb/CSS/StyleComputer.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/Fetch/Fetching/Fetching.h>
  14. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  15. #include <LibWeb/Fetch/Response.h>
  16. #include <LibWeb/HTML/CORSSettingAttribute.h>
  17. #include <LibWeb/HTML/DecodedImageData.h>
  18. #include <LibWeb/HTML/EventNames.h>
  19. #include <LibWeb/HTML/HTMLImageElement.h>
  20. #include <LibWeb/HTML/HTMLLinkElement.h>
  21. #include <LibWeb/HTML/HTMLPictureElement.h>
  22. #include <LibWeb/HTML/HTMLSourceElement.h>
  23. #include <LibWeb/HTML/ImageRequest.h>
  24. #include <LibWeb/HTML/ListOfAvailableImages.h>
  25. #include <LibWeb/HTML/Parser/HTMLParser.h>
  26. #include <LibWeb/HTML/PotentialCORSRequest.h>
  27. #include <LibWeb/Layout/ImageBox.h>
  28. #include <LibWeb/Loader/ResourceLoader.h>
  29. #include <LibWeb/Painting/PaintableBox.h>
  30. #include <LibWeb/Platform/ImageCodecPlugin.h>
  31. namespace Web::HTML {
  32. HTMLImageElement::HTMLImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  33. : HTMLElement(document, move(qualified_name))
  34. {
  35. m_animation_timer = Core::Timer::try_create().release_value_but_fixme_should_propagate_errors();
  36. m_animation_timer->on_timeout = [this] { animate(); };
  37. }
  38. HTMLImageElement::~HTMLImageElement() = default;
  39. JS::ThrowCompletionOr<void> HTMLImageElement::initialize(JS::Realm& realm)
  40. {
  41. MUST_OR_THROW_OOM(Base::initialize(realm));
  42. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLImageElementPrototype>(realm, "HTMLImageElement"));
  43. m_current_request = TRY_OR_THROW_OOM(vm(), ImageRequest::create());
  44. return {};
  45. }
  46. void HTMLImageElement::apply_presentational_hints(CSS::StyleProperties& style) const
  47. {
  48. for_each_attribute([&](auto& name, auto& value) {
  49. if (name == HTML::AttributeNames::width) {
  50. if (auto parsed_value = parse_dimension_value(value))
  51. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  52. } else if (name == HTML::AttributeNames::height) {
  53. if (auto parsed_value = parse_dimension_value(value))
  54. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  55. } else if (name == HTML::AttributeNames::hspace) {
  56. if (auto parsed_value = parse_dimension_value(value)) {
  57. style.set_property(CSS::PropertyID::MarginLeft, *parsed_value);
  58. style.set_property(CSS::PropertyID::MarginRight, *parsed_value);
  59. }
  60. } else if (name == HTML::AttributeNames::vspace) {
  61. if (auto parsed_value = parse_dimension_value(value)) {
  62. style.set_property(CSS::PropertyID::MarginTop, *parsed_value);
  63. style.set_property(CSS::PropertyID::MarginBottom, *parsed_value);
  64. }
  65. }
  66. });
  67. }
  68. void HTMLImageElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  69. {
  70. HTMLElement::parse_attribute(name, value);
  71. if (name == HTML::AttributeNames::crossorigin) {
  72. m_cors_setting = cors_setting_attribute_from_keyword(String::from_deprecated_string(value).release_value_but_fixme_should_propagate_errors());
  73. }
  74. if (name.is_one_of(HTML::AttributeNames::src, HTML::AttributeNames::srcset)) {
  75. update_the_image_data(true).release_value_but_fixme_should_propagate_errors();
  76. }
  77. if (name == HTML::AttributeNames::alt) {
  78. if (layout_node())
  79. verify_cast<Layout::ImageBox>(*layout_node()).dom_node_did_update_alt_text({});
  80. }
  81. }
  82. void HTMLImageElement::did_remove_attribute(DeprecatedFlyString const& name)
  83. {
  84. Base::did_remove_attribute(name);
  85. if (name == HTML::AttributeNames::crossorigin) {
  86. m_cors_setting = CORSSettingAttribute::NoCORS;
  87. }
  88. }
  89. JS::GCPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  90. {
  91. return heap().allocate_without_realm<Layout::ImageBox>(document(), *this, move(style), *this);
  92. }
  93. RefPtr<Gfx::Bitmap const> HTMLImageElement::bitmap() const
  94. {
  95. return current_image_bitmap();
  96. }
  97. RefPtr<Gfx::Bitmap const> HTMLImageElement::current_image_bitmap() const
  98. {
  99. if (auto data = m_current_request->image_data())
  100. return data->bitmap(m_current_frame_index);
  101. return nullptr;
  102. }
  103. void HTMLImageElement::set_visible_in_viewport(bool)
  104. {
  105. // FIXME: Loosen grip on image data when it's not visible, e.g via volatile memory.
  106. }
  107. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
  108. unsigned HTMLImageElement::width() const
  109. {
  110. const_cast<DOM::Document&>(document()).update_layout();
  111. // Return the rendered width of the image, in CSS pixels, if the image is being rendered.
  112. if (auto* paintable_box = this->paintable_box())
  113. return paintable_box->content_width().value();
  114. // NOTE: This step seems to not be in the spec, but all browsers do it.
  115. auto width_attr = get_attribute(HTML::AttributeNames::width);
  116. if (auto converted = width_attr.to_uint(); converted.has_value())
  117. return *converted;
  118. // ...or else the density-corrected intrinsic width and height of the image, in CSS pixels,
  119. // if the image has intrinsic dimensions and is available but not being rendered.
  120. if (auto bitmap = current_image_bitmap())
  121. return bitmap->width();
  122. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  123. return 0;
  124. }
  125. void HTMLImageElement::set_width(unsigned width)
  126. {
  127. MUST(set_attribute(HTML::AttributeNames::width, DeprecatedString::number(width)));
  128. }
  129. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-height
  130. unsigned HTMLImageElement::height() const
  131. {
  132. const_cast<DOM::Document&>(document()).update_layout();
  133. // Return the rendered height of the image, in CSS pixels, if the image is being rendered.
  134. if (auto* paintable_box = this->paintable_box())
  135. return paintable_box->content_height().value();
  136. // NOTE: This step seems to not be in the spec, but all browsers do it.
  137. auto height_attr = get_attribute(HTML::AttributeNames::height);
  138. if (auto converted = height_attr.to_uint(); converted.has_value())
  139. return *converted;
  140. // ...or else the density-corrected intrinsic height and height of the image, in CSS pixels,
  141. // if the image has intrinsic dimensions and is available but not being rendered.
  142. if (auto bitmap = current_image_bitmap())
  143. return bitmap->height();
  144. // ...or else 0, if the image is not available or does not have intrinsic dimensions.
  145. return 0;
  146. }
  147. void HTMLImageElement::set_height(unsigned height)
  148. {
  149. MUST(set_attribute(HTML::AttributeNames::height, DeprecatedString::number(height)));
  150. }
  151. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalwidth
  152. unsigned HTMLImageElement::natural_width() const
  153. {
  154. // Return the density-corrected intrinsic width of the image, in CSS pixels,
  155. // if the image has intrinsic dimensions and is available.
  156. if (auto bitmap = current_image_bitmap())
  157. return bitmap->width();
  158. // ...or else 0.
  159. return 0;
  160. }
  161. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalheight
  162. unsigned HTMLImageElement::natural_height() const
  163. {
  164. // Return the density-corrected intrinsic height of the image, in CSS pixels,
  165. // if the image has intrinsic dimensions and is available.
  166. if (auto bitmap = current_image_bitmap())
  167. return bitmap->height();
  168. // ...or else 0.
  169. return 0;
  170. }
  171. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
  172. bool HTMLImageElement::complete() const
  173. {
  174. // The IDL attribute complete must return true if any of the following conditions is true:
  175. // - Both the src attribute and the srcset attribute are omitted.
  176. if (!has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::srcset))
  177. return true;
  178. // - The srcset attribute is omitted and the src attribute's value is the empty string.
  179. if (!has_attribute(HTML::AttributeNames::srcset) && attribute(HTML::AttributeNames::src) == ""sv)
  180. return true;
  181. // - The img element's current request's state is completely available and its pending request is null.
  182. // - The img element's current request's state is broken and its pending request is null.
  183. // FIXME: This is ad-hoc and should be updated once we are loading images via the Fetch mechanism.
  184. if (auto bitmap = current_image_bitmap())
  185. return true;
  186. return false;
  187. }
  188. Optional<ARIA::Role> HTMLImageElement::default_role() const
  189. {
  190. // https://www.w3.org/TR/html-aria/#el-img
  191. // https://www.w3.org/TR/html-aria/#el-img-no-alt
  192. if (alt().is_null() || !alt().is_empty())
  193. return ARIA::Role::img;
  194. // https://www.w3.org/TR/html-aria/#el-img-empty-alt
  195. return ARIA::Role::presentation;
  196. }
  197. // https://html.spec.whatwg.org/multipage/images.html#use-srcset-or-picture
  198. bool HTMLImageElement::uses_srcset_or_picture() const
  199. {
  200. // An img element is said to use srcset or picture if it has a srcset attribute specified
  201. // or if it has a parent that is a picture element.
  202. return has_attribute(HTML::AttributeNames::srcset) || (parent() && is<HTMLPictureElement>(*parent()));
  203. }
  204. // https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
  205. ErrorOr<void> HTMLImageElement::update_the_image_data(bool restart_animations)
  206. {
  207. // 1. If the element's node document is not fully active, then:
  208. if (!document().is_fully_active()) {
  209. // FIXME: 1. Continue running this algorithm in parallel.
  210. // FIXME: 2. Wait until the element's node document is fully active.
  211. // FIXME: 3. If another instance of this algorithm for this img element was started after this instance
  212. // (even if it aborted and is no longer running), then return.
  213. // FIXME: 4. Queue a microtask to continue this algorithm.
  214. }
  215. // 2. FIXME: If the user agent cannot support images, or its support for images has been disabled,
  216. // then abort the image request for the current request and the pending request,
  217. // set current request's state to unavailable, set pending request to null, and return.
  218. // 3. Let selected source be null and selected pixel density be undefined.
  219. Optional<String> selected_source;
  220. Optional<float> selected_pixel_density;
  221. // 4. If the element does not use srcset or picture
  222. // and it has a src attribute specified whose value is not the empty string,
  223. // then set selected source to the value of the element's src attribute
  224. // and set selected pixel density to 1.0.
  225. if (!uses_srcset_or_picture() && has_attribute(HTML::AttributeNames::src) && !attribute(HTML::AttributeNames::src).is_empty()) {
  226. selected_source = TRY(String::from_deprecated_string(attribute(HTML::AttributeNames::src)));
  227. selected_pixel_density = 1.0f;
  228. }
  229. // 5. Set the element's last selected source to selected source.
  230. m_last_selected_source = selected_source;
  231. // 6. If selected source is not null, then:
  232. if (selected_source.has_value()) {
  233. // 1. Parse selected source, relative to the element's node document.
  234. // If that is not successful, then abort this inner set of steps.
  235. // Otherwise, let urlString be the resulting URL string.
  236. auto url_string = document().parse_url(selected_source.value().to_deprecated_string());
  237. if (!url_string.is_valid())
  238. goto after_step_6;
  239. // 2. Let key be a tuple consisting of urlString, the img element's crossorigin attribute's mode,
  240. // and, if that mode is not No CORS, the node document's origin.
  241. ListOfAvailableImages::Key key;
  242. key.url = url_string;
  243. key.mode = m_cors_setting;
  244. key.origin = document().origin();
  245. // 3. If the list of available images contains an entry for key, then:
  246. if (auto entry = document().list_of_available_images().get(key)) {
  247. // 1. Set the ignore higher-layer caching flag for that entry.
  248. entry->ignore_higher_layer_caching = true;
  249. // 2. Abort the image request for the current request and the pending request.
  250. m_current_request->abort(realm());
  251. // FIXME: Spec bug? Seems like pending request can be null here.
  252. if (m_pending_request)
  253. m_pending_request->abort(realm());
  254. // 3. Set pending request to null.
  255. m_pending_request = nullptr;
  256. // 4. Let current request be a new image request whose image data is that of the entry and whose state is completely available.
  257. m_current_request = ImageRequest::create().release_value_but_fixme_should_propagate_errors();
  258. m_current_request->set_image_data(entry->image_data);
  259. m_current_request->set_state(ImageRequest::State::CompletelyAvailable);
  260. // 5. Prepare current request for presentation given img.
  261. m_current_request->prepare_for_presentation(*this);
  262. // 6. Set current request's current pixel density to selected pixel density.
  263. // FIXME: Spec bug! `selected_pixel_density` can be undefined here, per the spec.
  264. // That's why we value_or(1.0f) it.
  265. m_current_request->set_current_pixel_density(selected_pixel_density.value_or(1.0f));
  266. // 7. Queue an element task on the DOM manipulation task source given the img element and following steps:
  267. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this, restart_animations, url_string] {
  268. // 1. If restart animation is set, then restart the animation.
  269. if (restart_animations)
  270. restart_the_animation();
  271. // 2. Set current request's current URL to urlString.
  272. m_current_request->set_current_url(url_string);
  273. // 3. Fire an event named load at the img element.
  274. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load).release_value_but_fixme_should_propagate_errors());
  275. });
  276. // 8. Abort the update the image data algorithm.
  277. return {};
  278. }
  279. }
  280. after_step_6:
  281. // 7. Queue a microtask to perform the rest of this algorithm, allowing the task that invoked this algorithm to continue.
  282. queue_a_microtask(&document(), [this, restart_animations]() mutable {
  283. // FIXME: 8. If another instance of this algorithm for this img element was started after this instance
  284. // (even if it aborted and is no longer running), then return.
  285. // 9. Let selected source and selected pixel density be
  286. // the URL and pixel density that results from selecting an image source, respectively.
  287. Optional<ImageSource> selected_source;
  288. Optional<float> pixel_density;
  289. if (auto result = select_an_image_source(); result.has_value()) {
  290. selected_source = result.value().source;
  291. pixel_density = result.value().pixel_density;
  292. }
  293. // 10. If selected source is null, then:
  294. if (!selected_source.has_value()) {
  295. // 1. Set the current request's state to broken,
  296. // abort the image request for the current request and the pending request,
  297. // and set pending request to null.
  298. m_current_request->set_state(ImageRequest::State::Broken);
  299. m_current_request->abort(realm());
  300. // FIXME: Spec bug? Seems like the image's pending request can be null here.
  301. if (m_pending_request)
  302. m_pending_request->abort(realm());
  303. m_pending_request = nullptr;
  304. // 2. Queue an element task on the DOM manipulation task source given the img element and the following steps:
  305. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  306. // 1. Change the current request's current URL to the empty string.
  307. m_current_request->set_current_url(""sv);
  308. // 2. If the element has a src attribute or it uses srcset or picture, fire an event named error at the img element.
  309. if (has_attribute(HTML::AttributeNames::src) || uses_srcset_or_picture()) {
  310. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors());
  311. }
  312. });
  313. // 3. Return.
  314. return;
  315. }
  316. // 11. Parse selected source, relative to the element's node document, and let urlString be the resulting URL string.
  317. auto url_string = document().parse_url(selected_source.value().url.to_deprecated_string());
  318. // If that is not successful, then:
  319. if (!url_string.is_valid()) {
  320. // 1. Abort the image request for the current request and the pending request.
  321. m_current_request->abort(realm());
  322. m_pending_request->abort(realm());
  323. // 2. Set the current request's state to broken.
  324. m_current_request->set_state(ImageRequest::State::Broken);
  325. // 3. Set pending request to null.
  326. m_pending_request = nullptr;
  327. // 4. Queue an element task on the DOM manipulation task source given the img element and the following steps:
  328. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this, selected_source] {
  329. // 1. Change the current request's current URL to selected source.
  330. m_current_request->set_current_url(selected_source.value().url);
  331. // 2. Fire an event named error at the img element.
  332. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors());
  333. });
  334. // 5. Return.
  335. return;
  336. }
  337. // 12. If the pending request is not null and urlString is the same as the pending request's current URL, then return.
  338. if (m_pending_request && url_string == m_pending_request->current_url())
  339. return;
  340. // 13. If urlString is the same as the current request's current URL and current request's state is partially available,
  341. // then abort the image request for the pending request,
  342. // queue an element task on the DOM manipulation task source given the img element
  343. // to restart the animation if restart animation is set, and return.
  344. if (url_string == m_current_request->current_url() && m_current_request->state() == ImageRequest::State::PartiallyAvailable) {
  345. m_pending_request->abort(realm());
  346. if (restart_animations) {
  347. queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {
  348. restart_the_animation();
  349. });
  350. }
  351. return;
  352. }
  353. // 14. If the pending request is not null, then abort the image request for the pending request.
  354. if (m_pending_request)
  355. m_pending_request->abort(realm());
  356. // 15. Set image request to a new image request whose current URL is urlString.
  357. auto image_request = ImageRequest::create().release_value_but_fixme_should_propagate_errors();
  358. image_request->set_current_url(url_string);
  359. // 16. If current request's state is unavailable or broken, then set the current request to image request.
  360. // Otherwise, set the pending request to image request.
  361. if (m_current_request->state() == ImageRequest::State::Unavailable || m_current_request->state() == ImageRequest::State::Broken)
  362. m_current_request = image_request;
  363. else
  364. m_pending_request = image_request;
  365. // 17. Let request be the result of creating a potential-CORS request given urlString, "image",
  366. // and the current state of the element's crossorigin content attribute.
  367. auto request = create_potential_CORS_request(vm(), url_string, Fetch::Infrastructure::Request::Destination::Image, m_cors_setting);
  368. // 18. Set request's client to the element's node document's relevant settings object.
  369. request->set_client(&document().relevant_settings_object());
  370. // 19. If the element uses srcset or picture, set request's initiator to "imageset".
  371. if (uses_srcset_or_picture())
  372. request->set_initiator(Fetch::Infrastructure::Request::Initiator::ImageSet);
  373. // 20. Set request's referrer policy to the current state of the element's referrerpolicy attribute.
  374. request->set_referrer_policy(ReferrerPolicy::from_string(attribute(HTML::AttributeNames::referrerpolicy)));
  375. // FIXME: 21. Set request's priority to the current state of the element's fetchpriority attribute.
  376. // FIXME: 22. Let delay load event be true if the img's lazy loading attribute is in the Eager state, or if scripting is disabled for the img, and false otherwise.
  377. // FIXME: 23. If the will lazy load element steps given the img return true, then:
  378. // FIXME: 1. Set the img's lazy load resumption steps to the rest of this algorithm starting with the step labeled fetch the image.
  379. // FIXME: 2. Start intersection-observing a lazy loading element for the img element.
  380. // FIXME: 3. Return.
  381. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  382. fetch_algorithms_input.process_response = [this, image_request, url_string](JS::NonnullGCPtr<Fetch::Infrastructure::Response> response) {
  383. // 25. As soon as possible, jump to the first applicable entry from the following list:
  384. // FIXME: - If the resource type is multipart/x-mixed-replace
  385. // - If the resource type and data corresponds to a supported image format, as described below
  386. // - The next task that is queued by the networking task source while the image is being fetched must run the following steps:
  387. queue_an_element_task(HTML::Task::Source::Networking, [this, response, image_request, url_string] {
  388. auto process_body = [image_request, url_string, this](ByteBuffer data) {
  389. handle_successful_fetch(url_string, image_request, move(data));
  390. };
  391. auto process_body_error = [this](auto&) {
  392. handle_failed_fetch();
  393. };
  394. // FIXME: See HTMLLinkElement::default_fetch_and_process_linked_resource for thorough notes on the workaround
  395. // added here for CORS cross-origin responses. The gist is that all cross-origin responses will have a
  396. // null bodyBytes. So we must read the actual body from the unsafe response.
  397. // https://github.com/whatwg/html/issues/9066
  398. if (response->is_cors_cross_origin() && !response->body().has_value() && response->unsafe_response()->body().has_value()) {
  399. auto unsafe_response = static_cast<Fetch::Infrastructure::OpaqueFilteredResponse const&>(*response).internal_response();
  400. unsafe_response->body()->fully_read(realm(), move(process_body), move(process_body_error), JS::NonnullGCPtr { realm().global_object() }).release_value_but_fixme_should_propagate_errors();
  401. } else if (response->body().has_value()) {
  402. response->body().value().fully_read(realm(), move(process_body), move(process_body_error), JS::NonnullGCPtr { realm().global_object() }).release_value_but_fixme_should_propagate_errors();
  403. }
  404. });
  405. };
  406. // 24. Fetch the image: Fetch request.
  407. // Return from this algorithm, and run the remaining steps as part of the fetch's processResponse for the response response.
  408. auto fetch_controller = Fetch::Fetching::fetch(
  409. realm(),
  410. request,
  411. Fetch::Infrastructure::FetchAlgorithms::create(vm(), move(fetch_algorithms_input)))
  412. .release_value_but_fixme_should_propagate_errors();
  413. image_request->set_fetch_controller(fetch_controller);
  414. });
  415. return {};
  416. }
  417. void HTMLImageElement::handle_successful_fetch(AK::URL const& url_string, ImageRequest& image_request, ByteBuffer data)
  418. {
  419. // AD-HOC: At this point, things gets very ad-hoc.
  420. // FIXME: Bring this closer to spec.
  421. auto result = Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes());
  422. if (!result.has_value()) {
  423. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors());
  424. return;
  425. }
  426. Vector<DecodedImageData::Frame> frames;
  427. for (auto& frame : result.value().frames) {
  428. frames.append(DecodedImageData::Frame {
  429. .bitmap = frame.bitmap,
  430. .duration = static_cast<int>(frame.duration),
  431. });
  432. }
  433. auto image_data = DecodedImageData::create(move(frames), result.value().loop_count, result.value().is_animated).release_value_but_fixme_should_propagate_errors();
  434. image_request.set_image_data(image_data);
  435. ListOfAvailableImages::Key key;
  436. key.url = url_string;
  437. key.mode = m_cors_setting;
  438. key.origin = document().origin();
  439. // 1. If image request is the pending request, abort the image request for the current request,
  440. // upgrade the pending request to the current request
  441. // and prepare image request for presentation given the img element.
  442. if (image_request == m_pending_request) {
  443. m_current_request->abort(realm());
  444. upgrade_pending_request_to_current_request();
  445. image_request.prepare_for_presentation(*this);
  446. }
  447. // 2. Set image request to the completely available state.
  448. image_request.set_state(ImageRequest::State::CompletelyAvailable);
  449. // 3. Add the image to the list of available images using the key key, with the ignore higher-layer caching flag set.
  450. document().list_of_available_images().add(key, image_data, true).release_value_but_fixme_should_propagate_errors();
  451. // 4. Fire an event named load at the img element.
  452. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load).release_value_but_fixme_should_propagate_errors());
  453. set_needs_style_update(true);
  454. document().set_needs_layout();
  455. if (image_data->is_animated() && image_data->frame_count() > 1) {
  456. m_current_frame_index = 0;
  457. m_animation_timer->set_interval(image_data->frame_duration(0));
  458. m_animation_timer->start();
  459. }
  460. }
  461. // https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
  462. void HTMLImageElement::upgrade_pending_request_to_current_request()
  463. {
  464. // 1. Let the img element's current request be the pending request.
  465. VERIFY(m_pending_request);
  466. m_current_request = m_pending_request;
  467. // 2. Let the img element's pending request be null.
  468. m_pending_request = nullptr;
  469. }
  470. void HTMLImageElement::handle_failed_fetch()
  471. {
  472. // AD-HOC
  473. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors());
  474. }
  475. // https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
  476. void HTMLImageElement::restart_the_animation()
  477. {
  478. m_current_frame_index = 0;
  479. m_animation_timer->start();
  480. }
  481. // https://html.spec.whatwg.org/multipage/images.html#update-the-source-set
  482. static void update_the_source_set(DOM::Element& element)
  483. {
  484. // When asked to update the source set for a given img or link element el, user agents must do the following:
  485. VERIFY(is<HTMLImageElement>(element) || is<HTMLLinkElement>(element));
  486. // 1. Set el's source set to an empty source set.
  487. if (is<HTMLImageElement>(element))
  488. static_cast<HTMLImageElement&>(element).set_source_set(SourceSet {});
  489. else if (is<HTMLLinkElement>(element))
  490. TODO();
  491. // 2. Let elements be « el ».
  492. JS::MarkedVector<DOM::Element*> elements(element.heap());
  493. elements.append(&element);
  494. // 3. If el is an img element whose parent node is a picture element,
  495. // then replace the contents of elements with el's parent node's child elements, retaining relative order.
  496. if (is<HTMLImageElement>(element) && element.parent() && is<HTMLPictureElement>(*element.parent())) {
  497. elements.clear();
  498. element.parent()->for_each_child_of_type<DOM::Element>([&](auto& child) {
  499. elements.append(&child);
  500. });
  501. }
  502. // 4. For each child in elements:
  503. for (auto child : elements) {
  504. // 1. If child is el:
  505. if (child == &element) {
  506. // 1. Let default source be the empty string.
  507. String default_source;
  508. // 2. Let srcset be the empty string.
  509. String srcset;
  510. // 3. Let sizes be the empty string.
  511. String sizes;
  512. // 4. If el is an img element that has a srcset attribute, then set srcset to that attribute's value.
  513. if (is<HTMLImageElement>(element)) {
  514. if (auto srcset_value = element.attribute(HTML::AttributeNames::srcset); !srcset_value.is_null())
  515. srcset = String::from_deprecated_string(srcset_value).release_value_but_fixme_should_propagate_errors();
  516. }
  517. // 5. Otherwise, if el is a link element that has an imagesrcset attribute, then set srcset to that attribute's value.
  518. else if (is<HTMLLinkElement>(element)) {
  519. if (auto imagesrcset_value = element.attribute(HTML::AttributeNames::imagesrcset); !imagesrcset_value.is_null())
  520. srcset = String::from_deprecated_string(imagesrcset_value).release_value_but_fixme_should_propagate_errors();
  521. }
  522. // 6. If el is an img element that has a sizes attribute, then set sizes to that attribute's value.
  523. if (is<HTMLImageElement>(element)) {
  524. if (auto sizes_value = element.attribute(HTML::AttributeNames::sizes); !sizes_value.is_null())
  525. sizes = String::from_deprecated_string(sizes_value).release_value_but_fixme_should_propagate_errors();
  526. }
  527. // 7. Otherwise, if el is a link element that has an imagesizes attribute, then set sizes to that attribute's value.
  528. else if (is<HTMLLinkElement>(element)) {
  529. if (auto imagesizes_value = element.attribute(HTML::AttributeNames::imagesizes); !imagesizes_value.is_null())
  530. sizes = String::from_deprecated_string(imagesizes_value).release_value_but_fixme_should_propagate_errors();
  531. }
  532. // 8. If el is an img element that has a src attribute, then set default source to that attribute's value.
  533. if (is<HTMLImageElement>(element)) {
  534. if (auto src_value = element.attribute(HTML::AttributeNames::src); !src_value.is_null())
  535. default_source = String::from_deprecated_string(src_value).release_value_but_fixme_should_propagate_errors();
  536. }
  537. // 9. Otherwise, if el is a link element that has an href attribute, then set default source to that attribute's value.
  538. else if (is<HTMLLinkElement>(element)) {
  539. if (auto href_value = element.attribute(HTML::AttributeNames::href); !href_value.is_null())
  540. default_source = String::from_deprecated_string(href_value).release_value_but_fixme_should_propagate_errors();
  541. }
  542. // 10. Let el's source set be the result of creating a source set given default source, srcset, and sizes.
  543. if (is<HTMLImageElement>(element))
  544. static_cast<HTMLImageElement&>(element).set_source_set(SourceSet::create(element.document(), default_source, srcset, sizes));
  545. else if (is<HTMLLinkElement>(element))
  546. TODO();
  547. return;
  548. }
  549. // 2. If child is not a source element, then continue.
  550. if (!is<HTMLSourceElement>(child))
  551. continue;
  552. // 3. If child does not have a srcset attribute, continue to the next child.
  553. if (!child->has_attribute(HTML::AttributeNames::srcset))
  554. continue;
  555. // 4. Parse child's srcset attribute and let the returned source set be source set.
  556. auto source_set = parse_a_srcset_attribute(child->attribute(HTML::AttributeNames::srcset));
  557. // 5. If source set has zero image sources, continue to the next child.
  558. if (source_set.is_empty())
  559. continue;
  560. // FIXME: 6. If child has a media attribute, and its value does not match the environment, continue to the next child.
  561. // 7. Parse child's sizes attribute, and let source set's source size be the returned value.
  562. source_set.m_source_size = parse_a_sizes_attribute(element.document(), child->attribute(HTML::AttributeNames::sizes));
  563. // FIXME: 8. If child has a type attribute, and its value is an unknown or unsupported MIME type, continue to the next child.
  564. if (child->has_attribute(HTML::AttributeNames::type)) {
  565. }
  566. // FIXME: 9. If child has width or height attributes, set el's dimension attribute source to child.
  567. // Otherwise, set el's dimension attribute source to el.
  568. // 10. Normalize the source densities of source set.
  569. source_set.normalize_source_densities();
  570. // 11. Let el's source set be source set.
  571. if (is<HTMLImageElement>(element))
  572. static_cast<HTMLImageElement&>(element).set_source_set(move(source_set));
  573. else if (is<HTMLLinkElement>(element))
  574. TODO();
  575. // 12. Return.
  576. return;
  577. }
  578. }
  579. // https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
  580. Optional<ImageSourceAndPixelDensity> HTMLImageElement::select_an_image_source()
  581. {
  582. // 1. Update the source set for el.
  583. update_the_source_set(*this);
  584. // 2. If el's source set is empty, return null as the URL and undefined as the pixel density.
  585. if (m_source_set.is_empty())
  586. return {};
  587. // 3. Return the result of selecting an image from el's source set.
  588. return m_source_set.select_an_image_source();
  589. }
  590. void HTMLImageElement::set_source_set(SourceSet source_set)
  591. {
  592. m_source_set = move(source_set);
  593. }
  594. void HTMLImageElement::animate()
  595. {
  596. auto image_data = m_current_request->image_data();
  597. if (!image_data) {
  598. return;
  599. }
  600. m_current_frame_index = (m_current_frame_index + 1) % image_data->frame_count();
  601. auto current_frame_duration = image_data->frame_duration(m_current_frame_index);
  602. if (current_frame_duration != m_animation_timer->interval()) {
  603. m_animation_timer->restart(current_frame_duration);
  604. }
  605. if (m_current_frame_index == image_data->frame_count() - 1) {
  606. ++m_loops_completed;
  607. if (m_loops_completed > 0 && m_loops_completed == image_data->loop_count()) {
  608. m_animation_timer->stop();
  609. }
  610. }
  611. if (layout_node())
  612. layout_node()->set_needs_display();
  613. }
  614. }