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

dx(runtime-dom): warn when a style value ends in a semicolon #7062

Merged
merged 1 commit into from
Nov 9, 2022

Conversation

skirtles-code
Copy link
Contributor

This adds a warning when an object style value ends with a semicolon. e.g. :style="{ color: 'red;' }".

When a value ends with a semicolon, the entire value gets discarded, with no apparent error or warning from the browser. If the style already has a previous value then that will be retained, leading to reports of 'reactivity' issues.

Typically when this has been reported on Vue Land it has been for complex values, such a gradients. Consider this example:

SFC Playground

I've already given away the punchline, the problem is the semicolon, but if you didn't already know, would that have been obvious? It might have been a misuse of .value, or a reactivity problem, or a mistake somewhere in the linear-gradient() syntax, or who knows what else?

Compounding the problem, if you tweak the code above to use :style="`background: ${background}`" it works fine. The semicolon is just treated as a delimiter and does no harm. This leads to the impression that it's a problem with Vue not handling the style object correctly.

We may not be able to fully validate all style values, but I think eliminating the semicolon problem would save a bit of frustration.

Browsers do allow semicolons at the end of style values, but as far as I'm aware this can only happen if they're escaped with a \. In practice I can't think of a legitimate use case for that, but I used custom properties to confirm that an escaped semicolon is retained, whereas an unescaped semicolon leads to the value being ignored. The RegExp in this PR will not match escaped semicolons.

Copy link
Member

@sxzz sxzz left a comment

Choose a reason for hiding this comment

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

LGTM

@yyx990803 yyx990803 merged commit 9a816dc into vuejs:main Nov 9, 2022
chrislone pushed a commit to chrislone/core that referenced this pull request Feb 4, 2023
zhangzhonghe pushed a commit to zhangzhonghe/core that referenced this pull request Apr 12, 2023
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.

3 participants