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

feat: [#1427] Support submitter in formData constructor and exclude s… #1759

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions packages/happy-dom/src/form-data/FormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import File from '../file/File.js';
import HTMLInputElement from '../nodes/html-input-element/HTMLInputElement.js';
import HTMLFormElement from '../nodes/html-form-element/HTMLFormElement.js';
import BrowserWindow from '../window/BrowserWindow.js';
import HTMLElement from '../nodes/html-element/HTMLElement.js';
import DOMException from '../exception/DOMException.js';
import HTMLButtonElement from '../nodes/html-button-element/HTMLButtonElement.js';

type FormDataEntry = {
name: string;
Expand All @@ -25,12 +28,45 @@ export default class FormData implements Iterable<[string, string | File]> {
* Constructor.
*
* @param [form] Form.
* @param [submitter] A submit button that is a member of the form.
*/
constructor(form?: HTMLFormElement) {
constructor(form?: HTMLFormElement, submitter?: HTMLInputElement | HTMLButtonElement) {
if (!form) {
return;
}

if (submitter && submitter instanceof HTMLElement) {
Copy link
Owner

Choose a reason for hiding this comment

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

Looking at JSDOM, it seems like it should be implemented a bit different. We will have to look into the spec a bit more.

Link to JSDOM implementation:
https://github.com/jsdom/jsdom/blob/main/lib/jsdom/living/xhr/FormData-impl.js

const isInTheForm = submitter.form === form;
const formId = form.getAttribute('id');
const submitterFormAttr = submitter.getAttribute('form');
const isRefferingToTheForm = formId && submitterFormAttr === formId;

if (!isInTheForm && !isRefferingToTheForm) {
throw new DOMException(
"Failed to construct 'FormData': The specified element is not owned by this form element.",
'NotFoundError'
);
}

const isSubmitButton =
submitter[PropertySymbol.tagName] === 'BUTTON' && submitter.type === 'submit';
const isSubmitInput =
submitter[PropertySymbol.tagName] === 'INPUT' &&
['submit', 'image'].includes(submitter.type);

if (!isSubmitButton && !isSubmitInput) {
throw new TypeError(
"Failed to construct 'FormData': The specified element is not a submit button."
);
}

const submitterName = submitter.name;
if (submitterName) {
const submitterValue = submitter.value;
this.append(submitterName, submitterValue);
}
}

const items = form[PropertySymbol.getFormControlItems]();

for (const item of items) {
Expand All @@ -56,6 +92,8 @@ export default class FormData implements Iterable<[string, string | File]> {
}
break;
case 'submit':
case 'image':
break;
case 'reset':
case 'button':
if ((<HTMLInputElement>item).value) {
Expand All @@ -67,11 +105,6 @@ export default class FormData implements Iterable<[string, string | File]> {
break;
}
break;
case 'BUTTON':
if ((<HTMLInputElement>item).value) {
this.append(name, (<HTMLInputElement>item).value);
}
break;
case 'TEXTAREA':
case 'SELECT':
this.append(name, (<HTMLInputElement>item).value);
Expand Down
51 changes: 49 additions & 2 deletions packages/happy-dom/test/form-data/FormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ describe('FormData', () => {
});

describe('constructor', () => {
it('throws TypeError when an invalid submitter type is provided', () => {
const form = document.createElement('form');
const input = document.createElement('input');
input.type = 'text';
input.name = 'test';
form.appendChild(input);

expect(() => new window.FormData(form, input)).toThrow(
"Failed to construct 'FormData': The specified element is not a submit button."
);
});

it('throws NotFoundError when submitter is not owned by the form', () => {
const form = document.createElement('form');
const button = document.createElement('button');
button.type = 'submit';
button.name = 'submit-btn';

// Not appending the button to the form
expect(() => new window.FormData(form, button)).toThrow(
"Failed to construct 'FormData': The specified element is not owned by this form element."
);
});

it('Supports sending in an HTMLFormElement to the contructor.', () => {
const form = document.createElement('form');
const file = new File([Buffer.from('fileContent')], 'file.txt', { type: 'text/plain' });
Expand Down Expand Up @@ -106,8 +130,31 @@ describe('FormData', () => {
expect(formData.getAll('checkboxInput')).toEqual(['checkbox value 2']);
expect(formData.get('button1')).toBe(null);
expect(formData.get('button2')).toBe(null);
expect(formData.get('button3')).toBe('button3');
expect(formData.get('button4')).toBe('button4');
expect(formData.get('button3')).toBe(null);
expect(formData.get('button4')).toBe(null);
});

it('Only includes button values when they are passed in as submitter', () => {
const form = document.createElement('form');
const input = document.createElement('input');
const button = document.createElement('button');

input.name = 'input';
input.value = 'testing';

button.name = 'button';
button.value = 'buttonValue';

form.appendChild(input);
form.appendChild(button);

const formDataWithoutButton = new window.FormData(form);
expect(formDataWithoutButton.get('input')).toBe('testing');
expect(formDataWithoutButton.get('button')).toBeNull();

const formDataWithSubmitter = new window.FormData(form, button);
expect(formDataWithSubmitter.get('input')).toBe('testing');
expect(formDataWithSubmitter.get('button')).toBe('buttonValue');
});

it('Supports input elements with empty values.', () => {
Expand Down