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

🐛 Add missing cancel event to <input type=file> #29639

Merged
merged 19 commits into from
Nov 20, 2023
Merged

Conversation

FurryR
Copy link
Contributor

@FurryR FurryR commented Oct 14, 2023

Description

Add cancel event to <input type=file>.

Motivation

👎 People are using hacks (window.addEventListener('focus')) to detect cancellations of file dialogs.

An example from GoogleChromeLabs/browser-fs-access

However, in https://html.spec.whatwg.org/multipage/indices.html#event-cancel:

Fired at dialog elements when they are canceled by the user (e.g., by pressing the Escape key), or at input elements in the File state when the user does not change their selection

It helps a lot.

function select_file() {
  return new Promise((resolve, reject) => {
    const elem = document.createElement('input')
    elem.type = 'file'
    elem.addEventListener('cancel', () => {
      reject(new Error('Cancelled')) // hooray!
    })
    elem.addEventListener('change', () => {
      if (elem.files.length == 1) resolve(elem.files.item(0))
      else reject(new Error('Cancelled'))
    })
    elem.click()
  })
}

Additional details

Reference: https://html.spec.whatwg.org/multipage/indices.html#event-cancel

Related issues and pull requests

🔨 Fixes #22157.

https://html.spec.whatwg.org/multipage/indices.html#event-cancel:
Fired at dialog elements when they are canceled by the user (e.g., by pressing the Escape key), or at input elements in the File state when the user does not change their selection
@FurryR FurryR requested a review from a team as a code owner October 14, 2023 08:10
@FurryR FurryR requested review from estelle and removed request for a team October 14, 2023 08:10
@github-actions github-actions bot added the Content:HTML Hypertext Markup Language docs label Oct 14, 2023
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@FurryR FurryR marked this pull request as draft October 14, 2023 08:16
@FurryR FurryR marked this pull request as ready for review October 14, 2023 08:36
@github-actions
Copy link
Contributor

github-actions bot commented Oct 16, 2023

Preview URLs (7 pages)
Flaws (9)

Note! 4 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/HTMLElement/cancel_event
Title: HTMLElement: cancel event
Flaw count: 1

  • bad_bcd_queries:
    • No BCD data for query: api.HTMLElement.cancel_event

URL: /en-US/docs/Web/API/Element
Title: Element
Flaw count: 1

  • macros:
    • /en-US/docs/Web/API/Element/getBoxQuads does not exist

URL: /en-US/docs/Web/Events
Title: Event reference
Flaw count: 7

  • broken_links:
    • Can't resolve /en-US/docs/Web/API/Element/cancel_event
    • Can't resolve /en-US/docs/Web/API/SVGElement/abort_event
    • Can't resolve /en-US/docs/Web/API/SVGElement/resize_event
    • Can't resolve /en-US/docs/Web/API/SVGElement/scroll_event
    • Can't resolve /en-US/docs/Web/API/SVGElement/unload_event
    • and 2 more flaws omitted

(comment last updated: 2023-11-20 14:54:46)

@estelle
Copy link
Member

estelle commented Oct 16, 2023

Before adding a link to the cancel event in the technical summary, the page needs to be created. See my comment in the issue

@FurryR
Copy link
Contributor Author

FurryR commented Nov 4, 2023

Before adding a link to the cancel event in the technical summary, the page needs to be created. See my comment in the issue

Sorry for replying so late, I will create the page later.

@FurryR FurryR marked this pull request as draft November 4, 2023 04:03
@github-actions github-actions bot added the Content:WebAPI Web API docs label Nov 4, 2023
@github-actions github-actions bot added the Content:Events Events docs label Nov 4, 2023
@FurryR FurryR marked this pull request as ready for review November 4, 2023 04:56
@FurryR FurryR requested review from a team as code owners November 4, 2023 04:56
@FurryR FurryR requested review from wbamberg and removed request for a team November 4, 2023 04:56
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

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

I made some suggested edits. I think we need to be clearer about the multiple ways the cancel event may be thrown.

@FurryR
Copy link
Contributor Author

FurryR commented Nov 11, 2023

@estelle suggested changes are all merged. Don't mind the commit messages, I forgot that #1 means #1.

Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

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

I find the behavior of reselecting the same files unexpected, so want to make sure that it's clear. I made two suggestions to emphasize that. I am not sure if my suggestions are overkill.

also, when you're ready for a re-review, click the little icon next to my name so that you see a small brown dot instead of the icon. This informs me that the PR is ready for re-review.

@teoli2003 teoli2003 removed request for a team and wbamberg November 18, 2023 08:57
FurryR and others added 2 commits November 19, 2023 09:10
Co-authored-by: Estelle Weyl <[email protected]>
Co-authored-by: Estelle Weyl <[email protected]>
@FurryR

This comment was marked as spam.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

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

THank you!

@estelle estelle merged commit 0900a56 into mdn:main Nov 20, 2023
@skyclouds2001
Copy link
Contributor

I think instead of moving page from HTMLDialogElement: cancel to HTMLElement: cancel, it is better to create a page for HTMLInputElement: cancel, this is also the behavior of BCD

@FurryR FurryR deleted the patch-1 branch September 27, 2024 13:00
skyclouds2001 added a commit to skyclouds2001/content that referenced this pull request Oct 22, 2024
estelle added a commit that referenced this pull request Oct 29, 2024
* Revert ":bug: Add missing `cancel` event to `<input type=file>` (#29639)"

This reverts commit 0900a56.

* update & finish docs

* Update files/en-us/web/api/htmlinputelement/cancel_event/index.md

Co-authored-by: Joshua Chen <[email protected]>

---------

Co-authored-by: Estelle Weyl <[email protected]>
Co-authored-by: Joshua Chen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Events Events docs Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document HTMLInputElement's (type=file) cancel event
3 participants