mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: update file chooser events (#11057)
This commit is contained in:
parent
eb99509a3a
commit
317f82055b
@ -10,10 +10,10 @@ Closes the file chooser without selecting any files.
|
||||
|
||||
```typescript
|
||||
class FileChooser {
|
||||
cancel(): void;
|
||||
cancel(): Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
**Returns:**
|
||||
|
||||
void
|
||||
Promise<void>
|
||||
|
@ -133,30 +133,37 @@ export class CdpElementHandle<
|
||||
return path.resolve(filePath);
|
||||
}
|
||||
});
|
||||
const {node} = await this.client.send('DOM.describeNode', {
|
||||
objectId: this.id,
|
||||
});
|
||||
const {backendNodeId} = node;
|
||||
|
||||
/* The zero-length array is a special case, it seems that
|
||||
DOM.setFileInputFiles does not actually update the files in that case,
|
||||
so the solution is to eval the element value to a new FileList directly.
|
||||
/**
|
||||
* The zero-length array is a special case, it seems that
|
||||
* DOM.setFileInputFiles does not actually update the files in that case, so
|
||||
* the solution is to eval the element value to a new FileList directly.
|
||||
*/
|
||||
if (files.length === 0) {
|
||||
// XXX: These events should converted to trusted events. Perhaps do this
|
||||
// in `DOM.setFileInputFiles`?
|
||||
await this.evaluate(element => {
|
||||
element.files = new DataTransfer().files;
|
||||
|
||||
// Dispatch events for this case because it should behave akin to a user action.
|
||||
element.dispatchEvent(new Event('input', {bubbles: true}));
|
||||
element.dispatchEvent(
|
||||
new Event('input', {bubbles: true, composed: true})
|
||||
);
|
||||
element.dispatchEvent(new Event('change', {bubbles: true}));
|
||||
});
|
||||
} else {
|
||||
await this.client.send('DOM.setFileInputFiles', {
|
||||
objectId: this.id,
|
||||
files,
|
||||
backendNodeId,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
node: {backendNodeId},
|
||||
} = await this.client.send('DOM.describeNode', {
|
||||
objectId: this.id,
|
||||
});
|
||||
await this.client.send('DOM.setFileInputFiles', {
|
||||
objectId: this.id,
|
||||
files,
|
||||
backendNodeId,
|
||||
});
|
||||
}
|
||||
|
||||
@throwIfDisposed()
|
||||
|
@ -87,11 +87,16 @@ export class FileChooser {
|
||||
/**
|
||||
* Closes the file chooser without selecting any files.
|
||||
*/
|
||||
cancel(): void {
|
||||
async cancel(): Promise<void> {
|
||||
assert(
|
||||
!this.#handled,
|
||||
'Cannot cancel FileChooser which is already handled!'
|
||||
);
|
||||
this.#handled = true;
|
||||
// XXX: These events should converted to trusted events. Perhaps do this
|
||||
// in `DOM.setFileInputFiles`?
|
||||
await this.#element.evaluate(element => {
|
||||
element.dispatchEvent(new Event('cancel', {bubbles: true}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -358,7 +358,7 @@ describe('input tests', function () {
|
||||
let error!: Error;
|
||||
|
||||
try {
|
||||
fileChooser.cancel();
|
||||
await fileChooser.cancel();
|
||||
} catch (error_) {
|
||||
error = error_ as Error;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user