use crate::dom::bindings::codegen::Bindings::CSSKeywordValueBinding::CSSKeywordValueMethods;
use dom_struct::dom_struct;
use js::context::JSContext;
use js::rust::HandleObject;
use script_bindings::cell::DomRefCell;
use script_bindings::reflector::{reflect_dom_object, reflect_dom_object_with_proto};
use super::cssstylevalue::CSSStyleValue;
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
#[dom_struct]
pub(crate) struct CSSKeywordValue {
css_style_value: CSSStyleValue,
value: DomRefCell<String>,
}
impl CSSKeywordValue {
fn new_inherited(value: String) -> CSSKeywordValue {
CSSKeywordValue {
css_style_value: CSSStyleValue::new_inherited(String::new(), None),
value: DomRefCell::new(value),
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &GlobalScope,
value: String,
) -> DomRoot<CSSKeywordValue> {
reflect_dom_object(cx, Box::new(CSSKeywordValue::new_inherited(value)), global)
}
fn new_with_proto(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
value: String,
) -> DomRoot<CSSKeywordValue> {
reflect_dom_object_with_proto(
cx,
Box::new(CSSKeywordValue::new_inherited(value)),
global,
proto,
)
}
pub(crate) fn value(&self) -> String {
self.value.borrow().clone()
}
}
impl CSSKeywordValueMethods<crate::DomTypeHolder> for CSSKeywordValue {
fn Constructor(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
value: USVString,
) -> Fallible<DomRoot<CSSKeywordValue>> {
if value.0.is_empty() {
return Err(Error::Type(
c"CSSKeywordValue cannot be constructed with an empty string".to_owned(),
));
}
Ok(CSSKeywordValue::new_with_proto(cx, global, proto, value.0))
}
fn GetValue(&self) -> Fallible<USVString> {
Ok(USVString(self.value()))
}
fn SetValue(&self, value: USVString) -> ErrorResult {
if value.0.is_empty() {
return Err(Error::Type(
c"CSSKeywordValue cannot be set to an empty string".to_owned(),
));
}
*self.value.borrow_mut() = value.0;
Ok(())
}
}