Skip to content

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

@cmd-johnson

Description

@cmd-johnson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions