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

Introduce FormLinkInterceptor #631

Merged
merged 2 commits into from
Jul 17, 2022
Merged

Conversation

seanpdoyle
Copy link
Contributor

In an effort to match the patterns established by LinkInterceptor
and FormInterceptor, this commit introduces a
FormLinkInterceptor and FormLinkInterceptorDelegate.

Behind the scenes, the FormLinkInterceptor relies on an instance of
the LinkInterceptor to intervene in <a> element clicks when
[data-turbo-method] or [data-turbo-stream] are present. When those
clicks are detected, it creates a <form hidden> element, attaches it
to the document, delegates to a FormLinkInterceptorDelegate to map
the <a> element's attributes to the <form> element, submits the form
through the polyfilled HTMLFormElement.requestSubmit method, then
removes the <form> from the document.

The Session serves as a FormLinkInterceptorDelegate, making sure
to start and stop the observer before its LinkInterceptor
instance, so that clicks that are intercepted by the
FormLinkInterceptor are not also intercepted by the
LinkInterceptor.

In an effort to match the patterns established by `LinkInterceptor`
and `FormInterceptor`, this commit introduces a
`FormLinkInterceptor` and `FormLinkInterceptorDelegate`.

Behind the scenes, the `FormLinkInterceptor` relies on an instance of
the `LinkInterceptor` to intervene in `<a>` element clicks when
`[data-turbo-method]` or `[data-turbo-stream]` are present. When those
clicks are detected, it creates a `<form hidden>` element, attaches it
to the document, delegates to a `FormLinkInterceptorDelegate` to map
the `<a>` element's attributes to the `<form>` element, submits the form
through the polyfilled [HTMLFormElement.requestSubmit][] method, then
removes the `<form>` from the document.

The `Session` serves as a `FormLinkInterceptorDelegate`, making sure
to start and stop the observer _before_ its `LinkInterceptor`
instance, so that clicks that are intercepted by the
`FormLinkInterceptor` are not also intercepted by the
`LinkInterceptor`.

[HTMLFormElement.requestSubmit]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit
@seanpdoyle
Copy link
Contributor Author

This might resolve some <a>-related issues like #587 & #588.

linkClickIntercepted(link: Element, action: string): void {
const form = document.createElement("form")
form.setAttribute("action", action)
form.hidden = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies if this edge case is already handled, I don't have a full understanding of all of the internals of Turbo.

But in the case that Turbo.session.drive = false, but on this link data-turbo="true" is specified, then wouldn't the form created by this also need to set data-turbo="true" on the form element for it to be handled by Turbo?

I have a PR open to address this (#500) but the addition of this FormLinkInterceptor would change where that behavior should be implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for raising these concerns! I've pushed up d317319 to add test coverage for the cases you've outlined.

@dhh
Copy link
Member

dhh commented Jul 17, 2022

Nice cleanup!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants