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

[TASK] Use sabberworm/php-css-parser to parse the CSS #1015

Merged
merged 6 commits into from
Jun 12, 2021

Conversation

JakeQZ
Copy link
Contributor

@JakeQZ JakeQZ commented Apr 13, 2021

No description provided.

@JakeQZ JakeQZ self-assigned this Apr 13, 2021
@JakeQZ JakeQZ marked this pull request as draft April 13, 2021 23:42
@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch 5 times, most recently from fdcc73e to f51de35 Compare April 17, 2021 23:41
@oliverklee
Copy link
Contributor

sabberworm/php-css-parser has not been very well-maintained lately. We might need to help out there (or choose a different library).

@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from f51de35 to df8f1a6 Compare April 18, 2021 23:47
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Apr 18, 2021

sabberworm/php-css-parser has not been very well-maintained lately. We might need to help out there (or choose a different library).

Last release was 10 months ago. Last commit was 5 months ago. But I'm not aware of any new CSS syntaxes being introduced, so there may be no pressing reason to update anything particularly regularly. Of course new PHP 7 or Psalm-compatible type hints would be nice.

Inevitably the new tests are failing since rebase due to CSS whitespace differences (and @import "foo.css" being rendered as @import url("foo.css").

@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from df8f1a6 to 15c4a7a Compare April 19, 2021 00:10
@oliverklee
Copy link
Contributor

I'm mostly worried about situations where we need a particular bug fix or a compatibility fix for newer PHP versions. (And I'm hopeful that we can contribute to the project, and that the maintainer will accept our changes.)

@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from 15c4a7a to 5a326c0 Compare April 26, 2021 23:47
@oliverklee
Copy link
Contributor

In the meantime, it looks like the maintainers of the Sabberworm CSS parser review and merge new PRs (slowly but steadily). We might need to take care of updating old PRs and fixing old bugs ourselves, though (including bugs reported by Nikita Popov from the PHP core team).

@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from 5a326c0 to e8d4f16 Compare May 4, 2021 23:10
@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch 2 times, most recently from 139680f to c73a134 Compare May 23, 2021 22:48
@bytestream
Copy link

I think adding Sabberworm CSS parser is a good idea. With respect to other libraries, the only other one I'm aware of is https://github.com/Cerdic/CSSTidy but I think you'll find similar issues with this one.

@JakeQZ
Copy link
Contributor Author

JakeQZ commented May 25, 2021

With respect to other libraries, the only other one I'm aware of is https://github.com/Cerdic/CSSTidy

Thanks, I was not aware of that library. I think it would certainly be worth looking into before committing more heavily to the other one - e.g. starting to use its classes further downstream (which this PR doesn't go as far as).

@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from c73a134 to 7a4226f Compare May 25, 2021 21:36
JakeQZ added a commit that referenced this pull request May 25, 2021
This is currently a demonstration to see the code changes and potential test
failures involved with using this CSS parsing package rather than
`sabberworm/php-css-parser` as proposed in #1015.
JakeQZ added a commit that referenced this pull request May 26, 2021
This is currently a demonstration to see the code changes and potential test
failures involved with using this CSS parsing package rather than
`sabberworm/php-css-parser` as proposed in #1015.
@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from 7a4226f to 112a73d Compare May 26, 2021 00:02
@JakeQZ JakeQZ added this to the 6.0.0 milestone May 26, 2021
@JakeQZ JakeQZ requested a review from oliverklee May 26, 2021 00:02
@JakeQZ
Copy link
Contributor Author

JakeQZ commented May 26, 2021

https://github.com/Cerdic/CSSTidy

I think it would certainly be worth looking into before committing more heavily to the other one.

I should have been getting on with something else, but thought it worth checking out how CSSTidy might pan out if we were to decide to go with that instead. I've created a draft PR, #1076, for that alternative, so you can see what the code changes look like and what tests would have to be modified or disabled.

The advantages of one vs the other appear to be as follows:

CSSTidy

  • Seems possibly able to combine shorthand and longhand properties;
  • Has potentially revealed a bug in our code.

Sabberworm

  • Supports PHP 8;
  • Provides classes we could use downstream;
  • Rejects invalid (malformed) property names;
  • Allows invalid @import rules to be rejected.

Coincidentally, this PR itself is now ready for review, should we decide to proceed with the Sabberworm parser...

@JakeQZ JakeQZ marked this pull request as ready for review May 26, 2021 00:17
@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from 112a73d to 3eda043 Compare May 26, 2021 00:20
src/CssInliner.php Outdated Show resolved Hide resolved
tests/Unit/CssInlinerTest.php Outdated Show resolved Hide resolved
@JakeQZ JakeQZ marked this pull request as draft May 26, 2021 01:05
@JakeQZ
Copy link
Contributor Author

JakeQZ commented May 26, 2021

Coincidentally, this PR itself is now ready for review, should we decide to proceed with the Sabberworm parser...

Marked as draft again, pending #1077.

@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from 3eda043 to b84f00c Compare May 26, 2021 14:54
JakeQZ added a commit that referenced this pull request May 26, 2021
This is currently a demonstration to see the code changes and potential test
failures involved with using this CSS parsing package rather than
`sabberworm/php-css-parser` as proposed in #1015.
@JakeQZ
Copy link
Contributor Author

JakeQZ commented May 26, 2021

Marked as draft again, pending #1077.

Now rebased with #1077. This is ready for review, in conjunction with the possible alternative in #1076.

@JakeQZ JakeQZ marked this pull request as ready for review May 26, 2021 14:58
JakeQZ added a commit that referenced this pull request May 26, 2021
This is currently a demonstration to see the code changes and potential test
failures involved with using this CSS parsing package rather than
`sabberworm/php-css-parser` as proposed in #1015.
JakeQZ added a commit that referenced this pull request May 26, 2021
This is currently a demonstration to see the code changes and potential test
failures involved with using this CSS parsing package rather than
`sabberworm/php-css-parser` as proposed in #1015.
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.

I've added my thoughts on reading through the first version.

src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
continue;
}
}
$media = '@media ' . $mediaQueryList;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a big fan of having the types in variable names. Maybe we can rename the variable to $mediaQueries?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the spec, or at least MDN, it is quite specifically called a "media query list". Changing it to something else I think could sow more confusion.

src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
&& $rule->getRules('font-family') !== []
&& $rule->getRules('src') !== [];
default:
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please let's have early returns only for guard clauses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there can be a case for breaking this rule, if all of the following apply:

  • The swtich statement is the only statement in the method;
  • All case branches have a return statment and not a break statement, except where a 'follow-through' is allowed or for the last case or default if null or void is the obviosuly desired return value in that case.

However, maintaining that standard through future code changes may be harder than it seems. So I will probably change it when I revisit the code changes on this - perhaps tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if all of the following apply

Such a method would be atomic. If there was any future need to add pre-amble or post-amble code, then it should be moved to a new atomic method, with the original method becoming a wrapper with the additional pre- or post- code. Though we know from experience it often doesn't turn out that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please let's have early returns only for guard clauses.

I've changed the switch statement, though not the preceding code.

I don't think I agree with a "one size fits all" approach regarding where a return statement is or isn't acceptable, provided the complexity of an individual method can be kept low, as I've indicated above.

src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
@JakeQZ JakeQZ force-pushed the task/sabberworm-css-parser branch from b84f00c to fb7a7f3 Compare June 10, 2021 23:33
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Jun 11, 2021

I've hopefully now resolved all the code review issues, and rebased to include #1081. Unfortunately the latter has uncovered a bug in the Sabberworm CSS parser which is beyond the scope of this PR, so it will still not be as clean a commit as desired, because some newly-added tests must now be temporarily disabled.

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.

Looks very good! Now we only need the final polish.

src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
src/Utilities/CssDocument.php Outdated Show resolved Hide resolved
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Jun 12, 2021

Looks very good! Now we only need the final polish.

Have made suggested changes.

Do I get the prize of not having to make a rebase involving a file rename? ;))

@JakeQZ
Copy link
Contributor Author

JakeQZ commented Jun 12, 2021

... rebased to include #1081. Unfortunately the latter has uncovered a bug in the Sabberworm CSS parser which is beyond the scope of this PR, so it will still not be as clean a commit as desired, because some newly-added tests must now be temporarily disabled.

MyIntervals/PHP-CSS-Parser#261

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.

Beautiful!

@oliverklee oliverklee merged commit c2ad938 into main Jun 12, 2021
@oliverklee oliverklee deleted the task/sabberworm-css-parser branch June 12, 2021 07:53
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.

None yet

3 participants