csso icon indicating copy to clipboard operation
csso copied to clipboard

Allow to enforce applying optimisations to custom property values

Open ygoe opened this issue 3 years ago • 8 comments

When a CSS declaration contains multiple spaces/tabs or even a line break, all of those is preserved in a function context (like calc()). I believe these could be reduced to a single space (if not removed entirely, I'm not sure about that, sometimes CSS requires spaces between operators/parameters).

Usually the minified file contains only a single line, but with such source code (writted for readability), all the line breaks in the functions are kept and the minified file contains all the line breaks including the following indentation.

ygoe avatar May 07 '22 14:05 ygoe

@ygoe Could you please provide an example? The single case I can imaging is a value of custom property.

lahmatiy avatar May 07 '22 15:05 lahmatiy

Indeed, I observed this with a custom property:

.something
{
  --link-hover-color-l: calc(var(--link-color-l)
  	+ max(0%, (100% - var(--link-color-l)) * var(--link-hover-lf))
  	- max(0%, var(--link-color-l) * (0 - (var(--link-hover-lf)))));
}

ygoe avatar May 07 '22 21:05 ygoe

Custom properties can contain any content, that's not a regular value but a part of it when substituted. Removing anything from a custom property may break something. So CSSO do nothing with such values for now. Probably we need to add an option to enforce optimisation for such value in case you are sure it doesn't break anything in your case.

lahmatiy avatar Jul 14 '22 09:07 lahmatiy

Custom properties currently get trimmed, which creates a different output for development/production modes:


.c {
  --x: aaa bbb ; 
  --y: zzz ; 
}
/* OUTPUT: removed whitespace before and after the custom properties values */
.c{--x:aaa bbb;--y:zzz}

Edit: Although this is the opposite of what the author is requesting, I think that the default behavior should preserve the value as is, untrimmed, and an option could be added to select between preserve/trim/min-whitespace

idoros avatar Oct 11 '22 13:10 idoros

What's the difference of these additional spaces? I'm not aware of any situation where it's relevant.

ygoe avatar Oct 12 '22 08:10 ygoe

getComputedStyle(node).getPropertyValue('--x') returns the untrimmed value

idoros avatar Oct 12 '22 09:10 idoros

I still don't need those extra spaces and cannot imagine a scenario where I would ever need it. CSS usually doesn't preserve such spaces and any application that now does is highly unexpected and probably bad design.

ygoe avatar Oct 12 '22 09:10 ygoe

I take it back, looking at the csswg it seems that custom properties should be trimmed. Browser implementation currently fail in my case.

idoros avatar Oct 12 '22 11:10 idoros