Skip to content

blitz: responsive srcset/sizes selection and inline SVG stylesheet fill/stroke - #210

Open
BernardoPe wants to merge 5 commits into
bpereira/compat-blitz-layout-transformsfrom
bpereira/compat-svg-responsive-images
Open

BernardoPe wants to merge 5 commits into
bpereira/compat-blitz-layout-transformsfrom
bpereira/compat-svg-responsive-images

Conversation

@BernardoPe

Copy link
Copy Markdown
Collaborator

Part 5 of 5 in a stacked compatibility series (stacked on #209).

Changes

  • Responsive srcset/sizes and <picture><source media> (blitz-dom patch 0105): Evaluate sizes (defaulting to 100vw) against the viewport width and DPR, selecting the smallest width descriptor that covers source_size * dpr (or the largest available if none does), and skip <picture><source> elements whose media condition does not match the viewport (nasa.gov, docs.github.com).
  • Stylesheet fill/stroke on inline SVG descendants (blitz-dom patch 0106): Stamp non-initial computed fill and stroke onto SVG graphics and group descendants during usvg serialization via paint_attr so author CSS rules targeting SVG children reach usvg, while leaving initial values unstamped so explicit presentation attributes and inherited <svg fill> stay intact (mit.edu).

Verification

  • packages/engine/test/e2e/img-srcset-sizes.e2e.test.ts
  • packages/core/src/paint/tests/svg_mutation_resnapshot.rs (stylesheet_fill_and_stroke_reach_inline_svg_snapshot)

@BernardoPe
BernardoPe added this pull request to stack #211 November 23, 2026 12:25
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from 8f03280 to 996a5a3 Compare November 23, 2026 12:48
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from 996a5a3 to 0d9efa7 Compare November 23, 2026 13:43
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch 2 times, most recently from 01cad62 to b7b647b Compare November 23, 2026 14:13
@github-actions github-actions Bot added the vouch:trusted Author is vouched, a collaborator, or a bot. label Nov 24, 2026
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from b7b647b to a47553a Compare November 24, 2026 21:46
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from a47553a to 349fe1e Compare November 24, 2026 22:27
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from 349fe1e to 502d3e5 Compare November 24, 2026 22:50
+ && parent_styles
+ .as_ref()
+ .is_some_and(|ps| ps.clone_fill().kind == *fill_kind);
+ if !fill_is_initial && !fill_inherited {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beep-boop-rui-agent-🤖

Blocking: an inherited CSS fill still overrides a fill set on an ancestor <g> or <use>.

This fixes the case where the element has its own fill= attribute, but not the other half of the multi-colour icon case from the earlier thread.

When the element has no fill attribute, has_own_fill is false, so fill_inherited is false. The code then stamps the value the element inherited through Stylo. Stylo never sees the fill attribute on the <g> in between. The stamped attribute on the child beats inheritance in usvg, so the child paints the CSS colour instead of the <g> colour.

Two common patterns hit this:

  • Sketch-style exports: svg { fill: currentColor } with <g fill="none"><g fill="#FFF"><path/></g></g>. The path paints currentColor, not white.
  • Same-document sprites: <symbol> paths used through <use fill="red">. The DOM parent chain is symbol → defs → svg, so the path is stamped currentColor, not red.

On the base branch nothing is stamped, so both cases inherit the <g> or <use> colour, as in Chrome. stroke has the same problem (line 440).

Fix: stamp only where the computed value differs from the DOM parent's computed value, whether or not the element has its own attribute. Stamp the snapshot root too, so that direct children of a CSS-filled <svg> still get the CSS colour. Tests in svg_mutation_resnapshot.rs:

  • svg { fill: #dd0000 } with <g fill="#0000ff"><rect/></g> paints a blue rect.
  • The same with <g fill="none"> paints no fill.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aa36fa1c: updated blitz-dom patch 0112 (src/node/serialize.rs) to stamp computed fill/stroke on the root <svg> when non-initial and on descendants only when their computed paint (with currentColor resolved) differs from their DOM parent, so ancestor <g fill> / <use fill> attributes are preserved. Added tests for <g fill="#0000ff">, <g fill="none">, and <use fill="#0000ff"> in packages/core/src/paint/tests/svg_mutation_resnapshot.rs.

@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from 502d3e5 to aa36fa1 Compare November 25, 2026 22:16

@ruifigueira ruifigueira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beep-boop-rui-agent-🤖

The blocking findings are fixed. Approving.

@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch 2 times, most recently from abb73ba to a6e90cd Compare November 26, 2026 17:47
`select_image_url` ignored `sizes` and `<source media="...">`, always picking
the largest width descriptor regardless of the viewport or declared slot size
(causing oversized hero and article images on nasa.gov and docs.github.com).

Evaluate `sizes` (defaulting to `100vw`) against the viewport width and DPR,
picking the smallest width candidate that covers `source_size * dpr` (or the
largest available if none does), and skip `<picture><source>` elements whose
`media` condition does not match the viewport.

https://html.spec.whatwg.org/multipage/images.html#selecting-an-image-source-from-the-source-set
Signed-off-by: bpereira <bpereira@cloudflare.com>
usvg only sees the serialized SVG snapshot, never document stylesheets, so
author CSS rules targeting SVG children (such as `.logo path { fill: #a31f34 }`
for the MIT wordmark) were dropped and elements painted with default black fill
or no stroke.

Stamp non-initial computed `fill` and `stroke` onto SVG graphics and group
descendants during usvg serialization via `paint_attr`, leaving initial values
unstamped so explicit presentation attributes and inherited `<svg fill>` remain
intact.

https://svgwg.org/svg2-draft/styling.html#PresentationAttributes
Signed-off-by: bpereira <bpereira@cloudflare.com>
…ributes and honour viewport for responsive images

Skip stamping computed CSS fill/stroke onto inline SVG descendants when
the element carries its own attribute and its computed value matches its
parent's (so inherited rules like `svg { fill: currentColor }` no longer
overwrite `<path fill="none">` or explicit child colours), lazily
borrowing parent styles only when own fill/stroke attributes are present.

Pass the initial viewport dimensions into DomHandle::newUtf8 before HTML
parsing and track responsive_image_urls on BaseDocument so viewport width
changes re-select only responsive <img> candidates whose URL changed.

Cap calc() nesting depth in sizes/media parsing, inherit <img sizes> for
<picture><source> elements without sizes, and evaluate prefers-color-scheme,
orientation, height, and case-insensitive AND in media conditions.

Signed-off-by: bpereira <bpereira@cloudflare.com>
…iffer

Inherited CSS fill/stroke was still stamped on descendants without their
own attribute, overriding fill/stroke set on an ancestor <g> or <use>.
Stamp the snapshot root when non-initial and stamp descendants only when
the computed paint differs from the DOM parent's.

Signed-off-by: bpereira <bpereira@cloudflare.com>
…rentColor

Compute parent_styles for the root <svg> from its HTML parent so inherited
CSS fill/stroke does not override the root element's own presentation
attributes. Stamp literal currentColor on the root so usvg resolves it
against color= attributes on <svg> or descendant <g> elements.

Signed-off-by: bpereira <bpereira@cloudflare.com>
@BernardoPe
BernardoPe force-pushed the bpereira/compat-svg-responsive-images branch from a6e90cd to e5a99ae Compare November 27, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

vouch:trusted Author is vouched, a collaborator, or a bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants