Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit a5404fc

Browse files
Polyfill form attribute for submit buttons
1 parent 21d8ddf commit a5404fc

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

packages/library/src/html.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ export class Form extends Screen {
5454
...options,
5555
})
5656

57+
// Polyfill the form attribute for IE11 and Edge
58+
// (the click is sufficient here, because of the
59+
// specification of implicit submissions in HTML5,
60+
// see https://www.w3.org/TR/html5/single-page.html#implicit-submission)
61+
// Note that this is not perfect -- in modern browsers,
62+
// the type attribute is not required for submission;
63+
// but it will help push users toward standard-conformant
64+
// behavior so that the polyfill can be removed safely
65+
// at some point in the future.
66+
this.options.events['click button[type="submit"]'] = (e) => {
67+
// If the button references another form...
68+
if (e.target.getAttribute('form')) {
69+
// ... find it and ...
70+
const targetForm = this.options.el.querySelector(
71+
`form#${ e.target.getAttribute('form') }`
72+
)
73+
74+
// ... submit that form instead
75+
// (this overrides the page structure, as per standard)
76+
if (targetForm) {
77+
return this.submit(e)
78+
}
79+
}
80+
81+
// Otherwise stick to default behavior
82+
return false
83+
}
84+
5785
// Capture form submissions
5886
this.options.events['submit form'] = e => this.submit(e)
5987
}

packages/library/test/html.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ describe('HTML-based components', () => {
253253
})
254254
})
255255

256+
it('polyfills form attribute on submit buttons', () => {
257+
// Test on the actual page, as above
258+
f.options.el = null
259+
f.options.content = '' +
260+
'<form id="test">' +
261+
'</form>' +
262+
'<button type="submit" form="test">Click me</button>'
263+
264+
const spy = sinon.spy(f, 'submit')
265+
266+
return f.run().then(() => {
267+
f.options.el.querySelector('button').click()
268+
assert.ok(spy.calledOnce)
269+
f.options.el.innerHTML = ''
270+
})
271+
})
272+
256273
it('ends after successful submission', () => {
257274
f.options.content = exampleForm
258275

0 commit comments

Comments
 (0)