-
Notifications
You must be signed in to change notification settings - Fork 91
Polyfill remote-dom FormData using the native Worker implementation #621
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
Open
developit
wants to merge
3
commits into
main
Choose a base branch
from
cx-formdata-worker-polyfill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@remote-dom/polyfill': minor | ||
| --- | ||
|
|
||
| Support constructing native worker `FormData` from Remote DOM forms. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type {Element} from './Element.ts'; | ||
|
|
||
| const NativeFormData = globalThis.FormData; | ||
|
|
||
| export function FormData(form?: Element) { | ||
| const data = new NativeFormData(); | ||
|
|
||
| if (form) { | ||
| for (const control of form.querySelectorAll('[name]')) { | ||
| const name = control.getAttribute('name'); | ||
| if (!name) continue; | ||
|
|
||
| const disabled = control.disabled ?? control.hasAttribute('disabled'); | ||
| if (disabled) continue; | ||
| if (control.localName.toLowerCase() === 'button') continue; | ||
|
|
||
| const type = String( | ||
| control.type ?? control.getAttribute('type') ?? '', | ||
| ).toLowerCase(); | ||
| if (type === 'button') continue; | ||
| if (type === 'image') continue; | ||
| if (type === 'reset') continue; | ||
| if (type === 'submit') continue; | ||
|
|
||
| if ( | ||
| 'checked' in control && | ||
| !(control.checked ?? control.hasAttribute('checked')) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| data.append(name, control.value ?? control.getAttribute('value') ?? ''); | ||
| } | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| if (NativeFormData) FormData.prototype = NativeFormData.prototype; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import {beforeEach, describe, expect, it, vi} from 'vitest'; | ||
|
|
||
| import {Window} from '../index.ts'; | ||
|
|
||
| const NativeFormData = globalThis.FormData; | ||
|
|
||
| describe('FormData', () => { | ||
| beforeEach(() => { | ||
| Window.setGlobalThis(new Window()); | ||
| }); | ||
|
|
||
| it('constructs an empty mutable native FormData', () => { | ||
| const data = new FormData(); | ||
|
|
||
| data.append('message', 'hello'); | ||
|
|
||
| expect(data.get('message')).toBe('hello'); | ||
| expect(data).toBeInstanceOf(NativeFormData); | ||
| expect(data).toBeInstanceOf(FormData); | ||
| expect(data.constructor).toBe(NativeFormData); | ||
| expect(Object.prototype.toString.call(data)).toBe('[object FormData]'); | ||
| }); | ||
|
|
||
| it('is installed by Window.setGlobal()', () => { | ||
| const window = new Window(); | ||
|
|
||
| Window.setGlobal(window); | ||
|
|
||
| expect(globalThis.FormData).toBe(window.FormData); | ||
| }); | ||
|
|
||
| it('collects named controls in tree order and preserves duplicate names', () => { | ||
| const form = document.createElement('form'); | ||
| const first = document.createElement('input'); | ||
| const group = document.createElement('div'); | ||
| const second = document.createElement('input'); | ||
| const last = document.createElement('textarea'); | ||
|
|
||
| first.setAttribute('name', 'item'); | ||
| first.setAttribute('value', 'first'); | ||
| second.setAttribute('name', 'item'); | ||
| second.setAttribute('value', 'second'); | ||
| last.setAttribute('name', 'note'); | ||
| last.setAttribute('value', 'last'); | ||
| group.append(second); | ||
| form.append(first, group, last); | ||
|
|
||
| expect([...new FormData(form)]).toEqual([ | ||
| ['item', 'first'], | ||
| ['item', 'second'], | ||
| ['note', 'last'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('prefers current property values to initial attributes', () => { | ||
| const form = document.createElement('form'); | ||
| const input = document.createElement('input'); | ||
|
|
||
| input.setAttribute('name', 'message'); | ||
| input.setAttribute('value', 'initial'); | ||
| input.value = 'current'; | ||
| form.append(input); | ||
|
|
||
| expect(new FormData(form).get('message')).toBe('current'); | ||
| }); | ||
|
|
||
| it('stops reading control state after an early omission', () => { | ||
| const form = document.createElement('form'); | ||
| const unnamed = document.createElement('remote-input'); | ||
| const disabled = document.createElement('remote-input'); | ||
|
|
||
| unnamed.setAttribute('name', ''); | ||
| Object.defineProperties(unnamed, { | ||
| disabled: { | ||
| get() { | ||
| throw new Error('disabled should not be read'); | ||
| }, | ||
| }, | ||
| type: { | ||
| get() { | ||
| throw new Error('type should not be read'); | ||
| }, | ||
| }, | ||
| }); | ||
| disabled.setAttribute('name', 'disabled'); | ||
| Object.defineProperties(disabled, { | ||
| disabled: {value: true}, | ||
| type: { | ||
| get() { | ||
| throw new Error('type should not be read'); | ||
| }, | ||
| }, | ||
| checked: { | ||
| get() { | ||
| throw new Error('checked should not be read'); | ||
| }, | ||
| }, | ||
| }); | ||
| form.append(unnamed, disabled); | ||
|
|
||
| expect(() => new FormData(form)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('omits unsuccessful controls', () => { | ||
| const form = document.createElement('form'); | ||
| const controls = [ | ||
| ['input', null, 'unnamed'], | ||
| ['input', 'disabled-attribute', 'disabled'], | ||
| ['input', 'disabled-property', null], | ||
| ['remote-checkbox', 'unchecked-checkbox', null], | ||
| ['remote-radio', 'unchecked-radio', null], | ||
| ['button', 'button-element', null], | ||
| ['BUTTON', 'uppercase-button-element', null], | ||
| ['input', 'button-input', 'button'], | ||
| ['input', 'image-input', 'image'], | ||
| ['input', 'reset-input', 'reset'], | ||
| ['input', 'submit-input', 'submit'], | ||
| ] as const; | ||
|
|
||
| for (const [tag, name, type] of controls) { | ||
| const control = document.createElement(tag) as any; | ||
| if (name) control.setAttribute('name', name); | ||
| control.setAttribute('value', name ?? 'unnamed'); | ||
| if (type) control.setAttribute('type', type); | ||
| if (name === 'disabled-attribute') { | ||
| control.setAttribute('disabled', ''); | ||
| } else if (name === 'disabled-property') { | ||
| control.disabled = true; | ||
| } else if (name?.startsWith('unchecked-')) { | ||
| control.checked = false; | ||
| } | ||
| form.append(control); | ||
| } | ||
|
|
||
| const unchecked = document.createElement('remote-toggle') as any; | ||
| unchecked.setAttribute('name', 'currently-unchecked'); | ||
| unchecked.setAttribute('checked', ''); | ||
| unchecked.checked = false; | ||
| form.append(unchecked); | ||
|
|
||
| expect([...new FormData(form)]).toEqual([]); | ||
| }); | ||
|
|
||
| it('uses the checked property exposed by custom elements', () => { | ||
| const form = document.createElement('form'); | ||
| const checked = document.createElement('remote-checkbox') as any; | ||
| const initiallyChecked = document.createElement('remote-radio') as any; | ||
| const unmodeled = document.createElement('input'); | ||
|
|
||
| checked.setAttribute('name', 'choice'); | ||
| checked.setAttribute('value', 'one'); | ||
| checked.checked = true; | ||
| initiallyChecked.setAttribute('name', 'choice'); | ||
| initiallyChecked.setAttribute('value', 'two'); | ||
| initiallyChecked.setAttribute('checked', ''); | ||
| initiallyChecked.checked = undefined; | ||
| unmodeled.setAttribute('name', 'choice'); | ||
| unmodeled.setAttribute('type', 'checkbox'); | ||
| unmodeled.setAttribute('value', 'three'); | ||
| form.append(checked, initiallyChecked, unmodeled); | ||
|
|
||
| expect(new FormData(form).getAll('choice')).toEqual([ | ||
| 'one', | ||
| 'two', | ||
| 'three', | ||
| ]); | ||
| }); | ||
|
|
||
| it('passes Blob and File values to the native FormData', async () => { | ||
| const form = document.createElement('form'); | ||
| const blobInput = document.createElement('input'); | ||
| const fileInput = document.createElement('input'); | ||
| const file = new File(['file contents'], 'example.txt'); | ||
|
|
||
| blobInput.setAttribute('name', 'blob'); | ||
| blobInput.value = new Blob(['blob contents'], { | ||
| type: 'text/plain', | ||
| }) as any; | ||
| fileInput.setAttribute('name', 'file'); | ||
| fileInput.value = file as any; | ||
| form.append(blobInput, fileInput); | ||
|
|
||
| const data = new FormData(form); | ||
| const blob = data.get('blob'); | ||
|
|
||
| expect(blob).toBeInstanceOf(Blob); | ||
| expect((blob as Blob).type).toBe('text/plain'); | ||
| expect(await (blob as Blob).text()).toBe('blob contents'); | ||
| expect(data.get('file')).toBe(file); | ||
| }); | ||
|
|
||
| it('can be imported when native FormData is unavailable', async () => { | ||
| const InstalledFormData = globalThis.FormData; | ||
|
|
||
| try { | ||
| delete (globalThis as any).FormData; | ||
| vi.resetModules(); | ||
|
|
||
| await expect(import('../FormData.ts')).resolves.toHaveProperty( | ||
| 'FormData', | ||
| ); | ||
| } finally { | ||
| globalThis.FormData = InstalledFormData; | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checked elements submit
"on"as a fallback if the value attribute isn't therestep 7.1 here https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-form-data-set