Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont crash due to invalid var() values #977

Closed
wants to merge 7 commits into from

Conversation

Tontyna
Copy link
Contributor

@Tontyna Tontyna commented Oct 23, 2019

When a css property is defined via the var() function and the variable contains an invalid value a WARNING is generated and either an initial or the parent-value is assigned.

In most cases the fallback is an appropriate value and computation succeeds without further intervention, but for some properties this is not the case. We could adjust css.properties.INITIAL_VALUES, but I suppose there are reasons for the improper values, probably they are required elsewhere.

That means we must take care for those special fallback values in the corresponding @register_computer-function. Like in border_width() -- this function pays attention since fadb60c, 8 years ago.

The css validation functions raise `InvalidValues`. Basic CSS validation, done
in Step 2, catches and emits the exception as a WARNING.

In Step 3, when a var() needs to be resolved, those validation functions are
called again. We must catch the `InvalidValues` in compute(), too.

Currently only the `transform` property seems to be endangered.
Both, parent_style['font_size'] and INITIAL_VALUES['font_size'], are unitless
integers.
@Tontyna
Copy link
Contributor Author

Tontyna commented Oct 23, 2019

af78171 is related to #974: @sureshvv's cutestrap style sheet won't crash any more.
Though it won't render as expected since the css is calc()ulating a lot and WeasyPrint doesn't (yet) support calc.

Both, parent_style['word_spacing'] and INITIAL_VALUES['word_spacing'], are
unitless integers.
Fallbacks for border-spacing and size contain unitless numbers
The initial values 'none' and 'normal' of string-set resp. content crash with
`UnboundLocalError` in `_content_list` unless deterred.
The INITIAL_VALUES for anchor, link and lang (oops, css properties!?) is None.

If the fallback value is None there is no use in calling the @registered
function. Just return None.
@Tontyna
Copy link
Contributor Author

Tontyna commented Oct 23, 2019

Now all previously crashing fallback values -- see the list in #974 (comment) -- don't crash anymore.

@Tontyna
Copy link
Contributor Author

Tontyna commented Oct 23, 2019

Was tempted to pass those insane fallback values through the proven validation functions of Step 2 and apply the @register_computer-function afterwards. But my first candidate was font-size ... and I had to abandon the good lazy idea.

Nevertheless it's nagging my brain that the validation must be done twofold. Yes, I know: specified and computed are different entities, but nevertheless...

@Tontyna
Copy link
Contributor Author

Tontyna commented Oct 23, 2019

WOW! My first unit-test! There you go 😏

Since all KNOWN_PROPERTIES are tested with an improper value, this should be future-proof.

Be aware that if the INITIAL_VALUES are altered (by adding new properties or changing a default value) you mustn't only adapt the basic validation functions for Step 2 but also prepare the corresponding @register_computer-function for those 'raw' default values.

liZe added a commit that referenced this pull request Dec 19, 2019
Validators return None when value is incorrect, let’s use the same behaviour
for transform.

Related to #974, #977.
@liZe
Copy link
Member

liZe commented Dec 19, 2019

Shamefully forgotten by my tired eyes, half cherry-picked and half replaced by a1df1de, largely discussed in #974, delicately enlightened by @Tontyna’s first unit test…

This pull request has had an incredible life.

@liZe liZe closed this Dec 19, 2019
@Tontyna Tontyna deleted the var_crash branch December 19, 2019 11:28
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.

2 participants