fix(images): track current request transitions

This commit is contained in:
ldm0
2026-11-20 22:47:33 +08:00
parent 7926b9f4bb
commit 42db7643eb
16 changed files with 901 additions and 404 deletions
@@ -2854,7 +2854,6 @@ html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-n
html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.html
html/semantics/embedded-content/the-img-element/image-loading-lazy-in-viewport-dynamic.html
html/semantics/embedded-content/the-img-element/image-loading-lazy-move-document.html
html/semantics/embedded-content/the-img-element/img-picture-ancestor.html
html/semantics/embedded-content/the-img-element/natural-size-orientation.html
html/semantics/embedded-content/the-img-element/naturalWidth-naturalHeight-width-height.html
html/semantics/embedded-content/the-img-element/relevant-mutations-lazy.html
@@ -2866,7 +2865,6 @@ html/semantics/embedded-content/the-img-element/source-media-outside-doc.html
html/semantics/embedded-content/the-img-element/source-relevant-mutations.html
html/semantics/embedded-content/the-img-element/svg-img-density-vs-zoom.html
html/semantics/embedded-content/the-img-element/update-the-image-data/current-request-microtask-002.html
html/semantics/embedded-content/the-img-element/update-the-image-data/current-request-microtask.html
html/semantics/embedded-content/the-img-element/update-the-image-data/lazy-out-of-band-load.html
html/semantics/embedded-content/the-img-element/update-the-image-data/not-broken-while-load-task-scheduled.html
html/semantics/embedded-content/the-img-element/update-the-image-data/src-then-lazy-load.html
@@ -6895,6 +6895,7 @@ html/semantics/embedded-content/the-img-element/image-loading-lazy-subframe-deta
html/semantics/embedded-content/the-img-element/image-loading-lazy-to-eager.html
html/semantics/embedded-content/the-img-element/image-loading-lazy-use-list-of-available-images.html
html/semantics/embedded-content/the-img-element/img-created-in-active-document-crash.html
html/semantics/embedded-content/the-img-element/img-picture-ancestor.html
html/semantics/embedded-content/the-img-element/img-src-in-synthetic-document.html
html/semantics/embedded-content/the-img-element/img.complete.html
html/semantics/embedded-content/the-img-element/invalid-src.html
@@ -6915,6 +6916,7 @@ html/semantics/embedded-content/the-img-element/srcset/select-an-image-source.ht
html/semantics/embedded-content/the-img-element/srcset/srcset-media-dynamic.html
html/semantics/embedded-content/the-img-element/update-media.html
html/semantics/embedded-content/the-img-element/update-src-complete.html
html/semantics/embedded-content/the-img-element/update-the-image-data/current-request-microtask.html
html/semantics/embedded-content/the-img-element/update-the-image-data/fail-to-resolve.html
html/semantics/embedded-content/the-object-element/block-object-with-ruby-crash.html
html/semantics/embedded-content/the-object-element/document-getters-return-null-for-cross-origin.html
@@ -486,6 +486,7 @@ impl PageScriptHost {
super::user_interaction_tasks::UserInteractionTaskState::default(),
pending_image_load_events: HashMap::new(),
next_image_load_event_id: 1,
current_image_requests: HashMap::new(),
pending_media_load_sequences: HashMap::new(),
next_media_load_sequence_id: 1,
pending_text_track_load_sequences: HashMap::new(),
@@ -1689,7 +1690,7 @@ impl PageScriptHost {
&self,
handle: DomHandle,
) -> Option<super::PendingImageLoadEvent> {
self.pending_image_load_events.get(&handle).copied()
self.pending_image_load_events.get(&handle).cloned()
}
pub(crate) fn next_image_load_event_id(&mut self) -> super::ImageLoadEventId {
@@ -1832,6 +1833,24 @@ impl PageScriptHost {
self.lazy_media_load_candidates.iter().copied().collect()
}
pub(crate) fn set_current_image_request(
&mut self,
handle: DomHandle,
request_key: Option<crate::types::ImageRequestKey>,
) {
if let Some(request_key) = request_key {
self.current_image_requests.insert(handle, request_key);
} else {
self.current_image_requests.remove(&handle);
}
}
pub(crate) fn current_image_request_url(&self, handle: DomHandle) -> Option<&str> {
self.current_image_requests
.get(&handle)
.map(crate::types::ImageRequestKey::url)
}
pub(crate) fn begin_form_data_construction(&mut self, form_handle: DomHandle) -> bool {
if self.constructing_form_data_forms.contains(&form_handle) {
return false;
@@ -106,7 +106,8 @@ impl PageScriptHost {
.pending_image_load_events
.get(&request.element)
.is_some_and(|pending| {
self.pending_image_load_event_is_current(request.element, *pending)
self.pending_image_load_event_is_current(request.element, pending)
&& pending.request_key() == Some(&request.request_key)
&& !matches!(
pending.network_state,
PendingImageLoadNetworkState::Failed(_)
@@ -352,5 +353,8 @@ fn image_source_can_decode(runtime: &PageScriptHost, handle: DomHandle, source:
{
return false;
}
runtime.image_resource_is_ready(handle) && image_intrinsic_dimensions(runtime, handle).is_some()
(runtime.image_resource_is_ready(handle)
&& image_intrinsic_dimensions(runtime, handle).is_some())
|| image_selected_request_key(runtime, handle)
.is_some_and(|request_key| runtime.has_ready_image_request(&request_key))
}
@@ -97,11 +97,10 @@ impl PageScriptHost {
request_initiator_type,
resource_timing_initiator_type,
);
if !self.insert_pending_image_load_event(image_handle, pending) {
if !self.insert_pending_image_load_event(image_handle, pending.clone()) {
let _ = self.settle_pending_image_load_event(pending, true);
return None;
}
self.begin_pending_image_resource(image_handle, pending);
match pending.owner() {
PendingImageLoadEventOwner::Main(binding) => tracing::debug!(
image = image_handle.index(),
@@ -125,7 +124,7 @@ impl PageScriptHost {
pub(crate) fn pending_image_load_event_is_current(
&self,
image_handle: DomHandle,
pending: PendingImageLoadEvent,
pending: &PendingImageLoadEvent,
) -> bool {
if self.dom_host().owner_document_handle(image_handle)
!= Some(pending.owner_document_handle())
@@ -145,6 +144,34 @@ impl PageScriptHost {
}
}
pub(crate) fn prepare_pending_image_load_request_if_matches(
&mut self,
image_handle: DomHandle,
id: ImageLoadEventId,
request_key: crate::types::ImageRequestKey,
) -> bool {
let pending = {
let Some(pending) = self.pending_image_load_events.get_mut(&image_handle) else {
return false;
};
if pending.id != id
|| pending.request_key.is_some()
|| pending.network_state != PendingImageLoadNetworkState::Unbound
{
return false;
}
pending.request_key = Some(request_key);
pending.clone()
};
self.begin_pending_image_resource(image_handle, &pending);
tracing::debug!(
image = image_handle.index(),
sequence = id.get(),
"selected image request identity"
);
true
}
pub(crate) fn bind_pending_image_load_network_request_if_matches(
&mut self,
image_handle: DomHandle,
@@ -154,7 +181,10 @@ impl PageScriptHost {
let Some(pending) = self.pending_image_load_events.get_mut(&image_handle) else {
return false;
};
if pending.id != id || pending.network_state != PendingImageLoadNetworkState::Unbound {
if pending.id != id
|| pending.request_key.is_none()
|| pending.network_state != PendingImageLoadNetworkState::Unbound
{
return false;
}
pending.network_state = PendingImageLoadNetworkState::Pending(internal_id);
@@ -173,24 +203,32 @@ impl PageScriptHost {
id: ImageLoadEventId,
successful: bool,
) -> Option<RendererPageImageLoadEventKind> {
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
if pending.id != id || pending.network_state != PendingImageLoadNetworkState::Unbound {
return None;
}
self.pending_image_load_events
.get(&image_handle)
.filter(|pending| {
pending.id == id && pending.network_state == PendingImageLoadNetworkState::Unbound
})?;
let _ = self.retire_image_resource_for_element(image_handle);
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
pending.network_state = if successful {
PendingImageLoadNetworkState::Ready(PendingImageLoadTerminalSource::Local)
} else {
PendingImageLoadNetworkState::Failed(PendingImageLoadTerminalSource::Local)
let (followup, current_request) = {
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
pending.network_state = if successful {
PendingImageLoadNetworkState::Ready(PendingImageLoadTerminalSource::Local)
} else {
PendingImageLoadNetworkState::Failed(PendingImageLoadTerminalSource::Local)
};
(
pending_image_load_terminal_followup(pending),
pending.request_key.clone(),
)
};
self.set_current_image_request(image_handle, current_request);
tracing::debug!(
image = image_handle.index(),
sequence = id.get(),
successful,
"completed local or policy image resource"
);
pending_image_load_terminal_followup(pending)
followup
}
pub(crate) fn complete_pending_image_load_reused_resource_if_matches(
@@ -201,18 +239,25 @@ impl PageScriptHost {
if !self.image_resource_is_ready(image_handle) {
return None;
}
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
if pending.id != id || pending.network_state != PendingImageLoadNetworkState::Unbound {
return None;
}
pending.network_state =
PendingImageLoadNetworkState::Ready(PendingImageLoadTerminalSource::Reused);
let (followup, current_request) = {
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
if pending.id != id || pending.network_state != PendingImageLoadNetworkState::Unbound {
return None;
}
pending.network_state =
PendingImageLoadNetworkState::Ready(PendingImageLoadTerminalSource::Reused);
(
pending_image_load_terminal_followup(pending),
pending.request_key.clone(),
)
};
self.set_current_image_request(image_handle, current_request);
tracing::debug!(
image = image_handle.index(),
sequence = id.get(),
"reused active decoded image resource without another fetch or decode"
);
pending_image_load_terminal_followup(pending)
followup
}
pub(crate) fn complete_pending_image_load_network_request_if_matches(
@@ -222,9 +267,13 @@ impl PageScriptHost {
internal_id: u64,
successful: bool,
) -> Option<RendererPageImageLoadEventKind> {
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
if pending.id != id
|| pending.network_state != PendingImageLoadNetworkState::Pending(internal_id)
if !self
.pending_image_load_events
.get(&image_handle)
.is_some_and(|pending| {
pending.id == id
&& pending.network_state == PendingImageLoadNetworkState::Pending(internal_id)
})
{
tracing::debug!(
image = image_handle.index(),
@@ -236,12 +285,19 @@ impl PageScriptHost {
return None;
}
let _ = self.retire_image_resource_for_element(image_handle);
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
pending.network_state = if successful {
PendingImageLoadNetworkState::Ready(PendingImageLoadTerminalSource::Network)
} else {
PendingImageLoadNetworkState::Failed(PendingImageLoadTerminalSource::Network)
let (followup, current_request) = {
let pending = self.pending_image_load_events.get_mut(&image_handle)?;
pending.network_state = if successful {
PendingImageLoadNetworkState::Ready(PendingImageLoadTerminalSource::Network)
} else {
PendingImageLoadNetworkState::Failed(PendingImageLoadTerminalSource::Network)
};
(
pending_image_load_terminal_followup(pending),
pending.request_key.clone(),
)
};
self.set_current_image_request(image_handle, current_request);
tracing::debug!(
image = image_handle.index(),
sequence = id.get(),
@@ -249,7 +305,7 @@ impl PageScriptHost {
successful,
"applied image network terminal to lifecycle sequence"
);
pending_image_load_terminal_followup(pending)
followup
}
pub(crate) fn pending_image_load_terminal_followup_if_ready(
@@ -271,7 +327,7 @@ impl PageScriptHost {
let pending = self
.pending_image_load_event(task_id.element())
.filter(|pending| pending.id() == task_id.sequence())?;
if !self.pending_image_load_event_is_current(task_id.element(), pending) {
if !self.pending_image_load_event_is_current(task_id.element(), &pending) {
return None;
}
let kind = pending
@@ -90,7 +90,7 @@ impl ImageResponseCompletion {
}
impl super::PendingImageLoadEvent {
fn document_task_owner(self) -> FrameDocumentTaskOwner {
fn document_task_owner(&self) -> FrameDocumentTaskOwner {
match self.owner() {
super::PendingImageLoadEventOwner::Main(binding) => binding.owner(),
super::PendingImageLoadEventOwner::Child(binding) => binding.owner(),
@@ -114,21 +114,26 @@ impl super::PageScriptHost {
pub(super) fn begin_pending_image_resource(
&mut self,
element: DomHandle,
pending: super::PendingImageLoadEvent,
pending: &super::PendingImageLoadEvent,
) {
let Some(request_key) =
crate::native_bridge::element::image_selected_request_key(self, element)
else {
let Some(request_key) = pending.request_key().cloned() else {
self.image_resources.retire_element(element);
return;
};
self.image_resources.begin(ImageResourceRequestIdentity {
element,
sequence: pending.id(),
document_handle: pending.owner_document_handle(),
document_owner: pending.document_task_owner(),
request_key,
});
let retain_current = self.dom_host().is_html_element_named(element, "img");
let retained_current = self.image_resources.begin(
ImageResourceRequestIdentity {
element,
sequence: pending.id(),
document_handle: pending.owner_document_handle(),
document_owner: pending.document_task_owner(),
request_key: request_key.clone(),
},
retain_current,
);
if !retained_current {
self.set_current_image_request(element, Some(request_key));
}
}
pub(crate) fn retire_image_resource_for_element(&mut self, element: DomHandle) -> bool {
@@ -330,14 +335,16 @@ impl super::PageScriptHost {
descriptor: Option<ImageResponseDescriptor>,
encoded: ParkableImage,
) -> ImageResponseCompletion {
self.complete_pending_image_response_if_matches(
let completion = self.complete_pending_image_response_if_matches(
element,
sequence,
None,
super::PendingImageLoadTerminalSource::Local,
descriptor,
encoded,
)
);
self.update_current_image_request_after_response(element, completion);
completion
}
pub(crate) fn complete_pending_image_load_network_response_if_matches(
@@ -348,14 +355,34 @@ impl super::PageScriptHost {
descriptor: Option<ImageResponseDescriptor>,
encoded: ParkableImage,
) -> ImageResponseCompletion {
self.complete_pending_image_response_if_matches(
let completion = self.complete_pending_image_response_if_matches(
element,
sequence,
Some(internal_id),
super::PendingImageLoadTerminalSource::Network,
descriptor,
encoded,
)
);
self.update_current_image_request_after_response(element, completion);
completion
}
fn update_current_image_request_after_response(
&mut self,
element: DomHandle,
completion: ImageResponseCompletion,
) {
if !completion.accepted() {
return;
}
let Some(pending) = self
.pending_image_load_events
.get(&element)
.filter(|pending| pending.terminal_source().is_some())
else {
return;
};
self.set_current_image_request(element, pending.request_key().cloned());
}
fn complete_pending_image_response_if_matches(
@@ -568,18 +595,22 @@ impl super::PageScriptHost {
false
}
};
let Some(pending) = self.pending_image_load_events.get_mut(&task_id.element()) else {
return false;
let request_key = {
let Some(pending) = self.pending_image_load_events.get_mut(&task_id.element()) else {
return false;
};
if pending.id != task_id.sequence() {
return false;
}
pending.network_state = if successful {
super::PendingImageLoadNetworkState::Ready(source)
} else {
super::PendingImageLoadNetworkState::Failed(source)
};
pending.terminal_followup_queued = true;
pending.request_key.clone()
};
if pending.id != task_id.sequence() {
return false;
}
pending.network_state = if successful {
super::PendingImageLoadNetworkState::Ready(source)
} else {
super::PendingImageLoadNetworkState::Failed(source)
};
pending.terminal_followup_queued = true;
self.set_current_image_request(task_id.element(), request_key);
true
}
}
@@ -591,7 +591,7 @@ mod tests {
document_owner: owner(1),
request_key: key,
};
resources.begin(identity.clone());
resources.begin(identity.clone(), false);
let claimed = resources
.claim_scanned_preload(element)
.expect("the exact element should claim the decoded preload");
@@ -36,9 +36,19 @@ pub(in crate::native_bridge::context_host) struct ImageResourceStore {
struct ImageResourceSlot {
identity: ImageResourceRequestIdentity,
state: ImageResourceState,
retained_current: Option<Arc<ReadyImageResource>>,
scanned_preload: Option<SharedScannedImagePreloadLoad>,
}
impl ImageResourceSlot {
fn current_ready(&self) -> Option<&Arc<ReadyImageResource>> {
match &self.state {
ImageResourceState::Ready(resource) => Some(resource),
_ => self.retained_current.as_ref(),
}
}
}
enum ImageResourceState {
Pending,
DecodeQueued(ImageResponseDescriptor),
@@ -118,17 +128,38 @@ impl ImageResourceStore {
}
}
pub(super) fn begin(&mut self, identity: ImageResourceRequestIdentity) {
pub(super) fn begin(
&mut self,
identity: ImageResourceRequestIdentity,
retain_current: bool,
) -> bool {
let ready = self.ready_by_request.get(&identity.request_key);
// The latest admission owns transport and decode. An available HTML
// image keeps its current response until the replacement settles.
let retained_current = if retain_current && ready.is_none() {
self.slots
.get(&identity.element)
.filter(|slot| {
slot.identity.document_handle == identity.document_handle
&& slot.identity.document_owner == identity.document_owner
})
.and_then(ImageResourceSlot::current_ready)
.cloned()
} else {
None
};
let has_retained_current = retained_current.is_some();
self.slots.insert(
identity.element,
ImageResourceSlot {
identity,
state: ready.map_or(ImageResourceState::Pending, ImageResourceState::Ready),
retained_current,
scanned_preload: None,
},
);
self.visual_generation.bump();
has_retained_current
}
pub(super) fn identity(&self, element: DomHandle) -> Option<&ImageResourceRequestIdentity> {
@@ -219,6 +250,7 @@ impl ImageResourceStore {
}
ImageResourceState::Pending => {
slot.state = ImageResourceState::Ready(resource);
slot.retained_current = None;
slot.scanned_preload = None;
self.visual_generation.bump();
true
@@ -251,6 +283,7 @@ impl ImageResourceStore {
self.ready_by_request
.insert(identity.request_key.clone(), &resource);
slot.state = ImageResourceState::Ready(resource);
slot.retained_current = None;
slot.scanned_preload = None;
self.visual_generation.bump();
true
@@ -264,16 +297,14 @@ impl ImageResourceStore {
return false;
}
slot.state = ImageResourceState::Failed;
slot.retained_current = None;
slot.scanned_preload = None;
self.visual_generation.bump();
true
}
pub(super) fn ready_for_layout(&self, element: DomHandle) -> Option<ReadyImageForLayout> {
let slot = self.slots.get(&element)?;
let ImageResourceState::Ready(resource) = &slot.state else {
return None;
};
let resource = self.slots.get(&element)?.current_ready()?;
let (intrinsic_width, intrinsic_height) = intrinsic_dimensions(resource);
Some(ReadyImageForLayout {
intrinsic_width,
@@ -294,10 +325,7 @@ impl ImageResourceStore {
}
pub(super) fn intrinsic_dimensions(&self, element: DomHandle) -> Option<(f32, f32)> {
let slot = self.slots.get(&element)?;
let ImageResourceState::Ready(resource) = &slot.state else {
return None;
};
let resource = self.slots.get(&element)?.current_ready()?;
Some(intrinsic_dimensions(resource))
}
@@ -721,7 +721,7 @@ pub(crate) enum PendingImageLoadEventOwner {
Child(FrameDocumentImageLoadEventBinding),
}
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub(crate) struct PendingImageLoadEvent {
id: ImageLoadEventId,
owner_document_handle: DomHandle,
@@ -729,6 +729,7 @@ pub(crate) struct PendingImageLoadEvent {
owner: PendingImageLoadEventOwner,
request_initiator_type: crate::types::SubresourceRequestInitiatorType,
resource_timing_initiator_type: &'static str,
request_key: Option<ImageRequestKey>,
network_state: PendingImageLoadNetworkState,
terminal_followup_queued: bool,
}
@@ -766,36 +767,41 @@ impl PendingImageLoadEvent {
owner,
request_initiator_type,
resource_timing_initiator_type,
request_key: None,
network_state: PendingImageLoadNetworkState::Unbound,
terminal_followup_queued: false,
}
}
pub(crate) fn id(self) -> ImageLoadEventId {
pub(crate) fn id(&self) -> ImageLoadEventId {
self.id
}
pub(crate) fn owner_document_handle(self) -> DomHandle {
pub(crate) fn owner_document_handle(&self) -> DomHandle {
self.owner_document_handle
}
pub(crate) fn target(self) -> WindowDocumentTaskTarget {
pub(crate) fn target(&self) -> WindowDocumentTaskTarget {
self.target
}
pub(crate) fn owner(self) -> PendingImageLoadEventOwner {
pub(crate) fn owner(&self) -> PendingImageLoadEventOwner {
self.owner
}
pub(crate) fn request_initiator_type(self) -> crate::types::SubresourceRequestInitiatorType {
pub(crate) fn request_initiator_type(&self) -> crate::types::SubresourceRequestInitiatorType {
self.request_initiator_type
}
pub(crate) fn resource_timing_initiator_type(self) -> &'static str {
pub(crate) fn resource_timing_initiator_type(&self) -> &'static str {
self.resource_timing_initiator_type
}
pub(crate) fn network_request_id(self) -> Option<u64> {
pub(crate) fn request_key(&self) -> Option<&ImageRequestKey> {
self.request_key.as_ref()
}
pub(crate) fn network_request_id(&self) -> Option<u64> {
match self.network_state {
PendingImageLoadNetworkState::Pending(internal_id) => Some(internal_id),
PendingImageLoadNetworkState::Unbound
@@ -805,7 +811,7 @@ impl PendingImageLoadEvent {
}
}
pub(crate) fn terminal_source(self) -> Option<PendingImageLoadTerminalSource> {
pub(crate) fn terminal_source(&self) -> Option<PendingImageLoadTerminalSource> {
match self.network_state {
PendingImageLoadNetworkState::Ready(source)
| PendingImageLoadNetworkState::Failed(source) => Some(source),
@@ -816,7 +822,7 @@ impl PendingImageLoadEvent {
}
pub(crate) fn terminal_followup(
self,
&self,
) -> Option<crate::page_task_queue::RendererPageImageLoadEventKind> {
if !self.terminal_followup_queued {
return None;
@@ -973,6 +979,9 @@ pub(crate) struct PageScriptHost {
user_interaction_tasks: user_interaction_tasks::UserInteractionTaskState,
pending_image_load_events: HashMap<DomHandle, PendingImageLoadEvent>,
next_image_load_event_id: u64,
// Source selection can change before its pending request becomes available or broken. Keep
// the request exposed by currentSrc separate from the element's mutable source attributes.
current_image_requests: HashMap<DomHandle, ImageRequestKey>,
pending_media_load_sequences: HashMap<DomHandle, PendingMediaLoadSequence>,
next_media_load_sequence_id: u64,
pending_text_track_load_sequences: HashMap<DomHandle, PendingTextTrackLoadSequence>,
@@ -6,7 +6,7 @@ use crate::page_task_queue::{
RendererPageImageLoadEventTaskId,
};
use crate::types::{ImageRequestCorsMode, ImageRequestKey, SubresourceRequestInitiatorType};
use crate::util::v8_string;
use crate::util::{context_host_ptr_from_global_bridge, v8_string};
use moli_browser_profile::DEFAULT_WINDOW_SURFACE_PROFILE;
use crate::style_engine::media_list::evaluate_media_query_list;
@@ -16,11 +16,15 @@ use super::super::geometry::compute_mock_client_rect;
use super::super::{construct_simple_event, dispatch_public_event, resolve_url_like_attribute};
use super::lazy::{image_load_is_deferred, revealed_lazy_image_handles};
#[derive(Clone, Copy)]
const IMAGE_UPDATE_HANDLE_INDEX: u32 = 0;
const IMAGE_UPDATE_ID_INDEX: u32 = 1;
const IMAGE_UPDATE_TRIGGER_INDEX: u32 = 2;
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(u32)]
enum ImageLoadQueueTrigger {
SourceOrInsertion,
LazyReveal,
DocumentAdoption,
}
pub(crate) fn queue_image_load_event_if_needed(
@@ -46,7 +50,7 @@ pub(crate) fn queue_image_load_event_after_document_adoption(
scope,
runtime_ptr,
handle,
ImageLoadQueueTrigger::DocumentAdoption,
ImageLoadQueueTrigger::SourceOrInsertion,
SubresourceRequestInitiatorType::Other,
);
}
@@ -99,7 +103,7 @@ pub(crate) fn queue_image_load_event_for_loading_change(
scope,
runtime_ptr,
handle,
ImageLoadQueueTrigger::SourceOrInsertion,
ImageLoadQueueTrigger::LazyReveal,
SubresourceRequestInitiatorType::Script,
);
}
@@ -116,15 +120,9 @@ fn queue_image_load_event(
if image_load_is_deferred(runtime, handle) && !allow_deferred_lazy {
return;
}
if image_load_is_suppressed_for_non_active_parent(runtime, handle)
&& !matches!(trigger, ImageLoadQueueTrigger::DocumentAdoption)
if image_selected_source(runtime, handle).is_none()
&& !connected_empty_src_without_srcset(runtime, handle)
{
return;
}
let selected_source = image_selected_source_url(runtime, handle);
let invalid_empty_source =
selected_source.is_empty() && connected_empty_src_without_srcset(runtime, handle);
if selected_source.is_empty() && !invalid_empty_source {
let _ = runtime.cancel_pending_image_load_event(handle);
let _ = runtime.process_image_decode_requests_for_element(scope, handle);
return;
@@ -149,7 +147,7 @@ fn queue_image_load_event(
if let Some(pending) = runtime
.pending_image_load_event(handle)
.or(pending_before_registration)
.filter(|pending| runtime.pending_image_load_event_is_current(handle, *pending))
.filter(|pending| runtime.pending_image_load_event_is_current(handle, pending))
{
let followup =
runtime.pending_image_load_terminal_followup_if_ready(handle, pending.id());
@@ -163,11 +161,91 @@ fn queue_image_load_event(
let _ = runtime.process_image_decode_requests_for_element(scope, handle);
return;
};
let request_url = if invalid_empty_source {
Err("an empty image source cannot be fetched".to_owned())
} else {
url::Url::parse(&selected_source).map_err(|error| error.to_string())
queue_image_update_microtask(scope, runtime_ptr, handle, pending.id(), trigger);
}
fn queue_image_update_microtask(
scope: &mut v8::PinScope<'_, '_>,
runtime_ptr: *mut PageScriptHost,
handle: DomHandle,
sequence: ImageLoadEventId,
trigger: ImageLoadQueueTrigger,
) {
let data = v8::Array::new(scope, 3);
let _ = data.set_index(
scope,
IMAGE_UPDATE_HANDLE_INDEX,
v8::BigInt::new_from_u64(scope, handle.index() as u64).into(),
);
let _ = data.set_index(
scope,
IMAGE_UPDATE_ID_INDEX,
v8::BigInt::new_from_u64(scope, sequence.get()).into(),
);
let _ = data.set_index(
scope,
IMAGE_UPDATE_TRIGGER_INDEX,
v8::Integer::new_from_unsigned(scope, trigger as u32).into(),
);
let Some(callback) = v8::Function::builder(queued_image_update_microtask_callback)
.data(data.into())
.build(scope)
else {
let _ = unsafe { &mut *runtime_ptr }
.cancel_pending_image_load_event_if_matches(handle, sequence);
return;
};
scope.enqueue_microtask(callback);
}
fn queued_image_update_microtask_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
_rv: v8::ReturnValue<'_, v8::Value>,
) {
let Some(runtime_ptr) = context_host_ptr_from_global_bridge(scope) else {
return;
};
let Some((handle, sequence, trigger)) =
image_update_target_from_callback_data(scope, args.data())
else {
return;
};
let runtime = unsafe { &mut *runtime_ptr };
let Some(pending) = runtime
.pending_image_load_event(handle)
.filter(|pending| pending.id() == sequence)
else {
return;
};
if !runtime.pending_image_load_event_is_current(handle, &pending) {
let _ = runtime.cancel_pending_image_load_event_if_matches(handle, sequence);
return;
}
if pending.request_key().is_some() {
return;
}
if image_load_is_deferred(runtime, handle) && trigger != ImageLoadQueueTrigger::LazyReveal {
let _ = runtime.cancel_pending_image_load_event_if_matches(handle, sequence);
return;
}
let Some(request_key) = image_selected_request_key(runtime, handle) else {
if connected_empty_src_without_srcset(runtime, handle) {
let followup = runtime
.complete_pending_image_load_local_resource_if_matches(handle, sequence, false);
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, sequence, followup);
} else {
let _ = runtime.cancel_pending_image_load_event_if_matches(handle, sequence);
}
let _ = runtime.process_image_decode_requests_for_element(scope, handle);
return;
};
if !runtime.prepare_pending_image_load_request_if_matches(handle, sequence, request_key.clone())
{
return;
}
let request_url = url::Url::parse(request_key.url()).map_err(|error| error.to_string());
// Cached pixels must pass the requesting element's current Document policy
// before they can satisfy its load or decode work.
if let Ok(request_url) = &request_url
@@ -180,91 +258,99 @@ fn queue_image_load_event(
)
.blocks_request()
{
let followup = runtime.complete_pending_image_load_local_resource_if_matches(
handle,
pending.id(),
false,
);
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, pending.id(), followup);
let followup =
runtime.complete_pending_image_load_local_resource_if_matches(handle, sequence, false);
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, sequence, followup);
let _ = runtime.process_image_decode_requests_for_element(scope, handle);
return;
}
// Register the current request before reconsidering decode() promises. An
// image may receive src and decode() while detached, then be inserted in
// the same script job. Processing the promise before this registration
// would reject it even though insertion has just started its exact request.
let _ = runtime.process_image_decode_requests_for_element(scope, handle);
if runtime.image_resource_is_ready(handle)
&& !runtime.has_scanned_image_preload_for_element(handle)
{
let followup =
runtime.complete_pending_image_load_reused_resource_if_matches(handle, pending.id());
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, pending.id(), followup);
runtime.complete_pending_image_load_reused_resource_if_matches(handle, sequence);
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, sequence, followup);
let _ = runtime.process_image_decode_requests_for_element(scope, handle);
return;
}
let start = request_url.and_then(|request_url| {
crate::network_host::start_image_element_resource_fetch(
scope,
runtime,
handle,
pending.id(),
sequence,
request_url,
)
});
match start {
Ok(crate::network_host::ImageElementResourceFetchStart::Pending) => {}
Ok(crate::network_host::ImageElementResourceFetchStart::PolicySkipped) => {
let followup = runtime.complete_pending_image_load_local_resource_if_matches(
handle,
pending.id(),
true,
);
queue_image_load_terminal_followup_if_ready(
runtime_ptr,
handle,
pending.id(),
followup,
);
let followup = runtime
.complete_pending_image_load_local_resource_if_matches(handle, sequence, true);
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, sequence, followup);
}
Ok(crate::network_host::ImageElementResourceFetchStart::Local {
descriptor,
encoded,
}) => {
let completion = runtime.complete_pending_image_load_local_response_if_matches(
handle,
pending.id(),
descriptor,
encoded,
handle, sequence, descriptor, encoded,
);
queue_image_load_terminal_followup_if_ready(
runtime_ptr,
handle,
pending.id(),
sequence,
completion.followup(),
);
}
Err(error) => {
tracing::debug!(
image = handle.index(),
sequence = pending.id().get(),
sequence = sequence.get(),
%error,
"image resource selection failed"
);
let followup = runtime.complete_pending_image_load_local_resource_if_matches(
handle,
pending.id(),
false,
);
queue_image_load_terminal_followup_if_ready(
runtime_ptr,
handle,
pending.id(),
followup,
);
let followup = runtime
.complete_pending_image_load_local_resource_if_matches(handle, sequence, false);
queue_image_load_terminal_followup_if_ready(runtime_ptr, handle, sequence, followup);
}
}
}
fn image_update_target_from_callback_data(
scope: &mut v8::PinScope<'_, '_>,
data: v8::Local<'_, v8::Value>,
) -> Option<(DomHandle, ImageLoadEventId, ImageLoadQueueTrigger)> {
let data = v8::Local::<v8::Array>::try_from(data).ok()?;
let handle = data.get_index(scope, IMAGE_UPDATE_HANDLE_INDEX)?;
let handle = v8::Local::<v8::BigInt>::try_from(handle).ok()?;
let (handle, handle_lossless) = handle.u64_value();
let sequence = data.get_index(scope, IMAGE_UPDATE_ID_INDEX)?;
let sequence = v8::Local::<v8::BigInt>::try_from(sequence).ok()?;
let (sequence, sequence_lossless) = sequence.u64_value();
let trigger = data
.get_index(scope, IMAGE_UPDATE_TRIGGER_INDEX)?
.uint32_value(scope)
.and_then(|value| match value {
value if value == ImageLoadQueueTrigger::SourceOrInsertion as u32 => {
Some(ImageLoadQueueTrigger::SourceOrInsertion)
}
value if value == ImageLoadQueueTrigger::LazyReveal as u32 => {
Some(ImageLoadQueueTrigger::LazyReveal)
}
_ => None,
})?;
(handle_lossless && sequence_lossless).then(|| {
(
DomHandle::new(handle as usize),
ImageLoadEventId::new(sequence),
trigger,
)
})
}
pub(crate) fn image_selected_source(runtime: &PageScriptHost, handle: DomHandle) -> Option<String> {
image_selected_source_candidate(runtime, handle).map(|candidate| candidate.url)
}
@@ -368,22 +454,6 @@ fn image_selected_picture_source(
None
}
fn image_load_is_suppressed_for_non_active_parent(
runtime: &PageScriptHost,
handle: DomHandle,
) -> bool {
runtime
.dom_host()
.node(handle)
.is_some_and(|node| !node.is_connected() && node.parent_node().is_some())
}
pub(crate) fn image_selected_source_url(runtime: &PageScriptHost, handle: DomHandle) -> String {
image_selected_resource(runtime, handle)
.map(|selected| selected.url)
.unwrap_or_default()
}
fn image_selected_resource(
runtime: &PageScriptHost,
handle: DomHandle,
@@ -523,7 +593,10 @@ pub(crate) fn apply_image_attribute_mutation_plan(
pub(crate) fn reset_image_load_dispatch(runtime: &mut PageScriptHost, handle: DomHandle) {
let _ = runtime.cancel_pending_image_load_event(handle);
let _ = runtime.retire_image_resource_for_element(handle);
if image_selected_request_key(runtime, handle).is_none() {
runtime.set_current_image_request(handle, None);
let _ = runtime.retire_image_resource_for_element(handle);
}
if let Some(element) = runtime
.dom_host_mut()
.node_mut(handle)
@@ -543,21 +616,18 @@ pub(crate) fn mark_image_load_dispatched(runtime: &mut PageScriptHost, handle: D
}
}
fn record_local_image_resource_performance_entry(
fn record_image_resource_performance_entry_for_pending(
scope: &mut v8::PinScope<'_, '_>,
runtime: &PageScriptHost,
handle: DomHandle,
initiator_type: &'static str,
pending: &crate::native_bridge::PendingImageLoadEvent,
) {
let url = image_selected_source_url(runtime, handle);
if url.is_empty() {
let Some(request_key) = pending.request_key() else {
return;
}
};
crate::context_bootstrap::record_resource_performance_entry(
scope,
crate::context_bootstrap::ResourcePerformanceEntry::without_network_result(
url,
initiator_type,
request_key.url(),
pending.resource_timing_initiator_type(),
None,
),
);
@@ -590,7 +660,7 @@ pub(crate) fn apply_authorized_image_load_event_in_context(
}
let pending =
runtime.take_pending_image_load_event_task_for_exact_target(task_id, target, kind)?;
let dispatched = dispatch_queued_image_load_event(scope, runtime_ptr, handle, pending, kind);
let dispatched = dispatch_queued_image_load_event(scope, runtime_ptr, handle, &pending, kind);
let _ = runtime.settle_pending_image_load_event(pending, true);
Some(if dispatched {
PageImageLoadEventTargetEffect::DispatchedToCurrentOwner
@@ -603,7 +673,7 @@ fn dispatch_queued_image_load_event(
scope: &mut v8::PinScope<'_, '_>,
runtime_ptr: *mut PageScriptHost,
handle: DomHandle,
pending: crate::native_bridge::PendingImageLoadEvent,
pending: &crate::native_bridge::PendingImageLoadEvent,
kind: RendererPageImageLoadEventKind,
) -> bool {
let runtime = unsafe { &mut *runtime_ptr };
@@ -615,12 +685,7 @@ fn dispatch_queued_image_load_event(
&& pending.terminal_source()
== Some(crate::native_bridge::PendingImageLoadTerminalSource::Local)
{
record_local_image_resource_performance_entry(
scope,
runtime,
handle,
pending.resource_timing_initiator_type(),
);
record_image_resource_performance_entry_for_pending(scope, pending);
}
let _ = runtime.process_pending_image_decode_requests(scope);
let already_dispatched = runtime
@@ -1072,17 +1137,10 @@ fn image_current_src_value<'s>(
}
fn image_current_src_value_for_handle(runtime: &PageScriptHost, handle: DomHandle) -> String {
let complete = runtime
.dom_host()
.node(handle)
.and_then(Node::as_element)
.is_some_and(|element| element.image_load_dispatched())
|| runtime.image_resource_is_ready(handle);
if complete {
image_selected_source_url(runtime, handle)
} else {
String::new()
}
runtime
.current_image_request_url(handle)
.unwrap_or_default()
.to_owned()
}
#[cfg(test)]
+7 -1
View File
@@ -170,9 +170,15 @@ pub(crate) fn start_image_element_resource_fetch(
.pending_image_load_event(image_handle)
.filter(|pending| pending.id() == sequence)
.ok_or_else(|| "image lifecycle sequence is no longer pending".to_owned())?;
if !host.pending_image_load_event_is_current(image_handle, pending) {
if !host.pending_image_load_event_is_current(image_handle, &pending) {
return Err("image lifecycle sequence owner is stale".to_owned());
}
if pending
.request_key()
.is_none_or(|request_key| request_key.url() != request_url.as_str())
{
return Err("image lifecycle request identity changed before fetch".to_owned());
}
let owner = match pending.owner() {
PendingImageLoadEventOwner::Main(binding)
if host.main_document_task_owner_is_current(binding.owner()) =>
@@ -187,7 +187,7 @@ image.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABA
}
#[tokio::test(flavor = "current_thread")]
async fn image_event_discards_a_document_open_task_before_applying_the_replacement_tail() {
async fn image_update_discards_a_retired_document_request_before_queuing_its_terminal_task() {
run_page_vm_async_test(async move {
let loader =
crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");
@@ -232,28 +232,8 @@ currentImage.src = "/current-without-network.png";
"image event dispatch must not acquire a PageTimer descriptor"
);
let stale_task = take_next_image_load_event_task_for_test(&mut page_vm)
.expect("retired image task should settle as one explicit turn");
let stale = page_vm.apply_selected_page_image_load_event_turn(stale_task)?;
let stale_action = stale.action;
assert_eq!(
stale_action.target_effect,
PageImageLoadEventTargetEffect::DiscardedStaleOwner {
current_owner: None,
stale_payload_effect: PageImageLoadEventStalePayloadEffect::NoSettledExactPayload,
}
);
assert_eq!(
page_vm
.vm_mut()
.eval("__documentExactImageEvents.join('|')")?,
""
);
let current = take_next_image_load_event_task_for_test(&mut page_vm)
.expect("replacement image task should survive stale-head settlement");
assert_ne!(stale_action.owner.target(), current.owner().target());
.expect("replacement image task should survive retired request cancellation");
page_vm
.run_claimed_dom_manipulation_task_through_selected_dispatcher_for_test(
crate::page_task_queue::RendererPageDomManipulationTask::ImageLoadEvent(current),
@@ -268,7 +248,7 @@ currentImage.src = "/current-without-network.png";
);
assert!(
take_next_image_load_event_task_for_test(&mut page_vm).is_none(),
"both exact-Document tasks must consume exactly two turns"
"the retired request must be cancelled before it manufactures a terminal task"
);
Ok::<_, anyhow::Error>(())
})
@@ -752,89 +752,96 @@ fn parser_document_fragment_insertion_queues_resource_followups_from_hoisted_roo
.expect("current-thread runtime should build");
runtime.block_on(tokio::task::LocalSet::new().run_until(async move {
let mut page_vm = new_phase_one_page_vm_for_test();
let body = create_connected_html_body_for_test(&mut page_vm);
let context_host = page_vm
let mut page_vm = new_phase_one_page_vm_for_test();
let body = create_connected_html_body_for_test(&mut page_vm);
let context_host = page_vm
.vm()
.context_host_weak_for_test()
.upgrade()
.expect("context host should be alive");
let (fragment, _container, image, video) =
create_parser_resource_fragment_for_test(&mut page_vm, "parser-fragment-resource");
assert!(
!context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"disconnected fragment setup should not queue image load events"
);
assert!(
!context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"disconnected fragment setup should not register lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"disconnected fragment setup should not queue text-track timers"
);
let custom_element_reaction_roots = {
apply_parser_dom_mutation_for_test(
&mut page_vm,
ParserDomMutation::AppendChild {
parent: body,
child: fragment,
},
"parser fragment resource insertion should apply",
)
};
assert!(
custom_element_reaction_roots.is_empty(),
"plain resource fragment insertion should not queue custom element reactions"
);
assert_eq!(
page_vm
.vm()
.context_host_weak_for_test()
.upgrade()
.expect("context host should be alive");
let (fragment, _container, image, video) =
create_parser_resource_fragment_for_test(&mut page_vm, "parser-fragment-resource");
assert!(
!context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"disconnected fragment setup should not queue image load events"
);
assert!(
!context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"disconnected fragment setup should not register lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"disconnected fragment setup should not queue text-track timers"
);
let custom_element_reaction_roots = {
apply_parser_dom_mutation_for_test(
&mut page_vm,
ParserDomMutation::AppendChild {
parent: body,
child: fragment,
},
"parser fragment resource insertion should apply",
)
};
assert!(
custom_element_reaction_roots.is_empty(),
"plain resource fragment insertion should not queue custom element reactions"
);
assert_eq!(
page_vm
.vm()
.document_runtime
.dom_host()
.child_handles(fragment)
.count(),
0,
"parser DocumentFragment resource insertion should hoist and empty the fragment"
);
assert!(
context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"parser DocumentFragment insertion should queue image load events for hoisted subtree children"
);
assert!(
context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"parser DocumentFragment insertion should register hoisted lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"parser DocumentFragment insertion must not represent text-track default-mode work as a timer"
);
assert!(matches!(
.document_runtime
.dom_host()
.child_handles(fragment)
.count(),
0,
"parser DocumentFragment resource insertion should hoist and empty the fragment"
);
assert!(
context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"parser DocumentFragment insertion should queue image load events for hoisted subtree children"
);
assert!(
context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"parser DocumentFragment insertion should register hoisted lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"parser DocumentFragment insertion must not represent text-track default-mode work as a timer"
);
page_vm
.vm_mut()
.perform_script_task_checkpoint(None)
.expect("parser task-end checkpoint should select the image request");
assert!(
matches!(
take_next_dom_manipulation_task_for_test(&page_vm),
crate::page_task_queue::RendererPageDomManipulationTask::TextTrackDefaultMode(_)
),
"the synchronous text-track task should precede the image terminal queued by the task-end microtask"
);
assert!(
matches!(
take_next_dom_manipulation_task_for_test(&page_vm),
crate::page_task_queue::RendererPageDomManipulationTask::ImageLoadEvent(_)
));
assert!(
matches!(
take_next_dom_manipulation_task_for_test(&page_vm),
crate::page_task_queue::RendererPageDomManipulationTask::TextTrackDefaultMode(_)
),
"hoisted text track should follow the earlier image in the shared DOM FIFO"
);
}));
),
"the image-update microtask should queue its terminal after the earlier text-track task"
);
}));
}
#[test]
fn parser_document_fragment_insert_before_queues_resource_followups_from_hoisted_roots() {
@@ -844,113 +851,120 @@ fn parser_document_fragment_insert_before_queues_resource_followups_from_hoisted
.expect("current-thread runtime should build");
runtime.block_on(tokio::task::LocalSet::new().run_until(async move {
let mut page_vm = new_phase_one_page_vm_for_test();
let body = create_connected_html_body_for_test(&mut page_vm);
let context_host = page_vm
.vm()
.context_host_weak_for_test()
.upgrade()
.expect("context host should be alive");
let reference = {
let dom_host = page_vm.vm_mut().document_runtime.dom_host_mut();
let reference = dom_host.create_parser_element_without_attributes(
"span".to_owned(),
"http://www.w3.org/1999/xhtml".to_owned(),
None,
);
assert!(dom_host.set_attribute(reference, "id", "parser-fragment-resource-ref"));
assert!(dom_host.append_child(body, reference));
reference
};
let mut page_vm = new_phase_one_page_vm_for_test();
let body = create_connected_html_body_for_test(&mut page_vm);
let context_host = page_vm
.vm()
.context_host_weak_for_test()
.upgrade()
.expect("context host should be alive");
let reference = {
let dom_host = page_vm.vm_mut().document_runtime.dom_host_mut();
let reference = dom_host.create_parser_element_without_attributes(
"span".to_owned(),
"http://www.w3.org/1999/xhtml".to_owned(),
None,
);
assert!(dom_host.set_attribute(reference, "id", "parser-fragment-resource-ref"));
assert!(dom_host.append_child(body, reference));
reference
};
let (fragment, container, image, video) = create_parser_resource_fragment_for_test(
let (fragment, container, image, video) = create_parser_resource_fragment_for_test(
&mut page_vm,
"parser-fragment-resource-before",
);
assert!(
!context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"disconnected fragment setup should not queue image load events"
);
assert!(
!context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"disconnected fragment setup should not register lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"disconnected fragment setup should not queue text-track timers"
);
let custom_element_reaction_roots = {
apply_parser_dom_mutation_for_test(
&mut page_vm,
"parser-fragment-resource-before",
);
assert!(
!context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"disconnected fragment setup should not queue image load events"
);
assert!(
!context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"disconnected fragment setup should not register lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"disconnected fragment setup should not queue text-track timers"
);
let custom_element_reaction_roots = {
apply_parser_dom_mutation_for_test(
&mut page_vm,
ParserDomMutation::InsertBefore {
parent: body,
child: fragment,
reference_child: Some(reference),
},
"parser fragment resource insertBefore should apply",
)
};
assert!(
custom_element_reaction_roots.is_empty(),
"plain resource fragment insertBefore should not queue custom element reactions"
);
assert_eq!(
page_vm
.vm()
.document_runtime
.dom_host()
.child_handles(fragment)
.count(),
0,
"parser DocumentFragment resource insertBefore should hoist and empty the fragment"
);
assert_eq!(
page_vm
.vm()
.document_runtime
.dom_host()
.child_handles(body)
.collect::<Vec<_>>(),
vec![container, reference],
"parser DocumentFragment resource insertBefore should place the hoisted root before the reference child"
);
assert!(
context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"parser DocumentFragment insertBefore should queue image load events for hoisted subtree children"
);
assert!(
context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"parser DocumentFragment insertBefore should register hoisted lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"parser DocumentFragment insertBefore must not represent text-track default-mode work as a timer"
);
assert!(matches!(
ParserDomMutation::InsertBefore {
parent: body,
child: fragment,
reference_child: Some(reference),
},
"parser fragment resource insertBefore should apply",
)
};
assert!(
custom_element_reaction_roots.is_empty(),
"plain resource fragment insertBefore should not queue custom element reactions"
);
assert_eq!(
page_vm
.vm()
.document_runtime
.dom_host()
.child_handles(fragment)
.count(),
0,
"parser DocumentFragment resource insertBefore should hoist and empty the fragment"
);
assert_eq!(
page_vm
.vm()
.document_runtime
.dom_host()
.child_handles(body)
.collect::<Vec<_>>(),
vec![container, reference],
"parser DocumentFragment resource insertBefore should place the hoisted root before the reference child"
);
assert!(
context_host
.borrow()
.has_pending_image_load_event_for_test(image),
"parser DocumentFragment insertBefore should queue image load events for hoisted subtree children"
);
assert!(
context_host
.borrow()
.lazy_media_load_candidates()
.contains(&video),
"parser DocumentFragment insertBefore should register hoisted lazy media candidates"
);
assert_eq!(
page_vm.vm().ms_to_next_timeout(),
None,
"parser DocumentFragment insertBefore must not represent text-track default-mode work as a timer"
);
page_vm
.vm_mut()
.perform_script_task_checkpoint(None)
.expect("parser task-end checkpoint should select the image request");
assert!(
matches!(
take_next_dom_manipulation_task_for_test(&page_vm),
crate::page_task_queue::RendererPageDomManipulationTask::TextTrackDefaultMode(_)
),
"the synchronous text-track task should precede the image terminal queued by the task-end microtask"
);
assert!(
matches!(
take_next_dom_manipulation_task_for_test(&page_vm),
crate::page_task_queue::RendererPageDomManipulationTask::ImageLoadEvent(_)
));
assert!(
matches!(
take_next_dom_manipulation_task_for_test(&page_vm),
crate::page_task_queue::RendererPageDomManipulationTask::TextTrackDefaultMode(_)
),
"insertBefore-hoisted text track should follow the image in the shared DOM FIFO"
);
}));
),
"the image-update microtask should queue its terminal after the earlier text-track task"
);
}));
}
#[test]
fn js_insert_before_inserted_lazy_media_registers_candidate_from_mutation_owner() {
@@ -262,6 +262,176 @@ async fn image_fetch_enabled_discards_stale_queued_terminal_event() {
);
}
#[tokio::test]
async fn image_current_src_switches_only_when_the_update_microtask_selects_a_current_request() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
loader.set_image_fetch_enabled(true);
let (mut vm, _resource_completion_queue) =
new_storage_test_vm_with_loader_and_resource_completion_queue(
"https://image-current-request.test/page.html",
&loader,
);
let before_update = vm
.eval(
r#"
(() => {
const picture = document.createElement("picture");
const nonMatchingSource = document.createElement("source");
nonMatchingSource.media = "not all";
nonMatchingSource.srcset = "data:,a";
picture.append(nonMatchingSource);
const matchingSource = document.createElement("source");
matchingSource.media = "all";
matchingSource.srcset = "data:,b";
picture.append(matchingSource);
const image = document.createElement("img");
globalThis.__imageCurrentRequestObservations = [];
image.src = "data:,c";
const beforeUpdate = image.currentSrc;
queueMicrotask(() => {
__imageCurrentRequestObservations.push(`first:${image.currentSrc}`);
picture.append(image);
__imageCurrentRequestObservations.push(`inserted:${image.currentSrc}`);
queueMicrotask(() => {
__imageCurrentRequestObservations.push(`second:${image.currentSrc}`);
});
});
return beforeUpdate;
})()
"#,
)
.expect("image current request setup should evaluate");
assert_eq!(before_update, "");
assert_eq!(
vm.eval("globalThis.__imageCurrentRequestObservations.join('|')")
.expect("image current request observations should evaluate"),
"first:data:,c|inserted:data:,c|second:data:,b"
);
}
#[tokio::test]
async fn image_current_src_distinguishes_initial_and_replacement_requests() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
loader.set_image_fetch_enabled(true);
let mut vm = new_storage_page_task_executor_test_vm_with_loader(
"https://image-current-request.test/page.html",
&loader,
);
vm.set_fetch_subresource_interception(
crate::types::SubresourceInterceptionClasses::for_resource_type(
crate::types::SubresourceResourceType::Image,
),
);
assert_eq!(
vm.eval(
r#"
(() => {
const image = globalThis.__currentRequestImage = new Image();
image.src = "first.gif";
image.srcset = "first.gif";
return image.currentSrc;
})()
"#,
)
.expect("initial request should be queued"),
""
);
let observe = |vm: &mut crate::runtime::PageVmTaskExecutorTestHarness| {
vm.eval(
r#"
(() => {
const image = __currentRequestImage;
return [image.currentSrc, image.naturalWidth].join("|");
})()
"#,
)
.expect("current request state should be readable")
};
let first = vm.take_pending_subresource_fetch_infos();
assert_eq!(first.len(), 1);
assert_eq!(first[0].url.path(), "/first.gif");
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/first.gif|0"
);
vm.fulfill_pending_subresource_fetch(
first[0].internal_id,
200,
vec![("Content-Type".to_owned(), "image/gif".to_owned())],
one_by_one_gif_response_body(),
)
.expect("initial image request should fulfill");
run_next_image_event_task(&mut vm, &loader, "initial image decode").await;
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/first.gif|1"
);
vm.eval(
r#"
__currentRequestImage.src = "replacement.gif";
__currentRequestImage.removeAttribute("srcset");
"#,
)
.expect("replacement image request should start");
let replacement = vm.take_pending_subresource_fetch_infos();
assert_eq!(replacement.len(), 1);
assert_eq!(replacement[0].url.path(), "/replacement.gif");
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/first.gif|1"
);
vm.fulfill_pending_subresource_fetch(
replacement[0].internal_id,
200,
vec![("Content-Type".to_owned(), "image/gif".to_owned())],
one_by_one_gif_response_body(),
)
.expect("replacement image request should fulfill");
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/first.gif|1",
"the current image survives until the replacement decode commits"
);
run_next_image_event_task(&mut vm, &loader, "replacement image decode").await;
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/replacement.gif|1"
);
vm.eval(r#"__currentRequestImage.src = "broken.gif""#)
.expect("broken replacement should start");
let broken = vm.take_pending_subresource_fetch_infos();
assert_eq!(broken.len(), 1);
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/replacement.gif|1"
);
vm.fulfill_pending_subresource_fetch(
broken[0].internal_id,
200,
vec![("Content-Type".to_owned(), "image/gif".to_owned())],
crate::runtime::RendererSyntheticResponseBody::from_bytes(b"invalid GIF".to_vec()),
)
.expect("broken image response should settle");
run_next_image_event_task(&mut vm, &loader, "broken image error").await;
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/broken.gif|0"
);
vm.eval(r#"__currentRequestImage.src = "recovery.gif""#)
.expect("recovery image request should start");
assert_eq!(
observe(&mut vm),
"https://image-current-request.test/recovery.gif|0"
);
}
#[tokio::test]
async fn image_fetch_enabled_dispatches_http_error_after_resource_timing() {
let (base_url, request_rx, server) =
@@ -517,6 +687,85 @@ async fn inserting_completed_detached_image_does_not_restart_request() {
);
}
#[tokio::test]
async fn image_update_microtask_freezes_request_after_observing_live_base_url() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
loader.set_image_fetch_enabled(true);
let mut vm = new_storage_page_task_executor_test_vm_with_loader(
"https://image-update-base.test/page.html",
&loader,
);
vm.set_fetch_subresource_interception(
crate::types::SubresourceInterceptionClasses::for_resource_type(
crate::types::SubresourceResourceType::Image,
),
);
vm.eval(
r#"
(() => {
const image = new Image();
globalThis.__baseSelectedImage = image;
globalThis.__baseSelectedImageEvents = [];
image.onload = () => __baseSelectedImageEvents.push("load");
image.onerror = () => __baseSelectedImageEvents.push("error");
image.src = "green.png";
const root = document.documentElement ||
document.appendChild(document.createElement("html"));
const head = document.head ||
root.insertBefore(document.createElement("head"), root.firstChild);
const base = document.createElement("base");
base.href = "/resources/";
head.appendChild(base);
globalThis.__baseSelectedImageBase = base;
})()
"#,
)
.expect("image and base should update in one script turn");
let pending = vm.take_pending_subresource_fetch_infos();
assert_eq!(pending.len(), 1);
assert_eq!(
pending[0].url.as_str(),
"https://image-update-base.test/resources/green.png",
"URL selection must happen in the queued image-update microtask"
);
vm.fulfill_pending_subresource_fetch(
pending[0].internal_id,
200,
vec![("Content-Type".to_owned(), "image/gif".to_owned())],
one_by_one_gif_response_body(),
)
.expect("base-selected image response should complete");
vm.eval(
r#"
(() => {
__baseSelectedImageBase.href = "/bogus/";
(document.body || document.documentElement).appendChild(__baseSelectedImage);
})()
"#,
)
.expect("completed detached image should insert after a later base change");
assert!(
vm.take_pending_subresource_fetch_infos().is_empty(),
"ordinary insertion must preserve the request selected by the update microtask"
);
run_next_image_event_task(&mut vm, &loader, "base-selected image event").await;
assert_eq!(
vm.eval(
r#"__baseSelectedImageEvents.join("|") + ":" +
performance.getEntriesByName(
"https://image-update-base.test/resources/green.png"
).length"#,
)
.expect("base-selected image result should evaluate"),
"load:1"
);
}
#[tokio::test]
async fn removed_lazy_image_suppresses_in_flight_terminal_event() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
@@ -550,6 +799,8 @@ async fn removed_lazy_image_suppresses_in_flight_terminal_event() {
vm.refresh_layout_snapshot_for_test(moli_layout::LayoutViewport::new(800, 600, 1.0,))
.expect("near lazy-image layout refresh should succeed")
);
vm.perform_owner_lane_task_microtask_checkpoints()
.expect("layout task should finish its image-update microtask checkpoint");
let pending = vm.take_pending_subresource_fetch_infos();
assert_eq!(pending.len(), 1);
@@ -893,7 +1144,7 @@ async fn image_request_interception_fulfills_through_image_continuation() {
}
#[tokio::test]
async fn image_decode_waits_for_in_flight_pixels_and_reuses_the_ready_resource() {
async fn image_decode_quick_attach_distinguishes_uncached_and_ready_requests() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
loader.set_image_fetch_enabled(true);
let mut vm = new_storage_page_task_executor_test_vm_with_loader(
@@ -928,8 +1179,8 @@ async fn image_decode_waits_for_in_flight_pixels_and_reuses_the_ready_resource()
assert_eq!(pending.len(), 1);
assert_eq!(
vm.eval("globalThis.__firstSharedDecode")
.expect("in-flight image decode state should evaluate"),
"pending"
.expect("uncached quick-attach image decode state should evaluate"),
"reject:EncodingError"
);
vm.fulfill_pending_subresource_fetch(
pending[0].internal_id,
@@ -942,7 +1193,7 @@ async fn image_decode_waits_for_in_flight_pixels_and_reuses_the_ready_resource()
assert_eq!(
vm.eval("globalThis.__firstSharedDecode")
.expect("first image decode result should evaluate"),
"resolve"
"reject:EncodingError"
);
let first_image = vm
.document_runtime
@@ -2605,6 +2605,7 @@ fn html_image_data_gif_exposes_intrinsic_dimensions() {
r#"
(() => {
const img = new Image(4, 5);
globalThis.__lmDataGifImage = img;
img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
return [
img.complete,
@@ -2618,7 +2619,16 @@ fn html_image_data_gif_exposes_intrinsic_dimensions() {
)
.expect("data image dimensions should be readable");
assert_eq!(result, "true|4|5|1|1");
assert_eq!(result, "true|4|5|0|0");
assert_eq!(
vm.eval(
"[__lmDataGifImage.complete, __lmDataGifImage.width, \
__lmDataGifImage.height, __lmDataGifImage.naturalWidth, \
__lmDataGifImage.naturalHeight].join('|')",
)
.expect("data image dimensions should settle at the task checkpoint"),
"true|4|5|1|1"
);
}
#[test]
@@ -2681,6 +2691,13 @@ fn html_image_data_svg_exposes_intrinsic_dimensions() {
const fractionalConcreteHeight = new Image();
fractionalConcreteHeight.src = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 12"/>';
globalThis.__lmDataSvgImages = {
sized,
ratioWidth,
noSize,
fractionalConcreteHeight
};
return [
[sized.naturalWidth, sized.naturalHeight].join('x'),
[ratioWidth.naturalWidth, ratioWidth.naturalHeight].join('x'),
@@ -2692,6 +2709,24 @@ fn html_image_data_svg_exposes_intrinsic_dimensions() {
)
.expect("data SVG image dimensions should be readable");
assert_eq!(result, "0x0|0x0|0x0|0x0");
let result = vm
.eval(
r#"
(() => {
const { sized, ratioWidth, noSize, fractionalConcreteHeight } = __lmDataSvgImages;
return [
[sized.naturalWidth, sized.naturalHeight].join('x'),
[ratioWidth.naturalWidth, ratioWidth.naturalHeight].join('x'),
[noSize.naturalWidth, noSize.naturalHeight].join('x'),
[fractionalConcreteHeight.naturalWidth, fractionalConcreteHeight.naturalHeight].join('x')
].join('|');
})()
"#,
)
.expect("data SVG dimensions should settle at the task checkpoint");
// Blink resolves an external SVG with neither dimensions nor a viewBox
// against the replaced-element default object size.
// Its DOM natural dimensions remain integers even when layout retains the
@@ -2774,7 +2809,7 @@ async fn html_image_load_runs_on_its_dom_task_not_window_load_dispatch() {
)
.expect("image setup should evaluate");
assert_eq!(before_load, "1|1|1|1|pending");
assert_eq!(before_load, "0|0|0|0|pending");
vm.dispatch_window_load_event()
.expect("window load should not dispatch pending image loads");
@@ -2925,8 +2960,8 @@ async fn html_image_complete_tracks_pending_src_and_srcset_loads() {
assert_eq!(
drain_canvas_image_load_event_tasks(&mut vm, &loader).await,
4,
"src, srcset, empty-src, and retired-source work must each settle one queued task"
3,
"src, srcset, and empty-src work must settle queued tasks; the retired source is cancelled before its update microtask can manufacture one"
);
let after_load = vm
@@ -3515,6 +3550,8 @@ async fn parser_image_load_honors_meta_img_src_csp() {
.expect("parser EOF should prepare interactive");
vm.apply_main_document_interactive_lifecycle_action(interactive)
.expect("interactive transition should register the parser image");
vm.perform_script_task_checkpoint(None)
.expect("parser task-end checkpoint should select the image request");
run_one_canvas_image_load_event_task(&mut vm, &loader).await;
assert_eq!(
drain_pre_domcontentloaded_non_script_page_tasks_for_test(&mut vm),
@@ -4111,6 +4148,8 @@ async fn html_image_lazy_detached_sources_wait_until_inserted() {
vm.refresh_layout_snapshot_for_test(moli_layout::LayoutViewport::new(800, 600, 1.0,))
.expect("inserted lazy-image layout refresh should succeed")
);
vm.perform_owner_lane_task_microtask_checkpoints()
.expect("layout task should finish its image-update microtask checkpoint");
assert!(
drain_canvas_image_load_event_tasks(&mut vm, &loader).await > 0,
"inserted lazy image events should dispatch"
@@ -4204,6 +4243,8 @@ async fn html_image_below_viewport_lazy_waits_until_scroll_reveal() {
vm.refresh_layout_snapshot_for_test(moli_layout::LayoutViewport::new(800, 600, 1.0,))
.expect("initial lazy-image layout refresh should succeed")
);
vm.perform_owner_lane_task_microtask_checkpoints()
.expect("layout task should finish its image-update microtask checkpoint");
assert!(
drain_canvas_image_load_event_tasks(&mut vm, &loader).await > 0,
"the near-viewport lazy image should be admitted"
@@ -1065,7 +1065,7 @@ async fn child_image_network_failure_releases_lifecycle_before_later_host_load()
assert!(
vm._context_host
.borrow()
.pending_image_load_event_is_current(child_image, detached_pending),
.pending_image_load_event_is_current(child_image, &detached_pending),
"same-document detach must preserve current image ownership"
);
assert!(