mirror of
https://github.com/lexmount/moli.git
synced 2026-11-24 08:02:58 +00:00
fix(print): skip background-only image requests when backgrounds are disabled
Apply capture background policy before dispatching newly discovered CSS images. Preserve any mask use of the same URL.
This commit is contained in:
@@ -46,7 +46,7 @@ where
|
||||
styles: &'a mut R,
|
||||
active_sources: HashSet<S::NodeId>,
|
||||
seen_sources: HashMap<S::NodeId, String>,
|
||||
seen_css_images: HashSet<(S::NodeId, String)>,
|
||||
seen_css_images: HashSet<(S::NodeId, String, crate::LayoutCssImagePurpose)>,
|
||||
css_image_references: Vec<crate::LayoutCssImageReference<S::NodeId>>,
|
||||
source_root: Option<S::NodeId>,
|
||||
root_is_html: bool,
|
||||
@@ -741,14 +741,16 @@ where
|
||||
.background_image
|
||||
.0
|
||||
.iter()
|
||||
.map(|image| self.css_image_resource(source, image))
|
||||
.map(|image| {
|
||||
self.css_image_resource(source, image, crate::LayoutCssImagePurpose::Background)
|
||||
})
|
||||
.collect();
|
||||
let mask = computed
|
||||
.get_svg()
|
||||
.mask_image
|
||||
.0
|
||||
.iter()
|
||||
.map(|image| self.css_image_resource(source, image))
|
||||
.map(|image| self.css_image_resource(source, image, crate::LayoutCssImagePurpose::Mask))
|
||||
.collect();
|
||||
crate::source::LayoutCssImageResources { background, mask }
|
||||
}
|
||||
@@ -757,6 +759,7 @@ where
|
||||
&mut self,
|
||||
source: S::NodeId,
|
||||
image: &style::values::computed::Image,
|
||||
purpose: crate::LayoutCssImagePurpose,
|
||||
) -> Option<crate::LayoutImageResource> {
|
||||
let GenericImage::Url(computed_url) = image else {
|
||||
return None;
|
||||
@@ -766,9 +769,13 @@ where
|
||||
return None;
|
||||
}
|
||||
let resolved_url = url.as_str().to_owned();
|
||||
if self.seen_css_images.insert((source, resolved_url.clone())) {
|
||||
if self
|
||||
.seen_css_images
|
||||
.insert((source, resolved_url.clone(), purpose))
|
||||
{
|
||||
self.css_image_references
|
||||
.push(crate::LayoutCssImageReference {
|
||||
purpose,
|
||||
source,
|
||||
resolved_url,
|
||||
});
|
||||
|
||||
@@ -9,12 +9,21 @@ use super::{
|
||||
tree::{FrozenLayoutTree, LayoutTreeRetentionMetrics},
|
||||
};
|
||||
|
||||
/// The output use of one computed CSS image. Capture consumers decide
|
||||
/// whether that use participates in the requested output before preparing it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum LayoutCssImagePurpose {
|
||||
Background,
|
||||
Mask,
|
||||
}
|
||||
|
||||
/// One direct computed CSS image URL observed while constructing a box.
|
||||
///
|
||||
/// The source node keeps the owning `Document` recoverable at the renderer
|
||||
/// boundary without teaching this crate about browsing contexts.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct LayoutCssImageReference<N> {
|
||||
pub purpose: LayoutCssImagePurpose,
|
||||
pub source: N,
|
||||
pub resolved_url: String,
|
||||
}
|
||||
|
||||
@@ -55,9 +55,9 @@ pub use form::{form_control_text, select_control_texts};
|
||||
pub use layout_tree::{
|
||||
FrozenCoordinateSpace, FrozenEmbeddedFrame, FrozenLayoutBox, FrozenLayoutTree,
|
||||
GeometryProvider, LayoutAnswers, LayoutBoxGeometry, LayoutBoxModel, LayoutCaretPosition,
|
||||
LayoutClipChainId, LayoutClipNode, LayoutCoordinateSpaceId, LayoutCssImageReference,
|
||||
LayoutDocumentMetrics, LayoutElementMetrics, LayoutFlushReason, LayoutFragment,
|
||||
LayoutFragmentBoxModel, LayoutFragmentId, LayoutFragmentKind, LayoutHit,
|
||||
LayoutClipChainId, LayoutClipNode, LayoutCoordinateSpaceId, LayoutCssImagePurpose,
|
||||
LayoutCssImageReference, LayoutDocumentMetrics, LayoutElementMetrics, LayoutFlushReason,
|
||||
LayoutFragment, LayoutFragmentBoxModel, LayoutFragmentId, LayoutFragmentKind, LayoutHit,
|
||||
LayoutIntersectionGeometry, LayoutNodeOutput, LayoutObjectGeometry, LayoutOutputBoxId,
|
||||
LayoutPaintedSurfaceHit, LayoutPassMetrics, LayoutPassResult, LayoutPoint, LayoutQuad,
|
||||
LayoutQuery, LayoutQueryAnswer, LayoutQueryBatch, LayoutRect, LayoutResizeObserverMetrics,
|
||||
|
||||
@@ -5,6 +5,70 @@ use crate::runtime::{
|
||||
};
|
||||
use moli_page_types::LayoutPolicy;
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn native_pdf_only_admits_css_images_used_by_the_capture() {
|
||||
run_page_vm_async_test(async move {
|
||||
let image = "data:image/webp;base64,UklGRhwAAABXRUJQVlA4TA8AAAAvAUAAAAcQ/Y/+ByKi/wEA";
|
||||
for policy in [LayoutPolicy::OnDemand, LayoutPolicy::Realtime] {
|
||||
for (print_background, uses_mask) in [(false, false), (true, false), (false, true)] {
|
||||
let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?;
|
||||
loader.set_optional_resource_fetch_mask(
|
||||
crate::protocol_types::OptionalResourceFetchMask::IMAGE,
|
||||
);
|
||||
let mut page = test_page_vm_with_loader_and_document_url(
|
||||
&loader,
|
||||
Vec::new(),
|
||||
Url::parse("https://print-css-image-admission.test/")?,
|
||||
);
|
||||
page.layout_configuration.policy = policy;
|
||||
page.vm_mut().set_layout_policy(policy);
|
||||
|
||||
let mask = if uses_mask {
|
||||
format!("mask-image: url('{image}');")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let css =
|
||||
format!("@media print {{ p {{ background-image: url('{image}'); {mask} }} }}");
|
||||
page.vm_mut().eval(&format!(
|
||||
"document.head.innerHTML='<style></style>';\
|
||||
document.querySelector('style').textContent={};\
|
||||
document.body.innerHTML='<p>Print CSS images</p>';",
|
||||
serde_json::to_string(&css)?,
|
||||
))?;
|
||||
assert!(
|
||||
page.vm()
|
||||
.css_image_resource_observability_for_test()
|
||||
.4
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
let request = RendererPrintToPdfRequest {
|
||||
print_background,
|
||||
..Default::default()
|
||||
};
|
||||
assert!(matches!(
|
||||
page.print_to_pdf(request)?,
|
||||
RendererPrintToPdfReply::Printed(_)
|
||||
));
|
||||
let admitted = page.vm().css_image_resource_observability_for_test().4;
|
||||
let expected = if print_background || uses_mask {
|
||||
vec![image.to_owned()]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
assert_eq!(
|
||||
admitted, expected,
|
||||
"{policy:?}, print_background={print_background}, uses_mask={uses_mask}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok::<_, anyhow::Error>(())
|
||||
})
|
||||
.await
|
||||
.expect("PDF capture admits its background and mask resources according to their use");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn native_pdf_embeds_a_loaded_font_under_its_print_declaration() {
|
||||
run_page_vm_async_test(async move {
|
||||
|
||||
@@ -264,10 +264,17 @@ impl ScriptVm {
|
||||
let Some(mut pass) = pass else {
|
||||
return Ok(None);
|
||||
};
|
||||
let css_images = if requests_paint {
|
||||
pass.css_image_references().to_vec()
|
||||
} else {
|
||||
Vec::new()
|
||||
let css_images = match (requests_paint, request.includes_backgrounds()) {
|
||||
(false, _) => Vec::new(),
|
||||
(true, true) => pass.css_image_references().to_vec(),
|
||||
(true, false) => pass
|
||||
.css_image_references()
|
||||
.iter()
|
||||
.filter(|image| {
|
||||
image.purpose != moli_layout::LayoutCssImagePurpose::Background
|
||||
})
|
||||
.cloned()
|
||||
.collect(),
|
||||
};
|
||||
let value = consume(&mut pass)?;
|
||||
context_host.finish_layout_publication(publication, pass)?;
|
||||
|
||||
Reference in New Issue
Block a user