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 htmx.ajax defaulting to swap body when target not found #2878

Merged
merged 7 commits into from
Oct 3, 2024

Conversation

MichaelWest22
Copy link
Contributor

@MichaelWest22 MichaelWest22 commented Sep 6, 2024

Description

The htmx.ajax helper function can cause issues when the target is not set correctly or the target selector happens to be missing in the DOM as it passes in null target override to the ajax request which defaults to targeting the body. Changing it to detect invalid targets and pass in a DUMMY_ELT which triggers the htmx:targetError solves this. Also had to handle situations where a valid source is supplied as its valid to fall back to source targeting an explicit target is not defined but if we still want it to fail if target was defined.

Corresponding issue:
#2869

Testing

Wrote some new tests to cover most invalid targeting situations and also tested calling htmx.ajax manually to confirm it rejects invalid targets but passes though valid ones fine.

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
  • I ran the test suite locally (npm run test) and verified that it succeeded

@Telroshan Telroshan added the bug Something isn't working label Sep 7, 2024
@MichaelWest22
Copy link
Contributor Author

rewrote the logic into an expanded if statement so its easier to understand and debug and added inline comment

@Telroshan Telroshan added the ready for review Issues that are ready to be considered for merging label Sep 20, 2024
@1cg 1cg merged commit b98e4f2 into bigskysoftware:dev Oct 3, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready for review Issues that are ready to be considered for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants