amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Validator: inline css custom variable

Open xettri opened this issue 7 years ago • 22 comments

There is a bug during inline style

<style amp-custom>
  .test{
    color: var(--clr);
  }
</style>
<div class="test" style="--clr: #f00"></div>

I feel this kind of code is not invalid as per css and html rules

xettri avatar Aug 28 '19 10:08 xettri

What is the bug you are encountering? Is this failing to validate with the AMP validator?

sparhami avatar Aug 29 '19 18:08 sparhami

/to @Gregable

jridgewell avatar Aug 29 '19 18:08 jridgewell

Inline styles has a whitelisted set of css properties. Using style="--clr: #f00" does cause a validation issue due to this (The property '--clr' in attribute 'style' in tag 'div' is disallowed).

We may want to consider adding additional properties.

honeybadgerdontcare avatar Aug 29 '19 18:08 honeybadgerdontcare

Why make CSS variables subject to a whitelist? It's really useful to be able to apply CSS variables at the inline level (for example, for dynamic content / background images) and then use them elsewhere in your styles.

nightpool avatar Oct 25 '19 14:10 nightpool

Quick follow up to describe what's going on here currently in the validator for future reference.

Validator rules for <style amp-custom> were added much earlier in the validator lifetime than the rules for style= attributes. When style= attributes were later added, we introduced new constraints for CSS in this scope. These constraints we'd like to apply to <style amp-custom> as well, but are limited in doing so due to backwards compatibility.

One of those new constraints is an allow-list of supported CSS declaration names. For example foo: bar is allowed inside a rule in <style amp-custom> for backwards-compatibility, but not inside style= attributes.

The validator sees --clr: #foo as a css declaration name that isn't defined in the allow-list. In neither context is the validator aware that this is a CSS variable.

It shouldn't be too difficult to add support for variable declarations, it would simply require adding support for arbitrary declarations starting with -- prefix. That said, I imagine that it's uncommon for someone to define variables inside a style attribute, instead of a style tag, so I consider this issue of lowish priority.

Gregable avatar Mar 13 '20 21:03 Gregable

When we do get around to this, please reserve the prefix --i-amphtml for our use.

jridgewell avatar Mar 16 '20 21:03 jridgewell

@jridgewell, you want to reserve it as a variable name prefix?

Gregable avatar Mar 18 '20 20:03 Gregable

@Gregable yes we should. Anything that could be named within html/css should automatically have i-amphtml reserved.

honeybadgerdontcare avatar Mar 18 '20 20:03 honeybadgerdontcare

@jridgewell is that reserve prefix mean this??

.test{
    color: var(--i-amphtml-clr);
    font-size : var(--i-amphtml-fontsize);
    margin : var(--i-amphtml-margin);
}
<div class="test" style="--i-amphtml-clr: #f00; --i-amphtml-fontsize:20px; --i-amphtml-margin:50px">
   i am groot
</div>

xettri avatar Mar 19 '20 10:03 xettri

@bcrazydreamer , My understanding of @jridgewell 's comment is that the validator wouldn't allow the user style (either <style amp-custom> or style=) to use variables with the prefix --i-amphtml, so your examples would not be allowed, but if you used --i-foo-*, they would.

Gregable avatar Mar 19 '20 15:03 Gregable

@Gregable

.test{
    color: var(--i-foo-clr);
    font-size : var(--i-foo-fontsize);
    margin : var(--i-foo-margin);
}
<div class="test" style="--i-foo-clr: #f00; --i-foo-fontsize:20px; --i-foo-margin:50px">
   i am groot
</div>

its also invalid case by validator but valid by CSS and HTML should be handled by validator #[Category: Validator]

xettri avatar Mar 19 '20 16:03 xettri

Yes. We'll make --i-foo and --i-foo-x valid, but --i-amphtml and --i-amphtml-x will be invalid.

jridgewell avatar Mar 19 '20 22:03 jridgewell

What is the time horizon for changing the validator rules? I have integrated an editor for AMP stories based on the CSS variables into a CMS. In the end I had to find out that these are not valid AMP stories :-(

itsmsc avatar May 20 '20 07:05 itsmsc

/cc @mszylkowski this is the issue for using custom CSS properties in inline styles

For context, this would potentially be very useful for stories, as we produce several AMP components that are in shadow DOM, with custom properties for configuration

newmuis avatar Aug 12 '20 16:08 newmuis

This is a very significant omission, and I really hope you can prioritise it soon.

That said, I imagine that it's uncommon for someone to define variables inside a style attribute, instead of a style tag, so I consider this issue of lowish priority.

@Gregable this is not true at all; being able to control values per-element (or per-subtree) is one of the main reasons to use CSS custom properties!

Rich-Harris avatar Dec 15 '20 20:12 Rich-Harris

We're tracking this and seeing which AMP formats may be interested. b/177269431.

I'd appreciate any feedback around restricting the naming of custom properties. Would a regex of --[\w]?[\w-]* be sufficient for most use cases?

honeybadgerdontcare avatar Jan 12 '21 00:01 honeybadgerdontcare

It’s unclear to me reading this issue why there is any whitelist at all? Why not just remove it completely?

holm avatar Jan 12 '21 06:01 holm

@holm for security / privacy requirements of AMP documents.

honeybadgerdontcare avatar Jan 12 '21 06:01 honeybadgerdontcare

@honeybadgerdontcare: --[\w]?[\w-]* seems odd. --[\w][\w-]* should capture every valid case, and excludes -- which is invalid. (And we need to exclude --i-amphtml*, so:

--(?!i-amphtml)[\w][\w-]*

jridgewell avatar Jan 20 '21 22:01 jridgewell

We've gone a slightly different route but it will enable custom properties for inline styles. It should be going live middle of next week. cl/355888443

honeybadgerdontcare avatar Feb 05 '21 19:02 honeybadgerdontcare