Skip to content

Commit aa6f3b8

Browse files
skvaleflotwig
andauthored
fix: Allow submit button to be outside of the form for implicit submission (#21279)
Co-authored-by: Zach Bloomquist <github@chary.us> Co-authored-by: Zach Bloomquist <git@chary.us>
1 parent 05ef83a commit aa6f3b8

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

packages/driver/cypress/fixtures/dom.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@
323323
<button type="submit">submit me</button>
324324
</form>
325325

326+
<form id="multiple-inputs-and-button-submit.outside-form">
327+
<input name="fname" />
328+
<input name="lname" />
329+
</form>
330+
<button form="multiple-inputs-and-button-submit.outside-form" type="submit">submit me</button>
331+
326332
<form id="multiple-inputs-and-reset-and-submit-buttons">
327333
<input name="fname" />
328334
<input name="lname" />

packages/driver/cypress/integration/commands/actions/type_special_chars_spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,16 @@ describe('src/cy/commands/actions/type - #type special chars', () => {
14861486
cy.get('#multiple-inputs-and-button-submit input:first').type('foo{enter}')
14871487
})
14881488

1489+
it('triggers form submit when the submit button is outside of the form', function (done) {
1490+
this.$forms.find('[id="multiple-inputs-and-button-submit.outside-form"]').submit((e) => {
1491+
e.preventDefault()
1492+
1493+
done()
1494+
})
1495+
1496+
cy.get('[id="multiple-inputs-and-button-submit.outside-form"] input:first').type('foo{enter}')
1497+
})
1498+
14891499
it('causes click event on the button[type=submit]', function (done) {
14901500
this.$forms.find('#multiple-inputs-and-button-submit button[type=submit]').click((e) => {
14911501
e.preventDefault()

packages/driver/src/cy/commands/actions/type.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,15 @@ export default function (Commands, Cypress, cy, state, config) {
173173
const win = state('window')
174174

175175
const getDefaultButtons = (form) => {
176-
return form.find('input, button').filter((__, el) => {
176+
const formId = CSS.escape(form.attr('id'))
177+
const nestedButtons = form.find('input, button')
178+
179+
const possibleDefaultButtons: JQuery<any> = formId ? $dom.wrap(_.uniq([
180+
...nestedButtons,
181+
...$dom.query('body', form.prop('ownerDocument')).find(`input[form="${formId}"], button[form="${formId}"]`),
182+
])) : nestedButtons
183+
184+
return possibleDefaultButtons.filter((__, el) => {
177185
const $el = $dom.wrap(el)
178186

179187
return (

0 commit comments

Comments
 (0)