servo/components/script/dom/datatransferitem.rs
Josh Matthews c94ac5bccb
Move various reflector types and traits to script_bindings (#35279)
* script: Move Reflector to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Extract global() helper from DomObject into new trait. Move DomObject and related traits to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
2025-02-04 06:58:08 +00:00

75 lines
2.3 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::rc::Rc;
use dom_struct::dom_struct;
use crate::dom::bindings::callback::ExceptionHandling;
use crate::dom::bindings::codegen::Bindings::DataTransferItemBinding::{
DataTransferItemMethods, FunctionStringCallback,
};
use crate::dom::bindings::reflector::{reflect_dom_object, DomGlobal, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::file::File;
use crate::dom::globalscope::GlobalScope;
use crate::drag_data_store::Kind;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct DataTransferItem {
reflector_: Reflector,
#[ignore_malloc_size_of = "TODO"]
#[no_trace]
item: Kind,
}
impl DataTransferItem {
fn new_inherited(item: Kind) -> DataTransferItem {
DataTransferItem {
reflector_: Reflector::new(),
item,
}
}
pub(crate) fn new(
global: &GlobalScope,
can_gc: CanGc,
item: Kind,
) -> DomRoot<DataTransferItem> {
reflect_dom_object(
Box::new(DataTransferItem::new_inherited(item)),
global,
can_gc,
)
}
}
impl DataTransferItemMethods<crate::DomTypeHolder> for DataTransferItem {
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-kind>
fn Kind(&self) -> DOMString {
match self.item {
Kind::Text(_) => DOMString::from("string"),
Kind::File(_) => DOMString::from("file"),
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-type>
fn Type(&self) -> DOMString {
self.item.type_()
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-getasstring>
fn GetAsString(&self, callback: Option<Rc<FunctionStringCallback>>) {
if let (Some(callback), Some(data)) = (callback, self.item.as_string()) {
let _ = callback.Call__(data, ExceptionHandling::Report);
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-getasfile>
fn GetAsFile(&self, can_gc: CanGc) -> Option<DomRoot<File>> {
self.item.as_file(&self.global(), can_gc)
}
}