Skip to main content

script/dom/html/
htmlimageelement.rs

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