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

Add hx-prompt-default to set the default value of a hx-prompt (#601) #3117

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

maekoos
Copy link

@maekoos maekoos commented Jan 10, 2025

Description

Added hx-prompt-default attribute to (optionally) set the default value of a hx-prompt.
Also updated the documentation to include this new attribute.

Corresponding issue: #601

Testing

I added a manual test to confirm-and-prompt.html.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue - not explicitly approved
  • I ran the test suite locally (npm run test) and verified that it succeeded

@Telroshan Telroshan added the enhancement New feature or request label Jan 11, 2025
@MichaelWest22
Copy link
Contributor

May not be best to add too many additional hx- attributes to htmx where possible. Here we could keep the feature much more contained by just adding an appropriate separator that is unlikely for an existing user to have added to a existing hx-prompt question like | and make it take hx-prompt="do you like cats?|yes" . then it is just a .split() and pass in the two array values to prompt I think. Not sure if there is a better separator character to choose.

@maekoos
Copy link
Author

maekoos commented Jan 11, 2025

Sure, but that would also require more sanitisation on the server side - most template engines already sanitize attributes, so it's nice to not have to think about it....

Would it be any better to rename it to something else? hx-prompt::default?

@MichaelWest22
Copy link
Contributor

templating engines may sanitize attribute names a bit but we are just talking about a attribute string value which should only have basic sanitizing to prevent escaping out of the "". This is why it is important to pick a good sensible text character to split on. needs to be a character that won't upset engines and is unlikely to be used by existing hx-prompt user questions. most htmx attributes use colon ":" as the seperator but it is likely someone could have used this inside a prompt already. It is also common in htmx to use a word as the seperator and this could work as well. "do you like cats?default:yes" for example with split("default:") in the code as an example.

@maekoos
Copy link
Author

maekoos commented Jan 11, 2025

Most template engines don't sanitize attribute names at all - what I meant was that I can't do

hx-prompt="How old is {{ userName }}?|{{ defaultAge }}"

Since I'd have to look for | in the userName variable in case someone intentionally tries to break stuff...

Also, unless a really weird character is chosen we'd also have to allow escaping (like \|), and so a regex or similar is probably necessary.

@maekoos
Copy link
Author

maekoos commented Jan 11, 2025

Plus, reusing the hx-prompt attribute is not backwards compatible enough imo.

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

Successfully merging this pull request may close these issues.

3 participants