Skip to main content

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