Skip to content

[StimulusBundle] feat: normalize parameters names given to twig helper 'stimulus_action' #1193

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

Conversation

norival
Copy link
Contributor

@norival norival commented Oct 16, 2023

Q A
Bug fix? no
New feature? yes
Tickets NA
License MIT

This allows giving a parameter name as 'myParam', and it will be printed as 'data-controller-name-my-param-param', which will be interpreted as 'myParam' by the Stimulus controller.

@smnandre
Copy link
Member

Nice catch @norival !

@weaverryan : Is it seen as a bugfix.. or a new feature ? In the later, BC break alert :)

@weaverryan
Copy link
Member

This allows giving a parameter name as 'myParam', and it will be printed as 'data-controller-name-my-param-param', which will be interpreted as 'myParam' by the Stimulus controller.

What would the myParam have data-controller... attribute looked like before this PR? I'm also interested if there is any possibility that changing from the old string to the new string could potentially break someone's app... or if the old attribute was totally wrong and unusable, and now this fixes it.

@smnandre
Copy link
Member

smnandre commented Oct 17, 2023

Well after playing with this a bit, i'm 100% to consider this a bugfix.

And it's more a "HTML/DOM/JS" problem than a specific Stimulus one.

<div data-foo-bar="foo-bar" data-fooBar="fooBar">...</div>
<div data-fooBar="fooBar" data-foo-bar="foo-bar">...</div>
data-foobar

Data attributes are case-incensitive (and parsed in lowercase), and the snake to camelCase conversion between HTML and JS is the expected behaviour.

Big Crocodile

Given a parameter named bigCrocodile

Before

Before the fix made by @norival , the HTML code rendered by the function was

<button 
    data-action="foo#bar" 
    data-foo-bigCrocodile-param="green" 
  >...</button>

But in the Stimulus controller, the parameters passed were in lowercase

{bigcrocodile: "green"}

After

After the fix, the HTML code will be

<button 
    data-action="foo#bar" 
    data-foo-big-crocodile-param="green" 
  >...</button>

and the parameters passed to the action

{bigCrocodile: "green"}

That also means that is someone called the stimulusAction with snake-case parameters, nothing changes (they still get the parameter in camelCase)

@norival
Copy link
Contributor Author

norival commented Oct 17, 2023

Great analysis @smnandre ! 😃

Actually, I think there is a case where it would break things.

If you declare the parameters in the twig function like this:

{{ stimulus_action('controller', 'action', 'click', {bigCrocodile: "green"}) }}

this will render ...data-controller-bigcrocodile-param="green" and register it as event.params.bigcrocodile. After the 'fix', it would render ...data-controller-big-crocodile-param="green" and register it as event.params.bigCrocodile. So this would break the controller code using this parameter.

@smnandre
Copy link
Member

this will render ...data-controller-bigcrocodile-param="green"

I don't find where this transformation is done (bigCrocodile -> bigcrocodile in HTML) ?

@norival
Copy link
Contributor Author

norival commented Oct 17, 2023

I don't find where this transformation is done (bigCrocodile -> bigcrocodile in HTML) ?

My bad, the HTML is indeed data-controller-bigCrocodile-param="green", but in the DOM, it is interpreted as data-controller-bigcrocodile-param="green", and, when getting the dataset from JS, it is {controllerBigcrocodileParam: "green"} (in Firefox and Chrome, at least).

@smnandre
Copy link
Member

I checked, and Stimulus handles it (on macOs / Firefox + Chrome) as {bigcrocodile: green} indeed. So there is a difference.

But... if a developer passed "bigCrocodile" in HTML and then manually used "bigcrocodile" in the JS code.... they should have made the PR you just wrote instead :))

@norival
Copy link
Contributor Author

norival commented Oct 17, 2023

But... if a developer passed "bigCrocodile" in HTML and then manually used "bigcrocodile" in the JS code.... they should have made the PR you just wrote instead :))

Haha, I agree, that would be a bit weird 😅

@weaverryan
Copy link
Member

Can you update the CHANGELOG entry to make it a little longer and explain the behavior change if you were relying on the broken behavior?

@norival norival force-pushed the feat/stimulus-bundle/normalize-parameters-names branch from ccb221a to 557553b Compare October 17, 2023 14:24
@norival
Copy link
Contributor Author

norival commented Oct 17, 2023

Can you update the CHANGELOG entry to make it a little longer and explain the behavior change if you were relying on the broken behavior?

@weaverryan done! Is it ok like that?

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

One last tweak :)

@@ -1,5 +1,9 @@
# CHANGELOG

## 2.13.0

- Normalize parameters names given to twig helper 'stimulus_action()'. **BC Break**: previously, parameters given in camelCase (eg. `bigCrocodile`) would be registered by the controller as flatcase (`event.params.bigcrocodile`). Now, they are registered as camelCase (`event.params.bigCrocodile`) as expected.
Copy link
Member

Choose a reason for hiding this comment

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

Break onto multiple lines when it gets long (around 72ish chars) and indent the other lines the same as the word Normalize. Also, slight rewording:

camelCase (eg. bigCrocodile) were incorrectly registered ...

And change the last sentence to:

This was fixed, which means they are now correctly registered as camelCase (event.params.bigCrocodile).

Copy link
Member

Choose a reason for hiding this comment

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

And... long live the crocodile 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 😃

…r 'stimulus_action()'

This allows giving a parameter name as 'myParam', and it will be printed
as 'data-controller-name-my-param-param', which will be interpreted as
'event.param.myParam' by the Stimulus controller.

**BC Break**: previously, parameters given in camelCase (eg.
`bigCrocodile`) were incorrectly registered by the controller as
flatcase (`event.params.bigcrocodile`). This was fixed, which means they
are now correctly registered as camelCase (`event.params.bigCrocodile`).
@norival norival force-pushed the feat/stimulus-bundle/normalize-parameters-names branch from 557553b to ed02dad Compare October 18, 2023 04:59
@weaverryan
Copy link
Member

Thank you Xavier!

@weaverryan weaverryan merged commit d27916b into symfony:2.x Oct 18, 2023
@norival norival deleted the feat/stimulus-bundle/normalize-parameters-names branch October 18, 2023 19:39
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