-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#833@minor: Adds support for Window.navigator.clipboard and Window.na…
…vigator.permissions. Improves support for DataTransfer.
- Loading branch information
1 parent
bce679e
commit 2b60090
Showing
17 changed files
with
661 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import DOMException from '../exception/DOMException.js'; | ||
import IWindow from '../window/IWindow.js'; | ||
import ClipboardItem from './ClipboardItem.js'; | ||
import Blob from '../file/Blob.js'; | ||
|
||
/** | ||
* Clipboard API. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/Clipboard. | ||
*/ | ||
export default class Clipboard { | ||
#ownerWindow: IWindow; | ||
#data: ClipboardItem[] = []; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ownerWindow Owner window. | ||
*/ | ||
constructor(ownerWindow: IWindow) { | ||
this.#ownerWindow = ownerWindow; | ||
} | ||
|
||
/** | ||
* Returns data. | ||
* | ||
* @returns Data. | ||
*/ | ||
public async read(): Promise<ClipboardItem[]> { | ||
const permissionStatus = await this.#ownerWindow.navigator.permissions.query({ | ||
name: 'clipboard-read' | ||
}); | ||
if (permissionStatus.state === 'denied') { | ||
throw new DOMException(`Failed to execute 'read' on 'Clipboard': The request is not allowed`); | ||
} | ||
return this.#data; | ||
} | ||
|
||
/** | ||
* Returns text. | ||
* | ||
* @returns Text. | ||
*/ | ||
public async readText(): Promise<string> { | ||
const permissionStatus = await this.#ownerWindow.navigator.permissions.query({ | ||
name: 'clipboard-read' | ||
}); | ||
if (permissionStatus.state === 'denied') { | ||
throw new DOMException(`Failed to execute 'read' on 'Clipboard': The request is not allowed`); | ||
} | ||
let text = ''; | ||
for (const item of this.#data) { | ||
text += await (await item.getType('text/plain')).text(); | ||
} | ||
return text; | ||
} | ||
|
||
/** | ||
* Writes data. | ||
* | ||
* @param data Data. | ||
*/ | ||
public async write(data: ClipboardItem[]): Promise<void> { | ||
const permissionStatus = await this.#ownerWindow.navigator.permissions.query({ | ||
name: 'clipboard-write' | ||
}); | ||
if (permissionStatus.state === 'denied') { | ||
throw new DOMException( | ||
`Failed to execute 'write' on 'Clipboard': The request is not allowed` | ||
); | ||
} | ||
this.#data = data; | ||
} | ||
|
||
/** | ||
* Writes text. | ||
* | ||
* @param text Text. | ||
*/ | ||
public async writeText(text: string): Promise<void> { | ||
const permissionStatus = await this.#ownerWindow.navigator.permissions.query({ | ||
name: 'clipboard-write' | ||
}); | ||
if (permissionStatus.state === 'denied') { | ||
throw new DOMException( | ||
`Failed to execute 'write' on 'Clipboard': The request is not allowed` | ||
); | ||
} | ||
this.#data = [new ClipboardItem({ 'text/plain': new Blob([text], { type: 'text/plain' }) })]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import DOMException from '../exception/DOMException.js'; | ||
import Blob from '../file/Blob.js'; | ||
|
||
/** | ||
* Clipboard Item API. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem. | ||
*/ | ||
export default class ClipboardItem { | ||
public readonly presentationStyle: 'unspecified' | 'inline' | 'attachment' = 'unspecified'; | ||
#data: { [mimeType: string]: Blob }; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param data Data. | ||
* @param [options] Options. | ||
* @param [options.presentationStyle] Presentation style. | ||
*/ | ||
constructor( | ||
data: { [mimeType: string]: Blob }, | ||
options?: { presentationStyle?: 'unspecified' | 'inline' | 'attachment' } | ||
) { | ||
for (const mimeType of Object.keys(data)) { | ||
if (mimeType !== data[mimeType].type) { | ||
throw new DOMException(`Type ${mimeType} does not match the blob's type`); | ||
} | ||
} | ||
this.#data = data; | ||
if (options?.presentationStyle) { | ||
this.presentationStyle = options.presentationStyle; | ||
} | ||
} | ||
|
||
/** | ||
* Returns types. | ||
* | ||
* @returns Types. | ||
*/ | ||
public get types(): string[] { | ||
return Object.keys(this.#data); | ||
} | ||
|
||
/** | ||
* Returns data by type. | ||
* | ||
* @param type Type. | ||
* @returns Data. | ||
*/ | ||
public async getType(type: string): Promise<Blob> { | ||
if (!this.#data[type]) { | ||
throw new DOMException( | ||
"Failed to execute 'getType' on 'ClipboardItem': The type was not found" | ||
); | ||
} | ||
return this.#data[type]; | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
packages/happy-dom/src/config/NonImplemenetedElementClasses.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import DataTransfer from '../DataTransfer.js'; | ||
import Event from '../Event.js'; | ||
import IClipboardEventInit from './IClipboardEventInit.js'; | ||
|
||
/** | ||
* | ||
*/ | ||
export default class ClipboardEvent extends Event { | ||
public clipboardData: DataTransfer | null; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param type Event type. | ||
* @param [eventInit] Event init. | ||
*/ | ||
constructor(type: string, eventInit: IClipboardEventInit | null = null) { | ||
super(type, eventInit); | ||
|
||
this.clipboardData = eventInit?.clipboardData ?? null; | ||
} | ||
} |
Oops, something went wrong.