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

fix: primitives rendering to equal react output #190

Merged
merged 5 commits into from
Jul 23, 2024

Conversation

damiennl
Copy link

@damiennl damiennl commented Apr 17, 2024

Hello 👋,

I've just removed the boolean convert to string in order to write some JSX code like this:

<>
  {flash.has('error'} && <span>{flash.get('error')}</span>}
</>

Because it could shows false which is not expected

It is by the way a regression because it was the behavior in v3.1.2

Thx 🙏

Copy link

changeset-bot bot commented Apr 17, 2024

🦋 Changeset detected

Latest commit: 76c45c5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@kitajs/html Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@damiennl damiennl force-pushed the fix_convert_boolean branch from 2cc2e4f to 95de098 Compare April 17, 2024 12:12
@arthurfiorette
Copy link
Member

arthurfiorette commented Apr 17, 2024

Hey @damiennl this was a regression of v3 because in v4 it now follows the exactly same behavior as React.

You implementation also ignores true which leads to other unexpected behaviors too

@arthurfiorette arthurfiorette self-assigned this Apr 17, 2024
@arthurfiorette
Copy link
Member

@JacopoPatroclo what's you thoughts on this?

@JacopoPatroclo
Copy link

@arthurfiorette The changes are sane. I agree with @damiennl that if you want to write something like this it's important that the boolean values are not rendered as strings. It makes the library more similar to React (and other jsx renderers) and I think it's a good thing.

I will also advise @damiennl to make sure to never put untrusted strings inside flash messages and/or escape them (using the primitive that this library gives you).

Thanks for the contribution @damiennl !

@arthurfiorette
Copy link
Member

arthurfiorette commented Apr 18, 2024

It was indeed a regression/breaking change, that's why we are at 4.0.0 now.

The only requirement is to follow what React does because React is a "baseline" for every JSX runtime. Does this regression follows react? If it doesn't then it's a bug and I'll merge this pr.

Can someone create a test / reproducible example so that we can test it out?

Kita/Html doesn't have to actually follow react, but I assume most of the devs who will try Kita/Html comes from a background with React and having different behaviors from React for simple thins such as how we transform values to html strings will be a bad thing long term.

I'm also open to new suggestions @JacopoPatroclo & @damiennl

@arthurfiorette arthurfiorette added help wanted Extra attention is needed question Further information is requested html-runtime Related to @kitajs/html labels Apr 18, 2024
@tqhoughton
Copy link

Is there any update on this? This behavior makes it annoying to do conditional rendering in templates since all of your condition && <Component /> conditions have to be condition ? <Component /> : null instead.

@arthurfiorette
Copy link
Member

Sadly, I do not have enough time to implement this right now.

In case someone wants to help me out, these are the functions doing the "fauly" work:

function contentsToString(contents, escape) {

function contentToString(content, safe) {

Copy link

codecov bot commented Jun 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.56%. Comparing base (5f68ce5) to head (781c73c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #190   +/-   ##
=======================================
  Coverage   99.56%   99.56%           
=======================================
  Files           5        5           
  Lines         456      456           
=======================================
  Hits          454      454           
  Misses          2        2           

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

@tqhoughton
Copy link

I may have some time to address this sometime this week, seems like at initial glance the issue is this line in each function:

case 'boolean':

where boolean variables are getting automatically converted to strings without regard for the false case.

@damiennl
Copy link
Author

damiennl commented Jun 3, 2024

Hey guys 👋
This is a MR, I already do the changes, please check it
I fixed the two functions and updated the unit tests

@arthurfiorette
Copy link
Member

Can someone create a test / reproducible example so that we can test it out?

#190 (comment)

@alexgorbatchev
Copy link

alexgorbatchev commented Jul 22, 2024

The only requirement is to follow what React does because React is a "baseline" for every JSX runtime. Does this regression follows react? If it doesn't then it's a bug and I'll merge this pr.

current:

react:  <>{false && 'hello'}</> -> ""
kitajs: <>{false && 'hello'}</> -> "false"

expected:

react:  <>{false && 'hello'}</> -> ""
kitajs: <>{false && 'hello'}</> -> ""

Could this PR please be merged or is there a blocker?

@arthurfiorette

This comment was marked as outdated.

@arthurfiorette arthurfiorette changed the title fix: do not convert boolean to string fix: primitives rendering to equal react output Jul 23, 2024
@arthurfiorette
Copy link
Member

I ended up re implementing this myself to exactly match what react renders to.

@arthurfiorette arthurfiorette enabled auto-merge (squash) July 23, 2024 15:28
@arthurfiorette arthurfiorette merged commit a9adfd4 into kitajs:master Jul 23, 2024
6 checks passed
@arthurfiorette arthurfiorette mentioned this pull request Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed html-runtime Related to @kitajs/html question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants