Skip to content

feat: Add keep_at_rules option #485

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kamilzych
Copy link

@kamilzych kamilzych commented Jun 27, 2025

@kamilzych kamilzych force-pushed the feat/keep-at-rules branch from 155e7b6 to 64161f7 Compare June 27, 2025 11:47
Copy link

codspeed-hq bot commented Jun 27, 2025

CodSpeed Performance Report

Merging #485 will not alter performance

Comparing kamilzych:feat/keep-at-rules (fdfd7c8) with master (bd04285)

Summary

✅ 6 untouched benchmarks

Copy link

codecov bot commented Jun 27, 2025

Codecov Report

Attention: Patch coverage is 99.24812% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.05%. Comparing base (d0ee769) to head (fdfd7c8).
Report is 31 commits behind head on master.

Files with missing lines Patch % Lines
css-inline/src/html/serializer.rs 97.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #485      +/-   ##
==========================================
+ Coverage   88.05%   89.05%   +0.99%     
==========================================
  Files          18       18              
  Lines        1993     2037      +44     
==========================================
+ Hits         1755     1814      +59     
+ Misses        238      223      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Owner

@Stranger6667 Stranger6667 left a comment

Choose a reason for hiding this comment

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

Great start! Definitely on the right track :) I left a few comments

@@ -114,6 +127,22 @@ impl<'a> Sink<'a> {

serializer.start_elem(&element.name, &element.attributes, style_node_id)?;

// TODO this part is the one that I don't like the most
Copy link
Owner

Choose a reason for hiding this comment

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

I am not 100% sure about this one, even though it is a sound approach.

The first thought that I have is that the least surprising thing would be to keep @ rules in their original places, instead of pushing everything into a single node. This won't work for external stylesheets, so we need to store them somewhere anyway.

On the other hand, this approach is better for performance, and probably it would not be super surprising for the user anyway if we put everything under one <style> tag.

So, in the end, I am leaning towards this approach.

A few other notes:

  1. We can reduce the performance hit of checking each node name by having two versions of serialize - the first one will work as before this PR. The new one will check for head and once it is found, it will switch to the other version of serialize that won't check the tag name. At the top level we check if at_rules are present and then call one of these two versions - I think it should minimize the impact as head is usually in the very beginning of the nodes vector.
  2. Are there cases when head is not present? Maybe in HTML fragments? see inline_fragment

Copy link
Owner

Choose a reason for hiding this comment

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

btw, if the user chose to keep style tags - I think we can only extract @ rule from link, as the old style tags will be around anyway

Copy link
Author

Choose a reason for hiding this comment

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

That's something I had no time to look closer into yet

Copy link
Owner

Choose a reason for hiding this comment

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

Given the current benchmark results, the performance hit is negligible, so we can skip this. For fragments, likely we can't really do it, as there is no regular HTML structure generally. Maybe we can return an error when the user sets an option to keep at rules, but uses inline_fragment?

@Stranger6667
Copy link
Owner

Hi @kamilzych

Let me know if you need any assistance or if you plan to continue working on this PR. I'd be happy to help or take over in case if you don't plan to continue this PR.

@kamilzych kamilzych force-pushed the feat/keep-at-rules branch from ffe46ee to c4adfc7 Compare July 19, 2025 17:27
@kamilzych
Copy link
Author

kamilzych commented Jul 19, 2025

@Stranger6667 Hi! Apologies for lack of activity, I simply had no time, life happened ¯\_(ツ)_/¯

I've just pushed a commit which addresses some of your comments. I'd love to finish the whole PR but I'm not sure when I have enough time to move this forward.
That said, if you have time to take it over, I'm absolutely fine with it. If you don't, I'll do my best to finish it ASAP and in such case I'd appreciate a review of the mentioned commit and perhaps some guidance about the next steps 🙏

@Stranger6667
Copy link
Owner

Awesome! Thank you so much! :) I’ll be on vacation till the end of next week, but I’ll review the changes in any event :) Let’s see how it goes

@Stranger6667
Copy link
Owner

Amazing to see that we already have a working version! There are a few minor things left; there is no time pressure, so I'm just noting what is left to do, and maybe I'll push some changes to it in the next few evenings:

  • Changelog entries for the main crate + bindings
  • Update all readmes with the new option + usage example
  • JS bindings
  • Java bindings
  • Python bindings
  • C bindings
  • Ruby bindings
  • Error for inline_fragment + keep_at_rules?

@kamilzych kamilzych force-pushed the feat/keep-at-rules branch from c4adfc7 to fdfd7c8 Compare July 19, 2025 22:14
@Stranger6667
Copy link
Owner

As for the next steps, I think most of them are about adding new config option to bindings, so they compile. This flag is the same as others in terms of behavior, so it should be more or less straightforward.

about the error, we can add an extra check to inline_fragment

@@ -199,6 +201,9 @@ OPTIONS:

--keep-link-tags
Keep "link" tags after inlining.

Copy link
Owner

Choose a reason for hiding this comment

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

There is a small place above where this flag should be added too. The handle_boolean_flag function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants