Skip to content

Implement upper filter #81

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

Merged
merged 7 commits into from
May 4, 2025

Conversation

LucasGrugru
Copy link
Contributor

Hello,

I find the project really interesting. I propose a very small contribution to add the uppercase filter.

I'm not a rust expert so feel free to tell me if anything needs to be changed.

Copy link
Owner

@LilyAcorn LilyAcorn left a comment

Choose a reason for hiding this comment

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

This is a really good start thanks!

Unfortunately, upper is a bit weird so this will need some updating to handle that.

Some(content) => Some(
content
.resolve_string(context)?
.map_content(|content| Cow::Owned(content.to_uppercase())),
Copy link
Owner

Choose a reason for hiding this comment

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

upper is a bit unusual because it's not considered HTML-safe upstream in Django.

To handle this correctly here, we'll need to replace .map_content with something like:

Suggested change
.map_content(|content| Cow::Owned(content.to_uppercase())),
.map(|content| content.content().to_uppercase())
.map(|content| Cow::Owned(Content::String(content))),

We'll also need a test to demonstrate we follow Django's behaviour here. (I'd add it in the Python tests.) This test should include a html tag which will become html-escaped as well as uppercased in the expected output.

For more information about this quirk, see my blog post or the ticket and Django PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This Django case is really interesting.

I've been trying to use upper filter for HTML text in Django, and I'm not sure about the proposed solution. Django uses uppercase to encode text before encoding HTML characters. In this case, the resolve_string() method will encode HTML characters; I always have to call resolve_string() before calling to_uppercase() in the code. So the encoded HTML characters will be capitalized, which Django doesn't do.

I added test cases, especially the last one which I feel is not solvable without change resolve_string for allowing to return a HtmlUnsafe ?

Copy link
Owner

Choose a reason for hiding this comment

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

That's a very good point - I think this is essentially a bug in the existing Rust implementation that's been exposed by your work here.

I've had a play around with your idea of introducing a HtmlUnsafe variant to the Content and ContentString enums and I've got the tests passing. 🚀

Copy link

codecov bot commented Apr 27, 2025

Codecov Report

Attention: Patch coverage is 97.44681% with 6 lines in your changes missing coverage. Please review.

Project coverage is 98.78%. Comparing base (0986a06) to head (3b89e91).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/render/types.rs 89.18% 0 Missing and 4 partials ⚠️
src/render/filters.rs 97.93% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #81      +/-   ##
==========================================
+ Coverage   98.73%   98.78%   +0.04%     
==========================================
  Files          47       48       +1     
  Lines        7832     7958     +126     
  Branches     2811     2819       +8     
==========================================
+ Hits         7733     7861     +128     
  Misses         61       61              
+ Partials       38       36       -2     

☔ 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.

@LilyAcorn LilyAcorn mentioned this pull request Apr 28, 2025
LucasGrugru and others added 4 commits May 4, 2025 19:16
I had hoped we only needed two variants, but I now think it's
conceptually simpler to have `String` for contexts where escaping
behaviour is irrelevant and `HtmlSafe` and `HtmlUnsafe` where it
matters.
This reduces some boilerplate when it doesn't matter which exact
`ContentString` variant is present.
@LilyAcorn LilyAcorn mentioned this pull request May 4, 2025
@LilyAcorn
Copy link
Owner

Thanks for making such a great start with this @LucasGrugru! I expect some of the other filters will be more straightforward to implement.

@LilyAcorn LilyAcorn merged commit 7ae1f6c into LilyAcorn:main May 4, 2025
6 of 7 checks passed
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.

3 participants