Site-wide: Examples with outdated HTML containing submittable forms can break
I first noticed this with the "Updating border style" example at https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style#updating_border_style (and opened #44346 to fix that one page), but subsequent investigation revealed this to be a more widespread issue.
If an example's demo HTML contains a <form> element (which many legacy examples do, if they use <button>, <input> or other elements previously restricted to use in HTML forms), and that form is submittable (the child elements have id attributes), then activating those elements will break the demo, because a form submission is attempted and the playground runner.html returns a 400 Bad Request error, crashing the example (all rendered output vanishes).
This can also be seen at https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#examples . Although the input field does demonstrate the beforeunload functionality properly, because the form is submittable (the <input> has an id= attribute), it will break if navigation is triggered.
Steps to reproduce
- Navigate to https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#examples and scroll down to the rendered output containing an input field.
- Enter some text into the input field
- Press Enter, triggering the
beforeunloadunsaved-changes alert - Click "Leave" (or whatever button signals acceptance of the navigation attempt)
- The example will crash (the input field disappears, leaving a blank white box), because a form submission was attempted and got a 400 response from the playground server.
Heavily modified from a comment originally posted by @ferdnyc in https://github.com/mdn/content/issues/44346#issuecomment-4616096392
It would be good to go through the rest of the site's content and find all of the other examples with submittable forms, as they'll all be broken the same way. (<form> elements wrapped around demo controls amount to superfluous noise at least 90% of the time, anyway. They're a legacy holdout from the pre-HTML5 web, and it's better if they're removed wherever possible just to make the examples more focused.)
I was able to reproduce this on the live site. Pressing Enter in the example triggers a form submission, which navigates to an mdnplay.dev URL that returns a 400 Bad Request, causing the demo to break. I'd be interested in investigating this further if nobody is currently working on it.
I managed to reproduce the issue locally and it seems that the underlying problem is in the playground itself. The existing examples can be fixed by removing unnecessary <form> elements from the affected pages, but this does not prevent the same problem from occurring in future content that contains a submittable form.
Does it make sense to also handle this at the playground level, e.g. to prevent demo-breaking navigation due to form submissions, or is the idea that this will only be addressed by content updates?
@caugner Does it make sense to move this to rari or fred? As suggested above it would definitely be better to fix this globally?
Geminis suggests we mightintercept the submit if it happens to be a redirect back to the playground:
document.addEventListener('submit', (event) => {
const form = event.target;
// Resolve the form's action URL relative to the current window
const actionUrl = new URL(form.action, window.location.href);
// If the form is trying to submit back to the playground backend
if (actionUrl.host === window.location.host) {
event.preventDefault(); // Stop the 400 error crash!
console.warn(
`[MDN Playground] Intercepted a form submission to ${actionUrl.pathname}. ` +
`The network request was blocked to prevent a 400 error crash.`
);
}
// If actionUrl points anywhere else (e.g., https://httpbin.org/post),
// it passes right through and works normally.
}, true);
@hamishwillee Counter-intuitively, that seems to break the beforeunload example I linked to above. It looks like, because the submit attempt gets blocked, there's no intent to unload so beforeunload never fires. (Even though navigation wouldn't actually be attempted until the beforeunload handler finishes obtaining permission to navigate.)
(Er, I should say, it breaks the form's response to submit attempts, like typing Enter in the input field. Other methods of attempting to navigate away from the page still work, of course.)
As one would hope, since all of the processing is client-local, adding it to the border-style example at https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style#updating_border_style does not affect the demo. (Except in the intended manner of preventing it from immediately breaking when one of the buttons is clicked.) 👍
FWIW, I raised this with some of the other maintainers and we felt that, whether or not the platform implements some generic defensive measure against this, these examples ought to be fixed in content. Basically, unless an example has to cause a form submission (for example, because it's an example of form submission) then it should not be able to cause a form submission, and at least both the examples cited in this thread would be better examples all round if they didn't.
MDN examples should if possible be self-contained on the client side, since MDN's example system only allows the author to control the client side behavior, (notwithstanding historical attempts to change this).
Since this could potentially affect a large number of pages, would it make sense to handle the content fixes in a series of smaller PRs rather than one repository-wide change? That seems like it would make review and testing easier, while still allowing a separate platform-level mitigation to be discussed independently.
@wbamberg Thanks. Good point.
Since this could potentially affect a large number of pages, would it make sense to handle the content fixes in a series of smaller PRs rather than one repository-wide change?
I suggest "someone" audit the examples that are problematic and list them all here with a checkbox so we can tick them off. If there are only a few they might be done as together, but any more than that they need to be dealt with separately.
I'd like to work on this
I'd like to work on this
@Archita9412 The first step is to audit all the examples and create a list here - see https://github.com/mdn/content/issues/44347#issuecomment-4644438205 . If you'd like to do that, would be much appreciated.
Here's a probably exhaustive list:
- url-validation — HTML
- label-input-button — Form controls
- appearance-tester — appearance: controlling OS-level styling — contains two forms
- ugly-styling — What can be done about the “ugly” elements?
- styled-file-picker — File input types
- simple-start-file — Basic starter file
- optional-required-styles — Styling inputs based on whether they are required or not
- aria-website-no-roles — Signposts/Landmarks
- aria-website-roles — Signposts/Landmarks
- form — Styling text input elements
- images-forms2-start — Images and forms 2
- images-forms2-finish — Images and forms 2
- forms-2 — Images and forms 3
- form-anatomy — The anatomy of a form
- form-other-controls — Other control types
- forms-buttons-1-finished — Forms and buttons 1
- forms-buttons-2 — Forms and buttons 2
- forms-buttons-2-finished — Forms and buttons 2
- forms-buttons-3 — Forms and buttons 3
- forms-buttons-3-finished — Forms and buttons 3
- forms-buttons-4 — Forms and buttons 4
- forms-buttons-4-finished — Forms and buttons 4
- forms-buttons-6 — Forms and buttons 6
- forms-buttons-6-finished — Forms and buttons 6
- form-finished — Example
For most of them, the <form> element is actually there to do something. However, we may be able to either remove the submit button, or add a preventDefault handler.
I believe this is fixed.