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