Parse percentage and invalid *-opacity values without crashing (#422)#453
Open
apoorvdarshan wants to merge 1 commit into
Open
Parse percentage and invalid *-opacity values without crashing (#422)#453apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
opacity, stroke-opacity and fill-opacity were read with a raw float(), so a percentage such as '50%' or a non-numeric value such as 'null' raised ValueError and aborted the whole render. Route the three reads through a new helpers.parse_opacity() that accepts a number or a percentage, clamps the result to [0, 1] per the SVG spec, and falls back to the default for unparseable values. Fixes Kozea#422
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #422
Root cause
opacity,stroke-opacityandfill-opacityare read insurface.pywith araw
float():Any value that is not a plain number makes
float()raiseValueError, whichpropagates out of
draw()and aborts the whole render. This covers both thepercentage syntax allowed for these properties (e.g.
50%) and non-numericvalues such as the
nullreported in the issue.Reproduction
Observed on
main(identical forfill-opacity="null",opacity="50%"andstroke-opacity="50%"):With this change the same input renders normally, and
fill-opacity="50%"produces byte-for-byte the same PNG as
fill-opacity="0.5".Fix
Add a small
helpers.parse_opacity()that accepts a number or a percentage,clamps the result to
[0, 1](as the SVG spec requires for opacity values),and falls back to the default for anything it cannot parse — so an invalid
*-opacityattribute never aborts the render. The three reads insurface.pynow go through it. The change is localized to the opacity parsing and does not
touch any other behaviour; valid numeric opacities keep rendering exactly as
before.
Tests
Added
test_opacity_percentageandtest_opacity_invalidincairosvg/test_api.py(parametrized overopacity,fill-opacityandstroke-opacity). They assert that a percentage renders identically to theequivalent fraction and that an invalid value falls back to opaque instead of
crashing. Both fail on the current code with the
ValueErrorabove and passwith this change. The full
cairosvg/test_api.pysuite passes (13 tests), andflake8/isortare clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally.