Skip to content

The headless browser for AI agents and web scraping

Improve render lifecycle, geometry, and PDF parity

SGavrl68f20cf5255d
diff --git a/crates/obscura-browser/src/lib.rs b/crates/obscura-browser/src/lib.rs
index 02e1266f..53c57257 100644
--- a/crates/obscura-browser/src/lib.rs
+++ b/crates/obscura-browser/src/lib.rs
@@ -12,7 +12,7 @@ pub use obscura_js::HTML_TO_MARKDOWN_JS;
pub use obscura_js::{validate_capture_region, CaptureError, CaptureRegion};
pub use page::{NetworkEvent, Page, PageError};
#[cfg(feature = "render")]
-pub use pdf::{RasterPdfError, RasterPdfOptions};
+pub use pdf::{RasterPdfError, RasterPdfOptions, RasterPdfPageRange};
// Re-exported so the embeddable `obscura` crate (which depends on obscura-browser,
// not obscura-js) can surface the interception channel types.
pub use obscura_js::ops::{InterceptResolution, InterceptedRequest};
diff --git a/crates/obscura-browser/src/pdf.rs b/crates/obscura-browser/src/pdf.rs
index 851eb355..b4208115 100644
--- a/crates/obscura-browser/src/pdf.rs
+++ b/crates/obscura-browser/src/pdf.rs
@@ -14,14 +14,28 @@ use crate::Page;
const POINTS_PER_INCH: f32 = 72.0;
const MAX_PAPER_INCHES: f32 = 200.0;
const MAX_PDF_PAGES: usize = 250;
+// Page ranges may select a small bounded subset from a much longer document.
+// Keep the arithmetic/index space finite without charging unselected pages
+// against the output-page limit.
+const MAX_PDF_DOCUMENT_PAGES: usize = 1_000_000;
const MAX_PDF_PAGE_PIXELS: u64 = 16 * 1024 * 1024;
const MAX_PDF_TOTAL_RASTER_PIXELS: u64 = 64 * 1024 * 1024;
const MAX_PDF_OUTPUT_BYTES: usize = 64 * 1024 * 1024;
-#[derive(Clone, Copy, Debug, PartialEq)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct RasterPdfPageRange {
+ /// One-based inclusive first page. `None` means the first page.
+ pub start: Option<usize>,
+ /// One-based inclusive last page. `None` means the final page.
+ pub end: Option<usize>,
+}
+
+#[derive(Clone, Debug, PartialEq)]
pub struct RasterPdfOptions {
pub landscape: bool,
pub print_background: bool,
+ pub scale: f32,
+ pub page_ranges: Vec<RasterPdfPageRange>,
pub paper_width_in: f32,
pub paper_height_in: f32,
pub margin_top_in: f32,
@@ -35,6 +49,8 @@ impl Default for RasterPdfOptions {
Self {
landscape: false,
print_background: false,
+ scale: 1.0,
+ page_ranges: Vec::new(),
paper_width_in: 8.5,
paper_height_in: 11.0,
// CDP's defaults are one centimetre.
@@ -52,6 +68,10 @@ pub enum RasterPdfError {
InvalidPaperSize,
#[error("PDF margins must be finite, non-negative, and leave a printable area")]
InvalidMargins,
+ #[error("PDF scale must be finite and between 0.1 and 2")]
+ InvalidScale,
+ #[error("PDF page ranges select no pages from this document")]
+ EmptyPageRange,
#[error("the page has no retained renderable document")]
NoRenderableDocument,
#[error("PDF pagination would exceed the {0}-page safety limit")]
@@ -85,7 +105,7 @@ struct PaginationPlan {
}
impl RasterPdfOptions {
- fn page_geometry(self) -> Result<(f32, f32, f32, f32, f32, f32), RasterPdfError> {
+ fn page_geometry(&self) -> Result<(f32, f32, f32, f32, f32, f32), RasterPdfError> {
let values = [self.paper_width_in, self.paper_height_in];
if values
.iter()
@@ -137,8 +157,12 @@ fn pagination_plan(
content_height: f32,
printable_width: f32,
printable_height: f32,
+ scale: f32,
) -> Result<PaginationPlan, RasterPdfError> {
- let points_per_css_pixel = printable_width / content_width;
+ if !scale.is_finite() || !(0.1..=2.0).contains(&scale) {
+ return Err(RasterPdfError::InvalidScale);
+ }
+ let points_per_css_pixel = printable_width / content_width * scale;
let css_page_height = printable_height / points_per_css_pixel;
if !points_per_css_pixel.is_finite()
|| points_per_css_pixel <= 0.0
@@ -149,10 +173,24 @@ fn pagination_plan(
}
let page_count_value = (content_height / css_page_height).ceil().max(1.0);
- if !page_count_value.is_finite() || page_count_value > MAX_PDF_PAGES as f32 {
- return Err(RasterPdfError::TooManyPages(MAX_PDF_PAGES));
+ if !page_count_value.is_finite() || page_count_value > MAX_PDF_DOCUMENT_PAGES as f32 {
+ return Err(RasterPdfError::TooManyPages(MAX_PDF_DOCUMENT_PAGES));
}
let page_count = page_count_value as usize;
+
+ Ok(PaginationPlan {
+ points_per_css_pixel,
+ css_page_height,
+ page_count,
+ })
+}
+
+fn validate_selected_raster_work(
+ content_width: f32,
+ content_height: f32,
+ plan: PaginationPlan,
+ selected_pages: &[usize],
+) -> Result<(), RasterPdfError> {
let pixel_width = content_width.ceil();
if !pixel_width.is_finite()
|| pixel_width <= 0.0
@@ -162,9 +200,12 @@ fn pagination_plan(
}
let pixel_width = pixel_width as u64;
let mut total_pixels = 0u64;
- for page_index in 0..page_count {
- let y = page_index as f32 * css_page_height;
- let slice_height = (content_height - y).min(css_page_height).ceil();
+ for &page_index in selected_pages {
+ if page_index >= plan.page_count {
+ return Err(RasterPdfError::EmptyPageRange);
+ }
+ let y = page_index as f32 * plan.css_page_height;
+ let slice_height = (content_height - y).min(plan.css_page_height).ceil();
if !slice_height.is_finite()
|| slice_height <= 0.0
|| slice_height > obscura_js::MAX_CAPTURE_DIMENSION as f32
@@ -185,11 +226,49 @@ fn pagination_plan(
}
}
- Ok(PaginationPlan {
- points_per_css_pixel,
- css_page_height,
- page_count,
- })
+ Ok(())
+}
+
+fn selected_page_indices(
+ page_count: usize,
+ ranges: &[RasterPdfPageRange],
+) -> Result<Vec<usize>, RasterPdfError> {
+ if page_count == 0 {
+ return Err(RasterPdfError::EmptyPageRange);
+ }
+ if ranges.is_empty() {
+ if page_count > MAX_PDF_PAGES {
+ return Err(RasterPdfError::TooManyPages(MAX_PDF_PAGES));
+ }
+ return Ok((0..page_count).collect());
+ }
+ let mut selected = std::collections::BTreeSet::new();
+ for range in ranges {
+ let start = range.start.unwrap_or(1);
+ let end = range.end.unwrap_or(page_count);
+ if start == 0 || end == 0 || start > end {
+ return Err(RasterPdfError::EmptyPageRange);
+ }
+ if start > page_count {
+ continue;
+ }
+ let end = end.min(page_count);
+ let span = end - start + 1;
+ if span > MAX_PDF_PAGES {
+ return Err(RasterPdfError::TooManyPages(MAX_PDF_PAGES));
+ }
+ for page in start..=end {
+ selected.insert(page - 1);
+ if selected.len() > MAX_PDF_PAGES {
+ return Err(RasterPdfError::TooManyPages(MAX_PDF_PAGES));
+ }
+ }
+ }
+ let selected = selected.into_iter().collect::<Vec<_>>();
+ if selected.is_empty() {
+ return Err(RasterPdfError::EmptyPageRange);
+ }
+ Ok(selected)
}
impl Page {
@@ -197,7 +276,7 @@ impl Page {
///
/// The full document width is scaled uniformly into the printable width;
/// vertical slices become pages. This does not reflow into `@media print`
- /// or implement CSS paged media, headers, footers, or page ranges.
+ /// or implement CSS paged media, headers, or footers.
pub fn raster_pdf(&self, options: RasterPdfOptions) -> Result<Vec<u8>, RasterPdfError> {
let (page_width, page_height, printable_width, printable_height, left, bottom) =
options.page_geometry()?;
@@ -221,16 +300,20 @@ impl Page {
content_height,
printable_width,
printable_height,
+ options.scale,
)?;
+ let selected_pages = selected_page_indices(plan.page_count, &options.page_ranges)?;
+ validate_selected_raster_work(content_width, content_height, plan, &selected_pages)?;
encode_pdf_pages(
- plan.page_count,
+ selected_pages.len(),
page_width,
page_height,
left,
bottom,
printable_height,
- |page_index| {
+ |output_page_index| {
+ let page_index = selected_pages[output_page_index];
let y = page_index as f32 * plan.css_page_height;
let slice_height = (content_height - y).min(plan.css_page_height);
let png = js
@@ -249,7 +332,7 @@ impl Page {
let rgb = decoded.into_rgb8();
Ok(RasterPage {
rgb,
- draw_width_pt: printable_width,
+ draw_width_pt: content_width * plan.points_per_css_pixel,
draw_height_pt: slice_height * plan.points_per_css_pixel,
#[cfg(test)]
_lifetime_probe: None,
@@ -443,26 +526,166 @@ mod tests {
fn pagination_preflight_bounds_pages_and_raster_work() {
let (_, _, printable_width, printable_height, _, _) =
RasterPdfOptions::default().page_geometry().unwrap();
- let ordinary = pagination_plan(1280.0, 10_000.0, printable_width, printable_height)
+ let ordinary = pagination_plan(1280.0, 10_000.0, printable_width, printable_height, 1.0)
.expect("an ordinary multi-page document stays inside the budget");
assert!(ordinary.page_count > 1);
+ let ordinary_pages = selected_page_indices(ordinary.page_count, &[]).unwrap();
+ validate_selected_raster_work(1280.0, 10_000.0, ordinary, &ordinary_pages).unwrap();
+ let oversized_page =
+ pagination_plan(5_000.0, 5_000.0, printable_width, printable_height, 1.0).unwrap();
+ let oversized_page_selection =
+ selected_page_indices(oversized_page.page_count, &[]).unwrap();
assert_eq!(
- pagination_plan(5_000.0, 5_000.0, printable_width, printable_height).unwrap_err(),
+ validate_selected_raster_work(
+ 5_000.0,
+ 5_000.0,
+ oversized_page,
+ &oversized_page_selection,
+ )
+ .unwrap_err(),
RasterPdfError::RasterWorkLimitExceeded,
"one excessively large raster page must fail before capture"
);
+
+ let too_much_total =
+ pagination_plan(1_000.0, 70_000.0, printable_width, printable_height, 1.0).unwrap();
+ let too_much_total_selection =
+ selected_page_indices(too_much_total.page_count, &[]).unwrap();
assert_eq!(
- pagination_plan(1_000.0, 70_000.0, printable_width, printable_height).unwrap_err(),
+ validate_selected_raster_work(
+ 1_000.0,
+ 70_000.0,
+ too_much_total,
+ &too_much_total_selection,
+ )
+ .unwrap_err(),
RasterPdfError::RasterWorkLimitExceeded,
"many individually valid pages must still respect a total work budget"
);
+
+ let too_many =
+ pagination_plan(1_000.0, 400_000.0, printable_width, printable_height, 1.0).unwrap();
assert_eq!(
- pagination_plan(1_000.0, 400_000.0, printable_width, printable_height).unwrap_err(),
+ selected_page_indices(too_many.page_count, &[]).unwrap_err(),
RasterPdfError::TooManyPages(MAX_PDF_PAGES),
);
}
+ #[test]
+ fn selected_ranges_alone_determine_output_and_raster_budgets() {
+ let (_, _, printable_width, printable_height, _, _) =
+ RasterPdfOptions::default().page_geometry().unwrap();
+ let long =
+ pagination_plan(1_000.0, 400_000.0, printable_width, printable_height, 1.0).unwrap();
+ assert!(long.page_count > MAX_PDF_PAGES);
+ let selected = selected_page_indices(
+ long.page_count,
+ &[RasterPdfPageRange {
+ start: Some(1),
+ end: Some(1),
+ }],
+ )
+ .unwrap();
+ assert_eq!(selected, vec![0]);
+ validate_selected_raster_work(1_000.0, 400_000.0, long, &selected).unwrap();
+
+ assert_eq!(
+ selected_page_indices(
+ long.page_count,
+ &[RasterPdfPageRange {
+ start: Some(1),
+ end: Some(MAX_PDF_PAGES + 1),
+ }],
+ ),
+ Err(RasterPdfError::TooManyPages(MAX_PDF_PAGES))
+ );
+
+ let base = pagination_plan(800.0, 2_000.0, printable_width, printable_height, 1.0)
+ .expect("base geometry");
+ let impossible_height =
+ base.css_page_height * (MAX_PDF_DOCUMENT_PAGES as f32 + 16.0);
+ assert_eq!(
+ pagination_plan(
+ 800.0,
+ impossible_height,
+ printable_width,
+ printable_height,
+ 1.0,
+ )
+ .unwrap_err(),
+ RasterPdfError::TooManyPages(MAX_PDF_DOCUMENT_PAGES)
+ );
+ }
+
+ #[test]
+ fn scale_changes_css_page_span_and_rejects_invalid_values() {
+ let (_, _, printable_width, printable_height, _, _) =
+ RasterPdfOptions::default().page_geometry().unwrap();
+ let normal =
+ pagination_plan(800.0, 2_000.0, printable_width, printable_height, 1.0).unwrap();
+ let enlarged =
+ pagination_plan(800.0, 2_000.0, printable_width, printable_height, 2.0).unwrap();
+ assert_eq!(
+ enlarged.points_per_css_pixel,
+ normal.points_per_css_pixel * 2.0
+ );
+ assert_eq!(enlarged.css_page_height, normal.css_page_height / 2.0);
+ assert!(enlarged.page_count >= normal.page_count);
+ assert_eq!(
+ pagination_plan(800.0, 2_000.0, printable_width, printable_height, 0.09,)
+ .unwrap_err(),
+ RasterPdfError::InvalidScale
+ );
+ }
+
+ #[test]
+ fn page_ranges_clip_deduplicate_and_preserve_document_order() {
+ assert_eq!(selected_page_indices(4, &[]).unwrap(), vec![0, 1, 2, 3]);
+ assert_eq!(
+ selected_page_indices(
+ 6,
+ &[
+ RasterPdfPageRange {
+ start: Some(3),
+ end: Some(5),
+ },
+ RasterPdfPageRange {
+ start: Some(1),
+ end: Some(3),
+ },
+ RasterPdfPageRange {
+ start: Some(5),
+ end: None,
+ },
+ ],
+ )
+ .unwrap(),
+ vec![0, 1, 2, 3, 4, 5]
+ );
+ assert_eq!(
+ selected_page_indices(
+ 6,
+ &[RasterPdfPageRange {
+ start: None,
+ end: Some(2),
+ }],
+ )
+ .unwrap(),
+ vec![0, 1]
+ );
+ assert_eq!(
+ selected_page_indices(
+ 3,
+ &[RasterPdfPageRange {
+ start: Some(9),
+ end: Some(12),
+ }],
+ ),
+ Err(RasterPdfError::EmptyPageRange)
+ );
+ }
+
#[test]
fn writer_emits_xref_and_one_image_per_page() {
let pdf = encode_pdf_pages(1, 612.0, 792.0, 36.0, 36.0, 720.0, |_| {
diff --git a/crates/obscura-cdp/src/domains/pdf.rs b/crates/obscura-cdp/src/domains/pdf.rs
index 0a749ffe..ef416a2e 100644
--- a/crates/obscura-cdp/src/domains/pdf.rs
+++ b/crates/obscura-cdp/src/domains/pdf.rs
@@ -6,6 +6,10 @@ use crate::dispatch::CdpContext;
#[cfg(feature = "render")]
const MAX_BASE64_PDF_BYTES: usize = 64 * 1024 * 1024;
+#[cfg(feature = "render")]
+const MAX_PAGE_RANGES_BYTES: usize = 16 * 1024;
+#[cfg(feature = "render")]
+const MAX_PAGE_RANGE_PARTS: usize = 512;
#[cfg(feature = "render")]
fn base64_encoded_len(raw_len: usize) -> Option<usize> {
@@ -20,7 +24,7 @@ enum PdfTransferMode {
}
#[cfg(feature = "render")]
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Debug)]
struct ParsedPdfOptions {
raster: obscura_browser::RasterPdfOptions,
transfer_mode: PdfTransferMode,
@@ -49,6 +53,92 @@ fn boolean(params: &Value, name: &str, default: bool) -> Result<bool, String> {
}
}
+#[cfg(feature = "render")]
+fn parse_page_ranges(value: &str) -> Result<Vec<obscura_browser::RasterPdfPageRange>, String> {
+ let value = value.trim();
+ if value.is_empty() {
+ return Ok(Vec::new());
+ }
+ if value.len() > MAX_PAGE_RANGES_BYTES {
+ return Err(format!(
+ "Invalid parameters: pageRanges exceeds the {MAX_PAGE_RANGES_BYTES}-byte limit"
+ ));
+ }
+
+ let mut ranges = Vec::new();
+ for (index, part) in value.split(',').enumerate() {
+ if index >= MAX_PAGE_RANGE_PARTS {
+ return Err(format!(
+ "Invalid parameters: pageRanges exceeds the {MAX_PAGE_RANGE_PARTS}-range limit"
+ ));
+ }
+ let part = part.trim();
+ if part.is_empty() {
+ return Err("Invalid parameters: pageRanges contains an empty range".to_string());
+ }
+ let page = |text: &str| -> Result<Option<usize>, String> {
+ let text = text.trim();
+ if text.is_empty() {
+ return Ok(None);
+ }
+ text.parse::<usize>()
+ .ok()
+ .filter(|page| *page > 0)
+ .map(Some)
+ .ok_or_else(|| {
+ format!("Invalid parameters: pageRanges has invalid page {text:?}")
+ })
+ };
+ let (start, end) = match part.split_once('-') {
+ Some((start, end)) if !end.contains('-') => (page(start)?, page(end)?),
+ Some(_) => {
+ return Err(format!(
+ "Invalid parameters: pageRanges has invalid range {part:?}"
+ ))
+ }
+ None => {
+ let page = page(part)?.ok_or_else(|| {
+ "Invalid parameters: pageRanges contains an empty page".to_string()
+ })?;
+ (Some(page), Some(page))
+ }
+ };
+ if start.is_none() && end.is_none() {
+ return Err("Invalid parameters: pageRanges range '-' is empty".to_string());
+ }
+ if matches!((start, end), (Some(start), Some(end)) if start > end) {
+ return Err(format!(
+ "Invalid parameters: pageRanges range {part:?} is descending"
+ ));
+ }
+ ranges.push(obscura_browser::RasterPdfPageRange { start, end });
+ }
+
+ // Normalize before crossing the CDP/browser boundary. This bounds later
+ // selection work and makes repeated or overlapping user ranges occupy one
+ // entry while preserving Chromium's document-order, print-once semantics.
+ ranges.sort_by_key(|range| (range.start.unwrap_or(1), range.end.unwrap_or(usize::MAX)));
+ let mut normalized: Vec<obscura_browser::RasterPdfPageRange> = Vec::new();
+ for range in ranges {
+ let start = range.start.unwrap_or(1);
+ if let Some(previous) = normalized.last_mut() {
+ let previous_end = previous.end.unwrap_or(usize::MAX);
+ if start <= previous_end.saturating_add(1) {
+ previous.end = match (previous.end, range.end) {
+ (None, _) | (_, None) => None,
+ (Some(previous), Some(next)) => Some(previous.max(next)),
+ };
+ continue;
+ }
+ }
+ normalized.push(obscura_browser::RasterPdfPageRange {
+ start: Some(start),
+ end: range.end,
+ });
+ }
+ Ok(normalized)
+}
+
#[cfg(feature = "render")]
fn parse_options(params: &Value) -> Result<ParsedPdfOptions, String> {
if !params.is_object() {
@@ -72,20 +162,18 @@ fn parse_options(params: &Value) -> Result<ParsedPdfOptions, String> {
// unusable. Accept and truthfully report `taggedPdf:false` in the response.
let requested_tagged_pdf = boolean(params, "generateTaggedPDF", false)?;
let scale = number(params, "scale", 1.0)?;
- if (scale - 1.0).abs() > f32::EPSILON {
- return Err(
- "Page.printToPDF scale is not supported by the raster paginator; only scale=1 is accepted"
- .to_string(),
- );
+ if !(0.1..=2.0).contains(&scale) {
+ return Err("Invalid parameters: scale must be between 0.1 and 2".to_string());
}
- if let Some(value) = params.get("pageRanges") {
- let ranges = value
- .as_str()
- .ok_or("Invalid parameters: pageRanges must be a string")?;
- if !ranges.trim().is_empty() {
- return Err("Page.printToPDF pageRanges are not yet supported".to_string());
+ let page_ranges = match params.get("pageRanges") {
+ Some(value) => {
+ let ranges = value
+ .as_str()
+ .ok_or("Invalid parameters: pageRanges must be a string")?;
+ parse_page_ranges(ranges)?
}
- }
+ None => Vec::new(),
+ };
for name in ["headerTemplate", "footerTemplate"] {
if let Some(value) = params.get(name) {
let template = value
@@ -111,6 +199,8 @@ fn parse_options(params: &Value) -> Result<ParsedPdfOptions, String> {
raster: obscura_browser::RasterPdfOptions {
landscape: boolean(params, "landscape", defaults.landscape)?,
print_background: requested_print_background,
+ scale,
+ page_ranges,
paper_width_in: number(params, "paperWidth", defaults.paper_width_in)?,
paper_height_in: number(params, "paperHeight", defaults.paper_height_in)?,
margin_top_in: number(params, "marginTop", defaults.margin_top_in)?,
@@ -148,6 +238,8 @@ pub async fn print_to_pdf(
"obscuraCapabilities": {
"cssPagedMedia": false,
"honorsPrintBackground": true,
+ "honorsScale": true,
+ "pageRanges": true,
"printColorAdjustExact": false,
"taggedPdf": false,
},
@@ -212,17 +304,78 @@ mod tests {
assert!(!standard.requested_print_background);
assert!(!standard.raster.print_background);
assert!(standard.requested_tagged_pdf);
+ assert_eq!(standard.raster.scale, 1.0);
+ assert!(standard.raster.page_ranges.is_empty());
for params in [
json!({"displayHeaderFooter": true}),
json!({"preferCSSPageSize": true}),
- json!({"scale": 0.5}),
- json!({"pageRanges": "2-3"}),
json!({"headerTemplate": "<span>title</span>"}),
json!({"generateDocumentOutline": true}),
] {
assert!(parse_options(&params).is_err(), "must reject {params}");
}
+
+ let ranged = parse_options(&json!({
+ "scale": 0.5,
+ "pageRanges": "1, 3-5, 8-",
+ }))
+ .expect("CDP scale and page ranges");
+ assert_eq!(ranged.raster.scale, 0.5);
+ assert_eq!(
+ ranged.raster.page_ranges,
+ vec![
+ obscura_browser::RasterPdfPageRange {
+ start: Some(1),
+ end: Some(1),
+ },
+ obscura_browser::RasterPdfPageRange {
+ start: Some(3),
+ end: Some(5),
+ },
+ obscura_browser::RasterPdfPageRange {
+ start: Some(8),
+ end: None,
+ },
+ ]
+ );
+ assert_eq!(
+ parse_page_ranges("5-8, 1-3, 3-6, 10, 9, 12-").unwrap(),
+ vec![
+ obscura_browser::RasterPdfPageRange {
+ start: Some(1),
+ end: Some(10),
+ },
+ obscura_browser::RasterPdfPageRange {
+ start: Some(12),
+ end: None,
+ },
+ ],
+ "overlapping, adjacent, and repeated ranges must normalize before pagination"
+ );
+ for params in [
+ json!({"scale": 0.09}),
+ json!({"scale": 2.01}),
+ json!({"pageRanges": "0"}),
+ json!({"pageRanges": "3-2"}),
+ json!({"pageRanges": "1,,2"}),
+ json!({"pageRanges": "1-2-3"}),
+ ] {
+ assert!(parse_options(&params).is_err(), "must reject {params}");
+ }
+
+ let too_many_parts = std::iter::repeat_n("1", MAX_PAGE_RANGE_PARTS + 1)
+ .collect::<Vec<_>>()
+ .join(",");
+ assert!(
+ parse_page_ranges(&too_many_parts).is_err(),
+ "range-token work must remain bounded before output allocation"
+ );
+ let too_many_bytes = "1".repeat(MAX_PAGE_RANGES_BYTES + 1);
+ assert!(
+ parse_page_ranges(&too_many_bytes).is_err(),
+ "range input bytes must be bounded before numeric parsing"
+ );
}
#[test]
@@ -317,6 +470,8 @@ mod tests {
streamed["obscuraCapabilities"]["honorsPrintBackground"],
true
);
+ assert_eq!(streamed["obscuraCapabilities"]["honorsScale"], true);
+ assert_eq!(streamed["obscuraCapabilities"]["pageRanges"], true);
assert_eq!(streamed["obscuraTaggedPDF"], false);
assert_eq!(streamed["obscuraRequestedTaggedPDF"], true);
assert_eq!(
@@ -353,5 +508,25 @@ mod tests {
.is_err(),
"closed PDF handles must release their buffer"
);
+
+ let ranged = print_to_pdf(
+ &json!({
+ "paperWidth": 4.0, "paperHeight": 6.0,
+ "marginTop": 0.5, "marginBottom": 0.5,
+ "marginLeft": 0.5, "marginRight": 0.5,
+ "scale": 1,
+ "pageRanges": "2",
+ }),
+ &mut ctx,
+ &session,
+ )
+ .await
+ .expect("single selected page");
+ let ranged_bytes = base64::engine::general_purpose::STANDARD
+ .decode(ranged["data"].as_str().unwrap())
+ .unwrap();
+ let ranged_text = String::from_utf8_lossy(&ranged_bytes);
+ assert!(ranged_text.contains("/Count 1"));
+ assert_eq!(ranged_text.matches("/Subtype /Image").count(), 1);
}
}
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 4de6ced4..e6c7976b 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -90,7 +90,14 @@ const _DOM_MUTATION_COMMANDS = new Set([
]);
const _dom = (cmd, a1, a2) => {
const result = Deno.core.ops.op_dom(cmd, String(a1 ?? ""), String(a2 ?? ""));
- if (_DOM_MUTATION_COMMANDS.has(cmd)) _domMutationEpoch++;
+ if (_DOM_MUTATION_COMMANDS.has(cmd)) {
+ _domMutationEpoch++;
+ // Resize observation is tied to rendering-invalidating DOM work. The
+ // hook is installed later in bootstrap, before page script can run.
+ if (typeof globalThis.__obscura_recompute_resizes === "function") {
+ globalThis.__obscura_recompute_resizes();
+ }
+ }
return result;
};
@@ -3240,6 +3247,7 @@ class Element extends Node {
if (changed && !this._scrollSuppress) this._fireScroll();
if (changed &&
typeof globalThis.__obscura_recompute_intersections === "function") {
+ // Scrolling changes target positions, not ResizeObserver box sizes.
globalThis.__obscura_recompute_intersections();
}
}
@@ -4634,6 +4642,13 @@ class HTMLImageElement extends Element {
_applyImageMetadata(metadata, request, dispatchEvent) {
if (request !== this._imageRequest) return;
+ const previousLifecycle = [
+ this._imageComplete,
+ this._imageDecoded,
+ this._imageCurrentSrc,
+ this._imageNaturalWidth,
+ this._imageNaturalHeight,
+ ];
const selected = metadata && metadata.currentSrc
? String(metadata.currentSrc)
: "";
@@ -4666,6 +4681,18 @@ class HTMLImageElement extends Element {
try { this.dispatchEvent(new Event("error")); } catch (_error) {}
}
}
+ const lifecycleChanged =
+ previousLifecycle[0] !== this._imageComplete ||
+ previousLifecycle[1] !== this._imageDecoded ||
+ previousLifecycle[2] !== this._imageCurrentSrc ||
+ previousLifecycle[3] !== this._imageNaturalWidth ||
+ previousLifecycle[4] !== this._imageNaturalHeight;
+ if (lifecycleChanged) {
+ // Intrinsic dimensions can become layout input at request completion
+ // even though no DOM attribute changed. Stable cache-only getters must
+ // not manufacture rendering updates on every read.
+ _scheduleResizeRenderCheckpoint();
+ }
}
_resolveImageDecodes(request) {
@@ -5959,43 +5986,280 @@ if (!('isConnected' in Node.prototype)) {
});
}
+// Resize observation is part of the rendering update, not a timer. Keep the
+// last delivered size for each observed box and perform one coalesced geometry
+// checkpoint after DOM/viewport work. This follows the browser lifecycle and,
+// importantly, does not keep the event loop alive with speculative re-fires.
+globalThis.__resizeObservers = [];
+let _resizeRenderCheckpointPending = false;
+let _resizeRenderCheckpointRunning = false;
+let _resizeRenderCheckpointRerun = false;
+function _registerResizeObserver(observer) {
+ if (!globalThis.__resizeObservers.includes(observer)) {
+ globalThis.__resizeObservers.push(observer);
+ }
+}
+function _unregisterResizeObserver(observer) {
+ const index = globalThis.__resizeObservers.indexOf(observer);
+ if (index >= 0) globalThis.__resizeObservers.splice(index, 1);
+}
+function _scheduleResizeRenderCheckpoint() {
+ if (!globalThis.__resizeObservers.length) return;
+ if (_resizeRenderCheckpointRunning) {
+ _resizeRenderCheckpointRerun = true;
+ return;
+ }
+ if (_resizeRenderCheckpointPending) return;
+ _resizeRenderCheckpointPending = true;
+ _scheduleAfter(0, () => {
+ _resizeRenderCheckpointPending = false;
+ _resizeRenderCheckpointRunning = true;
+ let depth = 0;
+ let skipped = false;
+ // Depth strictly increases after each broadcast, so this is naturally
+ // bounded by tree depth. Keep a hard ceiling for adversarial callbacks
+ // that manufacture an ever-deeper subtree during one delivery cycle.
+ for (let iteration = 0; iteration < 64; iteration++) {
+ _resizeRenderCheckpointRerun = false;
+ const measurements = new Map();
+ let shallowest = Infinity;
+ let active = false;
+ skipped = false;
+ const observers = [...globalThis.__resizeObservers];
+ // Gather every observer before invoking any callback. A callback from an
+ // earlier observer must not change the geometry gathered for a later one.
+ for (const observer of observers) {
+ const gathered = observer._gather(measurements, depth);
+ active = active || gathered.active;
+ skipped = skipped || gathered.skipped;
+ shallowest = Math.min(shallowest, gathered.shallowest);
+ }
+ if (!active) break;
+ for (const observer of observers) observer._broadcast();
+ depth = shallowest;
+ if (!_resizeRenderCheckpointRerun) break;
+ if (iteration === 63) skipped = true;
+ }
+ _resizeRenderCheckpointRunning = false;
+ _resizeRenderCheckpointRerun = false;
+ if (skipped) {
+ // Match the standardized loop-limit signal without queuing another
+ // internal task that could keep a pathological page permanently busy.
+ try {
+ globalThis.dispatchEvent(new ErrorEvent("error", {
+ message: "ResizeObserver loop completed with undelivered notifications."
+ }));
+ } catch (_error) {}
+ }
+ });
+}
+globalThis.__obscura_recompute_resizes = _scheduleResizeRenderCheckpoint;
+function _roNumber(value) {
+ const number = Number.parseFloat(value);
+ return Number.isFinite(number) ? number : 0;
+}
+function _roPhysicalSize(inlineSize, blockSize, vertical) {
+ return vertical
+ ? new ResizeObserverSize(_roConstructionKey, blockSize, inlineSize)
+ : new ResizeObserverSize(_roConstructionKey, inlineSize, blockSize);
+}
+function _roNodeDepth(target) {
+ let depth = 1;
+ let node = target;
+ while (node && (node = node.parentNode || node.host || null)) depth++;
+ return depth;
+}
+function _roMeasurement(target) {
+ let geometry = null;
+ const hasRenderer = typeof Deno.core.ops.op_layout_geometry === "function";
+ if (hasRenderer && target?._nid != null) {
+ try {
+ const raw = Deno.core.ops.op_layout_geometry(String(target._nid | 0));
+ geometry = raw ? JSON.parse(raw) : null;
+ } catch (_error) {}
+ }
+
+ // Preserve deterministic geometry in non-render builds. This path has no
+ // native layout cache, but lifecycle behavior (initial delivery and
+ // change-only rechecks) should remain useful to automation consumers.
+ if (!hasRenderer && target?.getBoundingClientRect) {
+ const rect = target.getBoundingClientRect();
+ geometry = {
+ x: rect.x, y: rect.y,
+ clientWidth: rect.width, clientHeight: rect.height,
+ };
+ }
+
+ // No renderer box (detached, display:none) has zero sizes. The initial zero
+ // is still delivered because an observation starts without a reported size.
+ if (!geometry) {
+ const zero = _roPhysicalSize(0, 0, false);
+ return {
+ contentRect: _ioRect(0, 0, 0, 0),
+ contentBoxSize: [zero],
+ borderBoxSize: [_roPhysicalSize(0, 0, false)],
+ devicePixelContentBoxSize: [_roPhysicalSize(0, 0, false)],
+ selected: { "content-box": [0, 0], "border-box": [0, 0], "device-pixel-content-box": [0, 0] },
+ };
+ }
+
+ const style = getComputedStyle(target);
+ const paddingTop = _roNumber(style.paddingTop);
+ const paddingRight = _roNumber(style.paddingRight);
+ const paddingBottom = _roNumber(style.paddingBottom);
+ const paddingLeft = _roNumber(style.paddingLeft);
+ const borderTop = _roNumber(style.borderTopWidth);
+ const borderRight = _roNumber(style.borderRightWidth);
+ const borderBottom = _roNumber(style.borderBottomWidth);
+ const borderLeft = _roNumber(style.borderLeftWidth);
+ const clientWidth = Math.max(0, Number(geometry.clientWidth) || 0);
+ const clientHeight = Math.max(0, Number(geometry.clientHeight) || 0);
+ const contentWidth = Math.max(0, clientWidth - paddingLeft - paddingRight);
+ const contentHeight = Math.max(0, clientHeight - paddingTop - paddingBottom);
+ const borderWidth = Math.max(0, clientWidth + borderLeft + borderRight);
+ const borderHeight = Math.max(0, clientHeight + borderTop + borderBottom);
+ const vertical = /^(?:vertical|sideways)/.test(style.writingMode || "");
+ // Per Resize Observer, ordinary non-replaced inline elements have an empty
+ // observed box even though getBoundingClientRect() encloses their glyphs.
+ const replaced = /^(?:IMG|VIDEO|AUDIO|IFRAME|EMBED|OBJECT|INPUT|TEXTAREA|SELECT|CANVAS|SVG)$/.test(
+ target.tagName || ""
+ );
+ const emptyInline = style.display === "inline" && !replaced;
+ const observedContentWidth = emptyInline ? 0 : contentWidth;
+ const observedContentHeight = emptyInline ? 0 : contentHeight;
+ const observedBorderWidth = emptyInline ? 0 : borderWidth;
+ const observedBorderHeight = emptyInline ? 0 : borderHeight;
+ const contentSize = _roPhysicalSize(observedContentWidth, observedContentHeight, vertical);
+ const borderSize = _roPhysicalSize(observedBorderWidth, observedBorderHeight, vertical);
+
+ // Device-pixel content sizes snap the content edges, rather than merely
+ // rounding a CSS size multiplied by DPR. Preserve that distinction for
+ // fractional positions and dimensions.
+ const dpr = Math.max(0, Number(globalThis.devicePixelRatio) || 1);
+ const contentLeft = (Number(geometry.x) + borderLeft + paddingLeft) * dpr;
+ const contentTop = (Number(geometry.y) + borderTop + paddingTop) * dpr;
+ const deviceWidth = emptyInline ? 0 : Math.max(0,
+ Math.round(contentLeft + contentWidth * dpr) - Math.round(contentLeft));
+ const deviceHeight = emptyInline ? 0 : Math.max(0,
+ Math.round(contentTop + contentHeight * dpr) - Math.round(contentTop));
+ const deviceSize = _roPhysicalSize(deviceWidth, deviceHeight, vertical);
+ return {
+ contentRect: emptyInline
+ ? _ioRect(0, 0, 0, 0)
+ : _ioRect(paddingLeft, paddingTop, contentWidth, contentHeight),
+ contentBoxSize: [contentSize],
+ borderBoxSize: [borderSize],
+ devicePixelContentBoxSize: [deviceSize],
+ selected: {
+ "content-box": [contentSize.inlineSize, contentSize.blockSize],
+ "border-box": [borderSize.inlineSize, borderSize.blockSize],
+ "device-pixel-content-box": [deviceSize.inlineSize, deviceSize.blockSize],
+ },
+ };
+}
+
+const _roConstructionKey = {};
+const _roSizeValues = new WeakMap();
+globalThis.ResizeObserverSize = class ResizeObserverSize {
+ constructor(key, inlineSize, blockSize) {
+ if (key !== _roConstructionKey) throw new TypeError("Illegal constructor");
+ _roSizeValues.set(this, { inlineSize, blockSize });
+ }
+ get inlineSize() { return _roSizeValues.get(this)?.inlineSize; }
+ get blockSize() { return _roSizeValues.get(this)?.blockSize; }
+};
+const _roEntryValues = new WeakMap();
+globalThis.ResizeObserverEntry = class ResizeObserverEntry {
+ constructor(key, target, measurement) {
+ if (key !== _roConstructionKey) throw new TypeError("Illegal constructor");
+ _roEntryValues.set(this, { target, measurement });
+ }
+ get target() { return _roEntryValues.get(this)?.target; }
+ get contentRect() { return _roEntryValues.get(this)?.measurement.contentRect; }
+ get borderBoxSize() { return _roEntryValues.get(this)?.measurement.borderBoxSize; }
+ get contentBoxSize() { return _roEntryValues.get(this)?.measurement.contentBoxSize; }
+ get devicePixelContentBoxSize() {
+ return _roEntryValues.get(this)?.measurement.devicePixelContentBoxSize;
+ }
+};
globalThis.ResizeObserver = class ResizeObserver {
constructor(callback) {
+ if (typeof callback !== "function") {
+ throw new TypeError("ResizeObserver callback must be a function");
+ }
this._callback = callback;
- this._targets = new Set();
- this._connected = true;
- this._fireCount = 0;
+ this._targets = new Map();
+ this._active = [];
+ this._skipped = false;
+ }
+ _gather(measurements, depth) {
+ this._active = [];
+ this._skipped = false;
+ let shallowest = Infinity;
+ for (const [target, observation] of this._targets) {
+ let measurement = measurements.get(target);
+ if (!measurement) {
+ measurement = _roMeasurement(target);
+ measurements.set(target, measurement);
+ }
+ const size = measurement.selected[observation.box];
+ const last = observation.last;
+ if (last && last[0] === size[0] && last[1] === size[1]) continue;
+ const targetDepth = _roNodeDepth(target);
+ // A callback may disconnect and begin observing a different target.
+ // Browsers deliver that initial observation on the next rendering
+ // opportunity. We fold that opportunity into this bounded cycle so it
+ // does not require a persistent frame timer; already-reported targets
+ // still obey the loop-depth guard.
+ if (targetDepth <= depth && last) {
+ this._skipped = true;
+ continue;
+ }
+ shallowest = Math.min(shallowest, targetDepth);
+ this._active.push({ target, observation, measurement, size });
+ }
+ return {
+ active: this._active.length > 0,
+ skipped: this._skipped,
+ shallowest,
+ };
}
- _fireFor(targets) {
- if (!this._connected || !targets.length) return;
- const records = targets.map(target => {
- const r = target.getBoundingClientRect ? target.getBoundingClientRect() : { x: 0, y: 0, width: 100, height: 20 };
- return {
- target,
- contentRect: { x: r.x || 0, y: r.y || 0, width: r.width || 100, height: r.height || 20, top: r.top || 0, left: r.left || 0, bottom: r.bottom || 20, right: r.right || 100 },
- borderBoxSize: [{ blockSize: r.height || 20, inlineSize: r.width || 100 }],
- contentBoxSize: [{ blockSize: r.height || 20, inlineSize: r.width || 100 }],
- devicePixelContentBoxSize: [{ blockSize: r.height || 20, inlineSize: r.width || 100 }],
- };
+ _broadcast() {
+ if (!this._active.length) return;
+ const entries = this._active.map(({ target, observation, measurement, size }) => {
+ // Update before invoking callbacks. Callback-driven mutations are
+ // compared against this delivery in the same bounded delivery cycle.
+ observation.last = size.slice();
+ return new ResizeObserverEntry(_roConstructionKey, target, measurement);
});
- try { this._callback(records, this); } catch (e) { /* RO callbacks must not propagate */ }
+ this._active = [];
+ try { this._callback(entries, this); } catch (_error) {}
}
- observe(el) {
- if (!el || !this._connected) return;
- if (this._targets.has(el)) return;
- this._targets.add(el);
- Promise.resolve().then(() => this._fireFor([el]));
- [200, 800].forEach(delay => {
- setTimeout(() => {
- if (this._connected && this._targets.has(el) && this._fireCount < 16) {
- this._fireCount++;
- this._fireFor([el]);
- }
- }, delay);
- });
+ observe(target, options = {}) {
+ if (!(target instanceof Element)) {
+ throw new TypeError("ResizeObserver.observe requires an Element");
+ }
+ const box = options && options.box != null ? String(options.box) : "content-box";
+ if (box !== "content-box" && box !== "border-box" &&
+ box !== "device-pixel-content-box") {
+ throw new TypeError(`Invalid ResizeObserver box option: ${box}`);
+ }
+ const current = this._targets.get(target);
+ if (current && current.box === box) return;
+ this._targets.set(target, { box, last: null });
+ _registerResizeObserver(this);
+ _scheduleResizeRenderCheckpoint();
+ }
+ unobserve(target) {
+ this._targets.delete(target);
+ if (!this._targets.size) _unregisterResizeObserver(this);
+ }
+ disconnect() {
+ this._targets.clear();
+ this._active = [];
+ this._skipped = false;
+ _unregisterResizeObserver(this);
}
- unobserve(el) { this._targets.delete(el); }
- disconnect() { this._connected = false; this._targets.clear(); }
};
if (typeof TextEncoder === 'undefined') {
@@ -6786,12 +7050,19 @@ function _ioMargins(value) {
if (parsed.length === 3) return [parsed[0], parsed[1], parsed[2], parsed[1]];
return parsed;
}
-function _ioUnsupportedRoot() {
- const error = new Error(
- "Obscura IntersectionObserver element roots require element scroll-container layout"
- );
- error.name = "NotSupportedError";
- return error;
+function _ioClipsOverflow(value) {
+ return /^(?:auto|clip|hidden|overlay|scroll)$/.test(String(value || ""));
+}
+function _ioElementPaddingBox(element, style) {
+ const geometry = element._renderBoxGeometry();
+ const rect = geometry
+ ? element._rectFromRenderGeometry(geometry)
+ : element.getBoundingClientRect();
+ const borderLeft = _roNumber(style.borderLeftWidth);
+ const borderTop = _roNumber(style.borderTopWidth);
+ const width = geometry ? geometry.clientWidth : element.clientWidth;
+ const height = geometry ? geometry.clientHeight : element.clientHeight;
+ return _ioRect(rect.left + borderLeft, rect.top + borderTop, width, height);
}
globalThis.IntersectionObserver = class IntersectionObserver {
constructor(callback, options) {
@@ -6800,7 +7071,11 @@ globalThis.IntersectionObserver = class IntersectionObserver {
}
this._callback = callback;
this._options = options || {};
- if (this._options.root != null) throw _ioUnsupportedRoot();
+ this._root = this._options.root == null ? null : this._options.root;
+ if (this._root !== null && !(this._root instanceof Element) &&
+ this._root?.nodeType !== 9) {
+ throw new TypeError("IntersectionObserver root must be an Element or Document");
+ }
this._margins = _ioMargins(this._options.rootMargin || "0px");
if (!this._margins) throw new SyntaxError("Invalid IntersectionObserver rootMargin");
const raw = this._options.threshold == null
@@ -6819,8 +7094,29 @@ globalThis.IntersectionObserver = class IntersectionObserver {
globalThis.__intersectionObservers.push(this);
}
_rootBounds() {
- const width = globalThis.innerWidth || 1280;
- const height = globalThis.innerHeight || 720;
+ let x = 0, y = 0;
+ let width = globalThis.innerWidth || 1280;
+ let height = globalThis.innerHeight || 720;
+ if (this._root instanceof Element) {
+ const style = getComputedStyle(this._root);
+ const clips = _ioClipsOverflow(style.overflowX) ||
+ _ioClipsOverflow(style.overflowY);
+ if (clips) {
+ const paddingBox = _ioElementPaddingBox(this._root, style);
+ x = paddingBox.left;
+ y = paddingBox.top;
+ // The intersection root for a content-clipping element is its padding
+ // box (the CSSOM client box), independent of its current scroll offset.
+ width = paddingBox.width;
+ height = paddingBox.height;
+ } else {
+ const rect = this._root.getBoundingClientRect();
+ x = rect.left;
+ y = rect.top;
+ width = rect.width;
+ height = rect.height;
+ }
+ }
const resolve = (margin, basis) =>
margin.unit === "%" ? margin.value * basis / 100 : margin.value;
// IntersectionObserver resolves every rootMargin percentage against the
@@ -6829,27 +7125,55 @@ globalThis.IntersectionObserver = class IntersectionObserver {
const right = resolve(this._margins[1], width);
const bottom = resolve(this._margins[2], width);
const left = resolve(this._margins[3], width);
- return _ioRect(-left, -top, width + left + right, height + top + bottom);
+ return _ioRect(x - left, y - top, width + left + right, height + top + bottom);
}
- _entry(target) {
+ _entry(target, root) {
const rect = target.getBoundingClientRect();
- const root = this._rootBounds();
- const left = Math.max(rect.left, root.left);
- const top = Math.max(rect.top, root.top);
- const right = Math.min(rect.right, root.right);
- const bottom = Math.min(rect.bottom, root.bottom);
- const edgesTouch = right >= left && bottom >= top;
+ let inRootTree = !(this._root instanceof Element) || this._root.contains(target);
+ let left = Math.max(rect.left, root.left);
+ let top = Math.max(rect.top, root.top);
+ let right = Math.min(rect.right, root.right);
+ let bottom = Math.min(rect.bottom, root.bottom);
+
+ // Mapping a target to its intersection root clips it at every intervening
+ // overflow container. Intersecting only with the final root incorrectly
+ // exposes offscreen children of nested carousels, virtual lists, and lazy
+ // loading viewports. Use each ancestor's padding box, independently by
+ // axis, matching Chromium's rectangular overflow clip chain.
+ let ancestor = target.parentNode || target.host || null;
+ while (inRootTree && ancestor && ancestor !== this._root && ancestor.nodeType !== 9) {
+ if (ancestor instanceof Element) {
+ const style = getComputedStyle(ancestor);
+ const clipX = _ioClipsOverflow(style.overflowX);
+ const clipY = _ioClipsOverflow(style.overflowY);
+ if (clipX || clipY) {
+ const clip = _ioElementPaddingBox(ancestor, style);
+ if (clipX) {
+ left = Math.max(left, clip.left);
+ right = Math.min(right, clip.right);
+ }
+ if (clipY) {
+ top = Math.max(top, clip.top);
+ bottom = Math.min(bottom, clip.bottom);
+ }
+ }
+ }
+ ancestor = ancestor.parentNode || ancestor.host || null;
+ }
+ if (this._root instanceof Element && ancestor !== this._root) inRootTree = false;
+
+ const edgesTouch = inRootTree && right >= left && bottom >= top;
const width = Math.max(0, right - left);
const height = Math.max(0, bottom - top);
const targetArea = Math.max(0, rect.width) * Math.max(0, rect.height);
- const area = width * height;
const isIntersecting = edgesTouch;
+ const area = isIntersecting ? width * height : 0;
return {
target,
isIntersecting,
intersectionRatio: targetArea > 0 ? area / targetArea : (isIntersecting ? 1 : 0),
boundingClientRect: _ioRect(rect.x, rect.y, rect.width, rect.height),
- intersectionRect: _ioRect(left, top, width, height),
+ intersectionRect: isIntersecting ? _ioRect(left, top, width, height) : _ioRect(0, 0, 0, 0),
rootBounds: root,
time: performance.now(),
};
@@ -6859,8 +7183,8 @@ globalThis.IntersectionObserver = class IntersectionObserver {
while (index < this._thresholds.length && this._thresholds[index] <= ratio) index++;
return index;
}
- _queueChanged(target, forceInitial) {
- const entry = this._entry(target);
+ _queueChanged(target, forceInitial, root) {
+ const entry = this._entry(target, root);
const previous = this._previous.get(target);
const changed = forceInitial || !previous ||
previous.isIntersecting !== entry.isIntersecting ||
@@ -6874,8 +7198,9 @@ globalThis.IntersectionObserver = class IntersectionObserver {
}
_check(targets, forceInitial) {
if (!this._connected) return;
+ const root = this._rootBounds();
for (const target of targets) {
- if (this._targets.has(target)) this._queueChanged(target, !!forceInitial);
+ if (this._targets.has(target)) this._queueChanged(target, !!forceInitial, root);
}
if (!this._records.length || this._deliveryPending) return;
this._deliveryPending = true;
@@ -6915,23 +7240,30 @@ globalThis.IntersectionObserver = class IntersectionObserver {
if (index >= 0) globalThis.__intersectionObservers.splice(index, 1);
}
takeRecords() { return this._records.splice(0); }
- get root() { return null; }
+ get root() { return this._root; }
get rootMargin() {
return this._margins.map((margin) => `${margin.value}${margin.unit}`).join(" ");
}
get thresholds() { return this._thresholds.slice(); }
};
(function() {
- const recompute = () => {
+ const renderingUpdate = () => {
_scheduleIntersectionRenderCheckpoint();
+ _scheduleResizeRenderCheckpoint();
};
- globalThis.__obscura_recompute_intersections = recompute;
- globalThis.addEventListener("resize", recompute);
+ // Scrolling calls the IO-only hook. Actual viewport resizing remains a full
+ // rendering update and schedules both observer families.
+ globalThis.__obscura_recompute_intersections = _scheduleIntersectionRenderCheckpoint;
+ globalThis.addEventListener("resize", renderingUpdate);
const wireUp = () => {
- if (!globalThis.document?.body) return;
- const observer = new MutationObserver(recompute);
+ if (!globalThis.document) return;
+ // DOM writes synchronously mark ResizeObserver dirty through `_dom`; this
+ // MutationObserver is only needed for intersection geometry. Scheduling RO
+ // again here would escape its depth-bounded delivery cycle and allow a
+ // self-resizing callback to create an infinite chain of zero-delay tasks.
+ const observer = new MutationObserver(_scheduleIntersectionRenderCheckpoint);
try {
- observer.observe(globalThis.document.body, {
+ observer.observe(globalThis.document, {
childList: true,
subtree: true,
attributes: true,
@@ -6939,7 +7271,7 @@ globalThis.IntersectionObserver = class IntersectionObserver {
});
} catch {}
};
- if (globalThis.document?.body) wireUp();
+ if (globalThis.document) wireUp();
else Promise.resolve().then(wireUp);
})();
globalThis.IntersectionObserverEntry = class IntersectionObserverEntry {};
@@ -11491,6 +11823,7 @@ if (typeof FontFace === 'undefined') {
unicodeRange: face.unicodeRange
});
Deno.core.ops.op_set_dynamic_fonts(JSON.stringify(registrations.filter(face => !face.skip)));
+ _scheduleResizeRenderCheckpoint();
}
_faceChanged(face) {
this._syncNative();
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index 9bdbe129..d74b7309 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -3271,6 +3271,20 @@ fn op_image_metadata(state: &OpState, nid: u32, cached_only: bool) -> String {
}
let base_url = document_base_url(&gs);
let viewport = gs.viewport;
+ let previous_dimensions = (!cached_only)
+ .then(|| {
+ gs.dom.as_ref().and_then(|dom| {
+ gs.render_resources
+ .cached_image_element_metadata(
+ dom,
+ node_id,
+ viewport,
+ base_url.as_deref(),
+ )
+ .and_then(|(_, _, known, dimensions)| known.then_some(dimensions).flatten())
+ })
+ })
+ .flatten();
let ObscuraState {
dom,
render_resources,
@@ -3317,6 +3331,15 @@ fn op_image_metadata(state: &OpState, nid: u32, cached_only: bool) -> String {
})
.to_string();
}
+ // The page transport invalidates prepared layout when it seeds successful
+ // bytes. Cover the synchronous compatibility-loader path too, but only
+ // when this call actually discovers a new decodable intrinsic size. A
+ // cache hit or retained decode failure cannot change used geometry.
+ if !cached_only && dimensions.is_some() && dimensions != previous_dimensions {
+ gs.prepared_render = None;
+ gs.pending_style_mutations.clear();
+ gs.resolved_scroll = None;
+ }
match dimensions {
Some((width, height)) => serde_json::json!({
"state": "loaded",
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index bb3bd0f3..0a8ce2d7 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -412,6 +412,9 @@ impl ObscuraJsRuntime {
}}\
if(typeof globalThis.__obscura_recompute_intersections==='function'){{\
globalThis.__obscura_recompute_intersections();\
+ }}\
+ if(typeof globalThis.__obscura_recompute_resizes==='function'){{\
+ globalThis.__obscura_recompute_resizes();\
}}",
),
);
@@ -6444,6 +6447,310 @@ mod tests {
assert_eq!(values[2], values[3]);
}
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn resize_observer_reports_real_boxes_only_when_selected_size_changes() {
+ let dom = parse_html(
+ r#"<html style="margin:0"><body style="margin:0">
+ <div id="target" style="box-sizing:border-box;width:120px;height:80px;
+ padding:5px 7px;border:2px solid black"></div>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(300.0, 200.0);
+ rt.run_page_init();
+ rt.execute_script(
+ "resize-observer-boxes",
+ r#"
+ globalThis.__resizeRecords = [];
+ globalThis.__resizeObserver = new ResizeObserver(entries => {
+ __resizeRecords.push(...entries.map(entry => ({
+ interfaces: [
+ entry instanceof ResizeObserverEntry,
+ entry.contentBoxSize[0] instanceof ResizeObserverSize,
+ entry.borderBoxSize[0] instanceof ResizeObserverSize,
+ entry.devicePixelContentBoxSize[0] instanceof ResizeObserverSize,
+ ],
+ contentRect: [
+ entry.contentRect.x, entry.contentRect.y,
+ entry.contentRect.width, entry.contentRect.height,
+ ],
+ content: [
+ entry.contentBoxSize[0].inlineSize,
+ entry.contentBoxSize[0].blockSize,
+ ],
+ border: [
+ entry.borderBoxSize[0].inlineSize,
+ entry.borderBoxSize[0].blockSize,
+ ],
+ device: [
+ entry.devicePixelContentBoxSize[0].inlineSize,
+ entry.devicePixelContentBoxSize[0].blockSize,
+ ],
+ })));
+ });
+ __resizeObserver.observe(document.getElementById("target"));
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("__resizeRecords").unwrap(),
+ serde_json::json!([{
+ "interfaces": [true, true, true, true],
+ "contentRect": [7, 5, 102, 66],
+ "content": [102, 66],
+ "border": [120, 80],
+ "device": [102, 66],
+ }])
+ );
+
+ // A style mutation still causes a rendering checkpoint, but unchanged
+ // observed geometry must not produce a speculative notification.
+ rt.evaluate(r#"document.getElementById("target").style.color = "red""#)
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("__resizeRecords.length").unwrap().as_f64(),
+ Some(1.0)
+ );
+
+ rt.evaluate(r#"document.getElementById("target").style.width = "140px""#)
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate(
+ "__resizeRecords.map(record => [record.content[0], record.border[0]])"
+ )
+ .unwrap(),
+ serde_json::json!([[102, 120], [122, 140]])
+ );
+ assert_eq!(
+ rt.evaluate("__obscura_nextPendingTimeoutDelay()")
+ .unwrap()
+ .as_f64(),
+ Some(-1.0)
+ );
+ }
+
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn resize_observer_selected_box_and_viewport_lifecycle_match_chromium() {
+ let dom = parse_html(
+ r#"<html style="margin:0"><body style="margin:0">
+ <div id="target" style="box-sizing:border-box;width:50vw;height:40px;
+ padding:4px;border:2px solid"></div>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(200.0, 100.0);
+ rt.run_page_init();
+ rt.execute_script(
+ "resize-observer-selected-box",
+ r#"
+ globalThis.__contentWidths = [];
+ globalThis.__borderWidths = [];
+ const target = document.getElementById("target");
+ globalThis.__contentObserver = new ResizeObserver(entries => {
+ __contentWidths.push(entries[0].contentBoxSize[0].inlineSize);
+ });
+ globalThis.__borderObserver = new ResizeObserver(entries => {
+ __borderWidths.push(entries[0].borderBoxSize[0].inlineSize);
+ });
+ __contentObserver.observe(target, { box: "content-box" });
+ __borderObserver.observe(target, { box: "border-box" });
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("[__contentWidths, __borderWidths]").unwrap(),
+ serde_json::json!([[88], [100]])
+ );
+
+ // A viewport update is a rendering update even without a DOM mutation.
+ rt.set_viewport(300.0, 100.0);
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("[__contentWidths, __borderWidths]").unwrap(),
+ serde_json::json!([[88, 138], [100, 150]])
+ );
+
+ // With border-box sizing a thicker border shrinks the content box but
+ // leaves the selected border box unchanged.
+ rt.evaluate(r#"document.getElementById("target").style.borderWidth = "4px""#)
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("[__contentWidths, __borderWidths]").unwrap(),
+ serde_json::json!([[88, 138, 134], [100, 150]])
+ );
+ }
+
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn scrolling_does_not_remeasure_resize_observer_targets() {
+ let dom = parse_html(
+ r#"<html style="margin:0"><body style="margin:0;height:1000px">
+ <div id="probe" style="width:40px;height:20px"></div>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(200.0, 100.0);
+ rt.run_page_init();
+ rt.execute_script(
+ "observe-before-scroll",
+ r#"
+ globalThis.__scrollResizeRecords = 0;
+ globalThis.__scrollResizeObserver = new ResizeObserver(entries => {
+ __scrollResizeRecords += entries.length;
+ });
+ __scrollResizeObserver.observe(document.getElementById("probe"));
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("__scrollResizeRecords").unwrap().as_f64(),
+ Some(1.0)
+ );
+
+ rt.execute_script(
+ "count-scroll-geometry-reads",
+ r#"
+ globalThis.__scrollGeometryReads = 0;
+ globalThis.__nativeLayoutGeometry = Deno.core.ops.op_layout_geometry;
+ Deno.core.ops.op_layout_geometry = (...args) => {
+ __scrollGeometryReads++;
+ return __nativeLayoutGeometry(...args);
+ };
+ window.scrollTo(0, 50);
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ let result = rt
+ .evaluate("[scrollY, __scrollGeometryReads, __scrollResizeRecords]")
+ .unwrap();
+ rt.execute_script(
+ "restore-layout-geometry-op",
+ "Deno.core.ops.op_layout_geometry = __nativeLayoutGeometry;",
+ )
+ .unwrap();
+ assert_eq!(result, serde_json::json!([50, 0, 1]));
+ }
+
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn resize_observer_disconnect_is_reusable_and_inline_boxes_are_empty() {
+ let dom = parse_html(
+ r#"<html><body><div id="first" style="width:40px;height:20px"></div>
+ <span id="inline" style="padding:8px;border:2px solid">text</span>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(200.0, 100.0);
+ rt.run_page_init();
+ let result = rt
+ .evaluate_for_cdp(
+ r#"
+ new Promise(resolve => {
+ const deliveries = [];
+ const observer = new ResizeObserver(entries => {
+ deliveries.push(entries.map(entry => [
+ entry.target.id,
+ entry.contentRect.width,
+ entry.borderBoxSize[0].inlineSize,
+ ]));
+ if (deliveries.length === 1) {
+ observer.disconnect();
+ observer.observe(document.getElementById("inline"));
+ } else {
+ observer.disconnect();
+ resolve([deliveries, __resizeObservers.length]);
+ }
+ });
+ observer.observe(document.getElementById("first"));
+ setTimeout(() => resolve(["timed out"]), 100);
+ })
+ "#,
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!([[[["first", 40, 40]], [["inline", 0, 0]]], 0])
+ );
+
+ assert_eq!(
+ rt.evaluate(
+ r#"[
+ (() => { try { new ResizeObserver(null); } catch (e) { return e.name; } })(),
+ (() => { try { new ResizeObserver(() => {}).observe(document); } catch (e) { return e.name; } })(),
+ (() => { try { new ResizeObserver(() => {}).observe(document.body, {box:"margin-box"}); } catch (e) { return e.name; } })(),
+ ]"#,
+ )
+ .unwrap(),
+ serde_json::json!(["TypeError", "TypeError", "TypeError"])
+ );
+ }
+
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn resize_observer_self_resize_is_depth_bounded_without_timer_spin() {
+ let dom = parse_html(
+ r#"<html><body><div id="target" style="width:40px;height:20px"></div>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(200.0, 100.0);
+ rt.run_page_init();
+ rt.execute_script(
+ "resize-observer-loop-limit",
+ r#"
+ globalThis.__resizeCallbacks = 0;
+ globalThis.__resizeLoopErrors = 0;
+ addEventListener("error", event => {
+ if (event.message === "ResizeObserver loop completed with undelivered notifications.") {
+ __resizeLoopErrors++;
+ }
+ });
+ const target = document.getElementById("target");
+ globalThis.__loopingResizeObserver = new ResizeObserver(() => {
+ __resizeCallbacks++;
+ target.style.width = (40 + __resizeCallbacks) + "px";
+ });
+ __loopingResizeObserver.observe(target);
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate(
+ "[__resizeCallbacks, __resizeLoopErrors, __obscura_nextPendingTimeoutDelay()]"
+ )
+ .unwrap(),
+ serde_json::json!([1, 1, -1])
+ );
+
+ // A later external rendering change starts a fresh bounded cycle; the
+ // suppressed same-depth observation did not poison future delivery.
+ rt.evaluate(r#"document.getElementById("target").style.width = "60px""#)
+ .unwrap();
+ rt.run_event_loop_bounded(40).await.unwrap();
+ assert_eq!(
+ rt.evaluate("[__resizeCallbacks, __resizeLoopErrors]").unwrap(),
+ serde_json::json!([2, 2])
+ );
+ }
+
#[cfg(feature = "render")]
#[tokio::test(flavor = "current_thread")]
async fn intersection_observer_tracks_viewport_threshold_crossings() {
@@ -6492,6 +6799,134 @@ mod tests {
);
}
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn intersection_observer_element_root_uses_live_padding_box_and_scroll() {
+ let dom = parse_html(
+ r#"<html style="margin:0"><body style="margin:0">
+ <div id="root" style="position:absolute;left:10px;top:20px;width:100px;
+ height:80px;padding:10px;border:5px solid;overflow:auto">
+ <div style="height:100px"></div>
+ <div id="target" style="height:20px"></div>
+ </div>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(300.0, 200.0);
+ rt.run_page_init();
+
+ let result = rt
+ .evaluate_for_cdp(
+ r#"
+ new Promise(resolve => {
+ const records = [];
+ const root = document.getElementById("root");
+ const observer = new IntersectionObserver(entries => {
+ records.push(...entries.map(entry => ({
+ intersecting: entry.isIntersecting,
+ ratio: entry.intersectionRatio,
+ root: [
+ entry.rootBounds.x, entry.rootBounds.y,
+ entry.rootBounds.width, entry.rootBounds.height,
+ ],
+ intersection: [
+ entry.intersectionRect.x, entry.intersectionRect.y,
+ entry.intersectionRect.width, entry.intersectionRect.height,
+ ],
+ })));
+ }, { root, threshold: [0, 1] });
+ observer.observe(document.getElementById("target"));
+ setTimeout(() => { root.scrollTop = 999; }, 5);
+ setTimeout(() => resolve(records), 30);
+ })
+ "#,
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!([
+ {
+ "intersecting": false,
+ "ratio": 0,
+ "root": [15, 25, 120, 100],
+ "intersection": [0, 0, 0, 0],
+ },
+ {
+ "intersecting": true,
+ "ratio": 1,
+ "root": [15, 25, 120, 100],
+ "intersection": [25, 95, 100, 20],
+ },
+ ])
+ );
+ }
+
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn intersection_observer_clips_through_intermediate_overflow_ancestors() {
+ let dom = parse_html(
+ r#"<html style="margin:0"><body style="margin:0">
+ <div id="root" style="position:absolute;left:10px;top:20px;
+ width:300px;height:300px;overflow:visible">
+ <div id="clip" style="width:100px;height:100px;overflow:hidden">
+ <div style="height:150px"></div>
+ <div id="target" style="height:20px"></div>
+ </div>
+ </div>
+ </body></html>"#,
+ );
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(dom);
+ rt.set_viewport(400.0, 400.0);
+ rt.run_page_init();
+
+ let result = rt
+ .evaluate_for_cdp(
+ r#"
+ new Promise(resolve => {
+ const records = [];
+ const clip = document.getElementById("clip");
+ const observer = new IntersectionObserver(entries => {
+ records.push(...entries.map(entry => [
+ entry.isIntersecting,
+ entry.intersectionRatio,
+ [
+ entry.intersectionRect.x,
+ entry.intersectionRect.y,
+ entry.intersectionRect.width,
+ entry.intersectionRect.height,
+ ],
+ ]));
+ }, {
+ root: document.getElementById("root"),
+ threshold: [0, 1],
+ });
+ observer.observe(document.getElementById("target"));
+ setTimeout(() => { clip.scrollTop = 999; }, 5);
+ setTimeout(() => resolve(records), 30);
+ })
+ "#,
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+ // Chromium reports the initial target as non-intersecting: although it
+ // lies inside the explicit root, the intermediate overflow container
+ // clips it. Programmatic scrolling then reveals the complete box.
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!([
+ [false, 0, [0, 0, 0, 0]],
+ [true, 1, [10, 100, 100, 20]],
+ ])
+ );
+ }
+
#[tokio::test(flavor = "current_thread")]
async fn intersection_observer_initial_geometry_waits_for_one_render_checkpoint() {
let mut rt = setup_runtime(
@@ -6571,16 +7006,17 @@ mod tests {
);
marginObserver.observe(document.getElementById("margin-target"));
zeroObserver.observe(document.getElementById("zero"));
- let unsupported = "";
+ let elementRoot = false;
try {
- new IntersectionObserver(() => {}, {
+ const rooted = new IntersectionObserver(() => {}, {
root: document.getElementById("root")
});
+ elementRoot = rooted.root === document.getElementById("root");
} catch (error) {
- unsupported = error.name;
+ elementRoot = error.name;
}
setTimeout(() => resolve([
- marginRecords, zeroRecords, unsupported,
+ marginRecords, zeroRecords, elementRoot,
marginObserver.rootMargin, marginObserver.thresholds,
]), 200);
})
@@ -6595,7 +7031,7 @@ mod tests {
serde_json::json!([
[[true, 1, 120]],
[[true, 1]],
- "NotSupportedError",
+ true,
"0px 0px 20px 0px",
[0, 1],
])
@@ -7776,6 +8212,177 @@ mod tests {
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
}
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn parser_image_fallback_invalidates_only_new_intrinsic_geometry() {
+ let calls = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let loader_calls = calls.clone();
+ let png = two_by_three_png();
+ let mut rt = parser_image_runtime(
+ r#"<img id="late" src="late.png">"#,
+ move |_url: &str| {
+ loader_calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ Some(png.clone())
+ },
+ );
+ {
+ let mut state = rt.state.borrow_mut();
+ let previous = state.render_resources.set_sync_loading_enabled(false);
+ assert!(ensure_prepared_render(&mut state).is_some());
+ state
+ .render_resources
+ .set_sync_loading_enabled(previous);
+ assert!(state.prepared_render.is_some());
+ }
+ assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
+
+ rt.execute_script(
+ "load-image-after-layout",
+ r#"
+ globalThis.__lateEvents = [];
+ const late = document.getElementById("late");
+ late.addEventListener("load", () => __lateEvents.push("load"));
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(
+ rt.evaluate(
+ "[late.complete, late.naturalWidth, late.naturalHeight, __lateEvents]"
+ )
+ .unwrap(),
+ serde_json::json!([true, 2, 3, ["load"]])
+ );
+ assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
+ assert!(rt.state.borrow().prepared_render.is_none());
+
+ // Once the successful dimensions are retained, another loading-form
+ // metadata probe is only a cache hit and must preserve fresh layout.
+ {
+ let mut state = rt.state.borrow_mut();
+ assert!(ensure_prepared_render(&mut state).is_some());
+ assert!(state.prepared_render.is_some());
+ }
+ rt.execute_script(
+ "reload-retained-image",
+ r#"late.src = "late.png";"#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
+ assert!(rt.state.borrow().prepared_render.is_some());
+
+ let missing_calls = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let missing_loader_calls = missing_calls.clone();
+ let mut missing = parser_image_runtime(
+ r#"<img id="missing" src="missing.png">"#,
+ move |_url: &str| {
+ missing_loader_calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ None
+ },
+ );
+ {
+ let mut state = missing.state.borrow_mut();
+ let previous = state.render_resources.set_sync_loading_enabled(false);
+ assert!(ensure_prepared_render(&mut state).is_some());
+ state
+ .render_resources
+ .set_sync_loading_enabled(previous);
+ assert!(state.prepared_render.is_some());
+ }
+ missing
+ .execute_script(
+ "fail-image-after-layout",
+ r#"
+ globalThis.__missingEvents = [];
+ const missing = document.getElementById("missing");
+ missing.addEventListener("error", () => __missingEvents.push("error"));
+ "#,
+ )
+ .unwrap();
+ missing.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(
+ missing
+ .evaluate(
+ "[missing.complete, missing.naturalWidth, missing.naturalHeight, __missingEvents]"
+ )
+ .unwrap(),
+ serde_json::json!([true, 0, 0, ["error"]])
+ );
+ assert_eq!(
+ missing_calls.load(std::sync::atomic::Ordering::SeqCst),
+ 1
+ );
+ assert!(missing.state.borrow().prepared_render.is_some());
+ }
+
+ #[cfg(feature = "render")]
+ #[tokio::test(flavor = "current_thread")]
+ async fn stable_cached_image_getters_do_not_queue_resize_geometry_work() {
+ let png = two_by_three_png();
+ let mut rt = parser_image_runtime(
+ r#"<img id="cached" src="cached.png">
+ <div id="probe" style="width:40px;height:20px"></div>"#,
+ move |_url: &str| Some(png.clone()),
+ );
+ rt.execute_script(
+ "settle-cached-image",
+ r#"
+ const cached = document.getElementById("cached");
+ void cached.complete;
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(
+ rt.evaluate("[cached.complete, cached.naturalWidth, cached.naturalHeight]")
+ .unwrap(),
+ serde_json::json!([true, 2, 3])
+ );
+
+ rt.execute_script(
+ "observe-unrelated-geometry",
+ r#"
+ globalThis.__stableGetterResizeRecords = 0;
+ globalThis.__stableGetterObserver = new ResizeObserver(entries => {
+ __stableGetterResizeRecords += entries.length;
+ });
+ __stableGetterObserver.observe(document.getElementById("probe"));
+ "#,
+ )
+ .unwrap();
+ rt.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(
+ rt.evaluate(
+ "[__stableGetterResizeRecords, __obscura_nextPendingTimeoutDelay()]"
+ )
+ .unwrap(),
+ serde_json::json!([1, -1])
+ );
+
+ rt.execute_script(
+ "read-stable-image-cache",
+ r#"
+ for (let i = 0; i < 50; i++) {
+ void cached.complete;
+ void cached.currentSrc;
+ void cached.naturalWidth;
+ void cached.naturalHeight;
+ }
+ "#,
+ )
+ .unwrap();
+ // Cached lifecycle reads do not change intrinsic dimensions, so they
+ // must not enqueue a rendering checkpoint (and its geometry walk).
+ assert_eq!(
+ rt.evaluate(
+ "[__stableGetterResizeRecords, __obscura_nextPendingTimeoutDelay()]"
+ )
+ .unwrap(),
+ serde_json::json!([1, -1])
+ );
+ }
+
#[cfg(feature = "render")]
#[tokio::test(flavor = "current_thread")]
async fn parser_image_source_replacement_cancels_queued_completion() {
diff --git a/crates/obscura-render/src/css.rs b/crates/obscura-render/src/css.rs
index 51e19a86..ce176d99 100644
--- a/crates/obscura-render/src/css.rs
+++ b/crates/obscura-render/src/css.rs
@@ -79,36 +79,59 @@ pub(crate) struct RelationalInvalidation {
pub unrepresentable_outer_path: bool,
}
-impl RelationalInvalidation {
- pub(crate) fn anchor_may_match(&self, tree: &DomTree, node: NodeId) -> bool {
- let Some(dom_node) = tree.get_node(node) else {
- return false;
- };
- if dom_node.as_element().is_none() {
- return false;
- }
- let quirks = tree.is_quirks();
- match self.anchor_key.as_ref() {
- None => true,
- Some(RelationalSelectorKey::Id(expected)) => dom_node
- .get_attribute("id")
- .is_some_and(|actual| {
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) struct StructuralInvalidation {
+ pub rule_order: usize,
+ pub state: String,
+ subject_key: Option<RelationalSelectorKey>,
+ pub reaches: InvalidationReaches,
+ pub inside_relational: bool,
+}
+
+impl StructuralInvalidation {
+ pub(crate) fn subject_may_match(&self, tree: &DomTree, node: NodeId) -> bool {
+ relational_key_may_match(self.subject_key.as_ref(), tree, node)
+ }
+}
+
+fn relational_key_may_match(
+ key: Option<&RelationalSelectorKey>,
+ tree: &DomTree,
+ node: NodeId,
+) -> bool {
+ let Some(dom_node) = tree.get_node(node) else {
+ return false;
+ };
+ if dom_node.as_element().is_none() {
+ return false;
+ }
+ let quirks = tree.is_quirks();
+ match key {
+ None => true,
+ Some(RelationalSelectorKey::Id(expected)) => dom_node
+ .get_attribute("id")
+ .is_some_and(|actual| {
+ actual == expected || (quirks && actual.eq_ignore_ascii_case(expected))
+ }),
+ Some(RelationalSelectorKey::Class(expected)) => dom_node
+ .get_attribute("class")
+ .is_some_and(|classes| {
+ classes.split_whitespace().any(|actual| {
actual == expected || (quirks && actual.eq_ignore_ascii_case(expected))
- }),
- Some(RelationalSelectorKey::Class(expected)) => dom_node
- .get_attribute("class")
- .is_some_and(|classes| {
- classes.split_whitespace().any(|actual| {
- actual == expected || (quirks && actual.eq_ignore_ascii_case(expected))
- })
- }),
- Some(RelationalSelectorKey::Attribute(expected)) => {
- dom_node.get_attribute(expected).is_some()
- }
- Some(RelationalSelectorKey::LocalName(expected)) => dom_node
- .as_element()
- .is_some_and(|element| element.local.as_ref().eq_ignore_ascii_case(expected)),
+ })
+ }),
+ Some(RelationalSelectorKey::Attribute(expected)) => {
+ dom_node.get_attribute(expected).is_some()
}
+ Some(RelationalSelectorKey::LocalName(expected)) => dom_node
+ .as_element()
+ .is_some_and(|element| element.local.as_ref().eq_ignore_ascii_case(expected)),
+ }
+}
+
+impl RelationalInvalidation {
+ pub(crate) fn anchor_may_match(&self, tree: &DomTree, node: NodeId) -> bool {
+ relational_key_may_match(self.anchor_key.as_ref(), tree, node)
}
pub(crate) fn relative_path_may_match(&self, tree: &DomTree, node: NodeId) -> bool {
@@ -161,6 +184,10 @@ pub struct InvalidationMap {
relational_rule_orders: Vec<usize>,
unkeyed_relational_rule_orders: Vec<usize>,
relational_invalidations: Vec<RelationalInvalidation>,
+ structural_invalidations: Vec<StructuralInvalidation>,
+ adjacent_sibling_selectors: bool,
+ general_sibling_selectors: bool,
+ unkeyed_sibling_selectors: bool,
}
impl InvalidationMap {
@@ -213,6 +240,68 @@ impl InvalidationMap {
&self.relational_invalidations
}
+ pub(crate) fn structural_invalidations<'a>(
+ &'a self,
+ state: &str,
+ ) -> Vec<&'a StructuralInvalidation> {
+ self.structural_invalidations
+ .iter()
+ .filter(move |invalidation| invalidation.state == state)
+ .collect()
+ }
+
+ pub(crate) fn has_adjacent_sibling_selectors(&self) -> bool {
+ self.adjacent_sibling_selectors
+ }
+
+ pub(crate) fn has_general_sibling_selectors(&self) -> bool {
+ self.general_sibling_selectors
+ }
+
+ pub(crate) fn node_may_start_sibling_selector(
+ &self,
+ tree: &DomTree,
+ node: NodeId,
+ ) -> bool {
+ if self.unkeyed_sibling_selectors
+ || (tree.is_quirks()
+ && (self.adjacent_sibling_selectors || self.general_sibling_selectors))
+ {
+ return true;
+ }
+ let Some(node) = tree.get_node(node) else {
+ return false;
+ };
+ let reaches_sibling = |dependencies: &[InvalidationDependency]| {
+ dependencies.iter().any(|dependency| {
+ dependency.reaches.contains(InvalidationReaches::SIBLINGS)
+ })
+ };
+ if node
+ .get_attribute("id")
+ .is_some_and(|id| reaches_sibling(self.id_dependencies(id)))
+ {
+ return true;
+ }
+ if node.get_attribute("class").is_some_and(|classes| {
+ classes
+ .split_whitespace()
+ .any(|class| reaches_sibling(self.class_dependencies(class)))
+ }) {
+ return true;
+ }
+ if node.attrs().is_some_and(|attributes| {
+ attributes.iter().any(|attribute| {
+ reaches_sibling(self.attribute_dependencies(attribute.name.local.as_ref()))
+ })
+ }) {
+ return true;
+ }
+ node.as_element().is_some_and(|element| {
+ reaches_sibling(self.local_name_dependencies(element.local.as_ref()))
+ })
+ }
+
pub fn dependency_count(&self) -> usize {
self.ids
.values()
@@ -301,6 +390,12 @@ impl InvalidationMap {
self.relational_invalidations.push(invalidation);
}
}
+
+ fn push_structural_invalidation(&mut self, invalidation: StructuralInvalidation) {
+ if !self.structural_invalidations.contains(&invalidation) {
+ self.structural_invalidations.push(invalidation);
+ }
+ }
}
fn compose_invalidation_reach(
@@ -929,11 +1024,54 @@ fn selector_contains_adjacent_combinator(selector: &str) -> bool {
false
}
+/// Sibling combinators which participate in the current selector path.
+///
+/// Parentheses are deliberately skipped here. `:is()`/`:where()`/`:not()`
+/// alternatives are fed back through dependency collection separately, while
+/// `:has()` owns an upward invalidation path and must not poison ordinary
+/// sibling invalidation outside its anchor.
+fn selector_sibling_combinators(selector: &str) -> (bool, bool) {
+ let chars = selector.chars().collect::<Vec<_>>();
+ let mut index = 0usize;
+ let mut quote = None;
+ let mut bracket_depth = 0usize;
+ let mut paren_depth = 0usize;
+ let mut adjacent = false;
+ let mut general = false;
+ while index < chars.len() {
+ let ch = chars[index];
+ if ch == '\\' {
+ index = (index + 2).min(chars.len());
+ continue;
+ }
+ if let Some(active_quote) = quote {
+ if ch == active_quote {
+ quote = None;
+ }
+ index += 1;
+ continue;
+ }
+ match ch {
+ '\'' | '"' => quote = Some(ch),
+ '[' => bracket_depth += 1,
+ ']' => bracket_depth = bracket_depth.saturating_sub(1),
+ '(' if bracket_depth == 0 => paren_depth += 1,
+ ')' if bracket_depth == 0 => paren_depth = paren_depth.saturating_sub(1),
+ '+' if bracket_depth == 0 && paren_depth == 0 => adjacent = true,
+ '~' if bracket_depth == 0 && paren_depth == 0 => general = true,
+ _ => {}
+ }
+ index += 1;
+ }
+ (adjacent, general)
+}
+
fn note_compound_dependencies(
map: &mut InvalidationMap,
compound: &str,
reaches: InvalidationReaches,
rule_order: usize,
+ inside_relational: bool,
) {
if let Some(local_name) = compound_local_name(compound) {
map.push_local_name(local_name, rule_order, reaches);
@@ -1000,6 +1138,24 @@ fn note_compound_dependencies(
}
if chars.get(next) != Some(&'(') {
map.push_state(name.clone(), rule_order, reaches);
+ if matches!(
+ name.as_str(),
+ "empty"
+ | "first-child"
+ | "last-child"
+ | "only-child"
+ | "first-of-type"
+ | "last-of-type"
+ | "only-of-type"
+ ) {
+ map.push_structural_invalidation(StructuralInvalidation {
+ rule_order,
+ state: name.clone(),
+ subject_key: relational_anchor_key(compound),
+ reaches,
+ inside_relational,
+ });
+ }
if matches!(
name.as_str(),
"root"
@@ -1037,6 +1193,7 @@ fn note_compound_dependencies(
alternative.trim(),
reaches,
rule_order,
+ !inside_relational,
);
}
}
@@ -1105,11 +1262,19 @@ fn note_compound_dependencies(
alternative.trim(),
InvalidationReaches::CONSERVATIVE,
rule_order,
+ false,
);
}
}
"nth-child" | "nth-last-child" | "nth-of-type" | "nth-last-of-type" => {
- map.push_state(name, rule_order, reaches);
+ map.push_state(name.clone(), rule_order, reaches);
+ map.push_structural_invalidation(StructuralInvalidation {
+ rule_order,
+ state: name,
+ subject_key: relational_anchor_key(compound),
+ reaches,
+ inside_relational,
+ });
// Structural index changes and `of <complex-selector>`
// need sibling-wide bookkeeping not present in phase 1.
map.mark_conservative(rule_order);
@@ -1120,6 +1285,7 @@ fn note_compound_dependencies(
alternative.trim(),
InvalidationReaches::CONSERVATIVE,
rule_order,
+ !inside_relational,
);
}
}
@@ -1144,17 +1310,35 @@ fn note_selector_dependencies(
selector: &str,
outer_reaches: InvalidationReaches,
rule_order: usize,
+ record_tree_siblings: bool,
) {
+ if record_tree_siblings {
+ let (adjacent, general) = selector_sibling_combinators(selector);
+ map.adjacent_sibling_selectors |= adjacent;
+ map.general_sibling_selectors |= general;
+ }
let (compounds, malformed) = invalidation_compounds(selector);
if malformed {
map.mark_conservative(rule_order);
}
for (compound, local_reaches) in compounds {
+ if record_tree_siblings
+ && local_reaches.contains(InvalidationReaches::SIBLINGS)
+ && !relative_selector_subject_has_key(&compound)
+ {
+ map.unkeyed_sibling_selectors = true;
+ }
if local_reaches.contains(InvalidationReaches::CONSERVATIVE) {
map.mark_conservative(rule_order);
}
let reaches = compose_invalidation_reach(map, local_reaches, outer_reaches, rule_order);
- note_compound_dependencies(map, &compound, reaches, rule_order);
+ note_compound_dependencies(
+ map,
+ &compound,
+ reaches,
+ rule_order,
+ !record_tree_siblings,
+ );
}
}
@@ -1163,7 +1347,13 @@ fn note_selector_for_invalidation(
selector: &str,
rule_order: usize,
) {
- note_selector_dependencies(map, selector, InvalidationReaches::SELF, rule_order);
+ note_selector_dependencies(
+ map,
+ selector,
+ InvalidationReaches::SELF,
+ rule_order,
+ true,
+ );
}
/// Record element attributes read from declaration values.
@@ -6041,12 +6231,27 @@ fn extract_length(s: &str, prop: &str, viewport: (f32, f32), axis: LengthAxis) -
let start = s.find(prop)? + prop.len();
let rest = &s[start..];
if let Some(inner) = rest.strip_prefix("calc(") {
- let end = inner.find(')')?;
+ let end = matching_paren_end(inner)?;
return eval_length_sum(&inner[..end], viewport, axis);
}
parse_length_prefix(rest, viewport, axis)
}
+/// Find the closing parenthesis paired with an opening parenthesis immediately
+/// before `input`. Nested CSS math groups must not terminate the outer calc.
+fn matching_paren_end(input: &str) -> Option<usize> {
+ let mut depth = 0usize;
+ for (index, character) in input.char_indices() {
+ match character {
+ '(' => depth += 1,
+ ')' if depth == 0 => return Some(index),
+ ')' => depth -= 1,
+ _ => {}
+ }
+ }
+ None
+}
+
/// Read the length immediately before a range marker (`64rem<=width`).
fn extract_length_before(
s: &str,
@@ -6056,12 +6261,23 @@ fn extract_length_before(
) -> Option<f32> {
let end = s.find(marker)?;
let prefix = &s[..end];
- let start = prefix
- .rfind(|c: char| matches!(c, '(' | ')' | ':' | ','))
- .map_or(0, |idx| idx + 1);
- let value = &prefix[start..];
+ let mut depth = 0usize;
+ let mut start = 0usize;
+ for (index, character) in prefix.char_indices().rev() {
+ match character {
+ ')' => depth += 1,
+ '(' if depth > 0 => depth -= 1,
+ '(' | ':' | ',' if depth == 0 => {
+ start = index + character.len_utf8();
+ break;
+ }
+ _ => {}
+ }
+ }
+ let value = prefix[start..].trim();
if let Some(inner) = value.strip_prefix("calc(") {
- return eval_length_sum(inner, viewport, axis);
+ let end = matching_paren_end(inner)?;
+ return eval_length_sum(&inner[..end], viewport, axis);
}
parse_length_prefix(value, viewport, axis)
}
@@ -6102,32 +6318,23 @@ fn parse_length_prefix(input: &str, viewport: (f32, f32), axis: LengthAxis) -> O
px.is_finite().then_some(px)
}
-/// Sum the common media-query `calc()` form (`64rem - 1px`) left to right.
+/// Resolve media-query `calc()` lengths with the same typed CSS math used by
+/// layout. Real responsive breakpoints combine grouping and scalar arithmetic
+/// (for example `calc(1rem * 2 + (15rem + 2rem) * 2 + 31rem)`), so a flat
+/// plus/minus scanner silently turns those queries into unconditional rules.
fn eval_length_sum(expr: &str, viewport: (f32, f32), axis: LengthAxis) -> Option<f32> {
- let mut total = 0.0;
- let mut sign = 1.0;
- let mut term = String::new();
- let flush = |term: &mut String, sign: f32, total: &mut f32| -> Option<()> {
- let value = parse_length_prefix(term, viewport, axis)?;
- *total += sign * value;
- term.clear();
- Some(())
+ let percent_base = match axis {
+ LengthAxis::Width => viewport.0,
+ LengthAxis::Height => viewport.1,
};
- for (idx, c) in expr.char_indices() {
- match c {
- '+' if idx > 0 => {
- flush(&mut term, sign, &mut total)?;
- sign = 1.0;
- }
- '-' if idx > 0 => {
- flush(&mut term, sign, &mut total)?;
- sign = -1.0;
- }
- _ => term.push(c),
- }
- }
- flush(&mut term, sign, &mut total)?;
- Some(total)
+ crate::style::resolve_contextual_length(
+ &format!("calc({expr})"),
+ 16.0,
+ 16.0,
+ viewport.0 / 100.0,
+ viewport.1 / 100.0,
+ percent_base,
+ )
}
fn split_media_query_list(query: &str) -> Vec<&str> {
@@ -6214,6 +6421,25 @@ mod tests {
assert!(!map.requires_conservative_invalidation());
}
+ #[test]
+ fn invalidation_map_distinguishes_tree_sibling_paths_from_has_paths() {
+ let map = test_invalidation_map(
+ r#"
+ .left + .right { color:red }
+ :is(.early ~ .late, .plain) { color:blue }
+ .host:has(.inside + .peer) { color:green }
+ [data-token="+"] { color:black }
+ "#,
+ );
+ assert!(map.has_adjacent_sibling_selectors());
+ assert!(map.has_general_sibling_selectors());
+
+ let relational_only =
+ test_invalidation_map(".host:has(.inside + .peer){color:red}");
+ assert!(!relational_only.has_adjacent_sibling_selectors());
+ assert!(!relational_only.has_general_sibling_selectors());
+ }
+
#[test]
fn invalidation_map_indexes_attributes_states_functions_and_escaped_keys() {
let map = test_invalidation_map(
@@ -8674,6 +8900,35 @@ mod tests {
"@media (width > calc(60em - 1px))",
(900.0, 1000.0)
));
+ let two_sidebar_breakpoint =
+ "@media (width < calc(1rem * 2 + (15rem + 2rem) * 2 + 31rem))";
+ assert!(media_query_applies_for_viewport(
+ two_sidebar_breakpoint,
+ (1000.0, 900.0)
+ ));
+ assert!(!media_query_applies_for_viewport(
+ two_sidebar_breakpoint,
+ (1440.0, 1000.0)
+ ));
+ let left_width_calc =
+ "@media (calc(1rem * 2 + (15rem + 2rem) * 2 + 31rem) <= width)";
+ assert!(!media_query_applies_for_viewport(
+ left_width_calc,
+ (1000.0, 900.0)
+ ));
+ assert!(media_query_applies_for_viewport(
+ left_width_calc,
+ (1440.0, 1000.0)
+ ));
+ let left_height_calc = "@media (calc(40rem + (2rem * 2)) < height)";
+ assert!(!media_query_applies_for_viewport(
+ left_height_calc,
+ (1280.0, 704.0)
+ ));
+ assert!(media_query_applies_for_viewport(
+ left_height_calc,
+ (1280.0, 705.0)
+ ));
}
#[test]
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index 505553c7..fa0c638f 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -2596,20 +2596,337 @@ fn add_relational_tree_invalidation(
true
}
-/// A child-list change can alter every structural/sibling match under each
-/// affected parent. Re-cascade that complete parent subtree, the parent's
-/// following siblings (for `:empty +/~ ...`), and the ancestor context chain.
-/// This deliberately does not depend on whether the current sheet happens to
-/// contain nth/sibling selectors: the broad local scope remains correct when
-/// multiple queued mutations introduce selector keys before the next flush.
-fn add_tree_parent_scope(
+fn add_style_context_chain(tree: &DomTree, node: NodeId, dirty: &mut HashSet<NodeId>) {
+ dirty.insert(node);
+ dirty.extend(tree.ancestors(node));
+}
+
+fn add_table_row_child_scope(tree: &DomTree, parent: NodeId, dirty: &mut HashSet<NodeId>) {
+ let is_table_row = tree.get_node(parent).is_some_and(|node| {
+ node.as_element()
+ .is_some_and(|element| element.local.as_ref() == "tr")
+ });
+ if !is_table_row {
+ return;
+ }
+ // The table fallback assigns surplus growth to the trailing auto cell
+ // after cascade. A child-list change can move that role even without an
+ // authored structural selector, so reset every surviving cell from its
+ // specified style before running the fixup again.
+ for child in element_children(tree, parent) {
+ add_style_subtree(tree, child, dirty);
+ }
+}
+
+fn element_children(tree: &DomTree, parent: NodeId) -> Vec<NodeId> {
+ tree.children(parent)
+ .into_iter()
+ .filter(|child| tree.get_node(*child).is_some_and(|node| node.is_element()))
+ .collect()
+}
+
+fn element_local_name(tree: &DomTree, node: NodeId) -> Option<String> {
+ tree.get_node(node)
+ .and_then(|node| node.as_element().map(|element| element.local.to_string()))
+}
+
+/// Re-cascade a node whose structural pseudo state may have changed. Even a
+/// self-only selector can change an inherited property, so the node's complete
+/// subtree is the smallest renderer-independent safe unit. A structural state
+/// used left of a sibling combinator additionally reaches following siblings.
+fn add_structural_candidate_scope(
+ tree: &DomTree,
+ map: &crate::css::InvalidationMap,
+ state: &str,
+ candidate: NodeId,
+ parent: NodeId,
+ dirty: &mut HashSet<NodeId>,
+) {
+ let invalidations = map
+ .structural_invalidations(state)
+ .into_iter()
+ .filter(|invalidation| {
+ !invalidation.inside_relational
+ && invalidation.subject_may_match(tree, candidate)
+ })
+ .collect::<Vec<_>>();
+ if invalidations.is_empty() {
+ return;
+ }
+ if invalidations.iter().any(|invalidation| {
+ invalidation
+ .reaches
+ .contains(crate::css::InvalidationReaches::CONSERVATIVE)
+ }) {
+ add_style_subtree(tree, parent, dirty);
+ return;
+ }
+ add_style_subtree(tree, candidate, dirty);
+ if invalidations.iter().any(|invalidation| {
+ invalidation
+ .reaches
+ .contains(crate::css::InvalidationReaches::SIBLINGS)
+ }) {
+ add_following_sibling_subtrees(tree, candidate, dirty);
+ }
+}
+
+fn add_inserted_structural_scopes(
+ tree: &DomTree,
+ map: &crate::css::InvalidationMap,
+ node: NodeId,
+ parent: NodeId,
+ mutations: &[RetainedStyleMutation],
+ dirty: &mut HashSet<NodeId>,
+) {
+ let siblings = element_children(tree, parent);
+ let Some(position) = siblings.iter().position(|candidate| *candidate == node) else {
+ return;
+ };
+ let mut add = |state: &str, candidates: &[NodeId]| {
+ for candidate in candidates.iter().copied() {
+ add_structural_candidate_scope(tree, map, state, candidate, parent, dirty);
+ }
+ };
+
+ let insertion_count = mutations
+ .iter()
+ .filter(|mutation| {
+ matches!(
+ mutation,
+ RetainedStyleMutation::Tree(TreeStyleMutation::Insert {
+ node,
+ new_parent,
+ ..
+ }) if *new_parent == parent
+ && tree.get_node(*node).is_some_and(|node| node.is_element())
+ )
+ })
+ .count();
+ if insertion_count > 1 {
+ // Mutation records do not retain the old sibling boundaries. With two
+ // fresh insertions, the old first/last/only child can sit beyond the
+ // final two boundary nodes and would otherwise keep a stale match.
+ // Direct children are still a bounded scope, and keyed structural
+ // metadata avoids cascading unrelated candidates.
+ for state in [
+ "first-child",
+ "last-child",
+ "only-child",
+ "first-of-type",
+ "last-of-type",
+ "only-of-type",
+ ] {
+ add(state, &siblings);
+ }
+ }
+
+ if position == 0 {
+ add("first-child", &siblings[..siblings.len().min(2)]);
+ }
+ if position + 1 == siblings.len() {
+ add(
+ "last-child",
+ &siblings[position.saturating_sub(1)..],
+ );
+ }
+ if siblings.len() <= 2 {
+ add("only-child", &siblings);
+ }
+ add("nth-child", &siblings[position..]);
+ add("nth-last-child", &siblings[..=position]);
+
+ let Some(local) = element_local_name(tree, node) else {
+ return;
+ };
+ let same_type = siblings
+ .iter()
+ .copied()
+ .filter(|candidate| element_local_name(tree, *candidate).as_deref() == Some(&local))
+ .collect::<Vec<_>>();
+ let Some(type_position) = same_type.iter().position(|candidate| *candidate == node) else {
+ return;
+ };
+ if type_position == 0 {
+ add("first-of-type", &same_type[..same_type.len().min(2)]);
+ }
+ if type_position + 1 == same_type.len() {
+ add(
+ "last-of-type",
+ &same_type[type_position.saturating_sub(1)..],
+ );
+ }
+ if same_type.len() <= 2 {
+ add("only-of-type", &same_type);
+ }
+ add("nth-of-type", &same_type[type_position..]);
+ add("nth-last-of-type", &same_type[..=type_position]);
+}
+
+fn add_removed_structural_scopes(
+ tree: &DomTree,
+ map: &crate::css::InvalidationMap,
+ removed: NodeId,
+ parent: NodeId,
+ dirty: &mut HashSet<NodeId>,
+) {
+ let siblings = element_children(tree, parent);
+ if let Some(first) = siblings.first().copied() {
+ add_structural_candidate_scope(tree, map, "first-child", first, parent, dirty);
+ }
+ if let Some(last) = siblings.last().copied() {
+ add_structural_candidate_scope(tree, map, "last-child", last, parent, dirty);
+ }
+ if siblings.len() <= 1 {
+ for candidate in siblings.iter().copied() {
+ add_structural_candidate_scope(tree, map, "only-child", candidate, parent, dirty);
+ }
+ }
+ for state in ["nth-child", "nth-last-child"] {
+ for candidate in siblings.iter().copied() {
+ add_structural_candidate_scope(tree, map, state, candidate, parent, dirty);
+ }
+ }
+
+ let Some(local) = element_local_name(tree, removed) else {
+ return;
+ };
+ let same_type = siblings
+ .iter()
+ .copied()
+ .filter(|candidate| element_local_name(tree, *candidate).as_deref() == Some(&local))
+ .collect::<Vec<_>>();
+ if let Some(first) = same_type.first().copied() {
+ add_structural_candidate_scope(tree, map, "first-of-type", first, parent, dirty);
+ }
+ if let Some(last) = same_type.last().copied() {
+ add_structural_candidate_scope(tree, map, "last-of-type", last, parent, dirty);
+ }
+ if same_type.len() <= 1 {
+ for candidate in same_type.iter().copied() {
+ add_structural_candidate_scope(tree, map, "only-of-type", candidate, parent, dirty);
+ }
+ }
+ for state in ["nth-of-type", "nth-last-of-type"] {
+ for candidate in same_type.iter().copied() {
+ add_structural_candidate_scope(tree, map, state, candidate, parent, dirty);
+ }
+ }
+}
+
+fn add_inserted_sibling_scopes(
+ tree: &DomTree,
+ map: &crate::css::InvalidationMap,
+ node: NodeId,
+ parent: NodeId,
+ dirty: &mut HashSet<NodeId>,
+) {
+ let siblings = element_children(tree, parent);
+ let Some(position) = siblings.iter().position(|candidate| *candidate == node) else {
+ return;
+ };
+ let may_start_sibling_selector = map.node_may_start_sibling_selector(tree, node);
+ if map.has_adjacent_sibling_selectors() {
+ if position != 0 || may_start_sibling_selector {
+ if let Some(next) = siblings.get(position + 1).copied() {
+ add_style_subtree(tree, next, dirty);
+ }
+ }
+ }
+ if map.has_general_sibling_selectors() && may_start_sibling_selector {
+ for following in siblings.iter().skip(position + 1).copied() {
+ add_style_subtree(tree, following, dirty);
+ }
+ }
+}
+
+fn add_removed_sibling_scopes(
+ tree: &DomTree,
+ map: &crate::css::InvalidationMap,
+ parent: NodeId,
+ dirty: &mut HashSet<NodeId>,
+) {
+ if !map.has_adjacent_sibling_selectors() && !map.has_general_sibling_selectors() {
+ return;
+ }
+ // Removal records intentionally do not retain old sibling pointers. All
+ // direct element siblings are the bounded sound recovery set; their clean
+ // descendant subtrees remain reusable when no sibling selector reaches
+ // through them.
+ for sibling in element_children(tree, parent) {
+ add_style_subtree(tree, sibling, dirty);
+ }
+}
+
+fn empty_state_may_have_changed(
+ tree: &DomTree,
+ parent: NodeId,
+ mutations: &[RetainedStyleMutation],
+) -> bool {
+ let relevant_children = tree
+ .children(parent)
+ .into_iter()
+ .filter(|child| {
+ tree.get_node(*child).is_some_and(|node| match &node.data {
+ obscura_dom::tree::NodeData::Element { .. } => true,
+ obscura_dom::tree::NodeData::Text { contents } => !contents.is_empty(),
+ _ => false,
+ })
+ })
+ .count();
+ let boundary_mutations = mutations
+ .iter()
+ .filter(|mutation| match mutation {
+ RetainedStyleMutation::Attribute(_) => false,
+ RetainedStyleMutation::Tree(TreeStyleMutation::Insert {
+ old_parent,
+ new_parent,
+ ..
+ }) => *new_parent == parent || *old_parent == Some(parent),
+ RetainedStyleMutation::Tree(TreeStyleMutation::Remove { old_parent, .. }) => {
+ *old_parent == parent
+ }
+ RetainedStyleMutation::Tree(TreeStyleMutation::Text {
+ parent: text_parent,
+ ..
+ }) => *text_parent == Some(parent),
+ })
+ .count();
+ // At least one relevant child untouched by this mutation batch proves the
+ // parent was non-empty before and after every queued boundary operation.
+ relevant_children <= boundary_mutations
+}
+
+fn add_empty_parent_scope(
tree: &DomTree,
+ map: &crate::css::InvalidationMap,
parent: NodeId,
- dirty_following_siblings: bool,
+ mutations: &[RetainedStyleMutation],
dirty: &mut HashSet<NodeId>,
) {
+ if !empty_state_may_have_changed(tree, parent, mutations) {
+ return;
+ }
+ let invalidations = map
+ .structural_invalidations("empty")
+ .into_iter()
+ .filter(|invalidation| {
+ !invalidation.inside_relational
+ && invalidation.subject_may_match(tree, parent)
+ })
+ .collect::<Vec<_>>();
+ if invalidations.is_empty() {
+ return;
+ }
add_style_subtree(tree, parent, dirty);
- if dirty_following_siblings {
+ if invalidations.iter().any(|invalidation| {
+ invalidation
+ .reaches
+ .contains(crate::css::InvalidationReaches::SIBLINGS)
+ || invalidation
+ .reaches
+ .contains(crate::css::InvalidationReaches::CONSERVATIVE)
+ }) {
add_following_sibling_subtrees(tree, parent, dirty);
}
}
@@ -2630,14 +2947,6 @@ fn retained_style_plan(
unreachable!()
};
let map = sheet.invalidation_map();
- let dirty_parent_siblings = map.state_dependencies("empty").iter().any(|dependency| {
- dependency
- .reaches
- .contains(crate::css::InvalidationReaches::SIBLINGS)
- || dependency
- .reaches
- .contains(crate::css::InvalidationReaches::CONSERVATIVE)
- });
match *mutation {
TreeStyleMutation::Insert {
node,
@@ -2657,9 +2966,24 @@ fn retained_style_plan(
}
add_style_subtree(tree, node, &mut dirty);
if let Some(parent) = old_parent {
- add_tree_parent_scope(tree, parent, dirty_parent_siblings, &mut dirty);
+ add_style_context_chain(tree, parent, &mut dirty);
+ add_table_row_child_scope(tree, parent, &mut dirty);
+ add_removed_structural_scopes(tree, map, node, parent, &mut dirty);
+ add_removed_sibling_scopes(tree, map, parent, &mut dirty);
+ add_empty_parent_scope(tree, map, parent, mutations, &mut dirty);
}
- add_tree_parent_scope(tree, new_parent, dirty_parent_siblings, &mut dirty);
+ add_style_context_chain(tree, new_parent, &mut dirty);
+ add_table_row_child_scope(tree, new_parent, &mut dirty);
+ add_inserted_structural_scopes(
+ tree,
+ map,
+ node,
+ new_parent,
+ mutations,
+ &mut dirty,
+ );
+ add_inserted_sibling_scopes(tree, map, node, new_parent, &mut dirty);
+ add_empty_parent_scope(tree, map, new_parent, mutations, &mut dirty);
}
TreeStyleMutation::Remove { node, old_parent } => {
if subtree_contains_style_element(tree, node)
@@ -2670,7 +2994,11 @@ fn retained_style_plan(
if !add_relational_tree_invalidation(tree, map, mutation, &mut dirty) {
return RetainedStylePlan::Full;
}
- add_tree_parent_scope(tree, old_parent, dirty_parent_siblings, &mut dirty);
+ add_style_context_chain(tree, old_parent, &mut dirty);
+ add_table_row_child_scope(tree, old_parent, &mut dirty);
+ add_removed_structural_scopes(tree, map, node, old_parent, &mut dirty);
+ add_removed_sibling_scopes(tree, map, old_parent, &mut dirty);
+ add_empty_parent_scope(tree, map, old_parent, mutations, &mut dirty);
}
TreeStyleMutation::Text { node, parent } => {
if node_is_style_text(tree, node, parent) {
@@ -2680,7 +3008,8 @@ fn retained_style_plan(
return RetainedStylePlan::Full;
}
if let Some(parent) = parent {
- add_tree_parent_scope(tree, parent, dirty_parent_siblings, &mut dirty);
+ add_style_context_chain(tree, parent, &mut dirty);
+ add_empty_parent_scope(tree, map, parent, mutations, &mut dirty);
} else {
dirty.insert(node);
}
@@ -5787,10 +6116,89 @@ fn propagate_border_spacing(tree: &DomTree, styles: &mut HashMap<NodeId, crate::
}
}
+#[derive(Clone, Copy)]
+enum EffectiveGridChild {
+ Dom(NodeId),
+ Generated {
+ host: NodeId,
+ kind: GeneratedBoxKind,
+ },
+}
+
+fn collect_effective_grid_children(
+ tree: &DomTree,
+ children: &[NodeId],
+ styles: &HashMap<NodeId, crate::LayoutStyle>,
+ out: &mut Vec<EffectiveGridChild>,
+) {
+ for &child in children {
+ let transparent = styles
+ .get(&child)
+ .is_some_and(|style| style.display_contents && style.display != crate::Display::None);
+ if !transparent {
+ out.push(EffectiveGridChild::Dom(child));
+ continue;
+ }
+ if styles
+ .get(&child)
+ .and_then(|style| style.before_pseudo.as_ref())
+ .is_some()
+ {
+ out.push(EffectiveGridChild::Generated {
+ host: child,
+ kind: GeneratedBoxKind::Before,
+ });
+ }
+ collect_effective_grid_children(tree, &rendered_children(tree, child), styles, out);
+ if styles
+ .get(&child)
+ .and_then(|style| style.after_pseudo.as_ref())
+ .is_some()
+ {
+ out.push(EffectiveGridChild::Generated {
+ host: child,
+ kind: GeneratedBoxKind::After,
+ });
+ }
+ }
+}
+
+fn effective_grid_child_style<'a>(
+ child: EffectiveGridChild,
+ styles: &'a HashMap<NodeId, crate::LayoutStyle>,
+) -> Option<&'a crate::LayoutStyle> {
+ match child {
+ EffectiveGridChild::Dom(node) => styles.get(&node),
+ EffectiveGridChild::Generated { host, kind } => {
+ let host = styles.get(&host)?;
+ match kind {
+ GeneratedBoxKind::Before => host.before_pseudo.as_deref(),
+ GeneratedBoxKind::After => host.after_pseudo.as_deref(),
+ }
+ }
+ }
+}
+
+fn effective_grid_child_style_mut<'a>(
+ child: EffectiveGridChild,
+ styles: &'a mut HashMap<NodeId, crate::LayoutStyle>,
+) -> Option<&'a mut crate::LayoutStyle> {
+ match child {
+ EffectiveGridChild::Dom(node) => styles.get_mut(&node),
+ EffectiveGridChild::Generated { host, kind } => {
+ let host = styles.get_mut(&host)?;
+ match kind {
+ GeneratedBoxKind::Before => host.before_pseudo.as_deref_mut(),
+ GeneratedBoxKind::After => host.after_pseudo.as_deref_mut(),
+ }
+ }
+ }
+}
+
/// Walk the tree; for each `display: grid` element that declares
-/// `grid-template-areas`, resolve each direct child's `grid-area` name to a
-/// taffy line placement. This is how Vector 2022 (and most grid layouts) place
-/// their header/sidebar/content/footer regions.
+/// `grid-template-areas`, resolve each box child's `grid-area` name to a taffy
+/// line placement. `display:contents` wrappers are transparent here for the
+/// same reason they are transparent when the Taffy child list is built.
fn resolve_grid_areas(
tree: &DomTree,
root: NodeId,
@@ -5812,6 +6220,13 @@ fn resolve_grid_areas(
if areas.is_none() && col_lines.is_none() && row_lines.is_none() {
continue;
}
+ let mut grid_children = Vec::new();
+ collect_effective_grid_children(
+ tree,
+ &rendered_children(tree, id),
+ styles,
+ &mut grid_children,
+ );
if let Some(areas) = &areas {
// name -> (row_start, row_end, col_start, col_end) in 0-based track indices.
@@ -5833,8 +6248,8 @@ fn resolve_grid_areas(
}
}
- for cid in rendered_children(tree, id) {
- let Some(cstyle) = styles.get_mut(&cid) else {
+ for child in grid_children.iter().copied() {
+ let Some(cstyle) = effective_grid_child_style_mut(child, styles) else {
continue;
};
let Some(name) = cstyle.grid_area_name.clone() else {
@@ -5857,8 +6272,8 @@ fn resolve_grid_areas(
// Named grid lines: resolve children placed with `grid-column`/`grid-row`
// values that reference a line name against this container's maps.
if col_lines.is_some() || row_lines.is_some() {
- for cid in rendered_children(tree, id) {
- let Some(cstyle) = styles.get_mut(&cid) else {
+ for child in grid_children.iter().copied() {
+ let Some(cstyle) = effective_grid_child_style_mut(child, styles) else {
continue;
};
if let (Some(raw), Some(map)) = (cstyle.grid_column_raw.clone(), &col_lines) {
@@ -7374,6 +7789,14 @@ fn blockify_layout_children(
.get(&child)
.is_some_and(|style| style.display_contents && style.display != crate::Display::None);
if transparent {
+ if let Some(style) = styles.get_mut(&child) {
+ for pseudo in [style.before_pseudo.as_mut(), style.after_pseudo.as_mut()]
+ .into_iter()
+ .flatten()
+ {
+ crate::blockify_outer_display(pseudo);
+ }
+ }
blockify_layout_children(tree, child, styles);
} else if let Some(style) = styles.get_mut(&child) {
crate::blockify_outer_display(style);
@@ -7573,7 +7996,6 @@ fn build_any(
fn build_flex_grid_children(
tree: &DomTree,
parent: NodeId,
- dom_children: &[NodeId],
taffy_tree: &mut TaffyTree<usize>,
id_map: &mut HashMap<taffy::NodeId, NodeId>,
words: &mut HashMap<taffy::NodeId, (NodeId, String)>,
@@ -7581,37 +8003,64 @@ fn build_flex_grid_children(
ifc_items: &mut IfcRegistry,
styles: &HashMap<NodeId, crate::LayoutStyle>,
) -> Vec<taffy::NodeId> {
+ let mut effective_children = Vec::new();
+ collect_effective_grid_children(
+ tree,
+ &rendered_children(tree, parent),
+ styles,
+ &mut effective_children,
+ );
+ effective_children.retain(|child| match child {
+ EffectiveGridChild::Dom(node) => {
+ tree.get_node(*node).is_some_and(|node| node.is_element())
+ || !tree.text_content(*node).trim().is_empty()
+ }
+ EffectiveGridChild::Generated { .. } => true,
+ });
+ effective_children.sort_by_key(|child| {
+ effective_grid_child_style(*child, styles)
+ .map(|style| style.order)
+ .unwrap_or(0)
+ });
+
let mut children = Vec::new();
let mut index = 0;
- while index < dom_children.len() {
- let is_text = tree.get_node(dom_children[index]).map_or(false, |node| {
- matches!(node.data, obscura_dom::tree::NodeData::Text { .. })
- });
+ while index < effective_children.len() {
+ let is_text = match effective_children[index] {
+ EffectiveGridChild::Dom(node) => tree.get_node(node).is_some_and(|node| {
+ matches!(node.data, obscura_dom::tree::NodeData::Text { .. })
+ }),
+ EffectiveGridChild::Generated { .. } => false,
+ };
if !is_text {
- children.extend(build_any(
- tree,
- dom_children[index],
- taffy_tree,
- id_map,
- words,
- engine,
- ifc_items,
- styles,
- ));
+ match effective_children[index] {
+ EffectiveGridChild::Dom(node) => children.extend(build_any(
+ tree, node, taffy_tree, id_map, words, engine, ifc_items, styles,
+ )),
+ EffectiveGridChild::Generated { host, kind } => {
+ let pseudo = effective_grid_child_style(effective_children[index], styles);
+ if let Some((nodes, _)) = build_in_flow_pseudo(
+ host, kind, pseudo, taffy_tree, words, ifc_items,
+ ) {
+ children.extend(nodes);
+ }
+ }
+ }
index += 1;
continue;
}
- let start = index;
- while index < dom_children.len()
- && tree.get_node(dom_children[index]).map_or(false, |node| {
+ let mut run = Vec::new();
+ while let Some(EffectiveGridChild::Dom(node)) = effective_children.get(index).copied() {
+ if !tree.get_node(node).is_some_and(|node| {
matches!(node.data, obscura_dom::tree::NodeData::Text { .. })
- })
- {
+ }) {
+ break;
+ }
+ run.push(node);
index += 1;
}
- let run = &dom_children[start..index];
- if let Some(item) = engine.try_build_run(tree, parent, run, styles) {
+ if let Some(item) = engine.try_build_run(tree, parent, &run, styles) {
let style = taffy::Style {
display: taffy::style::Display::Block,
..Default::default()
@@ -7622,7 +8071,7 @@ fn build_flex_grid_children(
continue;
}
}
- for &text in run {
+ for text in run {
children.extend(build_any(
tree, text, taffy_tree, id_map, words, engine, ifc_items, styles,
));
@@ -9900,7 +10349,6 @@ fn build(
build_flex_grid_children(
tree,
id,
- &dom_children,
taffy_tree,
id_map,
words,
@@ -13175,14 +13623,37 @@ mod tests {
name: &str,
tree: &DomTree,
cache: &mut crate::css::StylesheetCache,
- mut initial: DomLayout,
+ initial: DomLayout,
mutation: TreeStyleMutation,
expectation: RetainedDifferentialExpectation,
+ ) -> DomLayout {
+ finish_retained_tree_batch_case(
+ name,
+ tree,
+ cache,
+ initial,
+ &[mutation],
+ expectation,
+ )
+ }
+
+ fn finish_retained_tree_batch_case(
+ name: &str,
+ tree: &DomTree,
+ cache: &mut crate::css::StylesheetCache,
+ mut initial: DomLayout,
+ mutations: &[TreeStyleMutation],
+ expectation: RetainedDifferentialExpectation,
) -> DomLayout {
let retained = RetainedStyleMaps {
styles: std::mem::take(&mut initial.styles),
custom_properties: std::mem::take(&mut initial.custom_properties),
};
+ let mutations = mutations
+ .iter()
+ .cloned()
+ .map(RetainedStyleMutation::from)
+ .collect::<Vec<_>>();
let (incremental, telemetry) = layout_dom_with_web_fonts_pass_limit(
tree,
(360.0, 260.0),
@@ -13191,7 +13662,7 @@ mod tests {
None,
Some(cache),
Some(retained),
- &[mutation.into()],
+ &mutations,
);
let full = layout_dom(tree, (360.0, 260.0));
match expectation {
@@ -13748,6 +14219,165 @@ mod tests {
);
}
+ #[test]
+ fn retained_mixed_outer_has_dependencies_match_forced_full() {
+ use RetainedDifferentialExpectation::Incremental;
+
+ // `:has()` owns its relative path, but structural state on the anchor
+ // remains an ordinary tree dependency and must not be discarded just
+ // because both occur in the same compiled rule.
+ let tree = parse_html(
+ r#"<style>.host:has(.signal):first-child .desc{width:173px}.clean{height:7px}</style>
+ <main id=parent><b id=blocker></b><section id=host class=host><i class=signal></i><span class=desc></span></section><aside class=clean></aside></main>"#,
+ );
+ let parent = tree.get_element_by_id("parent").unwrap();
+ let blocker = tree.get_element_by_id("blocker").unwrap();
+ let host = tree.get_element_by_id("host").unwrap();
+ tree.remove_child(blocker);
+ let mut cache = crate::css::StylesheetCache::default();
+ let initial = layout_dom_with_web_fonts_and_stylesheet_cache(
+ &tree,
+ (360.0, 260.0),
+ &HashMap::new(),
+ &[],
+ &mut cache,
+ );
+ tree.insert_before(host, blocker);
+ finish_retained_tree_case(
+ "outer structural state beside has",
+ &tree,
+ &mut cache,
+ initial,
+ TreeStyleMutation::Insert {
+ node: blocker,
+ old_parent: None,
+ new_parent: parent,
+ },
+ Incremental,
+ );
+
+ // A freshly inserted anchor at position zero can create both adjacent
+ // and general sibling matches outside `:has()`.
+ let tree = parse_html(
+ r#"<style>
+ .left:has(.signal) + .adjacent{width:179px}
+ .left:has(.signal) ~ .following{height:31px}
+ .clean{height:9px}
+ </style><main id=parent><section id=left class=left><i class=signal></i></section><b id=adjacent class=adjacent></b><b class=following></b><aside class=clean></aside></main>"#,
+ );
+ let parent = tree.get_element_by_id("parent").unwrap();
+ let left = tree.get_element_by_id("left").unwrap();
+ let adjacent = tree.get_element_by_id("adjacent").unwrap();
+ tree.remove_child(left);
+ let mut cache = crate::css::StylesheetCache::default();
+ let initial = layout_dom_with_web_fonts_and_stylesheet_cache(
+ &tree,
+ (360.0, 260.0),
+ &HashMap::new(),
+ &[],
+ &mut cache,
+ );
+ tree.insert_before(adjacent, left);
+ finish_retained_tree_case(
+ "outer sibling paths beside has",
+ &tree,
+ &mut cache,
+ initial,
+ TreeStyleMutation::Insert {
+ node: left,
+ old_parent: None,
+ new_parent: parent,
+ },
+ Incremental,
+ );
+
+ // Text can toggle an outer :empty while the relative `:has()` path is
+ // entirely sibling-based and therefore has no text side effect.
+ let tree = parse_html(
+ r#"<style>.host:has(~ .peer):empty ~ .panel{width:181px}.clean{height:11px}</style>
+ <main><section id=host class=host></section><i class=peer></i><b class=panel></b><aside class=clean></aside></main>"#,
+ );
+ let host = tree.get_element_by_id("host").unwrap();
+ let text = tree.new_node(obscura_dom::tree::NodeData::Text {
+ contents: String::new(),
+ });
+ tree.append_child(host, text);
+ let mut cache = crate::css::StylesheetCache::default();
+ let initial = layout_dom_with_web_fonts_and_stylesheet_cache(
+ &tree,
+ (360.0, 260.0),
+ &HashMap::new(),
+ &[],
+ &mut cache,
+ );
+ tree.with_node_mut(text, |node| {
+ if let obscura_dom::tree::NodeData::Text { contents } = &mut node.data {
+ *contents = "now non-empty".into();
+ }
+ });
+ finish_retained_tree_case(
+ "outer empty state beside has",
+ &tree,
+ &mut cache,
+ initial,
+ TreeStyleMutation::Text {
+ node: text,
+ parent: Some(host),
+ },
+ Incremental,
+ );
+ }
+
+ #[test]
+ fn retained_batched_insertions_restore_old_structural_boundaries() {
+ use RetainedDifferentialExpectation::Incremental;
+
+ let tree = parse_html(
+ r#"<style>
+ .item:first-child{width:191px}.item:last-child{height:37px}
+ .item:only-child{color:#123456}
+ i:first-of-type{margin-left:13px}i:last-of-type{margin-right:17px}
+ i:only-of-type{padding-top:19px}.clean{height:13px}
+ </style><main><section id=list><i id=before-a class=item></i><i id=before-b class=item></i><i id=old class=item></i><i id=after-a class=item></i><i id=after-b class=item></i></section><aside class=clean></aside></main>"#,
+ );
+ let list = tree.get_element_by_id("list").unwrap();
+ let old = tree.get_element_by_id("old").unwrap();
+ let before_a = tree.get_element_by_id("before-a").unwrap();
+ let before_b = tree.get_element_by_id("before-b").unwrap();
+ let after_a = tree.get_element_by_id("after-a").unwrap();
+ let after_b = tree.get_element_by_id("after-b").unwrap();
+ for inserted in [before_a, before_b, after_a, after_b] {
+ tree.remove_child(inserted);
+ }
+ let mut cache = crate::css::StylesheetCache::default();
+ let initial = layout_dom_with_web_fonts_and_stylesheet_cache(
+ &tree,
+ (360.0, 260.0),
+ &HashMap::new(),
+ &[],
+ &mut cache,
+ );
+ tree.insert_before(old, before_a);
+ tree.insert_before(old, before_b);
+ tree.append_child(list, after_a);
+ tree.append_child(list, after_b);
+ let mutations = [before_a, before_b, after_a, after_b].map(|node| {
+ TreeStyleMutation::Insert {
+ node,
+ old_parent: None,
+ new_parent: list,
+ }
+ });
+ finish_retained_tree_batch_case(
+ "batched first last only boundaries",
+ &tree,
+ &mut cache,
+ initial,
+ &mutations,
+ Incremental,
+ );
+ }
+
#[test]
fn retained_relational_invalidation_keeps_two_thousand_node_peer_clean() {
let mut clean = String::new();
@@ -13846,6 +14476,61 @@ mod tests {
assert!(telemetry.retained_fresh < 16, "{telemetry:?}");
}
+ #[test]
+ fn retained_keyed_start_insertion_keeps_large_body_branch_clean() {
+ let mut clean = String::new();
+ for index in 0..2_000 {
+ clean.push_str(&format!("<span class=clean data-index={index}></span>"));
+ }
+ let tree = parse_html(&format!(
+ r#"<!doctype html><html><head><style>
+ .item:nth-child(2){{width:31px}}
+ .left + .right{{height:7px}}
+ .early ~ .late{{height:9px}}
+ .maybe-empty:empty + .after{{height:11px}}
+ .clean{{height:1px}}
+ </style></head><body><main id=stable>{clean}</main><i id=watcher data-scroll-watcher></i></body></html>"#,
+ ));
+ let body = tree.query_selector("body").unwrap().unwrap();
+ let stable = tree.get_element_by_id("stable").unwrap();
+ let watcher = tree.get_element_by_id("watcher").unwrap();
+ tree.remove_child(watcher);
+ let mut cache = crate::css::StylesheetCache::default();
+ let mut initial = layout_dom_with_web_fonts_and_stylesheet_cache(
+ &tree,
+ (800.0, 600.0),
+ &HashMap::new(),
+ &[],
+ &mut cache,
+ );
+ tree.insert_before(stable, watcher);
+ let retained = RetainedStyleMaps {
+ styles: std::mem::take(&mut initial.styles),
+ custom_properties: std::mem::take(&mut initial.custom_properties),
+ };
+ let (incremental, telemetry) = layout_dom_with_web_fonts_pass_limit(
+ &tree,
+ (800.0, 600.0),
+ &HashMap::new(),
+ &[],
+ None,
+ Some(&mut cache),
+ Some(retained),
+ &[TreeStyleMutation::Insert {
+ node: watcher,
+ old_parent: None,
+ new_parent: body,
+ }
+ .into()],
+ );
+ let full = layout_dom(&tree, (800.0, 600.0));
+ assert_computed_styles_match("keyed start insertion large body branch", &incremental, &full);
+ assert_eq!(incremental.rects, full.rects);
+ assert_eq!(telemetry.retained_fallback, 0, "{telemetry:?}");
+ assert!(telemetry.retained_reused >= 2_000, "{telemetry:?}");
+ assert!(telemetry.retained_fresh < 16, "{telemetry:?}");
+ }
+
#[test]
fn container_convergence_classifies_self_consistent_stability() {
assert_eq!(
@@ -15105,6 +15790,104 @@ mod tests {
);
}
+ #[test]
+ fn display_contents_descendants_resolve_as_named_grid_area_items() {
+ let tree = parse_html(
+ r#"<style>
+ html,body{margin:0}
+ #grid{
+ display:grid;width:400px;height:100px;
+ grid-template-columns:100px 300px;
+ grid-template-rows:40px 60px;
+ grid-template-areas:"nav head" "nav main";
+ }
+ #contents{display:contents}
+ #nav{grid-area:nav}#head{grid-area:head}#main{grid-area:main}
+ </style>
+ <div id="grid"><div id="contents">
+ <nav id="nav"></nav><header id="head"></header><article id="main"></article>
+ </div></div>"#,
+ );
+ let laid = layout_dom(&tree, (600.0, 300.0));
+ let rect = |id| laid.rects[&tree.get_element_by_id(id).unwrap()];
+
+ assert_eq!(
+ rect("nav"),
+ Rect {
+ x: 0.0,
+ y: 0.0,
+ width: 100.0,
+ height: 100.0,
+ }
+ );
+ assert_eq!(
+ rect("head"),
+ Rect {
+ x: 100.0,
+ y: 0.0,
+ width: 300.0,
+ height: 40.0,
+ }
+ );
+ assert_eq!(
+ rect("main"),
+ Rect {
+ x: 100.0,
+ y: 40.0,
+ width: 300.0,
+ height: 60.0,
+ }
+ );
+ }
+
+ #[test]
+ fn display_contents_generated_pseudo_remains_a_named_grid_item() {
+ let tree = parse_html(
+ r#"<style>
+ html,body{margin:0}
+ #grid{
+ display:grid;width:400px;height:50px;
+ grid-template-columns:100px 300px;
+ grid-template-rows:50px;
+ grid-template-areas:"marker body";
+ }
+ #contents{display:contents}
+ #contents::before{content:"";display:block;grid-area:marker}
+ #body{grid-area:body}
+ </style>
+ <div id="grid"><div id="contents"><article id="body"></article></div></div>"#,
+ );
+ let laid = layout_dom(&tree, (600.0, 300.0));
+ let contents = tree.get_element_by_id("contents").unwrap();
+ let body = tree.get_element_by_id("body").unwrap();
+ let generated = laid
+ .generated_boxes
+ .iter()
+ .find(|box_| {
+ box_.host == contents && box_.kind == GeneratedBoxKind::Before
+ })
+ .expect("display:contents ::before must generate an effective grid item");
+
+ assert_eq!(
+ generated.rect,
+ Rect {
+ x: 0.0,
+ y: 0.0,
+ width: 100.0,
+ height: 50.0,
+ }
+ );
+ assert_eq!(
+ laid.rects[&body],
+ Rect {
+ x: 100.0,
+ y: 0.0,
+ width: 300.0,
+ height: 50.0,
+ }
+ );
+ }
+
#[test]
fn grid_auto_columns_size_successive_implicit_column_tracks() {
let tree = parse_html(
diff --git a/crates/obscura-render/src/paint.rs b/crates/obscura-render/src/paint.rs
index 6d3890c4..ea620289 100644
--- a/crates/obscura-render/src/paint.rs
+++ b/crates/obscura-render/src/paint.rs
@@ -2140,20 +2140,12 @@ fn transform_subtree_source_bounds(
width: rect.width,
height: rect.height,
};
- if let Some(shadow) = laid
- .styles
- .get(&id)
- .and_then(|style| style.box_shadow)
- .filter(|shadow| !shadow.inset && shadow.color[3] != 0)
- {
- let expansion = shadow.spread + shadow.blur.max(0.0);
- let shadow_rect = crate::Rect {
- x: visual.x + shadow.offset_x - expansion,
- y: visual.y + shadow.offset_y - expansion,
- width: (visual.width + 2.0 * expansion).max(0.0),
- height: (visual.height + 2.0 * expansion).max(0.0),
- };
- visual = visual.union(&shadow_rect);
+ if let Some(style) = laid.styles.get(&id) {
+ // The atomic source surface must retain every primitive that can
+ // extend beyond the border box. In particular, a thick outline
+ // is transformed with the element and cannot be reconstructed
+ // after the tight source layer has already clipped it.
+ visual = non_text_ink_bounds(&visual, style);
}
bounds = Some(match bounds {
Some(current) => current.union(&visual),
@@ -2241,6 +2233,11 @@ fn paint_laid_dom_scrolled(
// inline svg on the page rather than re-parsing the sprite per icon.
let mut sprite_cache: std::collections::HashMap<String, Option<String>> =
std::collections::HashMap::new();
+ // An overflow clip is inherited by every descendant until another clip
+ // joins the chain. Rasterizing the same full-surface alpha mask per child
+ // made clipped long pages scale as surface area times descendant count.
+ // Cache immutable masks for this one paint surface and share them by Arc.
+ let mut overflow_mask_cache: OverflowClipMaskCache = HashMap::new();
// Whether any element carries a `transform: translate()`. When none does
// (the overwhelmingly common case), every node's accumulated offset is
// zero, so skip the per-node ancestor walk entirely and keep the paint
@@ -2419,7 +2416,8 @@ fn paint_laid_dom_scrolled(
clip.viewport_rect(scroll_state.surface_extent.unwrap_or(viewport))
});
let clip_mask = overflow_clip.as_ref().and_then(|clip| {
- overflow_clip_mask(
+ cached_overflow_clip_mask(
+ &mut overflow_mask_cache,
pixmap.width(),
pixmap.height(),
clip,
@@ -2432,7 +2430,7 @@ fn paint_laid_dom_scrolled(
&mut pixmap,
offset,
clip,
- clip_mask.as_ref(),
+ clip_mask.as_deref(),
raster_scale,
print_economy,
);
@@ -2589,7 +2587,8 @@ fn paint_laid_dom_scrolled(
transform.f,
);
let clip_mask = outside_overflow_clip.as_ref().and_then(|clip| {
- overflow_clip_mask(
+ cached_overflow_clip_mask(
+ &mut overflow_mask_cache,
pixmap.width(),
pixmap.height(),
clip,
@@ -2602,7 +2601,7 @@ fn paint_laid_dom_scrolled(
layer.as_ref(),
&tiny_skia::PixmapPaint::default(),
transform,
- clip_mask.as_ref(),
+ clip_mask.as_deref(),
);
continue;
}
@@ -2688,14 +2687,6 @@ fn paint_laid_dom_scrolled(
let clip = overflow_clip
.as_ref()
.map(|clip| clip.viewport_rect(scroll_state.surface_extent.unwrap_or(viewport)));
- let ancestor_clip_mask = overflow_clip.as_ref().and_then(|clip| {
- overflow_clip_mask(
- pixmap.width(),
- pixmap.height(),
- clip,
- scroll_state.surface_extent.unwrap_or(viewport),
- )
- });
let cull_rect = rect;
let visible_rect = match clip {
Some(c) => match cull_rect.intersect(&c) {
@@ -2713,17 +2704,55 @@ fn paint_laid_dom_scrolled(
Some(rect) => rect,
None => continue,
};
- let clip_path_mask = style.clip_path.as_ref().and_then(|polygon| {
- polygon_clip_mask(
- pixmap.width(),
- pixmap.height(),
- polygon,
- &rect,
- style.font_size.unwrap_or(16.0),
- root_font_size,
- viewport,
- )
- });
+ let surface = paint_surface_rect(&pixmap, raster_scale);
+ let box_on_surface = visible_rect.intersect(&surface).is_some();
+ let ink_bounds = non_text_ink_bounds(&rect, style);
+ // Mask allocation follows the raw ink bounds, not the already-clipped
+ // bounds. A distant box can cast a large offset shadow onto the paint
+ // surface while its ancestor overflow clip remains offscreen. In that
+ // case the primitive still needs the ancestor mask so it cannot leak
+ // through the clip.
+ let non_text_on_surface = ink_bounds.intersect(&surface).is_some();
+ // A list marker or generated run may protrude modestly from the host
+ // border box. Keep its clip masks available without letting a box
+ // thousands of pixels away allocate full-surface masks.
+ let text_guard = style.font_size.unwrap_or(16.0) * 4.0 + 4.0;
+ let text_bounds = crate::Rect {
+ x: rect.x - text_guard,
+ y: rect.y - text_guard,
+ width: rect.width + 2.0 * text_guard,
+ height: rect.height + 2.0 * text_guard,
+ };
+ let text_on_surface = text_bounds.intersect(&surface).is_some();
+ let needs_host_masks = non_text_on_surface || text_on_surface;
+ let ancestor_clip_mask = needs_host_masks
+ .then(|| {
+ overflow_clip.as_ref().and_then(|clip| {
+ cached_overflow_clip_mask(
+ &mut overflow_mask_cache,
+ pixmap.width(),
+ pixmap.height(),
+ clip,
+ scroll_state.surface_extent.unwrap_or(viewport),
+ )
+ })
+ })
+ .flatten();
+ let clip_path_mask = needs_host_masks
+ .then(|| {
+ style.clip_path.as_ref().and_then(|polygon| {
+ polygon_clip_mask(
+ pixmap.width(),
+ pixmap.height(),
+ polygon,
+ &rect,
+ style.font_size.unwrap_or(16.0),
+ root_font_size,
+ viewport,
+ )
+ })
+ })
+ .flatten();
let has_background_box_paint = !style.background_clip_text
&& (style.background_color.is_some()
|| style.background_image.is_some()
@@ -2747,7 +2776,7 @@ fn paint_laid_dom_scrolled(
&rect,
style,
clip,
- ancestor_clip_mask.as_ref(),
+ ancestor_clip_mask.as_deref(),
root_font_size,
viewport,
base_url,
@@ -2767,7 +2796,7 @@ fn paint_laid_dom_scrolled(
&shadow,
&rect,
style.border_model.radii,
- ancestor_clip_mask.as_ref(),
+ ancestor_clip_mask.as_deref(),
);
}
}
@@ -2780,14 +2809,22 @@ fn paint_laid_dom_scrolled(
let has_radius = !radius.is_zero();
let background = background_geometry(&rect, style);
let background_path = background_clip_path(background);
- let element_clip_mask =
- background_extra_clip(ancestor_clip_mask.as_ref(), clip_path_mask.as_ref());
- let background_mask = element_clip_mask.clone();
+ let combined_clip_mask = clip_path_mask.as_ref().and_then(|polygon| {
+ intersect_clip_masks(ancestor_clip_mask.as_deref().cloned(), Some(polygon))
+ });
+ let element_clip_mask = combined_clip_mask
+ .as_ref()
+ .or_else(|| ancestor_clip_mask.as_deref());
+ let background_mask = element_clip_mask;
// A linear-gradient background (heavily used by modern hero sections);
// without this it paints white. Takes precedence over a solid color.
// `background-clip: text` clips the background to the glyphs, so it must
// not paint as a box here; the text paint path fills the glyphs instead.
- if !paints_inline_fragments && style.mask_image.is_none() && !style.background_clip_text {
+ if box_on_surface
+ && !paints_inline_fragments
+ && style.mask_image.is_none()
+ && !style.background_clip_text
+ {
if let Some(bg) = style.background_color {
if let Some(path) = background_path.as_ref() {
let mut paint = Paint::default();
@@ -2798,7 +2835,7 @@ fn paint_laid_dom_scrolled(
&paint,
FillRule::Winding,
raster_transform(raster_scale),
- background_mask.as_ref(),
+ background_mask,
);
}
}
@@ -2813,7 +2850,7 @@ fn paint_laid_dom_scrolled(
style,
root_font_size,
viewport,
- background_mask.as_ref(),
+ background_mask,
raster_scale,
);
}
@@ -2826,7 +2863,7 @@ fn paint_laid_dom_scrolled(
&background.origin_rect,
*center,
stops,
- background_mask.as_ref(),
+ background_mask,
raster_scale,
);
}
@@ -2840,7 +2877,7 @@ fn paint_laid_dom_scrolled(
*angle,
*center,
stops,
- background_mask.as_ref(),
+ background_mask,
);
}
if let Some((angle, stops)) = &style.background_gradient {
@@ -2851,7 +2888,7 @@ fn paint_laid_dom_scrolled(
&background.origin_rect,
*angle,
stops,
- background_mask.as_ref(),
+ background_mask,
raster_scale,
);
}
@@ -2859,7 +2896,7 @@ fn paint_laid_dom_scrolled(
}
}
- if !paints_inline_fragments {
+ if box_on_surface && !paints_inline_fragments {
if let Some(mask_url) = &style.mask_image {
let fill = style
.background_color
@@ -2876,7 +2913,7 @@ fn paint_laid_dom_scrolled(
style.background_conic_gradient.as_ref(),
style.mask_size,
style.mask_repeat,
- element_clip_mask.as_ref(),
+ element_clip_mask,
&mut pixmap,
image_cache,
);
@@ -2909,80 +2946,75 @@ fn paint_laid_dom_scrolled(
image_cache,
None,
background.clip_radii,
- background_mask.as_ref(),
+ background_mask,
);
}
}
}
}
- let positioned_pseudo_containing_block = {
- let mut ancestor = Some(nid);
- let mut found = None;
- while let Some(candidate) = ancestor {
- let candidate_style = laid.styles.get(&candidate);
- let establishes = candidate_style.is_some_and(|candidate_style| {
- candidate_style.position.is_some()
- || candidate_style.establishes_positioning_containing_block()
- });
- if establishes {
- if let Some(candidate_rect) = laid.rects.get(&candidate).copied() {
- let (candidate_x, candidate_y) =
- scroll_state.translation_for(laid, candidate);
- found = Some(crate::Rect {
- x: candidate_rect.x + candidate_x,
- y: candidate_rect.y + candidate_y,
- width: candidate_rect.width,
- height: candidate_rect.height,
- });
- break;
- }
- // A positioned inline can be flattened out of the taffy
- // tree when its block children form the real layout
- // boxes. It is still the CSS containing block, but has no
- // usable rectangle in this representation. Keep walking
- // to the nearest positioned ancestor that does have one
- // instead of falling back to the host line itself. The
- // latter places `left:0` generated gutters directly over
- // their margin-shifted text.
- }
- ancestor = tree.get_node(candidate).and_then(|node| node.parent);
- }
- found.unwrap_or(rect)
- };
- let positioned_pseudo_overflow_clip = scroll_state.descendant_overflow_clip_for(laid, nid);
- let positioned_pseudo_clip = positioned_pseudo_overflow_clip
- .as_ref()
- .map(|clip| clip.viewport_rect(scroll_state.surface_extent.unwrap_or(viewport)));
- let positioned_pseudo_clip_mask =
- positioned_pseudo_overflow_clip.as_ref().and_then(|clip| {
- overflow_clip_mask(
- pixmap.width(),
- pixmap.height(),
- clip,
- scroll_state.surface_extent.unwrap_or(viewport),
- )
- });
- for pseudo in [
+ let has_positioned_pseudo = [
style.before_pseudo.as_deref(),
style.after_pseudo.as_deref(),
]
.into_iter()
.flatten()
- {
- paint_positioned_pseudo(
- &mut laid.text_engine,
- &mut pixmap,
- pseudo,
- &positioned_pseudo_containing_block,
- &rect,
- viewport,
- root_font_size,
- positioned_pseudo_clip,
- positioned_pseudo_clip_mask.as_ref(),
- base_url,
- image_cache,
- raster_scale,
- );
+ .any(|pseudo| pseudo.position == Some(taffy::Position::Absolute));
+ if has_positioned_pseudo {
+ let positioned_pseudo_containing_block = {
+ let mut ancestor = Some(nid);
+ let mut found = None;
+ while let Some(candidate) = ancestor {
+ let candidate_style = laid.styles.get(&candidate);
+ let establishes = candidate_style.is_some_and(|candidate_style| {
+ candidate_style.position.is_some()
+ || candidate_style.establishes_positioning_containing_block()
+ });
+ if establishes {
+ if let Some(candidate_rect) = laid.rects.get(&candidate).copied() {
+ let (candidate_x, candidate_y) =
+ scroll_state.translation_for(laid, candidate);
+ found = Some(crate::Rect {
+ x: candidate_rect.x + candidate_x,
+ y: candidate_rect.y + candidate_y,
+ width: candidate_rect.width,
+ height: candidate_rect.height,
+ });
+ break;
+ }
+ // A positioned inline can be flattened out of the taffy
+ // tree when its block children form the real layout
+ // boxes. It is still the CSS containing block, but has no
+ // usable rectangle in this representation. Keep walking
+ // to the nearest positioned ancestor that does have one.
+ }
+ ancestor = tree.get_node(candidate).and_then(|node| node.parent);
+ }
+ found.unwrap_or(rect)
+ };
+ let positioned_pseudo_overflow_clip =
+ scroll_state.descendant_overflow_clip_for(laid, nid);
+ for pseudo in [
+ style.before_pseudo.as_deref(),
+ style.after_pseudo.as_deref(),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ paint_positioned_pseudo(
+ &mut laid.text_engine,
+ &mut pixmap,
+ pseudo,
+ &positioned_pseudo_containing_block,
+ &rect,
+ viewport,
+ root_font_size,
+ scroll_state.surface_extent.unwrap_or(viewport),
+ positioned_pseudo_overflow_clip.as_ref(),
+ base_url,
+ image_cache,
+ raster_scale,
+ );
+ }
}
if !paints_inline_fragments {
@@ -2990,7 +3022,7 @@ fn paint_laid_dom_scrolled(
&mut pixmap,
&rect,
style,
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
}
@@ -2998,11 +3030,11 @@ fn paint_laid_dom_scrolled(
&mut pixmap,
&rect,
style,
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
- if name.local.as_ref() == "img" {
+ if box_on_surface && name.local.as_ref() == "img" {
if let Some(source) = selected_images.get(&nid) {
// `visible_rect` is the border box already intersected with the
// ancestor overflow clip: the raster must not paint past it (a
@@ -3018,7 +3050,7 @@ fn paint_laid_dom_scrolled(
image_cache,
None,
radius,
- element_clip_mask.as_ref(),
+ element_clip_mask,
);
// Fall back when the image itself did not paint, following
// what browsers show for a broken image: a non-empty alt
@@ -3042,7 +3074,7 @@ fn paint_laid_dom_scrolled(
None,
0.0,
clip,
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
}
@@ -3072,61 +3104,63 @@ fn paint_laid_dom_scrolled(
// is drawn at its full border-box size (undistorted) and clipped to the
// overflow-visible region.
if name.local.as_ref() == "svg" {
- let mut markup = serialize_svg_styled(
- tree,
- nid,
- &laid.styles,
- (suppress_opacity_for == Some(nid)).then_some(nid),
- );
- // Resolve referenced symbols before carrying the host color into
- // the standalone document. A document-level/external symbol may
- // itself contain `currentColor`, and therefore has to be present
- // when the root color is established.
- inject_external_sprites(
- tree,
- nid,
- base_url,
- &mut markup,
- image_cache,
- &mut sprite_cache,
- );
- // resvg parses the serialized subtree as a standalone SVG
- // document, outside the page's author stylesheet. Preserve the
- // host element's computed `color` so paths using `currentColor`
- // (the standard framework-logo/icon pattern) do not fall back to
- // black.
- if let Some(color) = style.color {
- inject_svg_current_color(&mut markup, color);
- }
- // `<use href="url#id">` pointing at an EXTERNAL sprite file resolves
- // to nothing in resvg (the symbol lives in another document). Fetch
- // the sprite, splice the referenced `<symbol>` into a local `<defs>`,
- // and rewrite the href to a same-document `#id`. Same-document
- // `<use href="#id">` (empty url) is untouched.
- if let Some(content) = render_svg_with_font_database(
- markup.as_bytes(),
- rect.width as u32,
- rect.height as u32,
- &svg_fonts,
- ) {
- let mut mask = element_clip_mask.clone();
- if has_radius {
- let own = rounded_box_clip_mask_radii(
- pixmap.width(),
- pixmap.height(),
- &visible_rect,
- radius,
+ if box_on_surface {
+ let mut markup = serialize_svg_styled(
+ tree,
+ nid,
+ &laid.styles,
+ (suppress_opacity_for == Some(nid)).then_some(nid),
+ );
+ // Resolve referenced symbols before carrying the host color into
+ // the standalone document. A document-level/external symbol may
+ // itself contain `currentColor`, and therefore has to be present
+ // when the root color is established.
+ inject_external_sprites(
+ tree,
+ nid,
+ base_url,
+ &mut markup,
+ image_cache,
+ &mut sprite_cache,
+ );
+ // resvg parses the serialized subtree as a standalone SVG
+ // document, outside the page's author stylesheet. Preserve the
+ // host element's computed `color` so paths using `currentColor`
+ // (the standard framework-logo/icon pattern) do not fall back to
+ // black.
+ if let Some(color) = style.color {
+ inject_svg_current_color(&mut markup, color);
+ }
+ // `<use href="url#id">` pointing at an EXTERNAL sprite file resolves
+ // to nothing in resvg (the symbol lives in another document). Fetch
+ // the sprite, splice the referenced `<symbol>` into a local `<defs>`,
+ // and rewrite the href to a same-document `#id`. Same-document
+ // `<use href="#id">` (empty url) is untouched.
+ if let Some(content) = render_svg_with_font_database(
+ markup.as_bytes(),
+ rect.width as u32,
+ rect.height as u32,
+ &svg_fonts,
+ ) {
+ let mut mask = element_clip_mask.cloned();
+ if has_radius {
+ let own = rounded_box_clip_mask_radii(
+ pixmap.width(),
+ pixmap.height(),
+ &visible_rect,
+ radius,
+ );
+ mask = intersect_clip_masks(mask, own.as_ref());
+ }
+ pixmap.draw_pixmap(
+ rect.x as i32,
+ rect.y as i32,
+ content.as_ref(),
+ &tiny_skia::PixmapPaint::default(),
+ Transform::identity(),
+ mask.as_ref(),
);
- mask = intersect_clip_masks(mask, own.as_ref());
}
- pixmap.draw_pixmap(
- rect.x as i32,
- rect.y as i32,
- content.as_ref(),
- &tiny_skia::PixmapPaint::default(),
- Transform::identity(),
- mask.as_ref(),
- );
}
for child in tree.descendants(nid) {
svg_subtree_skip.insert(child);
@@ -3170,7 +3204,7 @@ fn paint_laid_dom_scrolled(
style.font_family.as_deref(),
style.letter_spacing.unwrap_or(0.0),
clip,
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
}
@@ -3196,7 +3230,7 @@ fn paint_laid_dom_scrolled(
style.font_family.as_deref(),
style.letter_spacing.unwrap_or(0.0),
clip,
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
}
@@ -3222,7 +3256,7 @@ fn paint_laid_dom_scrolled(
style.font_family.as_deref(),
style.letter_spacing.unwrap_or(0.0),
Some(visible_rect),
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
}
@@ -3244,7 +3278,7 @@ fn paint_laid_dom_scrolled(
&arrow_paint,
FillRule::Winding,
raster_transform(raster_scale),
- element_clip_mask.as_ref(),
+ element_clip_mask,
);
}
}
@@ -3276,7 +3310,7 @@ fn paint_laid_dom_scrolled(
style.font_family.as_deref(),
style.letter_spacing.unwrap_or(0.0),
clip,
- element_clip_mask.as_ref(),
+ element_clip_mask,
raster_scale,
);
}
@@ -3328,7 +3362,8 @@ fn paint_laid_dom_scrolled(
.as_ref()
.map(|clip| clip.viewport_rect(scroll_state.surface_extent.unwrap_or(viewport)));
let clip_mask = overflow_clip.as_ref().and_then(|clip| {
- overflow_clip_mask(
+ cached_overflow_clip_mask(
+ &mut overflow_mask_cache,
pixmap.width(),
pixmap.height(),
clip,
@@ -3341,7 +3376,7 @@ fn paint_laid_dom_scrolled(
&mut pixmap,
off,
clip,
- clip_mask.as_ref(),
+ clip_mask.as_deref(),
raster_scale,
print_economy,
);
@@ -3355,7 +3390,7 @@ fn paint_laid_dom_scrolled(
&mut pixmap,
off,
clip,
- clip_mask.as_ref(),
+ clip_mask.as_deref(),
raster_scale,
print_economy,
);
@@ -3370,6 +3405,67 @@ fn raster_transform(scale: f32) -> Transform {
Transform::from_scale(scale, scale)
}
+/// The raster target in the CSS-pixel coordinate space used by paint items.
+/// Region captures can raster directly above 1x, so device dimensions alone
+/// are not a valid cull rect.
+fn paint_surface_rect(pixmap: &Pixmap, raster_scale: f32) -> crate::Rect {
+ let scale = if raster_scale.is_finite() && raster_scale > 0.0 {
+ raster_scale
+ } else {
+ 1.0
+ };
+ crate::Rect {
+ x: 0.0,
+ y: 0.0,
+ width: pixmap.width() as f32 / scale,
+ height: pixmap.height() as f32 / scale,
+ }
+}
+
+fn rect_intersects_paint_surface(
+ rect: &crate::Rect,
+ pixmap: &Pixmap,
+ raster_scale: f32,
+) -> bool {
+ rect.width > 0.0
+ && rect.height > 0.0
+ && rect.intersect(&paint_surface_rect(pixmap, raster_scale)).is_some()
+}
+
+/// Conservative ink overflow for the non-text primitives emitted by one CSS
+/// box. Gecko performs the analogous dirty-rect test against a frame's ink
+/// overflow before constructing display items. A border box alone is not
+/// sufficient because an outset shadow or outline can enter the capture while
+/// the box itself remains outside it.
+fn non_text_ink_bounds(rect: &crate::Rect, style: &crate::LayoutStyle) -> crate::Rect {
+ let mut bounds = *rect;
+ if let Some(shadow) = style
+ .box_shadow
+ .filter(|shadow| !shadow.inset && shadow.color[3] != 0)
+ {
+ let expansion = shadow.spread + shadow.blur.max(0.0);
+ let shadow_bounds = crate::Rect {
+ x: rect.x + shadow.offset_x - expansion,
+ y: rect.y + shadow.offset_y - expansion,
+ width: (rect.width + 2.0 * expansion).max(0.0),
+ height: (rect.height + 2.0 * expansion).max(0.0),
+ };
+ if shadow_bounds.width > 0.0 && shadow_bounds.height > 0.0 {
+ bounds = bounds.union(&shadow_bounds);
+ }
+ }
+ let outline = (style.outline.offset + style.outline.used_width()).max(0.0);
+ if outline > 0.0 {
+ bounds = bounds.union(&crate::Rect {
+ x: rect.x - outline,
+ y: rect.y - outline,
+ width: rect.width + 2.0 * outline,
+ height: rect.height + 2.0 * outline,
+ });
+ }
+ bounds
+}
+
/// Paint an ordinary inline's sliced border boxes instead of its multiline
/// bounding union. Background coordinates stay anchored to the union so
/// gradients and images remain one joined `box-decoration-break:slice`
@@ -3420,6 +3516,16 @@ fn paint_inline_fragment_decorations(
fragment_style.border_model.radii.top_right = crate::CornerRadius::default();
fragment_style.border_model.radii.bottom_right = crate::CornerRadius::default();
}
+ let ink = non_text_ink_bounds(&fragment, &fragment_style);
+ let visible_ink = match clip {
+ Some(clip) => ink.intersect(&clip),
+ None => Some(ink),
+ };
+ if !visible_ink
+ .is_some_and(|ink| rect_intersects_paint_surface(&ink, pixmap, raster_scale))
+ {
+ continue;
+ }
let radius = fragment_style
.border_model
.radii
@@ -3900,6 +4006,9 @@ fn paint_css_border(
if widths.as_array().iter().all(|width| *width <= 0.0) {
return;
}
+ if !rect_intersects_paint_surface(rect, pixmap, raster_scale) {
+ return;
+ }
let current = style.color.unwrap_or([0, 0, 0, 255]);
let colors = style
.border_model
@@ -4052,6 +4161,16 @@ fn paint_css_outline(
if width <= 0.0 {
return;
}
+ let outer_expansion = (style.outline.offset + width).max(0.0);
+ let outline_bounds = crate::Rect {
+ x: rect.x - outer_expansion,
+ y: rect.y - outer_expansion,
+ width: rect.width + 2.0 * outer_expansion,
+ height: rect.height + 2.0 * outer_expansion,
+ };
+ if !rect_intersects_paint_surface(&outline_bounds, pixmap, raster_scale) {
+ return;
+ }
let center_expansion = style.outline.offset + width / 2.0;
let center_rect = crate::Rect {
x: rect.x - center_expansion,
@@ -4418,6 +4537,17 @@ fn paint_box_shadow(
let rx0 = (radius.0 + spread).max(0.0);
let ry0 = (radius.1 + spread).max(0.0);
let blur = shadow.blur.max(0.0);
+ let shadow_bounds = crate::Rect {
+ x: x0 - blur,
+ y: y0 - blur,
+ width: w0 + 2.0 * blur,
+ height: h0 + 2.0 * blur,
+ };
+ // Shadows disable the native >1x path, so their paint coordinate space is
+ // the pixmap's one-CSS-pixel-per-pixel surface.
+ if !rect_intersects_paint_surface(&shadow_bounds, pixmap, 1.0) {
+ return;
+ }
// Ancestor overflow clip is already the complete rect/rounded chain.
let mask = ancestor_clip;
let color = shadow.color;
@@ -6531,6 +6661,16 @@ fn paint_in_flow_generated_box(
if visible.width <= 0.0 || visible.height <= 0.0 {
return;
}
+ let ink = non_text_ink_bounds(&rect, style);
+ let visible_ink = match clip {
+ Some(clip) => ink.intersect(&clip),
+ None => Some(ink),
+ };
+ if !visible_ink
+ .is_some_and(|ink| rect_intersects_paint_surface(&ink, pixmap, raster_scale))
+ {
+ return;
+ }
let ancestor_clip_mask = overflow_clip.as_ref().and_then(|clip| {
overflow_clip_mask(
pixmap.width(),
@@ -6708,8 +6848,8 @@ fn paint_positioned_pseudo(
static_position_rect: &crate::Rect,
viewport: (f32, f32),
root_font_size: f32,
- ancestor_clip: Option<crate::Rect>,
- ancestor_clip_mask: Option<&tiny_skia::Mask>,
+ clip_extent: (f32, f32),
+ ancestor_overflow_clip: Option<&crate::dom::OverflowClip>,
base_url: Option<&str>,
image_cache: &mut RenderResourceCache,
raster_scale: f32,
@@ -6770,11 +6910,19 @@ fn paint_positioned_pseudo(
width,
height,
};
+ let ancestor_clip = ancestor_overflow_clip
+ .map(|clip| clip.viewport_rect(clip_extent));
let visible = match ancestor_clip {
Some(clip) => rect.intersect(&clip),
None => Some(rect),
};
let Some(visible) = visible else { return };
+ if !rect_intersects_paint_surface(&non_text_ink_bounds(&rect, style), pixmap, raster_scale) {
+ return;
+ }
+ let ancestor_clip_mask = ancestor_overflow_clip.and_then(|clip| {
+ overflow_clip_mask(pixmap.width(), pixmap.height(), clip, clip_extent)
+ });
let radius = style.border_model.radii.resolve(rect.width, rect.height);
let clip_path_mask = style.clip_path.as_ref().and_then(|polygon| {
polygon_clip_mask(
@@ -6789,7 +6937,8 @@ fn paint_positioned_pseudo(
});
let background = background_geometry(&rect, style);
let background_path = background_clip_path(background);
- let element_clip_mask = background_extra_clip(ancestor_clip_mask, clip_path_mask.as_ref());
+ let element_clip_mask =
+ background_extra_clip(ancestor_clip_mask.as_ref(), clip_path_mask.as_ref());
let background_mask = element_clip_mask.clone();
if style.mask_image.is_none() && !style.background_clip_text {
if let (Some(color), Some(path)) = (style.background_color, background_path.as_ref()) {
@@ -7386,6 +7535,12 @@ fn paint_image(
if rect.width <= 0.0 || rect.height <= 0.0 {
return false;
}
+ // Image-bearing display lists currently use the proven CSS-pixel raster
+ // path (`native_raster_scale_supported` rejects them). Cull before cache
+ // lookup, SVG parsing, or bitmap resizing.
+ if !rect_intersects_paint_surface(visible_rect, pixmap, 1.0) {
+ return false;
+ }
let Some(bytes) = fetch_bytes(src, base_url, cache) else {
return false;
};
@@ -7581,6 +7736,72 @@ fn overflow_clip_mask(
Some(mask)
}
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+struct OverflowClipMaskKey {
+ bounds: [u32; 4],
+ rounded_chain: usize,
+ rounded_offset: [u32; 2],
+}
+
+type OverflowClipMaskCache = HashMap<
+ OverflowClipMaskKey,
+ (
+ Option<Arc<crate::dom::RoundedOverflowClipChain>>,
+ Arc<tiny_skia::Mask>,
+ ),
+>;
+
+const MAX_OVERFLOW_CLIP_MASK_CACHE_ENTRIES: usize = 2;
+
+fn overflow_clip_mask_key(
+ clip: &crate::dom::OverflowClip,
+ viewport: (f32, f32),
+) -> OverflowClipMaskKey {
+ let bounds = clip.viewport_rect(viewport);
+ let rounded_offset = clip.rounded_offset();
+ OverflowClipMaskKey {
+ bounds: [
+ bounds.x.to_bits(),
+ bounds.y.to_bits(),
+ bounds.width.to_bits(),
+ bounds.height.to_bits(),
+ ],
+ rounded_chain: clip
+ .rounded_chain()
+ .map_or(0, |chain| Arc::as_ptr(chain) as usize),
+ rounded_offset: [rounded_offset.0.to_bits(), rounded_offset.1.to_bits()],
+ }
+}
+
+fn cached_overflow_clip_mask(
+ cache: &mut OverflowClipMaskCache,
+ pw: u32,
+ ph: u32,
+ clip: &crate::dom::OverflowClip,
+ viewport: (f32, f32),
+) -> Option<Arc<tiny_skia::Mask>> {
+ let key = overflow_clip_mask_key(clip, viewport);
+ if let Some((_, mask)) = cache.get(&key) {
+ return Some(Arc::clone(mask));
+ }
+ let mask = Arc::new(overflow_clip_mask(pw, ph, clip, viewport)?);
+ // A mask is one byte per output pixel. Two entries keep the ubiquitous
+ // shared scrollport fast without turning a page with many distinct clips
+ // into an unbounded surface-sized memory cache. DOM traversal is locally
+ // ordered, so clearing at the bound retains the useful descendant burst.
+ if cache.len() >= MAX_OVERFLOW_CLIP_MASK_CACHE_ENTRIES {
+ cache.clear();
+ }
+ // `clip_scope_root` can synthesize a temporary rounded chain. Keeping its
+ // root Arc alongside the pointer-based key prevents allocator reuse from
+ // making a later, different chain alias this entry during the same paint.
+ cache.insert(
+ key,
+ (clip.rounded_chain().cloned(), Arc::clone(&mask)),
+ );
+ Some(mask)
+}
+
fn intersect_clip_masks(
mut first: Option<tiny_skia::Mask>,
second: Option<&tiny_skia::Mask>,
@@ -8400,6 +8621,11 @@ fn paint_mask(
if rect.width <= 0.0 || rect.height <= 0.0 {
return false;
}
+ // Masks likewise force the CSS-pixel raster path. Avoid decoding and the
+ // O(box area) recolor loop when their destination misses this capture.
+ if !rect_intersects_paint_surface(rect, pixmap, 1.0) {
+ return false;
+ }
let Some(bytes) = fetch_bytes(src, base_url, cache) else {
return false;
};
@@ -12213,4 +12439,177 @@ mod tests {
image::imageops::resize(&source, 60, 60, image::imageops::FilterType::Lanczos3);
assert_eq!(two_x.data(), expected.as_raw());
}
+
+ #[test]
+ fn paint_surface_culling_uses_css_coordinates_and_ink_overflow() {
+ let pixmap = Pixmap::new(200, 160).expect("surface");
+ assert!(rect_intersects_paint_surface(
+ &crate::Rect {
+ x: 90.0,
+ y: 70.0,
+ width: 20.0,
+ height: 20.0,
+ },
+ &pixmap,
+ 2.0,
+ ));
+ assert!(!rect_intersects_paint_surface(
+ &crate::Rect {
+ x: 0.0,
+ y: 81.0,
+ width: 20.0,
+ height: 20.0,
+ },
+ &pixmap,
+ 2.0,
+ ));
+
+ let mut style = crate::LayoutStyle::default();
+ style.box_shadow = Some(crate::BoxShadow {
+ offset_x: -24.0,
+ offset_y: 0.0,
+ blur: 8.0,
+ spread: 2.0,
+ color: [0, 0, 0, 255],
+ inset: false,
+ });
+ style.outline.style = crate::BorderStyle::Solid;
+ style.outline.specified_width = 4.0;
+ style.outline.offset = 3.0;
+ let ink = non_text_ink_bounds(
+ &crate::Rect {
+ x: 110.0,
+ y: 10.0,
+ width: 10.0,
+ height: 10.0,
+ },
+ &style,
+ );
+ assert_eq!(ink.x, 76.0, "outset shadow must widen the cull rect");
+ assert_eq!(ink.y, 0.0, "shadow blur must widen the cull rect vertically");
+ assert_eq!(ink.x + ink.width, 127.0);
+ }
+
+ #[test]
+ fn offscreen_overflow_clip_suppresses_offset_shadow_and_outline_ink() {
+ let paint = |overflow: &str| {
+ let html = format!(
+ r#"<html style="margin:0"><body style="margin:0">
+ <div style="position:absolute;left:200px;top:0;width:100px;height:120px;
+ overflow:{overflow}">
+ <div style="position:absolute;left:50px;top:20px;width:20px;height:20px;
+ box-shadow:-210px 0 0 0 black"></div>
+ <div style="position:absolute;left:50px;top:75px;width:20px;height:20px;
+ outline:210px solid red"></div>
+ </div>
+ </body></html>"#
+ );
+ let tree = parse_html(&html);
+ paint_dom(&tree, (100.0, 120.0), None).expect("offscreen ink")
+ };
+
+ let unclipped = paint("visible");
+ for (x, y, label) in [(45, 25, "shadow"), (45, 85, "outline")] {
+ let pixel = unclipped.pixel(x, y).expect(label);
+ assert_ne!(
+ (pixel.red(), pixel.green(), pixel.blue()),
+ (255, 255, 255),
+ "control must place {label} ink at the regression sample"
+ );
+ }
+
+ let pixmap = paint("hidden");
+ for (x, y, label) in [(45, 25, "shadow"), (45, 85, "outline")] {
+ let pixel = pixmap.pixel(x, y).expect(label);
+ assert_eq!(
+ (pixel.red(), pixel.green(), pixel.blue(), pixel.alpha()),
+ (255, 255, 255, 255),
+ "offscreen ancestor overflow clip must suppress {label}: {pixel:?}"
+ );
+ }
+ }
+
+ #[test]
+ fn transformed_outline_survives_tight_atomic_source_bounds() {
+ let tree = parse_html(
+ r#"<html style="margin:0"><body style="margin:0">
+ <div style="position:absolute;left:40px;top:40px;width:20px;height:20px;
+ outline:10px solid red;transform-origin:0 0;transform:scale(2)">
+ </div>
+ </body></html>"#,
+ );
+ let pixmap = paint_dom(&tree, (120.0, 120.0), None).expect("transformed outline");
+ let outer_outline = pixmap.pixel(25, 50).expect("transformed outer outline");
+ assert!(
+ outer_outline.red() > 220
+ && outer_outline.green() < 40
+ && outer_outline.blue() < 40,
+ "outline ink outside the border-box source bounds must survive the transform: {outer_outline:?}"
+ );
+ }
+
+ #[test]
+ fn offscreen_image_is_rejected_before_resource_lookup_and_decode() {
+ let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let loader_calls = Arc::clone(&calls);
+ let mut resources = RenderResourceCache::with_loader(move |_url: &str| {
+ loader_calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ Some(vec![0; 1024])
+ });
+ let mut pixmap = Pixmap::new(100, 100).expect("surface");
+ let rect = crate::Rect {
+ x: 0.0,
+ y: 10_000.0,
+ width: 80.0,
+ height: 80.0,
+ };
+ assert!(!paint_image(
+ "https://example.test/offscreen.svg",
+ None,
+ &rect,
+ &rect,
+ crate::ObjectFit::Fill,
+ &mut pixmap,
+ &mut resources,
+ None,
+ crate::ResolvedBorderRadii::default(),
+ None,
+ ));
+ assert_eq!(
+ calls.load(std::sync::atomic::Ordering::SeqCst),
+ 0,
+ "offscreen culling must happen before any resource work"
+ );
+ }
+
+ #[test]
+ fn viewport_cull_uses_post_translate_fixed_and_sticky_coordinates() {
+ let tree = parse_html(
+ r#"<html style="margin:0"><body style="margin:0;height:1400px">
+ <div style="position:absolute;left:10px;top:1000px;width:20px;height:20px;
+ transform:translateY(-990px);background:red"></div>
+ <div style="position:fixed;left:40px;top:10px;width:20px;height:20px;
+ background:lime"></div>
+ <div style="height:1000px"></div>
+ <div style="position:sticky;left:70px;top:10px;width:20px;height:20px;
+ background:blue"></div>
+ </body></html>"#,
+ );
+ let mut resources = RenderResourceCache::with_loader(|_url: &str| None);
+ let mut prepared =
+ prepare_dom(&tree, (120.0, 80.0), None, &mut resources).expect("prepared render");
+ let top = prepared.resolve_scroll_state(&tree, (0.0, 0.0), &HashMap::new());
+ let top_pixmap = paint_prepared_with_scroll(&tree, &mut prepared, &mut resources, &top)
+ .expect("top paint");
+ let translated = top_pixmap.pixel(15, 15).expect("translated pixel");
+ assert!(translated.red() > 240 && translated.green() < 20);
+
+ let scroll = prepared.resolve_scroll_state(&tree, (0.0, 990.0), &HashMap::new());
+ let pixmap = paint_prepared_with_scroll(&tree, &mut prepared, &mut resources, &scroll)
+ .expect("scrolled paint");
+ let fixed = pixmap.pixel(45, 15).expect("fixed pixel");
+ assert!(fixed.green() > 240 && fixed.red() < 20);
+ let sticky = pixmap.pixel(75, 15).expect("sticky pixel");
+ assert!(sticky.blue() > 240 && sticky.red() < 20);
+ }
}
diff --git a/crates/obscura/tests/image_shim.rs b/crates/obscura/tests/image_shim.rs
index 5c7b14e2..e7540178 100644
--- a/crates/obscura/tests/image_shim.rs
+++ b/crates/obscura/tests/image_shim.rs
@@ -8,9 +8,15 @@ use std::io::{Read, Write};
use obscura::Browser;
-/// Minimal HTTP/1.1 server returning HTML that wraps document.createElement to
-/// plant a non-configurable `src` on every <img>, then calls `new Image()`.
+/// Minimal HTTP/1.1 server returning the test page and a valid 1x1 PNG.
fn spawn_server(html: &'static str) -> String {
+ const PIXEL_PNG: &[u8] = &[
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
+ 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
+ 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78,
+ 0xda, 0x63, 0xfc, 0xcf, 0xc0, 0x50, 0x0f, 0x00, 0x05, 0x83, 0x02, 0x7f, 0x94, 0xff,
+ 0x2f, 0x59, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
+ ];
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
std::thread::spawn(move || {
@@ -20,13 +26,19 @@ fn spawn_server(html: &'static str) -> String {
Err(_) => continue,
};
let mut buf = [0u8; 2048];
- let _ = s.read(&mut buf);
- let resp = format!(
- "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
- html.len(),
- html
+ let read = s.read(&mut buf).unwrap_or(0);
+ let is_pixel = buf[..read].starts_with(b"GET /pixel.png ");
+ let (content_type, body) = if is_pixel {
+ ("image/png", PIXEL_PNG)
+ } else {
+ ("text/html", html.as_bytes())
+ };
+ let headers = format!(
+ "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
+ body.len()
);
- let _ = s.write_all(resp.as_bytes());
+ let _ = s.write_all(headers.as_bytes());
+ let _ = s.write_all(body);
let _ = s.shutdown(std::net::Shutdown::Both);
}
});
@@ -100,3 +112,39 @@ async fn new_image_still_emulates_load_when_src_is_configurable() {
"load emulation regressed for the normal (configurable src) path"
);
}
+
+#[tokio::test]
+async fn invalid_image_bytes_emit_error_like_chromium() {
+ std::env::set_var("OBSCURA_ALLOW_PRIVATE_NETWORK", "1");
+ let base = spawn_server(
+ r#"<!doctype html><html><body><div id="r">waiting</div>
+<script>
+ var img = new Image();
+ img.onerror = function () {
+ document.getElementById('r').textContent =
+ 'error complete=' + img.complete +
+ ' natural=' + img.naturalWidth + 'x' + img.naturalHeight;
+ };
+ img.src = '/broken.png';
+</script>
+</body></html>"#,
+ );
+
+ let browser = Browser::new().unwrap();
+ let mut page = browser.new_page().await.unwrap();
+ page.goto(&base).await.unwrap();
+ for _ in 0..10 {
+ page.settle(500).await;
+ let text = page.evaluate("document.getElementById('r').textContent");
+ if text.as_str().unwrap_or("").starts_with("error") {
+ break;
+ }
+ }
+
+ let text = page.evaluate("document.getElementById('r').textContent");
+ assert_eq!(
+ text.as_str().unwrap_or(""),
+ "error complete=true natural=0x0",
+ "invalid image bytes must fail instead of being treated as a decoded image"
+ );
+}
diff --git a/render-repros/representative-suite/run.sh b/render-repros/representative-suite/run.sh
index 20bdb287..4dce64a6 100755
--- a/render-repros/representative-suite/run.sh
+++ b/render-repros/representative-suite/run.sh
@@ -13,6 +13,7 @@ usage() {
echo "SCROLL_Y is an integer CSS-pixel offset or 'bottom'." >&2
echo "Set CAPTURE_MODE=live to leave animations and runtime state unfrozen." >&2
echo "CAPTURE_MODE defaults to deterministic (Chromium animations sampled at T=0)." >&2
+ echo "Set SETTLE_MS=0 for latency runs; visual runs default to 3000." >&2
}
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
@@ -26,10 +27,15 @@ fi
OUT="$1"
CAPTURE_MODE="${CAPTURE_MODE:-deterministic}"
+SETTLE_MS="${SETTLE_MS:-3000}"
if [[ "$CAPTURE_MODE" != "deterministic" && "$CAPTURE_MODE" != "live" ]]; then
echo "CAPTURE_MODE must be 'deterministic' or 'live': $CAPTURE_MODE" >&2
exit 2
fi
+if [[ ! "$SETTLE_MS" =~ ^[0-9]+$ ]] || (( SETTLE_MS % 1000 != 0 )); then
+ echo "SETTLE_MS must be a non-negative whole number of seconds in milliseconds: $SETTLE_MS" >&2
+ exit 2
+fi
if [[ -e "$OUT" ]]; then
echo "output path already exists: $OUT" >&2
exit 2
@@ -46,7 +52,7 @@ args=(
--out "$OUT"
--width 1440
--height 1000
- --settle-ms 3000
+ --settle-ms "$SETTLE_MS"
--geometry-selector "header, nav, footer"
--geometry-selector "main, article"
--geometry-selector "section"
diff --git a/render-repros/representative-suite/test_suite.py b/render-repros/representative-suite/test_suite.py
index 58d87708..5d624101 100644
--- a/render-repros/representative-suite/test_suite.py
+++ b/render-repros/representative-suite/test_suite.py
@@ -129,6 +129,22 @@ class RepresentativeSuiteTests(unittest.TestCase):
self.assertEqual(result.returncode, 0, result.stderr)
self.assertNotIn("--animation-time-ms", arguments)
+ def test_zero_settle_latency_mode_is_forwarded(self):
+ result, arguments, _, _ = self.run_wrapper(
+ extra_environment={"SETTLE_MS": "0"},
+ )
+ self.assertEqual(result.returncode, 0, result.stderr)
+ settle_index = arguments.index("--settle-ms")
+ self.assertEqual(arguments[settle_index + 1], "0")
+
+ def test_fractional_settle_is_rejected_before_capture(self):
+ result, arguments, _, _ = self.run_wrapper(
+ extra_environment={"SETTLE_MS": "1500"},
+ )
+ self.assertEqual(result.returncode, 2)
+ self.assertEqual(arguments, [])
+ self.assertIn("SETTLE_MS must be", result.stderr)
+
def test_invalid_capture_mode_is_rejected_before_capture(self):
result, arguments, _, _ = self.run_wrapper(
extra_environment={"CAPTURE_MODE": "almost-live"},