Skip to content

Commit

Permalink
Treat blank [formaction] like a blank [action]
Browse files Browse the repository at this point in the history
When handling form submissions, a `<form>` element that declares a
`[action]` to be the empty string (i.e. `""`) is treated as if it were
targeting the current path:

> The `action` IDL attribute must reflect the content attribute of the
> same name, except that on getting, when the content attribute is
> missing or its value is the empty string, the element's node
> document's URL must be returned instead.
>
> [§ 4.10.18.6 Form Submission attributes][action]

Similarly, when handling form submissions with a submitter that declares
a `[formaction]` to be the empty string (i.e. `""`) is treated as if it
were targetting the current path.

> The `formAction` IDL attribute must reflect the formaction content
> attribute, except that on getting, when the content attribute is missing
> or its value is the empty string, the element's node document's URL must
>be returned instead.
>
> [§ 4.10.18.6 Form Submission attributes][formaction]

[action]: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-action
[formaction]: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-formaction
  • Loading branch information
seanpdoyle committed Feb 8, 2022
1 parent 201f8b3 commit 73e24bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export class FormSubmission {

get action(): string {
const formElementAction = typeof this.formElement.action === 'string' ? this.formElement.action : null
return this.submitter?.getAttribute("formaction") || this.formElement.getAttribute("action") || formElementAction || ""

if (this.submitter?.hasAttribute("formaction")) {
return this.submitter.getAttribute("formaction") || ""
} else {
return this.formElement.getAttribute("action") || formElementAction || ""
}
}

get body() {
Expand Down
5 changes: 5 additions & 0 deletions src/tests/fixtures/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ <h1>Form</h1>
<button type="submit" name="button">Submit</button>
</form>
</div>
<div id="blank-formaction">
<form action="/src/tests/fixtures/one.html">
<button formaction="">Submit</button>
</form>
</div>
<hr>
<div id="action-input">
<form class="no-action">
Expand Down
8 changes: 8 additions & 0 deletions src/tests/functional/form_submission_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.deepEqual(await this.getAllSearchParams("button"), [])
}

async "test submitter with blank formaction submits to the current page"() {
await this.clickSelector("#blank-formaction button")
await this.nextBody

this.assert.ok(await this.hasSelector("#blank-formaction"), "overrides form[action] navigation")
this.assert.equal(await this.pathname, "/src/tests/fixtures/form.html")
}

async "test input named action with no action attribute"() {
await this.clickSelector("#action-input form.no-action [type=submit]")
await this.nextBody
Expand Down

0 comments on commit 73e24bd

Please sign in to comment.