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

Fixed hugo:vars casting #10718

Merged
merged 2 commits into from
Feb 22, 2023
Merged

Fixed hugo:vars casting #10718

merged 2 commits into from
Feb 22, 2023

Conversation

acclassic
Copy link
Contributor

Hey there

This one took me a bit to figure out. I thought the problem was the Sass Compiler or the godartsass package but it turned out to be a problem on how we created the varsstylesheet map that is then passed to the compiler to import. All variables were quoted and therefore sass would interpret them as string. Now we pass some of these variables without quotes so that the sass compiler can do the correct type casting.
In the cssValues.go file I added all the identifier for the different css values. Now we can use them in functions and even pass functions like calc(), rgb(), etc.
I added a new test to check the type casting. All tests are passed other than the TestTransformErrors() function. I checked this with a clean clone of hugo and the error is still present there.

Fixes #10632

@bep
Copy link
Member

bep commented Feb 13, 2023

Thanks for this. I don't doubt that you describe a real problem, but I'm a little fearful about pulling parts of the Sass spec into Hugo (how do we know it's complete and how do we maintain it?)

I was rather hoping that Sass' unquote function would work as advertised.

  • I'm pretty sure I have tested the simple cases and that unquote("24px") gets converted correctly.
  • Is this PR covering the more complex cases, e.g. rgb(255, 255, 255)?

@acclassic
Copy link
Contributor Author

You're welcome.

I gave it another look and you are not completely wrong either. It seems like that all the variables passed to the Sass compiler are passed as string and interpreted like that even pixel values or numbers. So when using Sass specific functions like darken(), etc. it will throw an error because the variable is of type string. When the Sass file then is converted to Css the values will be casted with the correct type. That is why you can use them in Css functions like calc().
I created a showcase for this here https://github.com/acclassic/hugo-issue-10632-test. If you run hugo with the -v flag the debug function will showcase how Sass interprets the variables.

Your fear about implementing this directly in hugo is justified. Just as a clarification the specs come directly from Css and not Sass. Basically I took all the color specs from here https://developer.mozilla.org/en-US/docs/Web/CSS/color_value other than the experimental one. And all the identifiers for dimensions from here https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units#dimensions. This should only change when new features are added directly to Css.

Whit this PR you are also able to pass rgb(), hsl() and hwb() and use them in Sass functions. You could even pass a calc(x + y) directly from the config file if you wish. I added this so it would cover everything.

@bep
Copy link
Member

bep commented Feb 13, 2023

Thanks. I will take another look at this tomorrow -- as long as this is well defined and pretty stable, I guess it's something we can live with.

@acclassic
Copy link
Contributor Author

You're welcome.
Sure, if you want me to change something, just let me know.

Variables passed via the hugo:vars function where passed as type string.
This caused problems when using the variables in sass functions because
these expect a specific type. Now we check if the passed variables have
to be quoted and therefore are of type string or if they should not be
quoted and let the type interpretation up to the sass compiler.

Fixes gohugoio#10632
@bep bep force-pushed the hugo-issue-10632 branch from 4556a8a to 5ae57fd Compare February 21, 2023 17:11
@bep bep self-assigned this Feb 21, 2023
@bep bep added this to the v0.111.0 milestone Feb 21, 2023
Instead of maintaing a list of all CSS units and functions this commit:

* Uses 3 regexps to detect typed CSS values (e.g. `24px`) + properly handle numeric Go types.
* These regexps may have some false positives -- e.g. strings that needs to be quoted.
* For that rare case, you can mark the string with e.g. `"32xxx" | css.Quoted`
* For the opposite case:  `"32" | css.Unquoted`

Updates gohugoio#10632
@bep bep force-pushed the hugo-issue-10632 branch from b872d71 to 3db1a6d Compare February 22, 2023 11:21
@bep bep merged commit ecf3cd5 into gohugoio:master Feb 22, 2023
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants