Skip to content

Discard invalid color declarations instead of dropping the property (…#1083

Open
StefanoD wants to merge 1 commit into
linebender:mainfrom
StefanoD:fix/issue-914-invalid-color-declaration
Open

Discard invalid color declarations instead of dropping the property (…#1083
StefanoD wants to merge 1 commit into
linebender:mainfrom
StefanoD:fix/issue-914-invalid-color-declaration

Conversation

@StefanoD

Copy link
Copy Markdown

Fixes #914.

Figma exports SVGs with a dual stroke definition: a valid hex fallback
followed by a wide-gamut value, e.g.

style="stroke:#FAFAFA;stroke:color(display-p3 0.9804 0.9804 0.9804);stroke-opacity:1;"

When splitting a style attribute (or applying a CSS rule), the last
declaration for a property always won, regardless of validity. The
unsupported color(display-p3 ...) value overwrote the valid #FAFAFA
one, and since stroke parsing later falls back to "no stroke", the
element was rendered invisibly.

Per CSS error recovery, a declaration with an invalid value must be
discarded so a previously declared valid value (or the presentation
attribute) is kept. This PR validates the color-bearing presentation
properties (fill, stroke, color, flood-color, lighting-color,
stop-color) before inserting them and skips invalid declarations.

Behavior for a single invalid declaration is unchanged (stroke → none,
fill → default/inherited).

Testing

  • Added regression test invalid_style_declaration_falls_back_to_valid_one
    (fails before, passes after).
  • Full usvg test suite passes; cargo build clean with no new clippy warnings.

Generated by Claude


Note: I accidentally closed the original PR (#1065) by deleting my fork, which auto-closed it. This reopens the same change — the branch and commits are unchanged.

…inebender#914)

Figma exports SVGs with a dual stroke definition: a valid hex fallback
followed by a wide-gamut value, e.g.

    style="stroke:#FAFAFA;stroke:color(display-p3 0.9804 0.9804 0.9804);"

When splitting a `style` attribute (or applying a CSS rule), the last
declaration for a property always won, regardless of validity. The
unsupported `color(display-p3 ...)` value thus overwrote the valid
`#FAFAFA` one, and since stroke parsing later fell back to "no stroke",
the element was rendered invisibly.

Per CSS error recovery, a declaration with an invalid value must be
discarded so a previously declared valid value (or the presentation
attribute) is kept. We validate the color-bearing presentation
properties (`fill`, `stroke`, `color`, `flood-color`, `lighting-color`,
`stop-color`) and skip an invalid declaration, but only when it would
override an existing attribute. A lone invalid value keeps the previous
behavior (fallback at conversion), so the common, conflict-free path
incurs no extra parse.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SVG with both hex colored stroke and display-p3 stroke color is rendered with no stroke

1 participant