Skip to content

[css-variables] Custom properties should *not* serialize "exactly as specified" #7329

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

Open
Loirooriol opened this issue May 29, 2022 · 2 comments
Labels
css-variables-1 Current Work

Comments

@Loirooriol
Copy link
Contributor

Consider this code:

var s = document.body.style;
s.setProperty("--a", "a(");
s.setProperty("--b", "b");

The spec says "Specified values of custom properties must be serialized exactly as specified by the author". That's what Blink does, but it's bad!

s.getPropertyValue("--a"); // "a("
s.getPropertyValue("--b"); // "b"
s.cssText; // "--a:a(; --b:b;"

s.cssText = s.cssText;

s.getPropertyValue("--a"); // "a(; --b:b;"
s.getPropertyValue("--b"); // ""
s.cssText; // "--a:a(; --b:b;;"

s.cssText = s.cssText;

s.getPropertyValue("--a"); // "a(; --b:b;;"
s.getPropertyValue("--b"); // ""
s.cssText; // "--a:a(; --b:b;;;"

s.cssText = s.cssText;

s.getPropertyValue("--a"); // "a(; --b:b;;;"
s.getPropertyValue("--b"); // ""
s.cssText; // "--a:a(; --b:b;;;;"

I think Firefox is better, since it round-trips:

s.getPropertyValue("--a"); // "a()"
s.getPropertyValue("--b"); // "b"
s.cssText; // "--a: a(); --b: b;"

s.cssText = s.cssText;

s.getPropertyValue("--a"); // "a()"
s.getPropertyValue("--b"); // "b"
s.cssText; // "--a: a(); --b: b;" 
@Loirooriol Loirooriol added the css-variables-1 Current Work label May 29, 2022
@andruud
Copy link
Member

andruud commented Jun 1, 2022

Good one. I've never liked that behavior. It's the only thing that behaves like that, and what actually happens when things are substituted isn't clear either.

exactly as specified by the author". That's what Blink does

Sidenote: No, Blink serializes tokens. So it has both the problem in this issue, and also it does the normalization stuff 4.1. Serializing Custom Properties wants to avoid. (Yay.)

@tabatkins
Copy link
Member

Yeah, we should close open constructs, since the parser doesn't see that the construct isn't properly ended in that case anyway.

The problem is that we do need to exactly preserve things like long strings of digits.

and what actually happens when things are substituted isn't clear either.

That actually is clear if you're following the spec - the substitution is done with tokens, not characters, and in this example the --a property contains a single <function-token>, so it'll substitute that. When you later serialize the substituted result you should get a(), since you can't tell whether or not the ending token was supplied to a function token.

(To what extent browsers exactly follow the spec here is, uh, up for discussion.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css-variables-1 Current Work
Projects
None yet
Development

No branches or pull requests

3 participants