use core::ffi::c_void;
use std::sync::{OnceLock, RwLock};
use super::util::*;
use crate::glue;
pub trait ClipboardHost: Send + Sync {
fn platform(&self) -> &'static str;
fn read_text(&self) -> Result<String, String>;
fn write_text(&self, text: &str) -> Result<(), String>;
fn item_types(&self) -> &'static [&'static str] {
&[]
}
fn write_items(&self, items: &[(String, Vec<u8>)]) -> Result<(), String> {
let _ = items;
Err("this clipboard backend writes text only".into())
}
fn read_items(&self) -> Result<Vec<(String, Vec<u8>)>, String> {
let text = self.read_text()?;
Ok(if text.is_empty() { Vec::new() } else { vec![("text/plain".to_string(), text.into_bytes())] })
}
}
pub const ITEM_TYPES: &[&str] = &["text/plain", "text/html", "image/png"];
static HOST: RwLock<Option<&'static dyn ClipboardHost>> = RwLock::new(None);
pub fn set_host(host: &'static dyn ClipboardHost) {
*HOST.write().unwrap() = Some(host);
}
pub fn host() -> Option<&'static dyn ClipboardHost> {
*HOST.read().unwrap()
}
fn advise_no_host() {
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
eprintln!(
"[redjs] navigator.clipboard: no ClipboardHost installed — every call rejects. \
Call redjs::webapi::clipboard::set_host()."
);
});
}
unsafe fn rejected_named(g: *mut c_void, name: &str, msg: &str) -> u64 {
super::domexception::rejected(g, name, msg)
}
#[crate::host_fn]
unsafe fn clipboard_read_text(g: *mut c_void, _cf: *mut c_void) -> u64 {
match host() {
None => {
advise_no_host();
rejected_named(
g,
"NotAllowedError",
"Failed to execute 'readText' on 'Clipboard': no clipboard backend is installed \
in this runtime.",
)
}
Some(h) => match h.read_text() {
Ok(text) => resolved_promise(g, js_string(g, &text)),
Err(why) => rejected_named(
g,
"NotAllowedError",
&format!("Failed to execute 'readText' on 'Clipboard': {why}"),
),
},
}
}
#[crate::host_fn]
unsafe fn clipboard_write_text(g: *mut c_void, cf: *mut c_void) -> u64 {
let arg = cf_arg(cf, 0);
let mut sv = arg;
if glue::rjs_kind(g, arg) != 5 && glue::rjs_to_string(g, arg, &mut sv) != 0 {
return super::domexception::rejected_with(g, sv);
}
let text = read_string(g, sv).unwrap_or_default();
match host() {
None => {
advise_no_host();
rejected_named(
g,
"NotAllowedError",
"Failed to execute 'writeText' on 'Clipboard': no clipboard backend is installed \
in this runtime.",
)
}
Some(h) => match h.write_text(&text) {
Ok(()) => resolved_promise(g, UNDEF),
Err(why) => rejected_named(
g,
"NotAllowedError",
&format!("Failed to execute 'writeText' on 'Clipboard': {why}"),
),
},
}
}
#[derive(Clone, Copy)]
struct Slots {
proto: u64,
items: *mut c_void,
types: *mut c_void,
style: *mut c_void,
}
unsafe impl Send for Slots {}
fn registry() -> &'static std::sync::Mutex<std::collections::HashMap<usize, Slots>> {
static R: OnceLock<std::sync::Mutex<std::collections::HashMap<usize, Slots>>> = OnceLock::new();
R.get_or_init(Default::default)
}
fn slots(g: *mut c_void) -> Option<Slots> {
registry().lock().unwrap().get(&(g as usize)).copied()
}
pub(crate) fn forget(gone: crate::context::Gone) {
registry().lock().unwrap().remove(&gone.key);
}
#[derive(Clone, Copy)]
enum Slot {
Items,
Types,
Style,
}
unsafe fn slot_get(g: *mut c_void, obj: u64, slot: Slot) -> u64 {
let Some(s) = slots(g) else { return UNDEF };
if !is_cell(obj) {
return UNDEF;
}
get_a(g, obj, match slot {
Slot::Items => s.items,
Slot::Types => s.types,
Slot::Style => s.style,
})
}
unsafe fn slot_set(g: *mut c_void, obj: u64, slot: Slot, v: u64) {
let Some(s) = slots(g) else { return };
let a = match slot {
Slot::Items => s.items,
Slot::Types => s.types,
Slot::Style => s.style,
};
glue::rjs_define(g, obj, a, v, 0);
}
const PNG_SIGNATURE: &[u8] = b"\x89PNG\r\n\x1a\n";
unsafe fn call_static(g: *mut c_void, a: &str, b: &str, args: &[u64]) -> Result<u64, u64> {
let owner = get(g, glue::rjs_global(g), a);
call(g, get(g, owner, b), owner, args)
}
unsafe fn bind1(g: *mut c_void, f: u64, arg: u64) -> u64 {
call(g, get(g, f, "bind"), f, &[UNDEF, arg]).unwrap_or(f)
}
unsafe fn then(g: *mut c_void, p: u64, f: u64) -> u64 {
match call(g, get(g, p, "then"), p, &[f]) {
Ok(v) => v,
Err(e) => super::domexception::rejected_with(g, e),
}
}
unsafe fn dom_string(g: *mut c_void, v: u64) -> Result<String, u64> {
let mut sv = v;
if glue::rjs_kind(g, v) != 5 && glue::rjs_to_string(g, v, &mut sv) != 0 {
return Err(sv);
}
Ok(read_string(g, sv).unwrap_or_default())
}
unsafe fn make_item(g: *mut c_void, proto: u64, record: u64, types: u64, style: &str) -> u64 {
let item = new_object(g, proto);
slot_set(g, item, Slot::Items, record);
slot_set(g, item, Slot::Types, types);
slot_set(g, item, Slot::Style, js_string(g, style));
item
}
unsafe fn item_type_list(g: *mut c_void, types: u64) -> Vec<String> {
(0..array_len(g, types)).filter_map(|i| read_string(g, get_i(g, types, i))).collect()
}
#[crate::host_fn]
unsafe fn item_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
const WHO: &str = "Failed to construct 'ClipboardItem'";
let items = cf_arg(cf, 0);
if !super::blob::is_object(g, items) {
return throw_type_error(
g,
&format!("{WHO}: The provided value is not of type '(record<DOMString, (Blob or DOMString or Promise<(Blob or DOMString)>)>)'."),
);
}
let keys = match call_static(g, "Object", "keys", &[items]) {
Ok(k) => k,
Err(e) => {
glue::rjs_throw(g, e);
return UNDEF;
}
};
let n = array_len(g, keys);
if n == 0 {
return throw_type_error(g, &format!("{WHO}: Empty dictionary argument"));
}
let record = new_object(g, UNDEF);
let types = new_array(g);
for i in 0..n {
let key = get_i(g, keys, i);
let Some(name) = read_string(g, key) else { continue };
let a = atom(g, &name);
let mut v = UNDEF;
let rc = glue::rjs_get(g, items, a, &mut v);
glue::rjs_atom_free(g, a);
if rc != 0 {
glue::rjs_throw(g, v);
return UNDEF;
}
define(g, record, &name, v, 0);
set_i(g, types, i, key);
}
let _ = call_static(g, "Object", "freeze", &[types]);
let mut style = "unspecified".to_string();
let options = cf_arg(cf, 1);
if super::blob::is_object(g, options) {
let v = get(g, options, "presentationStyle");
if v != UNDEF {
match dom_string(g, v) {
Ok(s) if matches!(s.as_str(), "unspecified" | "inline" | "attachment") => style = s,
Ok(s) => {
return throw_type_error(
g,
&format!("{WHO}: Failed to read the 'presentationStyle' property from 'ClipboardItemOptions': The provided value '{s}' is not a valid enum value of type PresentationStyle."),
)
}
Err(e) => {
glue::rjs_throw(g, e);
return UNDEF;
}
}
}
}
let proto = slots(g).map_or(UNDEF, |s| s.proto);
let this = ordinary_create(g, cf_new_target(cf), proto);
slot_set(g, this, Slot::Items, record);
slot_set(g, this, Slot::Types, types);
slot_set(g, this, Slot::Style, js_string(g, &style));
this
}
#[crate::host_fn]
unsafe fn item_types(g: *mut c_void, cf: *mut c_void) -> u64 {
let types = slot_get(g, cf_this(cf), Slot::Types);
if !is_cell(types) {
return throw_type_error(g, "Illegal invocation");
}
types
}
#[crate::host_fn]
unsafe fn item_style(g: *mut c_void, cf: *mut c_void) -> u64 {
let style = slot_get(g, cf_this(cf), Slot::Style);
if style == UNDEF {
return throw_type_error(g, "Illegal invocation");
}
style
}
#[crate::host_fn]
unsafe fn to_blob(g: *mut c_void, cf: *mut c_void) -> u64 {
let ty = read_string(g, cf_arg(cf, 0)).unwrap_or_default();
let v = cf_arg(cf, 1);
if super::blob::data_of(g, v).is_some() {
return v;
}
match dom_string(g, v) {
Ok(s) => super::blob::create(g, UNDEF, s.into_bytes(), ty),
Err(e) => {
glue::rjs_throw(g, e);
UNDEF
}
}
}
#[crate::host_fn]
unsafe fn item_get_type(g: *mut c_void, cf: *mut c_void) -> u64 {
let this = cf_this(cf);
let record = slot_get(g, this, Slot::Items);
if !is_cell(record) {
return rejected_promise(g, 1, "Illegal invocation");
}
let ty = match dom_string(g, cf_arg(cf, 0)) {
Ok(t) => t,
Err(e) => return super::domexception::rejected_with(g, e),
};
if !item_type_list(g, slot_get(g, this, Slot::Types)).contains(&ty) {
return rejected_named(
g,
"NotFoundError",
"Failed to execute 'getType' on 'ClipboardItem': The type was not found",
);
}
let v = get(g, record, &ty);
let settled = match call_static(g, "Promise", "resolve", &[v]) {
Ok(p) => p,
Err(e) => return super::domexception::rejected_with(g, e),
};
let reaction = bind1(g, make_fn(g, "", 2, to_blob), js_string(g, &ty));
then(g, settled, reaction)
}
#[crate::host_fn]
unsafe fn item_supports(g: *mut c_void, cf: *mut c_void) -> u64 {
let ty = match dom_string(g, cf_arg(cf, 0)) {
Ok(t) => t,
Err(e) => {
glue::rjs_throw(g, e);
return UNDEF;
}
};
let takes = host().map(|h| h.item_types()).unwrap_or(&[]);
enc_bool(takes.contains(&ty.as_str()))
}
#[crate::host_fn]
unsafe fn clipboard_read(g: *mut c_void, _cf: *mut c_void) -> u64 {
const WHO: &str = "Failed to execute 'read' on 'Clipboard'";
let Some(h) = host() else {
advise_no_host();
return rejected_named(g, "NotAllowedError", &format!("{WHO}: no clipboard backend is installed in this runtime."));
};
let items = match h.read_items() {
Ok(items) => items,
Err(why) => return rejected_named(g, "NotAllowedError", &format!("{WHO}: {why}")),
};
if items.is_empty() {
return rejected_named(g, "DataError", "No valid data on clipboard.");
}
let record = new_object(g, UNDEF);
let types = new_array(g);
for (i, (ty, bytes)) in items.into_iter().enumerate() {
let blob = super::blob::create(g, UNDEF, bytes, ty.clone());
define(g, record, &ty, blob, 0);
set_i(g, types, i as u32, js_string(g, &ty));
}
let _ = call_static(g, "Object", "freeze", &[types]);
let proto = slots(g).map_or(UNDEF, |s| s.proto);
let list = new_array(g);
set_i(g, list, 0, make_item(g, proto, record, types, "unspecified"));
resolved_promise(g, list)
}
#[crate::host_fn]
unsafe fn write_settled(g: *mut c_void, cf: *mut c_void) -> u64 {
const WHO: &str = "Failed to execute 'write' on 'Clipboard'";
let types = item_type_list(g, cf_arg(cf, 0));
let values = cf_arg(cf, 1);
let mut items = Vec::with_capacity(types.len());
for (i, ty) in types.into_iter().enumerate() {
let v = get_i(g, values, i as u32);
let bytes = match super::blob::bytes_of(g, v, "navigator.clipboard.write()") {
Some(Ok(bytes)) => bytes,
Some(Err(_)) => {
return super::domexception::throw(g, "DataError", &format!("Failed to read or decode Blob for clipboard item type {ty}."))
}
None => match dom_string(g, v) {
Ok(s) => s.into_bytes(),
Err(e) => {
glue::rjs_throw(g, e);
return UNDEF;
}
},
};
if ty == "image/png" && !bytes.starts_with(PNG_SIGNATURE) {
return super::domexception::throw(g, "DataError", "Failed to read or decode Blob for clipboard item type image/png.");
}
items.push((ty, bytes));
}
let Some(h) = host() else {
return super::domexception::throw(g, "NotAllowedError", &format!("{WHO}: no clipboard backend is installed in this runtime."));
};
match h.write_items(&items) {
Ok(()) => UNDEF,
Err(why) => super::domexception::throw(g, "NotAllowedError", &format!("{WHO}: {why}")),
}
}
#[crate::host_fn]
unsafe fn clipboard_write(g: *mut c_void, cf: *mut c_void) -> u64 {
const WHO: &str = "Failed to execute 'write' on 'Clipboard'";
let list = match call_static(g, "Array", "from", &[cf_arg(cf, 0)]) {
Ok(l) => l,
Err(e) => return super::domexception::rejected_with(g, e),
};
let n = array_len(g, list);
if n == 0 {
return resolved_promise(g, UNDEF);
}
if n > 1 {
return rejected_named(g, "NotAllowedError", "Support for multiple ClipboardItems is not implemented.");
}
let item = get_i(g, list, 0);
let record = slot_get(g, item, Slot::Items);
let types = slot_get(g, item, Slot::Types);
if !is_cell(record) || !is_cell(types) {
return rejected_promise(g, 1, &format!("{WHO}: The provided value is not of type 'ClipboardItem'."));
}
let Some(h) = host() else {
advise_no_host();
return rejected_named(g, "NotAllowedError", &format!("{WHO}: no clipboard backend is installed in this runtime."));
};
let names = item_type_list(g, types);
if let Some(ty) = names.iter().find(|t| !h.item_types().contains(&t.as_str())) {
return rejected_named(g, "NotAllowedError", &format!("{WHO}: Type {ty} not supported on write."));
}
let values = new_array(g);
for (i, ty) in names.iter().enumerate() {
set_i(g, values, i as u32, get(g, record, ty));
}
let all = match call_static(g, "Promise", "all", &[values]) {
Ok(p) => p,
Err(e) => return super::domexception::rejected_with(g, e),
};
let reaction = bind1(g, make_fn(g, "", 2, write_settled), types);
then(g, all, reaction)
}
#[crate::host_fn]
unsafe fn exec_command(g: *mut c_void, cf: *mut c_void) -> u64 {
let cmd = read_string(g, cf_arg(cf, 0)).unwrap_or_default().to_ascii_lowercase();
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
eprintln!(
"[redjs] document.execCommand({cmd:?}) did nothing and returned false. execCommand is \
deprecated in every browser and depends on a contenteditable selection this runtime \
does not have; for the clipboard commands use navigator.clipboard.writeText() / \
readText() instead."
);
});
FALSE
}
#[crate::host_fn]
unsafe fn query_command_false(_g: *mut c_void, _cf: *mut c_void) -> u64 {
FALSE
}
pub unsafe fn install(g: *mut c_void, event_target_proto: u64) {
let clipboard_class = define_class(g, &ClassSpec {
name: "Clipboard",
arity: 0,
construct: None,
parent_proto: event_target_proto,
methods: &[
("readText", 0, clipboard_read_text),
("writeText", 1, clipboard_write_text),
("read", 0, clipboard_read),
("write", 1, clipboard_write),
],
getters: &[],
accessors: &[],
});
let clipboard = new_object(g, clipboard_class.proto);
crate::ffi::redjs_protect(g, clipboard);
let item_class = define_class(g, &ClassSpec {
name: "ClipboardItem",
arity: 1,
construct: Some(item_construct),
parent_proto: 0,
methods: &[("getType", 1, item_get_type)],
getters: &[("types", item_types), ("presentationStyle", item_style)],
accessors: &[],
});
define(g, item_class.ctor, "supports", make_fn(g, "supports", 1, item_supports), 2);
registry().lock().unwrap().insert(
g as usize,
Slots {
proto: item_class.proto,
items: atom_priv(g, "redjs#clipboardItems"),
types: atom_priv(g, "redjs#clipboardTypes"),
style: atom_priv(g, "redjs#clipboardStyle"),
},
);
let global = glue::rjs_global(g);
let existing = get(g, global, "navigator");
let navigator = if is_cell(existing) {
existing
} else {
let n = new_object(g, UNDEF);
define(g, global, "navigator", n, 2 );
n
};
define(g, navigator, "clipboard", clipboard, 2);
let document = state(g).document;
if is_cell(document) {
define(g, document, "execCommand", make_fn(g, "execCommand", 3, exec_command), 2);
define(
g,
document,
"queryCommandSupported",
make_fn(g, "queryCommandSupported", 1, query_command_false),
2,
);
define(
g,
document,
"queryCommandEnabled",
make_fn(g, "queryCommandEnabled", 1, query_command_false),
2,
);
}
}
#[cfg(test)]
mod jsc_tests {
use super::*;
use crate::api::Context;
use crate::webapi::testing::*;
struct FakeClipboard;
static TEXT: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());
static ITEMS: std::sync::Mutex<Vec<(String, Vec<u8>)>> = std::sync::Mutex::new(Vec::new());
static FAIL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
static TEXT_ONLY: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
impl ClipboardHost for FakeClipboard {
fn platform(&self) -> &'static str {
"scripted"
}
fn read_text(&self) -> Result<String, String> {
if FAIL.load(std::sync::atomic::Ordering::Relaxed) {
return Err("the clipboard is held by another process".into());
}
Ok(TEXT.lock().unwrap().clone())
}
fn write_text(&self, text: &str) -> Result<(), String> {
if FAIL.load(std::sync::atomic::Ordering::Relaxed) {
return Err("the clipboard is held by another process".into());
}
*TEXT.lock().unwrap() = text.to_string();
ITEMS.lock().unwrap().clear();
Ok(())
}
fn item_types(&self) -> &'static [&'static str] {
if TEXT_ONLY.load(std::sync::atomic::Ordering::Relaxed) { &[] } else { ITEM_TYPES }
}
fn write_items(&self, items: &[(String, Vec<u8>)]) -> Result<(), String> {
if FAIL.load(std::sync::atomic::Ordering::Relaxed) {
return Err("the clipboard is held by another process".into());
}
*ITEMS.lock().unwrap() = items.to_vec();
TEXT.lock().unwrap().clear();
Ok(())
}
fn read_items(&self) -> Result<Vec<(String, Vec<u8>)>, String> {
let items = ITEMS.lock().unwrap().clone();
if !items.is_empty() {
return Ok(items);
}
let text = self.read_text()?;
Ok(if text.is_empty() { Vec::new() } else { vec![("text/plain".into(), text.into_bytes())] })
}
}
fn install_fake() -> std::sync::MutexGuard<'static, ()> {
static L: std::sync::Mutex<()> = std::sync::Mutex::new(());
let guard = L.lock().unwrap_or_else(|e| e.into_inner());
static H: FakeClipboard = FakeClipboard;
set_host(&H);
FAIL.store(false, std::sync::atomic::Ordering::Relaxed);
TEXT_ONLY.store(false, std::sync::atomic::Ordering::Relaxed);
TEXT.lock().unwrap().clear();
ITEMS.lock().unwrap().clear();
guard
}
fn run(c: &Context, body: &str) -> String {
s(
c,
&format!(
"globalThis.__out = '<pending>'; (async () => {{ {body} }})().then(\
v => {{ globalThis.__out = String(v); }}, \
e => {{ globalThis.__out = 'ERROR: ' + (e instanceof DOMException ? '' : 'plain ') + \
e.name + ': ' + ((e && e.message) || e); }}); 'started'"
),
);
for _ in 0..64 {
c.drain_microtasks();
}
s(c, "globalThis.__out")
}
#[test]
fn clipboard_is_a_singleton_on_navigator_and_an_event_target() {
let c = ctx();
ok(&c, "navigator.clipboard === navigator.clipboard");
ok(&c, "navigator.clipboard instanceof Clipboard");
ok(&c, "navigator.clipboard instanceof EventTarget");
ok(&c, "typeof navigator.clipboard.readText === 'function'");
ok(&c, "typeof navigator.clipboard.writeText === 'function'");
ok(&c, "typeof navigator.permissions === 'object'");
assert!(throws(&c, "new Clipboard()").contains("Illegal constructor"));
}
#[test]
fn write_then_read_round_trips_through_the_host() {
let _g = install_fake();
let c = ctx();
assert_eq!(
run(
&c,
"await navigator.clipboard.writeText('hello native');
return await navigator.clipboard.readText();"
),
"hello native"
);
assert_eq!(*TEXT.lock().unwrap(), "hello native");
}
#[test]
fn write_text_coerces_its_argument_like_a_dom_string() {
let _g = install_fake();
let c = ctx();
assert_eq!(
run(
&c,
"await navigator.clipboard.writeText(42);
return await navigator.clipboard.readText();"
),
"42"
);
assert_eq!(run(&c, "return await navigator.clipboard.writeText('');"), "undefined");
let got = run(&c, "return await navigator.clipboard.writeText(Symbol('s'));");
assert!(got.starts_with("ERROR: plain TypeError"), "{got}");
}
#[test]
fn a_backend_failure_is_a_not_allowed_error_not_a_silent_empty_string() {
let _g = install_fake();
FAIL.store(true, std::sync::atomic::Ordering::Relaxed);
let c = ctx();
let got = run(&c, "return await navigator.clipboard.readText();");
assert!(got.starts_with("ERROR: NotAllowedError"), "{got}");
assert!(got.contains("held by another process"), "{got}");
let got = run(&c, "return await navigator.clipboard.writeText('x');");
assert!(got.starts_with("ERROR: NotAllowedError"), "{got}");
}
const PNG: &str = "new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3])";
#[test]
fn a_clipboard_item_is_its_record_read_once() {
let _g = install_fake();
let c = ctx();
ok(&c, "typeof ClipboardItem === 'function'");
ok(&c, "Object.prototype.toString.call(new ClipboardItem({'text/plain': 'x'})) === '[object ClipboardItem]'");
ok(&c, "JSON.stringify(new ClipboardItem({'text/plain': 'x', 'image/png': new Blob([])}).types) === '[\"text/plain\",\"image/png\"]'");
ok(&c, "Object.isFrozen(new ClipboardItem({'text/plain': 'x'}).types)");
ok(&c, "new ClipboardItem({'text/plain': 'x'}).presentationStyle === 'unspecified'");
ok(&c, "new ClipboardItem({'text/plain': 'x'}, {presentationStyle: 'attachment'}).presentationStyle === 'attachment'");
assert!(throws(&c, "new ClipboardItem({})").contains("Empty dictionary argument"));
assert!(throws(&c, "new ClipboardItem('text/plain')").contains("record"));
assert!(throws(&c, "ClipboardItem({'text/plain': 'x'})").contains("requires 'new'"));
assert!(throws(&c, "new ClipboardItem({'text/plain': 'x'}, {presentationStyle: 'nope'})").contains("PresentationStyle"));
ok(&c, "ClipboardItem.supports('image/png') && ClipboardItem.supports('text/html')");
ok(&c, "!ClipboardItem.supports('image/svg+xml') && !ClipboardItem.supports('web text/x-mine')");
assert_eq!(
run(&c, "const b = await new ClipboardItem({'text/plain': 'hi'}).getType('text/plain');
return (b instanceof Blob) + ':' + b.type + ':' + await b.text();"),
"true:text/plain:hi"
);
assert_eq!(
run(&c, "const b = await new ClipboardItem({'text/html': Promise.resolve('<b>x</b>')}).getType('text/html');
return b.type + ':' + await b.text();"),
"text/html:<b>x</b>"
);
let got = run(&c, "return await new ClipboardItem({'text/plain': 'hi'}).getType('image/png');");
assert!(got.starts_with("ERROR: NotFoundError"), "{got}");
}
#[test]
fn write_then_read_round_trips_an_image_and_its_text() {
let _g = install_fake();
let c = ctx();
assert_eq!(
run(
&c,
&format!(
"const png = new Blob([{PNG}], {{type: 'image/png'}});
await navigator.clipboard.write([new ClipboardItem({{
'image/png': Promise.resolve(png), 'text/plain': 'caption'
}})]);
const [item] = await navigator.clipboard.read();
const back = new Uint8Array(await (await item.getType('image/png')).arrayBuffer());
return item.types.join(',') + ':' + back.length + ':' + back[1] + ':' +
await (await item.getType('text/plain')).text();"
)
),
"image/png,text/plain:11:80:caption"
);
let items = ITEMS.lock().unwrap().clone();
assert_eq!(items.len(), 2);
assert_eq!(items[0].0, "image/png");
assert_eq!(&items[0].1[..4], b"\x89PNG");
assert_eq!(items[1], ("text/plain".to_string(), b"caption".to_vec()));
assert_eq!(
run(&c, "await navigator.clipboard.write([new ClipboardItem({'text/plain': 'only'})]);
return (await (await navigator.clipboard.read())[0].getType('text/plain')).text();"),
"only"
);
}
#[test]
fn write_refuses_what_a_browser_refuses_and_writes_nothing() {
let _g = install_fake();
let c = ctx();
*TEXT.lock().unwrap() = "untouched".into();
let got = run(&c, "return await navigator.clipboard.write([new ClipboardItem({'image/png': new Blob([]), 'web text/x-mine': 'x'})]);");
assert!(got.starts_with("ERROR: NotAllowedError"), "{got}");
assert!(got.contains("web text/x-mine"), "{got}");
let got = run(&c, "const i = () => new ClipboardItem({'text/plain': 'x'}); return await navigator.clipboard.write([i(), i()]);");
assert!(got.contains("multiple ClipboardItems"), "{got}");
let got = run(&c, "return await navigator.clipboard.write([new ClipboardItem({'image/png': new Blob(['GIF89a'])})]);");
assert!(got.starts_with("ERROR: DataError"), "{got}");
let got = run(&c, "return await navigator.clipboard.write([new ClipboardItem({'text/plain': Promise.reject(new Error('gone'))})]);");
assert!(got.contains("gone"), "{got}");
let got = run(&c, "return await navigator.clipboard.write([{}]);");
assert!(got.starts_with("ERROR: plain TypeError"), "{got}");
assert_eq!(*TEXT.lock().unwrap(), "untouched");
assert!(ITEMS.lock().unwrap().is_empty());
TEXT.lock().unwrap().clear();
let got = run(&c, "return await navigator.clipboard.read();");
assert!(got.starts_with("ERROR: DataError"), "{got}");
}
#[test]
fn a_text_only_backend_refuses_every_item_type_but_still_reads_its_text() {
let _g = install_fake();
TEXT_ONLY.store(true, std::sync::atomic::Ordering::Relaxed);
let c = ctx();
ok(&c, "!ClipboardItem.supports('text/plain')");
let got = run(&c, "return await navigator.clipboard.write([new ClipboardItem({'text/plain': 'x'})]);");
assert!(got.starts_with("ERROR: NotAllowedError"), "{got}");
*TEXT.lock().unwrap() = "from the OS".into();
assert_eq!(
run(&c, "const [i] = await navigator.clipboard.read(); return (await i.getType('text/plain')).text();"),
"from the OS"
);
}
#[test]
fn exec_command_is_inert_and_says_so_through_its_return_value() {
let c = ctx();
assert_eq!(s(&c, "document.execCommand('copy')"), "false");
assert_eq!(s(&c, "document.execCommand('paste')"), "false");
assert_eq!(s(&c, "document.queryCommandSupported('copy')"), "false");
assert_eq!(s(&c, "document.queryCommandEnabled('copy')"), "false");
}
}