script/dom/html/
htmlimageelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::default::Default;
7use std::rc::Rc;
8use std::sync::{Arc, LazyLock};
9use std::{char, mem};
10
11use app_units::Au;
12use cssparser::{Parser, ParserInput};
13use dom_struct::dom_struct;
14use euclid::default::Point2D;
15use html5ever::{LocalName, Prefix, QualName, local_name, ns};
16use js::context::JSContext;
17use js::realm::AutoRealm;
18use js::rust::HandleObject;
19use mime::{self, Mime};
20use net_traits::http_status::HttpStatus;
21use net_traits::image_cache::{
22    Image, ImageCache, ImageCacheResult, ImageLoadListener, ImageOrMetadataAvailable,
23    ImageResponse, PendingImageId,
24};
25use net_traits::request::{CorsSettings, Destination, Initiator, RequestId};
26use net_traits::{
27    FetchMetadata, FetchResponseMsg, NetworkError, ReferrerPolicy, ResourceFetchTiming,
28};
29use num_traits::ToPrimitive;
30use pixels::{CorsStatus, ImageMetadata, Snapshot};
31use regex::Regex;
32use rustc_hash::FxHashSet;
33use servo_url::ServoUrl;
34use servo_url::origin::MutableOrigin;
35use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_unsigned_integer};
36use style::stylesheets::CssRuleType;
37use style::values::specified::source_size_list::SourceSizeList;
38use style_traits::ParsingMode;
39use url::Url;
40
41use crate::css::parser_context_for_anonymous_content;
42use crate::document_loader::{LoadBlocker, LoadType};
43use crate::dom::activation::Activatable;
44use crate::dom::attr::Attr;
45use crate::dom::bindings::cell::{DomRefCell, RefMut};
46use crate::dom::bindings::codegen::Bindings::DOMRectBinding::DOMRect_Binding::DOMRectMethods;
47use crate::dom::bindings::codegen::Bindings::ElementBinding::Element_Binding::ElementMethods;
48use crate::dom::bindings::codegen::Bindings::HTMLImageElementBinding::HTMLImageElementMethods;
49use crate::dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
50use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
51use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
52use crate::dom::bindings::error::{Error, Fallible};
53use crate::dom::bindings::inheritance::Castable;
54use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
55use crate::dom::bindings::reflector::DomGlobal;
56use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
57use crate::dom::bindings::str::{DOMString, USVString};
58use crate::dom::csp::{GlobalCspReporting, Violation};
59use crate::dom::document::Document;
60use crate::dom::element::{
61    AttributeMutation, CustomElementCreationMode, Element, ElementCreator,
62    cors_setting_for_element, referrer_policy_for_element, reflect_cross_origin_attribute,
63    reflect_referrer_policy_attribute, set_cross_origin_attribute,
64};
65use crate::dom::event::Event;
66use crate::dom::eventtarget::EventTarget;
67use crate::dom::globalscope::GlobalScope;
68use crate::dom::html::htmlareaelement::HTMLAreaElement;
69use crate::dom::html::htmlelement::HTMLElement;
70use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
71use crate::dom::html::htmlmapelement::HTMLMapElement;
72use crate::dom::html::htmlpictureelement::HTMLPictureElement;
73use crate::dom::html::htmlsourceelement::HTMLSourceElement;
74use crate::dom::medialist::MediaList;
75use crate::dom::mouseevent::MouseEvent;
76use crate::dom::node::{
77    BindContext, MoveContext, Node, NodeDamage, NodeTraits, ShadowIncluding, UnbindContext,
78};
79use crate::dom::performance::performanceresourcetiming::InitiatorType;
80use crate::dom::promise::Promise;
81use crate::dom::virtualmethods::VirtualMethods;
82use crate::dom::window::Window;
83use crate::fetch::{RequestWithGlobalScope, create_a_potential_cors_request};
84use crate::microtask::{Microtask, MicrotaskRunnable};
85use crate::network_listener::{self, FetchResponseListener, ResourceTimingListener};
86use crate::realms::enter_auto_realm;
87use crate::script_runtime::CanGc;
88use crate::script_thread::ScriptThread;
89
90/// Supported image MIME types as defined by
91/// <https://mimesniff.spec.whatwg.org/#image-mime-type>.
92/// Keep this in sync with 'detect_image_format' from components/pixels/lib.rs
93const SUPPORTED_IMAGE_MIME_TYPES: &[&str] = &[
94    "image/bmp",
95    "image/gif",
96    "image/jpeg",
97    "image/jpg",
98    "image/pjpeg",
99    "image/png",
100    "image/apng",
101    "image/x-png",
102    "image/svg+xml",
103    "image/vnd.microsoft.icon",
104    "image/x-icon",
105    "image/webp",
106];
107
108#[derive(Clone, Copy, Debug)]
109enum ParseState {
110    InDescriptor,
111    InParens,
112    AfterDescriptor,
113}
114
115/// <https://html.spec.whatwg.org/multipage/#source-set>
116#[derive(MallocSizeOf)]
117pub(crate) struct SourceSet {
118    image_sources: Vec<ImageSource>,
119    source_size: SourceSizeList,
120}
121
122impl SourceSet {
123    fn new() -> SourceSet {
124        SourceSet {
125            image_sources: Vec::new(),
126            source_size: SourceSizeList::empty(),
127        }
128    }
129}
130
131#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
132pub struct ImageSource {
133    pub url: String,
134    pub descriptor: Descriptor,
135}
136
137#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
138pub struct Descriptor {
139    pub width: Option<u32>,
140    pub density: Option<f64>,
141}
142
143/// <https://html.spec.whatwg.org/multipage/#img-req-state>
144#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
145enum State {
146    Unavailable,
147    PartiallyAvailable,
148    CompletelyAvailable,
149    Broken,
150}
151
152#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
153enum ImageRequestPhase {
154    Pending,
155    Current,
156}
157
158/// <https://html.spec.whatwg.org/multipage/#image-request>
159#[derive(JSTraceable, MallocSizeOf)]
160#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
161struct ImageRequest {
162    state: State,
163    #[no_trace]
164    parsed_url: Option<ServoUrl>,
165    source_url: Option<USVString>,
166    blocker: DomRefCell<Option<LoadBlocker>>,
167    #[no_trace]
168    image: Option<Image>,
169    #[no_trace]
170    metadata: Option<ImageMetadata>,
171    #[no_trace]
172    final_url: Option<ServoUrl>,
173    current_pixel_density: Option<f64>,
174}
175
176#[dom_struct]
177pub(crate) struct HTMLImageElement {
178    htmlelement: HTMLElement,
179    image_request: Cell<ImageRequestPhase>,
180    current_request: DomRefCell<ImageRequest>,
181    pending_request: DomRefCell<ImageRequest>,
182    form_owner: MutNullableDom<HTMLFormElement>,
183    generation: Cell<u32>,
184    source_set: DomRefCell<SourceSet>,
185    /// <https://html.spec.whatwg.org/multipage/#concept-img-dimension-attribute-source>
186    /// Always non-null after construction.
187    dimension_attribute_source: MutNullableDom<Element>,
188    /// <https://html.spec.whatwg.org/multipage/#last-selected-source>
189    last_selected_source: DomRefCell<Option<USVString>>,
190    #[conditional_malloc_size_of]
191    image_decode_promises: DomRefCell<Vec<Rc<Promise>>>,
192    /// Line number this element was created on
193    line_number: u64,
194}
195
196impl HTMLImageElement {
197    // https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument
198    pub(crate) fn is_usable(&self) -> Fallible<bool> {
199        // If image has an intrinsic width or intrinsic height (or both) equal to zero, then return bad.
200        if let Some(image) = &self.current_request.borrow().image {
201            let intrinsic_size = image.metadata();
202            if intrinsic_size.width == 0 || intrinsic_size.height == 0 {
203                return Ok(false);
204            }
205        }
206
207        match self.current_request.borrow().state {
208            // If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
209            State::Broken => Err(Error::InvalidState(None)),
210            State::CompletelyAvailable => Ok(true),
211            // If image is not fully decodable, then return bad.
212            State::PartiallyAvailable | State::Unavailable => Ok(false),
213        }
214    }
215
216    pub(crate) fn image_data(&self) -> Option<Image> {
217        self.current_request.borrow().image.clone()
218    }
219
220    /// Gets the copy of the raster image data.
221    pub(crate) fn get_raster_image_data(&self) -> Option<Snapshot> {
222        let Some(raster_image) = self.image_data()?.as_raster_image() else {
223            warn!("Vector image is not supported as raster image source");
224            return None;
225        };
226        Some(raster_image.as_snapshot())
227    }
228}
229
230/// The context required for asynchronously loading an external image.
231struct ImageContext {
232    /// Reference to the script thread image cache.
233    image_cache: Arc<dyn ImageCache>,
234    /// Indicates whether the request failed, and why
235    status: Result<(), NetworkError>,
236    /// The cache ID for this request.
237    id: PendingImageId,
238    /// Used to mark abort
239    aborted: bool,
240    /// The document associated with this request
241    doc: Trusted<Document>,
242    url: ServoUrl,
243    element: Trusted<HTMLImageElement>,
244}
245
246impl FetchResponseListener for ImageContext {
247    fn should_invoke(&self) -> bool {
248        !self.aborted
249    }
250
251    fn process_request_body(&mut self, _: RequestId) {}
252
253    fn process_response(
254        &mut self,
255        _: &mut js::context::JSContext,
256        request_id: RequestId,
257        metadata: Result<FetchMetadata, NetworkError>,
258    ) {
259        debug!("got {:?} for {:?}", metadata.as_ref().map(|_| ()), self.url);
260        self.image_cache.notify_pending_response(
261            self.id,
262            FetchResponseMsg::ProcessResponse(request_id, metadata.clone()),
263        );
264
265        let metadata = metadata.ok().map(|meta| match meta {
266            FetchMetadata::Unfiltered(m) => m,
267            FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
268        });
269
270        // Step 14.5 of https://html.spec.whatwg.org/multipage/#img-environment-changes
271        if let Some(metadata) = metadata.as_ref() {
272            if let Some(ref content_type) = metadata.content_type {
273                let mime: Mime = content_type.clone().into_inner().into();
274                if mime.type_() == mime::MULTIPART && mime.subtype().as_str() == "x-mixed-replace" {
275                    self.aborted = true;
276                }
277            }
278        }
279
280        let status = metadata
281            .as_ref()
282            .map(|m| m.status.clone())
283            .unwrap_or_else(HttpStatus::new_error);
284
285        self.status = {
286            if status.is_error() {
287                Err(NetworkError::ResourceLoadError(
288                    "No http status code received".to_owned(),
289                ))
290            } else if status.is_success() {
291                Ok(())
292            } else {
293                Err(NetworkError::ResourceLoadError(format!(
294                    "HTTP error code {}",
295                    status.code()
296                )))
297            }
298        };
299    }
300
301    fn process_response_chunk(
302        &mut self,
303        _: &mut js::context::JSContext,
304        request_id: RequestId,
305        payload: Vec<u8>,
306    ) {
307        if self.status.is_ok() {
308            self.image_cache.notify_pending_response(
309                self.id,
310                FetchResponseMsg::ProcessResponseChunk(request_id, payload.into()),
311            );
312        }
313    }
314
315    fn process_response_eof(
316        self,
317        cx: &mut js::context::JSContext,
318        request_id: RequestId,
319        response: Result<(), NetworkError>,
320        timing: ResourceFetchTiming,
321    ) {
322        self.image_cache.notify_pending_response(
323            self.id,
324            FetchResponseMsg::ProcessResponseEOF(request_id, response.clone(), timing.clone()),
325        );
326        network_listener::submit_timing(cx, &self, &response, &timing);
327    }
328
329    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
330        let global = &self.resource_timing_global();
331        let elem = self.element.root();
332        let source_position = elem
333            .upcast::<Element>()
334            .compute_source_position(elem.line_number as u32);
335        global.report_csp_violations(violations, None, Some(source_position));
336    }
337}
338
339impl ResourceTimingListener for ImageContext {
340    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
341        (
342            InitiatorType::LocalName("img".to_string()),
343            self.url.clone(),
344        )
345    }
346
347    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
348        self.doc.root().global()
349    }
350}
351
352#[expect(non_snake_case)]
353impl HTMLImageElement {
354    /// Update the current image with a valid URL.
355    fn fetch_image(&self, img_url: &ServoUrl, cx: &mut js::context::JSContext) {
356        let window = self.owner_window();
357
358        let cache_result = window.image_cache().get_cached_image_status(
359            img_url.clone(),
360            window.origin().immutable().clone(),
361            cors_setting_for_element(self.upcast()),
362        );
363
364        match cache_result {
365            ImageCacheResult::Available(ImageOrMetadataAvailable::ImageAvailable {
366                image,
367                url,
368            }) => self.process_image_response(ImageResponse::Loaded(image, url), cx),
369            ImageCacheResult::Available(ImageOrMetadataAvailable::MetadataAvailable(
370                metadata,
371                id,
372            )) => {
373                self.process_image_response(ImageResponse::MetadataLoaded(metadata), cx);
374                self.register_image_cache_callback(id, ChangeType::Element);
375            },
376            ImageCacheResult::Pending(id) => {
377                self.register_image_cache_callback(id, ChangeType::Element);
378            },
379            ImageCacheResult::ReadyForRequest(id) => {
380                self.fetch_request(img_url, id);
381                self.register_image_cache_callback(id, ChangeType::Element);
382            },
383            ImageCacheResult::FailedToLoadOrDecode => {
384                self.process_image_response(ImageResponse::FailedToLoadOrDecode, cx)
385            },
386        };
387    }
388
389    fn register_image_cache_callback(&self, id: PendingImageId, change_type: ChangeType) {
390        let trusted_node = Trusted::new(self);
391        let generation = self.generation_id();
392        let window = self.owner_window();
393        let callback = window.register_image_cache_listener(id, move |response, _| {
394            let trusted_node = trusted_node.clone();
395            let window = trusted_node.root().owner_window();
396            let callback_type = change_type.clone();
397
398            window
399                .as_global_scope()
400                .task_manager()
401                .networking_task_source()
402                .queue(task!(process_image_response: move |cx| {
403                let element = trusted_node.root();
404
405                // Ignore any image response for a previous request that has been discarded.
406                if generation != element.generation_id() {
407                    return;
408                }
409
410                match callback_type {
411                    ChangeType::Element => {
412                        element.process_image_response(response.response, cx);
413                    }
414                    ChangeType::Environment { selected_source, selected_pixel_density } => {
415                        element.process_image_response_for_environment_change(
416                            response.response, selected_source, generation, selected_pixel_density, cx
417                        );
418                    }
419                }
420            }));
421        });
422
423        window.image_cache().add_listener(ImageLoadListener::new(
424            callback,
425            window.pipeline_id(),
426            id,
427        ));
428    }
429
430    fn fetch_request(&self, img_url: &ServoUrl, id: PendingImageId) {
431        let document = self.owner_document();
432        let window = self.owner_window();
433
434        let context = ImageContext {
435            image_cache: window.image_cache(),
436            status: Ok(()),
437            id,
438            aborted: false,
439            doc: Trusted::new(&document),
440            element: Trusted::new(self),
441            url: img_url.clone(),
442        };
443
444        // https://html.spec.whatwg.org/multipage/#update-the-image-data steps 17-20
445        // This function is also used to prefetch an image in `script::dom::servoparser::prefetch`.
446        let global = document.global();
447        let mut request = create_a_potential_cors_request(
448            Some(window.webview_id()),
449            img_url.clone(),
450            Destination::Image,
451            cors_setting_for_element(self.upcast()),
452            None,
453            global.get_referrer(),
454        )
455        .with_global_scope(&global)
456        .referrer_policy(referrer_policy_for_element(self.upcast()));
457
458        if self.uses_srcset_or_picture() {
459            request = request.initiator(Initiator::ImageSet);
460        }
461
462        // This is a background load because the load blocker already fulfills the
463        // purpose of delaying the document's load event.
464        document.fetch_background(request, context);
465    }
466
467    // Steps common to when an image has been loaded.
468    fn handle_loaded_image(&self, image: Image, url: ServoUrl, cx: &mut js::context::JSContext) {
469        self.current_request.borrow_mut().metadata = Some(image.metadata());
470        self.current_request.borrow_mut().final_url = Some(url);
471        self.current_request.borrow_mut().image = Some(image);
472        self.current_request.borrow_mut().state = State::CompletelyAvailable;
473        LoadBlocker::terminate(&self.current_request.borrow().blocker, cx);
474        // Mark the node dirty
475        self.upcast::<Node>().dirty(NodeDamage::Other);
476        self.resolve_image_decode_promises();
477    }
478
479    /// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
480    fn process_image_response(&self, image: ImageResponse, cx: &mut js::context::JSContext) {
481        // Step 27. As soon as possible, jump to the first applicable entry from the following list:
482
483        // TODO => "If the resource type is multipart/x-mixed-replace"
484
485        // => "If the resource type and data corresponds to a supported image format ...""
486        let (trigger_image_load, trigger_image_error) = match (image, self.image_request.get()) {
487            (ImageResponse::Loaded(image, url), ImageRequestPhase::Current) => {
488                self.handle_loaded_image(image, url, cx);
489                (true, false)
490            },
491            (ImageResponse::Loaded(image, url), ImageRequestPhase::Pending) => {
492                self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
493                self.image_request.set(ImageRequestPhase::Current);
494                self.handle_loaded_image(image, url, cx);
495                (true, false)
496            },
497            (ImageResponse::MetadataLoaded(meta), ImageRequestPhase::Current) => {
498                // Otherwise, if the user agent is able to determine image request's image's width
499                // and height, and image request is the current request, prepare image request for
500                // presentation given the img element and set image request's state to partially
501                // available.
502                self.current_request.borrow_mut().state = State::PartiallyAvailable;
503                self.current_request.borrow_mut().metadata = Some(meta);
504                (false, false)
505            },
506            (ImageResponse::MetadataLoaded(_), ImageRequestPhase::Pending) => {
507                // If the user agent is able to determine image request's image's width and height,
508                // and image request is the pending request, set image request's state to partially
509                // available.
510                self.pending_request.borrow_mut().state = State::PartiallyAvailable;
511                (false, false)
512            },
513            (ImageResponse::FailedToLoadOrDecode, ImageRequestPhase::Current) => {
514                // Otherwise, if the user agent is able to determine that image request's image is
515                // corrupted in some fatal way such that the image dimensions cannot be obtained,
516                // and image request is the current request:
517
518                // Step 1. Abort the image request for image request.
519                self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
520
521                self.load_broken_image_icon();
522
523                // Step 2. If maybe omit events is not set or previousURL is not equal to urlString,
524                // then fire an event named error at the img element.
525                // TODO: Add missing `maybe omit events` flag and previousURL.
526                (false, true)
527            },
528            (ImageResponse::FailedToLoadOrDecode, ImageRequestPhase::Pending) => {
529                // Otherwise, if the user agent is able to determine that image request's image is
530                // corrupted in some fatal way such that the image dimensions cannot be obtained,
531                // and image request is the pending request:
532
533                // Step 1. Abort the image request for the current request and the pending request.
534                self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
535                self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
536
537                // Step 2. Upgrade the pending request to the current request.
538                mem::swap(
539                    &mut *self.current_request.borrow_mut(),
540                    &mut *self.pending_request.borrow_mut(),
541                );
542                self.image_request.set(ImageRequestPhase::Current);
543
544                // Step 3. Set the current request's state to broken.
545                self.current_request.borrow_mut().state = State::Broken;
546
547                self.load_broken_image_icon();
548
549                // Step 4. Fire an event named error at the img element.
550                (false, true)
551            },
552        };
553
554        // Fire image.onload and loadend
555        if trigger_image_load {
556            // TODO: https://html.spec.whatwg.org/multipage/#fire-a-progress-event-or-event
557            self.upcast::<EventTarget>().fire_event(cx, atom!("load"));
558            self.upcast::<EventTarget>()
559                .fire_event(cx, atom!("loadend"));
560        }
561
562        // Fire image.onerror
563        if trigger_image_error {
564            self.upcast::<EventTarget>().fire_event(cx, atom!("error"));
565            self.upcast::<EventTarget>()
566                .fire_event(cx, atom!("loadend"));
567        }
568    }
569
570    /// The response part of
571    /// <https://html.spec.whatwg.org/multipage/#reacting-to-environment-changes>.
572    fn process_image_response_for_environment_change(
573        &self,
574        image: ImageResponse,
575        selected_source: USVString,
576        generation: u32,
577        selected_pixel_density: f64,
578        cx: &mut js::context::JSContext,
579    ) {
580        match image {
581            ImageResponse::Loaded(image, url) => {
582                self.pending_request.borrow_mut().metadata = Some(image.metadata());
583                self.pending_request.borrow_mut().final_url = Some(url);
584                self.pending_request.borrow_mut().image = Some(image);
585                self.finish_reacting_to_environment_change(
586                    selected_source,
587                    generation,
588                    selected_pixel_density,
589                );
590            },
591            ImageResponse::FailedToLoadOrDecode => {
592                // > Step 15.6: If response's unsafe response is a network error or if the
593                // > image format is unsupported (as determined by applying the image
594                // > sniffing rules, again as mentioned earlier), or if the user agent is
595                // > able to determine that image request's image is corrupted in some fatal
596                // > way such that the image dimensions cannot be obtained, or if the
597                // > resource type is multipart/x-mixed-replace, then set the pending
598                // > request to null and abort these steps.
599                self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
600            },
601            ImageResponse::MetadataLoaded(meta) => {
602                self.pending_request.borrow_mut().metadata = Some(meta);
603            },
604        };
605    }
606
607    /// <https://html.spec.whatwg.org/multipage/#abort-the-image-request>
608    fn abort_request(
609        &self,
610        state: State,
611        request: ImageRequestPhase,
612        cx: &mut js::context::JSContext,
613    ) {
614        let mut request = match request {
615            ImageRequestPhase::Current => self.current_request.borrow_mut(),
616            ImageRequestPhase::Pending => self.pending_request.borrow_mut(),
617        };
618        LoadBlocker::terminate(&request.blocker, cx);
619        request.state = state;
620        request.image = None;
621        request.metadata = None;
622        request.current_pixel_density = None;
623
624        if matches!(state, State::Broken) {
625            self.reject_image_decode_promises();
626        } else if matches!(state, State::CompletelyAvailable) {
627            self.resolve_image_decode_promises();
628        }
629    }
630
631    /// <https://html.spec.whatwg.org/multipage/#create-a-source-set>
632    fn create_source_set(&self) -> SourceSet {
633        let element = self.upcast::<Element>();
634
635        // Step 1. Let source set be an empty source set.
636        let mut source_set = SourceSet::new();
637
638        // Step 2. If srcset is not an empty string, then set source set to the result of parsing
639        // srcset.
640        if let Some(srcset) = element.get_attribute(&local_name!("srcset")) {
641            source_set.image_sources = parse_a_srcset_attribute(&srcset.value());
642        }
643
644        // Step 3. Set source set's source size to the result of parsing sizes with img.
645        if let Some(sizes) = element.get_attribute(&local_name!("sizes")) {
646            source_set.source_size = parse_a_sizes_attribute(&sizes.value());
647        }
648
649        // Step 4. If default source is not the empty string and source set does not contain an
650        // image source with a pixel density descriptor value of 1, and no image source with a width
651        // descriptor, append default source to source set.
652        let src = element.get_string_attribute(&local_name!("src"));
653        let no_density_source_of_1 = source_set
654            .image_sources
655            .iter()
656            .all(|source| source.descriptor.density != Some(1.));
657        let no_width_descriptor = source_set
658            .image_sources
659            .iter()
660            .all(|source| source.descriptor.width.is_none());
661        if !src.is_empty() && no_density_source_of_1 && no_width_descriptor {
662            source_set.image_sources.push(ImageSource {
663                url: src.to_string(),
664                descriptor: Descriptor {
665                    width: None,
666                    density: None,
667                },
668            })
669        }
670
671        // Step 5. Normalize the source densities of source set.
672        self.normalise_source_densities(&mut source_set);
673
674        // Step 6. Return source set.
675        source_set
676    }
677
678    /// <https://html.spec.whatwg.org/multipage/#update-the-source-set>
679    fn update_source_set(&self) {
680        // Step 1. Set el's source set to an empty source set.
681        *self.source_set.borrow_mut() = SourceSet::new();
682
683        // Step 2. Let elements be « el ».
684        // Step 3. If el is an img element whose parent node is a picture element, then replace the
685        // contents of elements with el's parent node's child elements, retaining relative order.
686        // Step 4. Let img be el if el is an img element, otherwise null.
687        let elem = self.upcast::<Element>();
688        let parent = elem.upcast::<Node>().GetParentElement();
689        let elements = match parent.as_ref() {
690            Some(p) => {
691                if p.is::<HTMLPictureElement>() {
692                    p.upcast::<Node>()
693                        .children()
694                        .filter_map(DomRoot::downcast::<Element>)
695                        .map(|n| DomRoot::from_ref(&*n))
696                        .collect()
697                } else {
698                    vec![DomRoot::from_ref(elem)]
699                }
700            },
701            None => vec![DomRoot::from_ref(elem)],
702        };
703
704        // Step 5. For each child in elements:
705        for element in &elements {
706            // Step 5.1. If child is el:
707            if *element == DomRoot::from_ref(elem) {
708                // Step 5.1.10. Set el's source set to the result of creating a source set given
709                // default source, srcset, sizes, and img.
710                *self.source_set.borrow_mut() = self.create_source_set();
711
712                // Step 5.1.11. Return.
713                return;
714            }
715            // Step 5.2. If child is not a source element, then continue.
716            if !element.is::<HTMLSourceElement>() {
717                continue;
718            }
719
720            let mut source_set = SourceSet::new();
721
722            // Step 5.3. If child does not have a srcset attribute, continue to the next child.
723            // Step 5.4. Parse child's srcset attribute and let source set be the returned source
724            // set.
725            match element.get_attribute(&local_name!("srcset")) {
726                Some(srcset) => {
727                    source_set.image_sources = parse_a_srcset_attribute(&srcset.value());
728                },
729                _ => continue,
730            }
731
732            // Step 5.5. If source set has zero image sources, continue to the next child.
733            if source_set.image_sources.is_empty() {
734                continue;
735            }
736
737            // Step 5.6. If child has a media attribute, and its value does not match the
738            // environment, continue to the next child.
739            if let Some(media) = element.get_attribute(&local_name!("media")) {
740                if !MediaList::matches_environment(&element.owner_document(), &media.value()) {
741                    continue;
742                }
743            }
744
745            // Step 5.7. Parse child's sizes attribute with img, and let source set's source size be
746            // the returned value.
747            if let Some(sizes) = element.get_attribute(&local_name!("sizes")) {
748                source_set.source_size = parse_a_sizes_attribute(&sizes.value());
749            }
750
751            // Step 5.8. If child has a type attribute, and its value is an unknown or unsupported
752            // MIME type, continue to the next child.
753            if let Some(type_) = element.get_attribute(&local_name!("type")) {
754                if !is_supported_image_mime_type(&type_.value()) {
755                    continue;
756                }
757            }
758
759            // Step 5.9. If child has width or height attributes, set el's dimension attribute
760            // source to child. Otherwise, set el's dimension attribute source to el.
761            if element.get_attribute(&local_name!("width")).is_some() ||
762                element.get_attribute(&local_name!("height")).is_some()
763            {
764                self.dimension_attribute_source.set(Some(element));
765            } else {
766                self.dimension_attribute_source.set(Some(elem));
767            }
768
769            // Step 5.10. Normalize the source densities of source set.
770            self.normalise_source_densities(&mut source_set);
771
772            // Step 5.11. Set el's source set to source set.
773            *self.source_set.borrow_mut() = source_set;
774
775            // Step 5.12. Return.
776            return;
777        }
778    }
779
780    fn evaluate_source_size_list(&self, source_size_list: &SourceSizeList) -> Au {
781        let document = self.owner_document();
782        let quirks_mode = document.quirks_mode();
783        source_size_list.evaluate(document.window().layout().device(), quirks_mode)
784    }
785
786    /// <https://html.spec.whatwg.org/multipage/#normalise-the-source-densities>
787    fn normalise_source_densities(&self, source_set: &mut SourceSet) {
788        // Step 1. Let source size be source set's source size.
789        let source_size = self.evaluate_source_size_list(&source_set.source_size);
790
791        // Step 2. For each image source in source set:
792        for image_source in &mut source_set.image_sources {
793            // Step 2.1. If the image source has a pixel density descriptor, continue to the next
794            // image source.
795            if image_source.descriptor.density.is_some() {
796                continue;
797            }
798
799            // Step 2.2. Otherwise, if the image source has a width descriptor, replace the width
800            // descriptor with a pixel density descriptor with a value of the width descriptor value
801            // divided by source size and a unit of x.
802            if image_source.descriptor.width.is_some() {
803                let width = image_source.descriptor.width.unwrap();
804                image_source.descriptor.density = Some(width as f64 / source_size.to_f64_px());
805            } else {
806                // Step 2.3. Otherwise, give the image source a pixel density descriptor of 1x.
807                image_source.descriptor.density = Some(1_f64);
808            }
809        }
810    }
811
812    /// <https://html.spec.whatwg.org/multipage/#select-an-image-source>
813    fn select_image_source(&self) -> Option<(USVString, f64)> {
814        // Step 1. Update the source set for el.
815        self.update_source_set();
816
817        // Step 2. If el's source set is empty, return null as the URL and undefined as the pixel
818        // density.
819        if self.source_set.borrow().image_sources.is_empty() {
820            return None;
821        }
822
823        // Step 3. Return the result of selecting an image from el's source set.
824        self.select_image_source_from_source_set()
825    }
826
827    /// <https://html.spec.whatwg.org/multipage/#select-an-image-source-from-a-source-set>
828    fn select_image_source_from_source_set(&self) -> Option<(USVString, f64)> {
829        // Step 1. If an entry b in sourceSet has the same associated pixel density descriptor as an
830        // earlier entry a in sourceSet, then remove entry b. Repeat this step until none of the
831        // entries in sourceSet have the same associated pixel density descriptor as an earlier
832        // entry.
833        let source_set = self.source_set.borrow();
834        let len = source_set.image_sources.len();
835
836        // Using FxHash is ok here as the indices are just 0..len
837        let mut repeat_indices = FxHashSet::default();
838        for outer_index in 0..len {
839            if repeat_indices.contains(&outer_index) {
840                continue;
841            }
842            let imgsource = &source_set.image_sources[outer_index];
843            let pixel_density = imgsource.descriptor.density.unwrap();
844            for inner_index in (outer_index + 1)..len {
845                let imgsource2 = &source_set.image_sources[inner_index];
846                if pixel_density == imgsource2.descriptor.density.unwrap() {
847                    repeat_indices.insert(inner_index);
848                }
849            }
850        }
851
852        let mut max = (0f64, 0);
853        let img_sources = &mut vec![];
854        for (index, image_source) in source_set.image_sources.iter().enumerate() {
855            if repeat_indices.contains(&index) {
856                continue;
857            }
858            let den = image_source.descriptor.density.unwrap();
859            if max.0 < den {
860                max = (den, img_sources.len());
861            }
862            img_sources.push(image_source);
863        }
864
865        // Step 2. In an implementation-defined manner, choose one image source from sourceSet. Let
866        // selectedSource be this choice.
867        let mut best_candidate = max;
868        let device_pixel_ratio = self
869            .owner_document()
870            .window()
871            .viewport_details()
872            .hidpi_scale_factor
873            .get() as f64;
874        for (index, image_source) in img_sources.iter().enumerate() {
875            let current_den = image_source.descriptor.density.unwrap();
876            if current_den < best_candidate.0 && current_den >= device_pixel_ratio {
877                best_candidate = (current_den, index);
878            }
879        }
880        let selected_source = img_sources.remove(best_candidate.1).clone();
881
882        // Step 3. Return selectedSource and its associated pixel density.
883        Some((
884            USVString(selected_source.url),
885            selected_source.descriptor.density.unwrap(),
886        ))
887    }
888
889    fn init_image_request(
890        &self,
891        request: &mut RefMut<'_, ImageRequest>,
892        url: &ServoUrl,
893        src: &USVString,
894        cx: &mut js::context::JSContext,
895    ) {
896        request.parsed_url = Some(url.clone());
897        request.source_url = Some(src.clone());
898        request.image = None;
899        request.metadata = None;
900        let document = self.owner_document();
901        LoadBlocker::terminate(&request.blocker, cx);
902        *request.blocker.borrow_mut() =
903            Some(LoadBlocker::new(&document, LoadType::Image(url.clone())));
904    }
905
906    /// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
907    fn prepare_image_request(
908        &self,
909        selected_source: &USVString,
910        selected_pixel_density: f64,
911        image_url: &ServoUrl,
912        cx: &mut js::context::JSContext,
913    ) {
914        match self.image_request.get() {
915            ImageRequestPhase::Pending => {
916                // Step 14. If the pending request is not null and urlString is the same as the
917                // pending request's current URL, then return.
918                if self
919                    .pending_request
920                    .borrow()
921                    .parsed_url
922                    .as_ref()
923                    .is_some_and(|parsed_url| *parsed_url == *image_url)
924                {
925                    return;
926                }
927            },
928            ImageRequestPhase::Current => {
929                // Step 16. Abort the image request for the pending request.
930                self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
931
932                // Step 17. Set image request to a new image request whose current URL is urlString.
933
934                let mut current_request = self.current_request.borrow_mut();
935                let mut pending_request = self.pending_request.borrow_mut();
936
937                match (current_request.parsed_url.as_ref(), current_request.state) {
938                    (Some(parsed_url), State::PartiallyAvailable) => {
939                        // Step 15. If urlString is the same as the current request's current URL
940                        // and the current request's state is partially available, then abort the
941                        // image request for the pending request, queue an element task on the DOM
942                        // manipulation task source given the img element to restart the animation
943                        // if restart animation is set, and return.
944                        if *parsed_url == *image_url {
945                            // TODO: queue a task to restart animation, if restart-animation is set
946                            return;
947                        }
948
949                        // Step 18. If the current request's state is unavailable or broken, then
950                        // set the current request to image request. Otherwise, set the pending
951                        // request to image request.
952                        self.image_request.set(ImageRequestPhase::Pending);
953                        self.init_image_request(
954                            &mut pending_request,
955                            image_url,
956                            selected_source,
957                            cx,
958                        );
959                        pending_request.current_pixel_density = Some(selected_pixel_density);
960                    },
961                    (_, State::Broken) | (_, State::Unavailable) => {
962                        // Step 18. If the current request's state is unavailable or broken, then
963                        // set the current request to image request. Otherwise, set the pending
964                        // request to image request.
965                        self.init_image_request(
966                            &mut current_request,
967                            image_url,
968                            selected_source,
969                            cx,
970                        );
971                        current_request.current_pixel_density = Some(selected_pixel_density);
972                        self.reject_image_decode_promises();
973                    },
974                    (_, _) => {
975                        // Step 18. If the current request's state is unavailable or broken, then
976                        // set the current request to image request. Otherwise, set the pending
977                        // request to image request.
978                        self.image_request.set(ImageRequestPhase::Pending);
979                        self.init_image_request(
980                            &mut pending_request,
981                            image_url,
982                            selected_source,
983                            cx,
984                        );
985                        pending_request.current_pixel_density = Some(selected_pixel_density);
986                    },
987                }
988            },
989        }
990
991        self.fetch_image(image_url, cx);
992    }
993
994    /// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
995    fn update_the_image_data_sync_steps(&self, cx: &mut js::context::JSContext) {
996        // Step 10. Let selected source and selected pixel density be the URL and pixel density that
997        // results from selecting an image source, respectively.
998        let Some((selected_source, selected_pixel_density)) = self.select_image_source() else {
999            // Step 11. If selected source is null, then:
1000
1001            // Step 11.1. Set the current request's state to broken, abort the image request for the
1002            // current request and the pending request, and set the pending request to null.
1003            self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
1004            self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
1005            self.image_request.set(ImageRequestPhase::Current);
1006
1007            // Step 11.2. Queue an element task on the DOM manipulation task source given the img
1008            // element and the following steps:
1009            let this = Trusted::new(self);
1010
1011            self.owner_global().task_manager().dom_manipulation_task_source().queue(
1012                task!(image_null_source_error: move |cx| {
1013                    let this = this.root();
1014
1015                    // Step 11.2.1. Change the current request's current URL to the empty string.
1016                    {
1017                        let mut current_request =
1018                            this.current_request.borrow_mut();
1019                        current_request.source_url = None;
1020                        current_request.parsed_url = None;
1021                    }
1022
1023                    // Step 11.2.2. If all of the following are true:
1024                    // the element has a src attribute or it uses srcset or picture; and
1025                    // maybe omit events is not set or previousURL is not the empty string,
1026                    // then fire an event named error at the img element.
1027                    // TODO: Add missing `maybe omit events` flag and previousURL.
1028                    let has_src_attribute = this.upcast::<Element>().has_attribute(&local_name!("src"));
1029
1030                    if has_src_attribute || this.uses_srcset_or_picture() {
1031                        this.upcast::<EventTarget>().fire_event(cx, atom!("error"));
1032                    }
1033                }));
1034
1035            // Step 11.2.3. Return.
1036            return;
1037        };
1038
1039        // Step 12. Let urlString be the result of encoding-parsing-and-serializing a URL given
1040        // selected source, relative to the element's node document.
1041        let Ok(image_url) = self.owner_document().base_url().join(&selected_source) else {
1042            // Step 13. If urlString is failure, then:
1043
1044            // Step 13.1. Abort the image request for the current request and the pending request.
1045            // Step 13.2. Set the current request's state to broken.
1046            self.abort_request(State::Broken, ImageRequestPhase::Current, cx);
1047            self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
1048
1049            // Step 13.3. Set the pending request to null.
1050            self.image_request.set(ImageRequestPhase::Current);
1051
1052            // Step 13.4. Queue an element task on the DOM manipulation task source given the img
1053            // element and the following steps:
1054            let this = Trusted::new(self);
1055
1056            self.owner_global()
1057                .task_manager()
1058                .dom_manipulation_task_source()
1059                .queue(task!(image_selected_source_error: move |cx| {
1060                    let this = this.root();
1061
1062                    // Step 13.4.1. Change the current request's current URL to selected source.
1063                    {
1064                        let mut current_request =
1065                            this.current_request.borrow_mut();
1066                        current_request.source_url = Some(selected_source);
1067                        current_request.parsed_url = None;
1068                    }
1069
1070                    // Step 13.4.2. If maybe omit events is not set or previousURL is not equal to
1071                    // selected source, then fire an event named error at the img element.
1072                    // TODO: Add missing `maybe omit events` flag and previousURL.
1073                    this.upcast::<EventTarget>().fire_event(cx, atom!("error"));
1074                }));
1075
1076            // Step 13.5. Return.
1077            return;
1078        };
1079
1080        self.prepare_image_request(&selected_source, selected_pixel_density, &image_url, cx);
1081    }
1082
1083    /// <https://html.spec.whatwg.org/multipage/#update-the-image-data>
1084    pub(crate) fn update_the_image_data(&self, cx: &mut js::context::JSContext) {
1085        // Cancel any outstanding tasks that were queued before.
1086        self.generation.set(self.generation.get() + 1);
1087
1088        // Step 1. If the element's node document is not fully active, then:
1089        if !self.owner_document().is_active() {
1090            // TODO Step 1.1. Continue running this algorithm in parallel.
1091            // TODO Step 1.2. Wait until the element's node document is fully active.
1092            // TODO Step 1.3. If another instance of this algorithm for this img element was started after
1093            // this instance (even if it aborted and is no longer running), then return.
1094            // TODO Step 1.4. Queue a microtask to continue this algorithm.
1095        }
1096
1097        // Step 2. If the user agent cannot support images, or its support for images has been
1098        // disabled, then abort the image request for the current request and the pending request,
1099        // set the current request's state to unavailable, set the pending request to null, and
1100        // return.
1101        // Nothing specific to be done here since the user agent supports image processing.
1102
1103        // Always first set the current request to unavailable, ensuring img.complete is false.
1104        // <https://html.spec.whatwg.org/multipage/#when-to-obtain-images>
1105        self.current_request.borrow_mut().state = State::Unavailable;
1106
1107        // TODO Step 3. Let previousURL be the current request's current URL.
1108
1109        // Step 4. Let selected source be null and selected pixel density be undefined.
1110        let mut selected_source = None;
1111        let mut selected_pixel_density = None;
1112
1113        // Step 5. If the element does not use srcset or picture and it has a src attribute
1114        // specified whose value is not the empty string, then set selected source to the value of
1115        // the element's src attribute and set selected pixel density to 1.0.
1116        let src = self
1117            .upcast::<Element>()
1118            .get_string_attribute(&local_name!("src"));
1119
1120        if !self.uses_srcset_or_picture() && !src.is_empty() {
1121            selected_source = Some(USVString(src.to_string()));
1122            selected_pixel_density = Some(1_f64);
1123        };
1124
1125        // Step 6. Set the element's last selected source to selected source.
1126        self.last_selected_source
1127            .borrow_mut()
1128            .clone_from(&selected_source);
1129
1130        // Step 7. If selected source is not null, then:
1131        if let Some(selected_source) = selected_source {
1132            // Step 7.1. Let urlString be the result of encoding-parsing-and-serializing a URL given
1133            // selected source, relative to the element's node document.
1134            // Step 7.2. If urlString is failure, then abort this inner set of steps.
1135            if let Ok(image_url) = self.owner_document().base_url().join(&selected_source) {
1136                // Step 7.3. Let key be a tuple consisting of urlString, the img element's
1137                // crossorigin attribute's mode, and, if that mode is not No CORS, the node
1138                // document's origin.
1139                let window = self.owner_window();
1140                let response = window.image_cache().get_image(
1141                    image_url.clone(),
1142                    window.origin().immutable().clone(),
1143                    cors_setting_for_element(self.upcast()),
1144                );
1145
1146                // Step 7.4. If the list of available images contains an entry for key, then:
1147                if let Some(image) = response {
1148                    // TODO Step 7.4.1. Set the ignore higher-layer caching flag for that entry.
1149
1150                    // Step 7.4.2. Abort the image request for the current request and the pending
1151                    // request.
1152                    self.abort_request(State::CompletelyAvailable, ImageRequestPhase::Current, cx);
1153                    self.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
1154
1155                    // Step 7.4.3. Set the pending request to null.
1156                    self.image_request.set(ImageRequestPhase::Current);
1157
1158                    // Step 7.4.4. Set the current request to a new image request whose image data
1159                    // is that of the entry and whose state is completely available.
1160                    let mut current_request = self.current_request.borrow_mut();
1161                    current_request.metadata = Some(image.metadata());
1162                    current_request.image = Some(image);
1163                    current_request.final_url = Some(image_url.clone());
1164
1165                    // TODO Step 7.4.5. Prepare the current request for presentation given the img
1166                    // element.
1167                    self.upcast::<Node>().dirty(NodeDamage::Other);
1168
1169                    // Step 7.4.6. Set the current request's current pixel density to selected pixel
1170                    // density.
1171                    current_request.current_pixel_density = selected_pixel_density;
1172
1173                    // Step 7.4.7. Queue an element task on the DOM manipulation task source given
1174                    // the img element and the following steps:
1175                    let this = Trusted::new(self);
1176
1177                    self.owner_global()
1178                        .task_manager()
1179                        .dom_manipulation_task_source()
1180                        .queue(task!(image_load_event: move |cx| {
1181                            let this = this.root();
1182
1183                            // TODO Step 7.4.7.1. If restart animation is set, then restart the
1184                            // animation.
1185
1186                            // Step 7.4.7.2. Set the current request's current URL to urlString.
1187                            {
1188                                let mut current_request =
1189                                    this.current_request.borrow_mut();
1190                                current_request.source_url = Some(selected_source);
1191                                current_request.parsed_url = Some(image_url);
1192                            }
1193
1194                            // Step 7.4.7.3. If maybe omit events is not set or previousURL is not
1195                            // equal to urlString, then fire an event named load at the img element.
1196                            // TODO: Add missing `maybe omit events` flag and previousURL.
1197                            this.upcast::<EventTarget>().fire_event(cx, atom!("load"));
1198                        }));
1199
1200                    // Step 7.4.8. Abort the update the image data algorithm.
1201                    return;
1202                }
1203            }
1204        }
1205
1206        // Step 8. Queue a microtask to perform the rest of this algorithm, allowing the task that
1207        // invoked this algorithm to continue.
1208        let task = ImageElementMicrotask::UpdateImageData {
1209            elem: DomRoot::from_ref(self),
1210            generation: self.generation.get(),
1211        };
1212
1213        ScriptThread::await_stable_state(Microtask::ImageElement(task));
1214    }
1215
1216    /// <https://html.spec.whatwg.org/multipage/#img-environment-changes>
1217    pub(crate) fn react_to_environment_changes(&self) {
1218        // Step 1. Await a stable state.
1219        let task = ImageElementMicrotask::EnvironmentChanges {
1220            elem: DomRoot::from_ref(self),
1221            generation: self.generation.get(),
1222        };
1223
1224        ScriptThread::await_stable_state(Microtask::ImageElement(task));
1225    }
1226
1227    /// <https://html.spec.whatwg.org/multipage/#img-environment-changes>
1228    fn react_to_environment_changes_sync_steps(
1229        &self,
1230        generation: u32,
1231        cx: &mut js::context::JSContext,
1232    ) {
1233        let document = self.owner_document();
1234        let has_pending_request = matches!(self.image_request.get(), ImageRequestPhase::Pending);
1235
1236        // Step 2. If the img element does not use srcset or picture, its node document is not fully
1237        // active, it has image data whose resource type is multipart/x-mixed-replace, or its
1238        // pending request is not null, then return.
1239        if !document.is_active() || !self.uses_srcset_or_picture() || has_pending_request {
1240            return;
1241        }
1242
1243        // Step 3. Let selected source and selected pixel density be the URL and pixel density that
1244        // results from selecting an image source, respectively.
1245        let Some((selected_source, selected_pixel_density)) = self.select_image_source() else {
1246            // Step 4. If selected source is null, then return.
1247            return;
1248        };
1249
1250        // Step 5. If selected source and selected pixel density are the same as the element's last
1251        // selected source and current pixel density, then return.
1252        let mut same_selected_source = self
1253            .last_selected_source
1254            .borrow()
1255            .as_ref()
1256            .is_some_and(|source| *source == selected_source);
1257
1258        // There are missing steps for the element's last selected source in specification so let's
1259        // check the current request's current URL as well.
1260        // <https://github.com/whatwg/html/issues/5060>
1261        same_selected_source = same_selected_source ||
1262            self.current_request
1263                .borrow()
1264                .source_url
1265                .as_ref()
1266                .is_some_and(|source| *source == selected_source);
1267
1268        let same_selected_pixel_density = self
1269            .current_request
1270            .borrow()
1271            .current_pixel_density
1272            .is_some_and(|pixel_density| pixel_density == selected_pixel_density);
1273
1274        if same_selected_source && same_selected_pixel_density {
1275            return;
1276        }
1277
1278        // Step 6. Let urlString be the result of encoding-parsing-and-serializing a URL given
1279        // selected source, relative to the element's node document.
1280        // Step 7. If urlString is failure, then return.
1281        let Ok(image_url) = document.base_url().join(&selected_source) else {
1282            return;
1283        };
1284
1285        // Step 13. Set the element's pending request to image request.
1286        self.image_request.set(ImageRequestPhase::Pending);
1287        self.init_image_request(
1288            &mut self.pending_request.borrow_mut(),
1289            &image_url,
1290            &selected_source,
1291            cx,
1292        );
1293
1294        // Step 15. If the list of available images contains an entry for key, then set image
1295        // request's image data to that of the entry. Continue to the next step.
1296        let window = self.owner_window();
1297        let cache_result = window.image_cache().get_cached_image_status(
1298            image_url.clone(),
1299            window.origin().immutable().clone(),
1300            cors_setting_for_element(self.upcast()),
1301        );
1302
1303        let change_type = ChangeType::Environment {
1304            selected_source: selected_source.clone(),
1305            selected_pixel_density,
1306        };
1307
1308        match cache_result {
1309            ImageCacheResult::Available(ImageOrMetadataAvailable::ImageAvailable { .. }) => {
1310                self.finish_reacting_to_environment_change(
1311                    selected_source,
1312                    generation,
1313                    selected_pixel_density,
1314                );
1315            },
1316            ImageCacheResult::Available(ImageOrMetadataAvailable::MetadataAvailable(m, id)) => {
1317                self.process_image_response_for_environment_change(
1318                    ImageResponse::MetadataLoaded(m),
1319                    selected_source,
1320                    generation,
1321                    selected_pixel_density,
1322                    cx,
1323                );
1324                self.register_image_cache_callback(id, change_type);
1325            },
1326            ImageCacheResult::FailedToLoadOrDecode => {
1327                self.process_image_response_for_environment_change(
1328                    ImageResponse::FailedToLoadOrDecode,
1329                    selected_source,
1330                    generation,
1331                    selected_pixel_density,
1332                    cx,
1333                );
1334            },
1335            ImageCacheResult::ReadyForRequest(id) => {
1336                self.fetch_request(&image_url, id);
1337                self.register_image_cache_callback(id, change_type);
1338            },
1339            ImageCacheResult::Pending(id) => {
1340                self.register_image_cache_callback(id, change_type);
1341            },
1342        }
1343    }
1344
1345    /// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
1346    fn react_to_decode_image_sync_steps(&self, promise: Rc<Promise>, can_gc: CanGc) {
1347        // Step 2.2. If any of the following are true: this's node document is not fully active; or
1348        // this's current request's state is broken, then reject promise with an "EncodingError"
1349        // DOMException.
1350        if !self.owner_document().is_fully_active() ||
1351            matches!(self.current_request.borrow().state, State::Broken)
1352        {
1353            promise.reject_error(Error::Encoding(None), can_gc);
1354        } else if matches!(
1355            self.current_request.borrow().state,
1356            State::CompletelyAvailable
1357        ) {
1358            // this doesn't follow the spec, but it's been discussed in <https://github.com/whatwg/html/issues/4217>
1359            promise.resolve_native(&(), can_gc);
1360        } else if matches!(self.current_request.borrow().state, State::Unavailable) &&
1361            self.current_request.borrow().source_url.is_none()
1362        {
1363            // Note: Despite being not explicitly stated in the specification but if current
1364            // request's state is unavailable and current URL is empty string (<img> without "src"
1365            // and "srcset" attributes) then reject promise with an "EncodingError" DOMException.
1366            // <https://github.com/whatwg/html/issues/11769>
1367            promise.reject_error(Error::Encoding(None), can_gc);
1368        } else {
1369            self.image_decode_promises.borrow_mut().push(promise);
1370        }
1371    }
1372
1373    /// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
1374    fn resolve_image_decode_promises(&self) {
1375        if self.image_decode_promises.borrow().is_empty() {
1376            return;
1377        }
1378
1379        // Step 2.3. If the decoding process completes successfully, then queue a global task on the
1380        // DOM manipulation task source with global to resolve promise with undefined.
1381        let trusted_image_decode_promises: Vec<TrustedPromise> = self
1382            .image_decode_promises
1383            .borrow()
1384            .iter()
1385            .map(|promise| TrustedPromise::new(promise.clone()))
1386            .collect();
1387
1388        self.image_decode_promises.borrow_mut().clear();
1389
1390        self.owner_global()
1391            .task_manager()
1392            .dom_manipulation_task_source()
1393            .queue(task!(fulfill_image_decode_promises: move |cx| {
1394                for trusted_promise in trusted_image_decode_promises {
1395                    trusted_promise.root().resolve_native(&(), CanGc::from_cx(cx));
1396                }
1397            }));
1398    }
1399
1400    /// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
1401    fn reject_image_decode_promises(&self) {
1402        if self.image_decode_promises.borrow().is_empty() {
1403            return;
1404        }
1405
1406        // Step 2.3. Queue a global task on the DOM manipulation task source with global to reject
1407        // promise with an "EncodingError" DOMException.
1408        let trusted_image_decode_promises: Vec<TrustedPromise> = self
1409            .image_decode_promises
1410            .borrow()
1411            .iter()
1412            .map(|promise| TrustedPromise::new(promise.clone()))
1413            .collect();
1414
1415        self.image_decode_promises.borrow_mut().clear();
1416
1417        self.owner_global()
1418            .task_manager()
1419            .dom_manipulation_task_source()
1420            .queue(task!(reject_image_decode_promises: move |cx| {
1421                for trusted_promise in trusted_image_decode_promises {
1422                    trusted_promise.root().reject_error(Error::Encoding(None), CanGc::from_cx(cx));
1423                }
1424            }));
1425    }
1426
1427    /// <https://html.spec.whatwg.org/multipage/#img-environment-changes>
1428    fn finish_reacting_to_environment_change(
1429        &self,
1430        selected_source: USVString,
1431        generation: u32,
1432        selected_pixel_density: f64,
1433    ) {
1434        // Step 16. Queue an element task on the DOM manipulation task source given the img element
1435        // and the following steps:
1436        let this = Trusted::new(self);
1437
1438        self.owner_global()
1439            .task_manager()
1440            .dom_manipulation_task_source()
1441            .queue(task!(image_load_event: move |cx| {
1442                let this = this.root();
1443
1444                // Step 16.1. If the img element has experienced relevant mutations since this
1445                // algorithm started, then set the pending request to null and abort these steps.
1446                if this.generation.get() != generation {
1447                    this.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
1448                    this.image_request.set(ImageRequestPhase::Current);
1449                    return;
1450                }
1451
1452                // Step 16.2. Set the img element's last selected source to selected source and the
1453                // img element's current pixel density to selected pixel density.
1454                *this.last_selected_source.borrow_mut() = Some(selected_source);
1455
1456                {
1457                    let mut pending_request = this.pending_request.borrow_mut();
1458
1459                    // Step 16.3. Set the image request's state to completely available.
1460                    pending_request.state = State::CompletelyAvailable;
1461
1462                    pending_request.current_pixel_density = Some(selected_pixel_density);
1463
1464                    // Step 16.4. Add the image to the list of available images using the key key,
1465                    // with the ignore higher-layer caching flag set.
1466                    // Already a part of the list of available images due to Step 15.
1467
1468                    // Step 16.5. Upgrade the pending request to the current request.
1469                    mem::swap(&mut *this.current_request.borrow_mut(), &mut *pending_request);
1470                }
1471
1472                this.abort_request(State::Unavailable, ImageRequestPhase::Pending, cx);
1473                this.image_request.set(ImageRequestPhase::Current);
1474
1475                // TODO Step 16.6. Prepare image request for presentation given the img element.
1476                this.upcast::<Node>().dirty(NodeDamage::Other);
1477
1478                // Step 16.7. Fire an event named load at the img element.
1479                this.upcast::<EventTarget>().fire_event(cx, atom!("load"));
1480            }));
1481    }
1482
1483    /// <https://html.spec.whatwg.org/multipage/#use-srcset-or-picture>
1484    fn uses_srcset_or_picture(&self) -> bool {
1485        let element = self.upcast::<Element>();
1486
1487        let has_srcset_attribute = element.has_attribute(&local_name!("srcset"));
1488        let has_parent_picture = element
1489            .upcast::<Node>()
1490            .GetParentElement()
1491            .is_some_and(|parent| parent.is::<HTMLPictureElement>());
1492        has_srcset_attribute || has_parent_picture
1493    }
1494
1495    fn new_inherited(
1496        local_name: LocalName,
1497        prefix: Option<Prefix>,
1498        document: &Document,
1499        creator: ElementCreator,
1500    ) -> HTMLImageElement {
1501        HTMLImageElement {
1502            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
1503            image_request: Cell::new(ImageRequestPhase::Current),
1504            current_request: DomRefCell::new(ImageRequest {
1505                state: State::Unavailable,
1506                parsed_url: None,
1507                source_url: None,
1508                image: None,
1509                metadata: None,
1510                blocker: DomRefCell::new(None),
1511                final_url: None,
1512                current_pixel_density: None,
1513            }),
1514            pending_request: DomRefCell::new(ImageRequest {
1515                state: State::Unavailable,
1516                parsed_url: None,
1517                source_url: None,
1518                image: None,
1519                metadata: None,
1520                blocker: DomRefCell::new(None),
1521                final_url: None,
1522                current_pixel_density: None,
1523            }),
1524            form_owner: Default::default(),
1525            generation: Default::default(),
1526            source_set: DomRefCell::new(SourceSet::new()),
1527            dimension_attribute_source: Default::default(),
1528            last_selected_source: DomRefCell::new(None),
1529            image_decode_promises: DomRefCell::new(vec![]),
1530            line_number: creator.return_line_number(),
1531        }
1532    }
1533
1534    pub(crate) fn new(
1535        cx: &mut js::context::JSContext,
1536        local_name: LocalName,
1537        prefix: Option<Prefix>,
1538        document: &Document,
1539        proto: Option<HandleObject>,
1540        creator: ElementCreator,
1541    ) -> DomRoot<HTMLImageElement> {
1542        let image_element = Node::reflect_node_with_proto(
1543            cx,
1544            Box::new(HTMLImageElement::new_inherited(
1545                local_name, prefix, document, creator,
1546            )),
1547            document,
1548            proto,
1549        );
1550        image_element
1551            .dimension_attribute_source
1552            .set(Some(image_element.upcast()));
1553        image_element
1554    }
1555
1556    pub(crate) fn areas(&self) -> Option<Vec<DomRoot<HTMLAreaElement>>> {
1557        let elem = self.upcast::<Element>();
1558        let usemap_attr = elem.get_attribute(&local_name!("usemap"))?;
1559
1560        let value = usemap_attr.value();
1561
1562        if value.is_empty() || !value.is_char_boundary(1) {
1563            return None;
1564        }
1565
1566        let (first, last) = value.split_at(1);
1567
1568        if first != "#" || last.is_empty() {
1569            return None;
1570        }
1571
1572        let useMapElements = self
1573            .owner_document()
1574            .upcast::<Node>()
1575            .traverse_preorder(ShadowIncluding::No)
1576            .filter_map(DomRoot::downcast::<HTMLMapElement>)
1577            .find(|n| {
1578                n.upcast::<Element>()
1579                    .get_name()
1580                    .is_some_and(|n| *n == *last)
1581            });
1582
1583        useMapElements.map(|mapElem| mapElem.get_area_elements())
1584    }
1585
1586    pub(crate) fn same_origin(&self, origin: &MutableOrigin) -> bool {
1587        if let Some(ref image) = self.current_request.borrow().image {
1588            return image.cors_status() == CorsStatus::Safe;
1589        }
1590
1591        self.current_request
1592            .borrow()
1593            .final_url
1594            .as_ref()
1595            .is_some_and(|url| url.scheme() == "data" || url.origin().same_origin(origin))
1596    }
1597
1598    fn generation_id(&self) -> u32 {
1599        self.generation.get()
1600    }
1601
1602    fn load_broken_image_icon(&self) {
1603        let window = self.owner_window();
1604        let Some(broken_image_icon) = window.image_cache().get_broken_image_icon() else {
1605            return;
1606        };
1607
1608        self.current_request.borrow_mut().metadata = Some(broken_image_icon.metadata);
1609        self.current_request.borrow_mut().image = Some(Image::Raster(broken_image_icon));
1610        self.upcast::<Node>().dirty(NodeDamage::Other);
1611    }
1612
1613    /// Get the full URL of the current image of this `<img>` element, returning `None` if the URL
1614    /// could not be joined with the `Document` URL.
1615    pub(crate) fn full_image_url_for_user_interface(&self) -> Option<ServoUrl> {
1616        self.owner_document()
1617            .base_url()
1618            .join(&self.CurrentSrc())
1619            .ok()
1620    }
1621}
1622
1623#[derive(JSTraceable, MallocSizeOf)]
1624pub(crate) enum ImageElementMicrotask {
1625    UpdateImageData {
1626        elem: DomRoot<HTMLImageElement>,
1627        generation: u32,
1628    },
1629    EnvironmentChanges {
1630        elem: DomRoot<HTMLImageElement>,
1631        generation: u32,
1632    },
1633    Decode {
1634        elem: DomRoot<HTMLImageElement>,
1635        #[conditional_malloc_size_of]
1636        promise: Rc<Promise>,
1637    },
1638}
1639
1640impl MicrotaskRunnable for ImageElementMicrotask {
1641    fn handler(&self, cx: &mut js::context::JSContext) {
1642        match *self {
1643            ImageElementMicrotask::UpdateImageData {
1644                ref elem,
1645                ref generation,
1646            } => {
1647                // <https://html.spec.whatwg.org/multipage/#update-the-image-data>
1648                // Step 9. If another instance of this algorithm for this img element was started
1649                // after this instance (even if it aborted and is no longer running), then return.
1650                if elem.generation.get() == *generation {
1651                    elem.update_the_image_data_sync_steps(cx);
1652                }
1653            },
1654            ImageElementMicrotask::EnvironmentChanges {
1655                ref elem,
1656                ref generation,
1657            } => {
1658                elem.react_to_environment_changes_sync_steps(*generation, cx);
1659            },
1660            ImageElementMicrotask::Decode {
1661                ref elem,
1662                ref promise,
1663            } => {
1664                elem.react_to_decode_image_sync_steps(promise.clone(), CanGc::from_cx(cx));
1665            },
1666        }
1667    }
1668
1669    fn enter_realm<'cx>(&self, cx: &'cx mut js::context::JSContext) -> AutoRealm<'cx> {
1670        match self {
1671            &ImageElementMicrotask::UpdateImageData { ref elem, .. } |
1672            &ImageElementMicrotask::EnvironmentChanges { ref elem, .. } |
1673            &ImageElementMicrotask::Decode { ref elem, .. } => enter_auto_realm(cx, &**elem),
1674        }
1675    }
1676}
1677
1678impl<'dom> LayoutDom<'dom, HTMLImageElement> {
1679    #[expect(unsafe_code)]
1680    fn current_request(self) -> &'dom ImageRequest {
1681        unsafe { self.unsafe_get().current_request.borrow_for_layout() }
1682    }
1683
1684    #[expect(unsafe_code)]
1685    fn dimension_attribute_source(self) -> LayoutDom<'dom, Element> {
1686        unsafe {
1687            self.unsafe_get()
1688                .dimension_attribute_source
1689                .get_inner_as_layout()
1690                .expect("dimension attribute source should be always non-null")
1691        }
1692    }
1693
1694    pub(crate) fn image_url(self) -> Option<ServoUrl> {
1695        self.current_request().parsed_url.clone()
1696    }
1697
1698    pub(crate) fn image_data(self) -> (Option<Image>, Option<ImageMetadata>) {
1699        let current_request = self.current_request();
1700        (current_request.image.clone(), current_request.metadata)
1701    }
1702
1703    pub(crate) fn image_density(self) -> Option<f64> {
1704        self.current_request().current_pixel_density
1705    }
1706
1707    pub(crate) fn showing_broken_image_icon(self) -> bool {
1708        matches!(self.current_request().state, State::Broken)
1709    }
1710
1711    pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
1712        self.dimension_attribute_source()
1713            .get_attr_for_layout(&ns!(), &local_name!("width"))
1714            .map(AttrValue::as_dimension)
1715            .cloned()
1716            .unwrap_or(LengthOrPercentageOrAuto::Auto)
1717    }
1718
1719    pub(crate) fn get_height(self) -> LengthOrPercentageOrAuto {
1720        self.dimension_attribute_source()
1721            .get_attr_for_layout(&ns!(), &local_name!("height"))
1722            .map(AttrValue::as_dimension)
1723            .cloned()
1724            .unwrap_or(LengthOrPercentageOrAuto::Auto)
1725    }
1726}
1727
1728/// <https://html.spec.whatwg.org/multipage/#parse-a-sizes-attribute>
1729fn parse_a_sizes_attribute(value: &str) -> SourceSizeList {
1730    let mut input = ParserInput::new(value);
1731    let mut parser = Parser::new(&mut input);
1732    let url_data = Url::parse("about:blank").unwrap().into();
1733    // FIXME(emilio): why ::empty() instead of ::DEFAULT? Also, what do
1734    // browsers do regarding quirks-mode in a media list?
1735    let context =
1736        parser_context_for_anonymous_content(CssRuleType::Style, ParsingMode::empty(), &url_data);
1737    SourceSizeList::parse(&context, &mut parser)
1738}
1739
1740impl HTMLImageElementMethods<crate::DomTypeHolder> for HTMLImageElement {
1741    /// <https://html.spec.whatwg.org/multipage/#dom-image>
1742    fn Image(
1743        cx: &mut JSContext,
1744        window: &Window,
1745        proto: Option<HandleObject>,
1746        width: Option<u32>,
1747        height: Option<u32>,
1748    ) -> Fallible<DomRoot<HTMLImageElement>> {
1749        // Step 1. Let document be the current global object's associated Document.
1750        let document = window.Document();
1751
1752        // Step 2. Let img be the result of creating an element given document, "img", and the HTML
1753        // namespace.
1754        let element = Element::create(
1755            cx,
1756            QualName::new(None, ns!(html), local_name!("img")),
1757            None,
1758            &document,
1759            ElementCreator::ScriptCreated,
1760            CustomElementCreationMode::Synchronous,
1761            proto,
1762        );
1763
1764        let image = DomRoot::downcast::<HTMLImageElement>(element).unwrap();
1765
1766        // Step 3. If width is given, then set an attribute value for img using "width" and width.
1767        if let Some(w) = width {
1768            image.SetWidth(cx, w);
1769        }
1770
1771        // Step 4. If height is given, then set an attribute value for img using "height" and
1772        // height.
1773        if let Some(h) = height {
1774            image.SetHeight(cx, h);
1775        }
1776
1777        // Step 5. Return img.
1778        Ok(image)
1779    }
1780
1781    // https://html.spec.whatwg.org/multipage/#dom-img-alt
1782    make_getter!(Alt, "alt");
1783    // https://html.spec.whatwg.org/multipage/#dom-img-alt
1784    make_setter!(SetAlt, "alt");
1785
1786    // https://html.spec.whatwg.org/multipage/#dom-img-src
1787    make_url_getter!(Src, "src");
1788
1789    // https://html.spec.whatwg.org/multipage/#dom-img-src
1790    make_url_setter!(SetSrc, "src");
1791
1792    // https://html.spec.whatwg.org/multipage/#dom-img-srcset
1793    make_url_getter!(Srcset, "srcset");
1794    // https://html.spec.whatwg.org/multipage/#dom-img-src
1795    make_url_setter!(SetSrcset, "srcset");
1796
1797    // <https://html.spec.whatwg.org/multipage/#dom-img-sizes>
1798    make_getter!(Sizes, "sizes");
1799
1800    // <https://html.spec.whatwg.org/multipage/#dom-img-sizes>
1801    make_setter!(SetSizes, "sizes");
1802
1803    /// <https://html.spec.whatwg.org/multipage/#dom-img-crossOrigin>
1804    fn GetCrossOrigin(&self) -> Option<DOMString> {
1805        reflect_cross_origin_attribute(self.upcast::<Element>())
1806    }
1807
1808    /// <https://html.spec.whatwg.org/multipage/#dom-img-crossOrigin>
1809    fn SetCrossOrigin(&self, cx: &mut JSContext, value: Option<DOMString>) {
1810        set_cross_origin_attribute(cx, self.upcast::<Element>(), value);
1811    }
1812
1813    // https://html.spec.whatwg.org/multipage/#dom-img-usemap
1814    make_getter!(UseMap, "usemap");
1815    // https://html.spec.whatwg.org/multipage/#dom-img-usemap
1816    make_setter!(SetUseMap, "usemap");
1817
1818    // https://html.spec.whatwg.org/multipage/#dom-img-ismap
1819    make_bool_getter!(IsMap, "ismap");
1820    // https://html.spec.whatwg.org/multipage/#dom-img-ismap
1821    make_bool_setter!(SetIsMap, "ismap");
1822
1823    // <https://html.spec.whatwg.org/multipage/#dom-img-width>
1824    fn Width(&self) -> u32 {
1825        let node = self.upcast::<Node>();
1826        node.content_box()
1827            .map(|rect| rect.size.width.to_px() as u32)
1828            .unwrap_or_else(|| self.NaturalWidth())
1829    }
1830
1831    // <https://html.spec.whatwg.org/multipage/#dom-img-width>
1832    make_dimension_uint_setter!(SetWidth, "width");
1833
1834    // <https://html.spec.whatwg.org/multipage/#dom-img-height>
1835    fn Height(&self) -> u32 {
1836        let node = self.upcast::<Node>();
1837        node.content_box()
1838            .map(|rect| rect.size.height.to_px() as u32)
1839            .unwrap_or_else(|| self.NaturalHeight())
1840    }
1841
1842    // <https://html.spec.whatwg.org/multipage/#dom-img-height>
1843    make_dimension_uint_setter!(SetHeight, "height");
1844
1845    /// <https://html.spec.whatwg.org/multipage/#dom-img-naturalwidth>
1846    fn NaturalWidth(&self) -> u32 {
1847        let request = self.current_request.borrow();
1848        if matches!(request.state, State::Broken) {
1849            return 0;
1850        }
1851
1852        let pixel_density = request.current_pixel_density.unwrap_or(1f64);
1853        match request.metadata {
1854            Some(ref metadata) => (metadata.width as f64 / pixel_density) as u32,
1855            None => 0,
1856        }
1857    }
1858
1859    /// <https://html.spec.whatwg.org/multipage/#dom-img-naturalheight>
1860    fn NaturalHeight(&self) -> u32 {
1861        let request = self.current_request.borrow();
1862        if matches!(request.state, State::Broken) {
1863            return 0;
1864        }
1865
1866        let pixel_density = request.current_pixel_density.unwrap_or(1f64);
1867        match request.metadata {
1868            Some(ref metadata) => (metadata.height as f64 / pixel_density) as u32,
1869            None => 0,
1870        }
1871    }
1872
1873    /// <https://html.spec.whatwg.org/multipage/#dom-img-complete>
1874    fn Complete(&self) -> bool {
1875        let element = self.upcast::<Element>();
1876
1877        // Step 1. If any of the following are true:
1878        // both the src attribute and the srcset attribute are omitted;
1879        let has_srcset_attribute = element.has_attribute(&local_name!("srcset"));
1880        if !element.has_attribute(&local_name!("src")) && !has_srcset_attribute {
1881            return true;
1882        }
1883
1884        // the srcset attribute is omitted and the src attribute's value is the empty string;
1885        let src = element.get_string_attribute(&local_name!("src"));
1886        if !has_srcset_attribute && src.is_empty() {
1887            return true;
1888        }
1889
1890        // the img element's current request's state is completely available and its pending request
1891        // is null; or the img element's current request's state is broken and its pending request
1892        // is null, then return true.
1893        if matches!(self.image_request.get(), ImageRequestPhase::Current) &&
1894            matches!(
1895                self.current_request.borrow().state,
1896                State::CompletelyAvailable | State::Broken
1897            )
1898        {
1899            return true;
1900        }
1901
1902        // Step 2. Return false.
1903        false
1904    }
1905
1906    /// <https://html.spec.whatwg.org/multipage/#dom-img-currentsrc>
1907    fn CurrentSrc(&self) -> USVString {
1908        let current_request = self.current_request.borrow();
1909        let url = &current_request.parsed_url;
1910        match *url {
1911            Some(ref url) => USVString(url.clone().into_string()),
1912            None => {
1913                let unparsed_url = &current_request.source_url;
1914                match *unparsed_url {
1915                    Some(ref url) => url.clone(),
1916                    None => USVString("".to_owned()),
1917                }
1918            },
1919        }
1920    }
1921
1922    /// <https://html.spec.whatwg.org/multipage/#dom-img-referrerpolicy>
1923    fn ReferrerPolicy(&self) -> DOMString {
1924        reflect_referrer_policy_attribute(self.upcast::<Element>())
1925    }
1926
1927    // <https://html.spec.whatwg.org/multipage/#dom-img-referrerpolicy>
1928    make_setter!(SetReferrerPolicy, "referrerpolicy");
1929
1930    /// <https://html.spec.whatwg.org/multipage/#dom-img-decode>
1931    fn Decode(&self, cx: &mut JSContext) -> Rc<Promise> {
1932        // Step 1. Let promise be a new promise.
1933        let promise = Promise::new2(cx, &self.global());
1934
1935        // Step 2. Queue a microtask to perform the following steps:
1936        let task = ImageElementMicrotask::Decode {
1937            elem: DomRoot::from_ref(self),
1938            promise: promise.clone(),
1939        };
1940
1941        ScriptThread::await_stable_state(Microtask::ImageElement(task));
1942
1943        // Step 3. Return promise.
1944        promise
1945    }
1946
1947    // https://html.spec.whatwg.org/multipage/#dom-img-name
1948    make_getter!(Name, "name");
1949
1950    // https://html.spec.whatwg.org/multipage/#dom-img-name
1951    make_atomic_setter!(SetName, "name");
1952
1953    // https://html.spec.whatwg.org/multipage/#dom-img-align
1954    make_getter!(Align, "align");
1955
1956    // https://html.spec.whatwg.org/multipage/#dom-img-align
1957    make_setter!(SetAlign, "align");
1958
1959    // https://html.spec.whatwg.org/multipage/#dom-img-hspace
1960    make_uint_getter!(Hspace, "hspace");
1961
1962    // https://html.spec.whatwg.org/multipage/#dom-img-hspace
1963    make_uint_setter!(SetHspace, "hspace");
1964
1965    // https://html.spec.whatwg.org/multipage/#dom-img-vspace
1966    make_uint_getter!(Vspace, "vspace");
1967
1968    // https://html.spec.whatwg.org/multipage/#dom-img-vspace
1969    make_uint_setter!(SetVspace, "vspace");
1970
1971    // https://html.spec.whatwg.org/multipage/#dom-img-longdesc
1972    make_url_getter!(LongDesc, "longdesc");
1973
1974    // https://html.spec.whatwg.org/multipage/#dom-img-longdesc
1975    make_url_setter!(SetLongDesc, "longdesc");
1976
1977    // https://html.spec.whatwg.org/multipage/#dom-img-border
1978    make_getter!(Border, "border");
1979
1980    // https://html.spec.whatwg.org/multipage/#dom-img-border
1981    make_setter!(SetBorder, "border");
1982}
1983
1984impl VirtualMethods for HTMLImageElement {
1985    fn super_type(&self) -> Option<&dyn VirtualMethods> {
1986        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
1987    }
1988
1989    fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
1990        self.super_type().unwrap().adopting_steps(cx, old_doc);
1991        self.update_the_image_data(cx);
1992    }
1993
1994    fn attribute_mutated(
1995        &self,
1996        cx: &mut js::context::JSContext,
1997        attr: &Attr,
1998        mutation: AttributeMutation,
1999    ) {
2000        self.super_type()
2001            .unwrap()
2002            .attribute_mutated(cx, attr, mutation);
2003        match attr.local_name() {
2004            &local_name!("src") |
2005            &local_name!("srcset") |
2006            &local_name!("width") |
2007            &local_name!("sizes") => {
2008                // <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
2009                // The element's src, srcset, width, or sizes attributes are set, changed, or
2010                // removed.
2011                self.update_the_image_data(cx);
2012            },
2013            &local_name!("crossorigin") => {
2014                // <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
2015                // The element's crossorigin attribute's state is changed.
2016                let cross_origin_state_changed = match mutation {
2017                    AttributeMutation::Removed | AttributeMutation::Set(None, _) => true,
2018                    AttributeMutation::Set(Some(old_value), _) => {
2019                        let new_cors_setting =
2020                            CorsSettings::from_enumerated_attribute(&attr.value());
2021                        let old_cors_setting = CorsSettings::from_enumerated_attribute(old_value);
2022
2023                        new_cors_setting != old_cors_setting
2024                    },
2025                };
2026
2027                if cross_origin_state_changed {
2028                    self.update_the_image_data(cx);
2029                }
2030            },
2031            &local_name!("referrerpolicy") => {
2032                // <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
2033                // The element's referrerpolicy attribute's state is changed.
2034                let referrer_policy_state_changed = match mutation {
2035                    AttributeMutation::Removed | AttributeMutation::Set(None, _) => {
2036                        ReferrerPolicy::from(&**attr.value()) != ReferrerPolicy::EmptyString
2037                    },
2038                    AttributeMutation::Set(Some(old_value), _) => {
2039                        ReferrerPolicy::from(&**attr.value()) != ReferrerPolicy::from(&**old_value)
2040                    },
2041                };
2042
2043                if referrer_policy_state_changed {
2044                    self.update_the_image_data(cx);
2045                }
2046            },
2047            _ => {},
2048        }
2049    }
2050
2051    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
2052        match attr.local_name() {
2053            &local_name!("width") | &local_name!("height") => true,
2054            _ => self
2055                .super_type()
2056                .unwrap()
2057                .attribute_affects_presentational_hints(attr),
2058        }
2059    }
2060
2061    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
2062        match name {
2063            &local_name!("width") | &local_name!("height") => {
2064                AttrValue::from_dimension(value.into())
2065            },
2066            &local_name!("hspace") | &local_name!("vspace") => AttrValue::from_u32(value.into(), 0),
2067            _ => self
2068                .super_type()
2069                .unwrap()
2070                .parse_plain_attribute(name, value),
2071        }
2072    }
2073
2074    fn handle_event(&self, cx: &mut js::context::JSContext, event: &Event) {
2075        if event.type_() != atom!("click") {
2076            return;
2077        }
2078
2079        let area_elements = self.areas();
2080        let elements = match area_elements {
2081            Some(x) => x,
2082            None => return,
2083        };
2084
2085        // Fetch click coordinates
2086        let mouse_event = match event.downcast::<MouseEvent>() {
2087            Some(x) => x,
2088            None => return,
2089        };
2090
2091        let point = Point2D::new(
2092            mouse_event.ClientX().to_f32().unwrap(),
2093            mouse_event.ClientY().to_f32().unwrap(),
2094        );
2095        let bcr = self.upcast::<Element>().GetBoundingClientRect(cx);
2096        let bcr_p = Point2D::new(bcr.X() as f32, bcr.Y() as f32);
2097
2098        // Walk HTMLAreaElements
2099        for element in elements {
2100            let shape = element.get_shape_from_coords();
2101            let shp = match shape {
2102                Some(x) => x.absolute_coords(bcr_p),
2103                None => return,
2104            };
2105            if shp.hit_test(&point) {
2106                element.activation_behavior(cx, event, self.upcast());
2107                return;
2108            }
2109        }
2110    }
2111
2112    /// <https://html.spec.whatwg.org/multipage/#the-img-element:html-element-insertion-steps>
2113    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
2114        if let Some(s) = self.super_type() {
2115            s.bind_to_tree(cx, context);
2116        }
2117        let document = self.owner_document();
2118        if context.tree_connected {
2119            document.register_responsive_image(self);
2120        }
2121
2122        let parent = self.upcast::<Node>().GetParentNode().unwrap();
2123
2124        // Step 1. If insertedNode's parent is a picture element, then, count this as a relevant
2125        // mutation for insertedNode.
2126        if parent.is::<HTMLPictureElement>() && std::ptr::eq(&*parent, context.parent) {
2127            self.update_the_image_data(cx);
2128        }
2129    }
2130
2131    /// <https://html.spec.whatwg.org/multipage/#the-img-element:html-element-removing-steps>
2132    fn unbind_from_tree(&self, cx: &mut js::context::JSContext, context: &UnbindContext) {
2133        self.super_type().unwrap().unbind_from_tree(cx, context);
2134        let document = self.owner_document();
2135        document.unregister_responsive_image(self);
2136
2137        // Step 1. If oldParent is a picture element, then, count this as a relevant mutation for
2138        // removedNode.
2139        if context.parent.is::<HTMLPictureElement>() && !self.upcast::<Node>().has_parent() {
2140            self.update_the_image_data(cx);
2141        }
2142    }
2143
2144    /// <https://html.spec.whatwg.org/multipage#the-img-element:html-element-moving-steps>
2145    fn moving_steps(&self, cx: &mut JSContext, context: &MoveContext) {
2146        if let Some(super_type) = self.super_type() {
2147            super_type.moving_steps(cx, context);
2148        }
2149
2150        // Step 1. If oldParent is a picture element, then, count this as a relevant mutation for movedNode.
2151        if let Some(old_parent) = context.old_parent {
2152            if old_parent.is::<HTMLPictureElement>() {
2153                self.update_the_image_data(cx);
2154            }
2155        }
2156    }
2157}
2158
2159impl FormControl for HTMLImageElement {
2160    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
2161        self.form_owner.get()
2162    }
2163
2164    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
2165        self.form_owner.set(form);
2166    }
2167
2168    fn to_element(&self) -> &Element {
2169        self.upcast::<Element>()
2170    }
2171
2172    fn is_listed(&self) -> bool {
2173        false
2174    }
2175}
2176
2177/// Collect sequence of code points
2178/// <https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points>
2179pub(crate) fn collect_sequence_characters(
2180    s: &str,
2181    mut predicate: impl FnMut(&char) -> bool,
2182) -> (&str, &str) {
2183    let i = s.find(|ch| !predicate(&ch)).unwrap_or(s.len());
2184    (&s[0..i], &s[i..])
2185}
2186
2187/// <https://html.spec.whatwg.org/multipage/#valid-non-negative-integer>
2188/// TODO(#39315): Use the validation rule from Stylo
2189fn is_valid_non_negative_integer_string(s: &str) -> bool {
2190    s.chars().all(|c| c.is_ascii_digit())
2191}
2192
2193/// <https://html.spec.whatwg.org/multipage/#valid-floating-point-number>
2194/// TODO(#39315): Use the validation rule from Stylo
2195fn is_valid_floating_point_number_string(s: &str) -> bool {
2196    static RE: LazyLock<Regex> =
2197        LazyLock::new(|| Regex::new(r"^-?(?:\d+\.\d+|\d+|\.\d+)(?:(e|E)(\+|\-)?\d+)?$").unwrap());
2198
2199    RE.is_match(s)
2200}
2201
2202/// Parse an `srcset` attribute:
2203/// <https://html.spec.whatwg.org/multipage/#parsing-a-srcset-attribute>.
2204pub fn parse_a_srcset_attribute(input: &str) -> Vec<ImageSource> {
2205    // > 1. Let input be the value passed to this algorithm.
2206    // > 2. Let position be a pointer into input, initially pointing at the start of the string.
2207    let mut current_index = 0;
2208
2209    // > 3. Let candidates be an initially empty source set.
2210    let mut candidates = vec![];
2211    while current_index < input.len() {
2212        let remaining_string = &input[current_index..];
2213
2214        // > 4. Splitting loop: Collect a sequence of code points that are ASCII whitespace or
2215        // > U+002C COMMA characters from input given position. If any U+002C COMMA
2216        // > characters were collected, that is a parse error.
2217        // NOTE: A parse error indicating a non-fatal mismatch between the input and the
2218        // requirements will be silently ignored to match the behavior of other browsers.
2219        // <https://html.spec.whatwg.org/multipage/#concept-microsyntax-parse-error>
2220        let (collected_characters, string_after_whitespace) =
2221            collect_sequence_characters(remaining_string, |character| {
2222                *character == ',' || character.is_ascii_whitespace()
2223            });
2224
2225        // Add the length of collected whitespace, to find the start of the URL we are going
2226        // to parse.
2227        current_index += collected_characters.len();
2228
2229        // > 5. If position is past the end of input, return candidates.
2230        if string_after_whitespace.is_empty() {
2231            return candidates;
2232        }
2233
2234        // 6. Collect a sequence of code points that are not ASCII whitespace from input
2235        // given position, and let that be url.
2236        let (url, _) =
2237            collect_sequence_characters(string_after_whitespace, |c| !char::is_ascii_whitespace(c));
2238
2239        // Add the length of `url` that we will parse to advance the index of the next part
2240        // of the string to prase.
2241        current_index += url.len();
2242
2243        // 7. Let descriptors be a new empty list.
2244        let mut descriptors = Vec::new();
2245
2246        // > 8. If url ends with U+002C (,), then:
2247        // >    1. Remove all trailing U+002C COMMA characters from url. If this removed
2248        // >       more than one character, that is a parse error.
2249        if url.ends_with(',') {
2250            let image_source = ImageSource {
2251                url: url.trim_end_matches(',').into(),
2252                descriptor: Descriptor {
2253                    width: None,
2254                    density: None,
2255                },
2256            };
2257            candidates.push(image_source);
2258            continue;
2259        }
2260
2261        // Otherwise:
2262        // > 8.1. Descriptor tokenizer: Skip ASCII whitespace within input given position.
2263        let descriptors_string = &input[current_index..];
2264        let (spaces, descriptors_string) =
2265            collect_sequence_characters(descriptors_string, |character| {
2266                character.is_ascii_whitespace()
2267            });
2268        current_index += spaces.len();
2269
2270        // > 8.2. Let current descriptor be the empty string.
2271        let mut current_descriptor = String::new();
2272
2273        // > 8.3. Let state be "in descriptor".
2274        let mut state = ParseState::InDescriptor;
2275
2276        // > 8.4. Let c be the character at position. Do the following depending on the value of
2277        // > state. For the purpose of this step, "EOF" is a special character representing
2278        // > that position is past the end of input.
2279        let mut characters = descriptors_string.chars();
2280        let mut character = characters.next();
2281        if let Some(character) = character {
2282            current_index += character.len_utf8();
2283        }
2284
2285        loop {
2286            match (state, character) {
2287                (ParseState::InDescriptor, Some(character)) if character.is_ascii_whitespace() => {
2288                    // > If current descriptor is not empty, append current descriptor to
2289                    // > descriptors and let current descriptor be the empty string. Set
2290                    // > state to after descriptor.
2291                    if !current_descriptor.is_empty() {
2292                        descriptors.push(current_descriptor);
2293                        current_descriptor = String::new();
2294                        state = ParseState::AfterDescriptor;
2295                    }
2296                },
2297                (ParseState::InDescriptor, Some(',')) => {
2298                    // > Advance position to the next character in input. If current descriptor
2299                    // > is not empty, append current descriptor to descriptors. Jump to the
2300                    // > step labeled descriptor parser.
2301                    if !current_descriptor.is_empty() {
2302                        descriptors.push(current_descriptor);
2303                    }
2304                    break;
2305                },
2306                (ParseState::InDescriptor, Some('(')) => {
2307                    // > Append c to current descriptor. Set state to in parens.
2308                    current_descriptor.push('(');
2309                    state = ParseState::InParens;
2310                },
2311                (ParseState::InDescriptor, Some(character)) => {
2312                    // > Append c to current descriptor.
2313                    current_descriptor.push(character);
2314                },
2315                (ParseState::InDescriptor, None) => {
2316                    // > If current descriptor is not empty, append current descriptor to
2317                    // > descriptors. Jump to the step labeled descriptor parser.
2318                    if !current_descriptor.is_empty() {
2319                        descriptors.push(current_descriptor);
2320                    }
2321                    break;
2322                },
2323                (ParseState::InParens, Some(')')) => {
2324                    // > Append c to current descriptor. Set state to in descriptor.
2325                    current_descriptor.push(')');
2326                    state = ParseState::InDescriptor;
2327                },
2328                (ParseState::InParens, Some(character)) => {
2329                    // Append c to current descriptor.
2330                    current_descriptor.push(character);
2331                },
2332                (ParseState::InParens, None) => {
2333                    // > Append current descriptor to descriptors. Jump to the step
2334                    // > labeled descriptor parser.
2335                    descriptors.push(current_descriptor);
2336                    break;
2337                },
2338                (ParseState::AfterDescriptor, Some(character))
2339                    if character.is_ascii_whitespace() =>
2340                {
2341                    // > Stay in this state.
2342                },
2343                (ParseState::AfterDescriptor, Some(_)) => {
2344                    // > Set state to in descriptor. Set position to the previous
2345                    // > character in input.
2346                    state = ParseState::InDescriptor;
2347                    continue;
2348                },
2349                (ParseState::AfterDescriptor, None) => {
2350                    // > Jump to the step labeled descriptor parser.
2351                    break;
2352                },
2353            }
2354
2355            character = characters.next();
2356            if let Some(character) = character {
2357                current_index += character.len_utf8();
2358            }
2359        }
2360
2361        // > 9. Descriptor parser: Let error be no.
2362        let mut error = false;
2363        // > 10. Let width be absent.
2364        let mut width: Option<u32> = None;
2365        // > 11. Let density be absent.
2366        let mut density: Option<f64> = None;
2367        // > 12. Let future-compat-h be absent.
2368        let mut future_compat_h: Option<u32> = None;
2369
2370        // > 13. For each descriptor in descriptors, run the appropriate set of steps from
2371        // > the following list:
2372        for descriptor in descriptors.into_iter() {
2373            let Some(last_character) = descriptor.chars().last() else {
2374                break;
2375            };
2376
2377            let first_part_of_string = &descriptor[0..descriptor.len() - last_character.len_utf8()];
2378            match last_character {
2379                // > If the descriptor consists of a valid non-negative integer followed by a
2380                // > U+0077 LATIN SMALL LETTER W character
2381                // > 1. If the user agent does not support the sizes attribute, let error be yes.
2382                // > 2. If width and density are not both absent, then let error be yes.
2383                // > 3. Apply the rules for parsing non-negative integers to the descriptor.
2384                // >    If the result is 0, let error be yes. Otherwise, let width be the result.
2385                'w' if is_valid_non_negative_integer_string(first_part_of_string) &&
2386                    density.is_none() &&
2387                    width.is_none() =>
2388                {
2389                    match parse_unsigned_integer(first_part_of_string.chars()) {
2390                        Ok(number) if number > 0 => {
2391                            width = Some(number);
2392                            continue;
2393                        },
2394                        _ => error = true,
2395                    }
2396                },
2397
2398                // > If the descriptor consists of a valid floating-point number followed by a
2399                // > U+0078 LATIN SMALL LETTER X character
2400                // > 1. If width, density and future-compat-h are not all absent, then let
2401                // >    error be yes.
2402                // > 2. Apply the rules for parsing floating-point number values to the
2403                // >    descriptor. If the result is less than 0, let error be yes. Otherwise, let
2404                // >    density be the result.
2405                //
2406                // The HTML specification has a procedure for parsing floats that is different enough from
2407                // the one that stylo uses, that it's better to use Rust's float parser here. This is
2408                // what Gecko does, but it also checks to see if the number is a valid HTML-spec compliant
2409                // number first. Not doing that means that we might be parsing numbers that otherwise
2410                // wouldn't parse.
2411                'x' if is_valid_floating_point_number_string(first_part_of_string) &&
2412                    width.is_none() &&
2413                    density.is_none() &&
2414                    future_compat_h.is_none() =>
2415                {
2416                    match first_part_of_string.parse::<f64>() {
2417                        Ok(number) if number.is_finite() && number >= 0. => {
2418                            density = Some(number);
2419                            continue;
2420                        },
2421                        _ => error = true,
2422                    }
2423                },
2424
2425                // > If the descriptor consists of a valid non-negative integer followed by a
2426                // > U+0068 LATIN SMALL LETTER H character
2427                // >   This is a parse error.
2428                // > 1. If future-compat-h and density are not both absent, then let error be
2429                // >    yes.
2430                // > 2. Apply the rules for parsing non-negative integers to the descriptor.
2431                // >    If the result is 0, let error be yes. Otherwise, let future-compat-h be the
2432                // >    result.
2433                'h' if is_valid_non_negative_integer_string(first_part_of_string) &&
2434                    future_compat_h.is_none() &&
2435                    density.is_none() =>
2436                {
2437                    match parse_unsigned_integer(first_part_of_string.chars()) {
2438                        Ok(number) if number > 0 => {
2439                            future_compat_h = Some(number);
2440                            continue;
2441                        },
2442                        _ => error = true,
2443                    }
2444                },
2445
2446                // > Anything else
2447                // >  Let error be yes.
2448                _ => error = true,
2449            }
2450
2451            if error {
2452                break;
2453            }
2454        }
2455
2456        // > 14. If future-compat-h is not absent and width is absent, let error be yes.
2457        if future_compat_h.is_some() && width.is_none() {
2458            error = true;
2459        }
2460
2461        // Step 15. If error is still no, then append a new image source to candidates whose URL is
2462        // url, associated with a width width if not absent and a pixel density density if not
2463        // absent. Otherwise, there is a parse error.
2464        if !error {
2465            let image_source = ImageSource {
2466                url: url.into(),
2467                descriptor: Descriptor { width, density },
2468            };
2469            candidates.push(image_source);
2470        }
2471
2472        // Step 16. Return to the step labeled splitting loop.
2473    }
2474    candidates
2475}
2476
2477#[derive(Clone)]
2478enum ChangeType {
2479    Environment {
2480        selected_source: USVString,
2481        selected_pixel_density: f64,
2482    },
2483    Element,
2484}
2485
2486/// Returns true if the given image MIME type is supported.
2487fn is_supported_image_mime_type(input: &str) -> bool {
2488    // Remove any leading and trailing HTTP whitespace from input.
2489    let mime_type = input.trim();
2490
2491    // <https://mimesniff.spec.whatwg.org/#mime-type-essence>
2492    let mime_type_essence = match mime_type.find(';') {
2493        Some(semi) => &mime_type[..semi],
2494        _ => mime_type,
2495    };
2496
2497    // The HTML specification says the type attribute may be present and if present, the value
2498    // must be a valid MIME type string. However an empty type attribute is implicitly supported
2499    // to match the behavior of other browsers.
2500    // <https://html.spec.whatwg.org/multipage/#attr-source-type>
2501    if mime_type_essence.is_empty() {
2502        return true;
2503    }
2504
2505    SUPPORTED_IMAGE_MIME_TYPES.contains(&mime_type_essence)
2506}