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

[FEATURE] Support CSS custom properties #1336

Merged
merged 6 commits into from
Sep 25, 2024
Merged

Conversation

JakeQZ
Copy link
Contributor

@JakeQZ JakeQZ commented Sep 23, 2024

Add a new HtmlProcessor class that can evaluate CSS custom properties (variables) after CSS which uses them has been inlined.

Resolves #1276, though an additional feature to remove unused variable definitions after evaluation could be provided.

@JakeQZ JakeQZ added this to the 8.0.0 milestone Sep 23, 2024
@JakeQZ JakeQZ self-assigned this Sep 23, 2024
@JakeQZ JakeQZ force-pushed the feature/css-variables branch 2 times, most recently from 7ab14cc to 9a010a3 Compare September 23, 2024 18:13
@coveralls
Copy link

coveralls commented Sep 23, 2024

Coverage Status

coverage: 97.062% (+0.2%) from 96.841%
when pulling 1569e42 on feature/css-variables
into 634155c on main.

@JakeQZ JakeQZ marked this pull request as draft September 23, 2024 18:16
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 23, 2024

This uses some DOMElement properties not available in PHP 7, so will need some changes there.

@JakeQZ JakeQZ marked this pull request as ready for review September 23, 2024 21:28
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 23, 2024

This uses some DOMElement properties not available in PHP 7, so will need some changes there.

Now fixed (as a separate commit, so it can still be seen what the PHP 8 code would look like).

JakeQZ and others added 4 commits September 24, 2024 09:19
Add a new `HtmlProcessor` class that can evaluate CSS custom properties
(variables) after CSS which uses them has been inlined.

Resolves #1276, though an additional feature to remove unused variable
definitions after evaluation could be provided.
Copy link
Contributor

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

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

Amazing work! I've added some comments, both regarding the general architecture as well as some nitpicks.

I've also rebased the branch and autoformatted the README. So you'll probably need to hard-reset your local branch to the remote one (or delete the local copy and then use the remote one).

/**
* @param array<non-empty-string, string> $declarations
*
* @return array<non-empty-string, string>|false `false` is returned if no substitutions were made.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to return null instead of false? Then we can make this a nullable type instead of a union type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, changed.

src/HtmlProcessor/CssVariableEvaluator.php Outdated Show resolved Hide resolved
tests/Unit/HtmlProcessor/CssVariableEvaluatorTest.php Outdated Show resolved Hide resolved
src/HtmlProcessor/CssVariableEvaluator.php Show resolved Hide resolved
src/HtmlProcessor/CssVariableEvaluator.php Show resolved Hide resolved
src/HtmlProcessor/CssVariableEvaluator.php Show resolved Hide resolved
{
$substitutionsMade = false;
$result = \array_map(
function (string $propertyValue) use (&$substitutionsMade): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Using all these used array functions feels to me like we maybe could use another class structure to have more type safety and to make the code more communicative. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean or are suggesting. The method performs a transformation on each element of an array, for which array_map does the job. The reason to keep track of whether this transformation made any changes is an optimization. PHP's copy-on-write handling of arrays helps, but if a change is made, comparing $newArray !== $oldArray means checking each element (until the difference is found). (If the array is unmodified, the test is instant, since internally they'll still be referencing the same array.)

@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 24, 2024

autoformatted the README.

Many of the other code examples (PHP, CSS or HTML) in the README still have only two spaces indentation. (Those that are CSS or HTML are in bullet-points, though the PHP ones are also at main-content level.)

I've spotted I mistake in the sample HTML I added, which I'll fix.

Copy link
Contributor

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

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

🎉

@oliverklee oliverklee merged commit 9c12961 into main Sep 25, 2024
24 checks passed
@oliverklee oliverklee deleted the feature/css-variables branch September 25, 2024 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support to convert CSS Variables?
3 participants