Skip to main content

script/dom/html/
htmlimageelement.rs

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