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

FormData constructed from a <form> includes buttons by default #1427

Open
cmd-johnson opened this issue May 7, 2024 · 1 comment
Open

FormData constructed from a <form> includes buttons by default #1427

cmd-johnson opened this issue May 7, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@cmd-johnson
Copy link

Describe the bug
When constructing a FormData instance from a HTMLFormElement containing a button with a name and value, that button's value is always included in the resulting FormData.

This isn't the case for any browser I've tested (Chromium, Firefox, WebKit). A submit button's value is only included in form submissions when that button is the one that triggered the form submission, so it makes sense to exclude button values by default.

The XMLHttpRequest Spec defines an optional submitter parameter that can be used to include a submit button's value in the resulting form data instead.

To Reproduce

describe('FormData', () => {
  describe('Constructor', () => {
    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.append(input);
      form.append(button);

      const formDataWithoutButton = new window.FormData(form);
      const formDataWithButton = new window.FormData(form, button);

      expect(formDataWithoutButton.get('input')).toBe('testing');
      expect(formDataWithoutButton.get('button')).toBeNull();

      expect(formDataWithButton.get('input')).toBe('testing');
      expect(formDataWithoutButton.get('button')).toBe('buttonValue');
    });
  });
});

Expected behavior
Constructing a new FormData instance from the form without passing in the button as submitter shouldn't include the button's value.

Additionally it would be nice if the submitter parameter could be supported by happy-dom.

Device:

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context
Looks like the submitter parameter has been in the spec since the end of January 2023 (ref), and implemented in most major browsers since April 2023.

FWIW jsdom doesn't support the submitter parameter either, but it does exclude button values by default.

@cmd-johnson cmd-johnson added the bug Something isn't working label May 7, 2024
@jenseng
Copy link

jenseng commented May 9, 2024

FYI jsdom has supported submitter since 22.1.0, but it looks like most people are still using 20.0.3 since the latest jest-environment-jsdom wants ^20.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants