1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#[macro_use]
mod macros;
use cfg_if::cfg_if;
use cfg_match::cfg_match;
cfg_if! {
if #[cfg(feature = "std_web")] {
mod listener_stdweb;
use stdweb::js;
use stdweb::unstable::{TryFrom, TryInto};
use stdweb::web::html_element::{InputElement, SelectElement, TextAreaElement};
use stdweb::web::{Element, EventListenerHandle, FileList, IElement, INode};
pub use listener_stdweb::*;
} else if #[cfg(feature = "web_sys")] {
mod listener_web_sys;
use wasm_bindgen::JsCast;
use web_sys::{
Element, FileList, HtmlInputElement as InputElement, HtmlSelectElement as SelectElement,
HtmlTextAreaElement as TextAreaElement,
};
pub use listener_web_sys::*;
}
}
#[derive(Debug)]
pub struct InputData {
pub value: String,
}
#[derive(Debug)]
pub enum ChangeData {
Value(String),
Select(SelectElement),
Files(FileList),
}
fn oninput_handler(this: &Element) -> InputData {
let (v1, v2) = cfg_match! {
feature = "std_web" => ({
(
this.clone()
.try_into()
.map(|input: InputElement| input.raw_value())
.ok(),
this.clone()
.try_into()
.map(|input: TextAreaElement| input.value())
.ok(),
)
}),
feature = "web_sys" => ({
(
this.dyn_ref().map(|input: &InputElement| input.value()),
this.dyn_ref().map(|input: &TextAreaElement| input.value()),
)
}),
};
let v3 = this.text_content();
let value = v1.or(v2).or(v3)
.expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener");
InputData { value }
}
fn onchange_handler(this: &Element) -> ChangeData {
match this.node_name().as_ref() {
"INPUT" => {
let input = cfg_match! {
feature = "std_web" => InputElement::try_from(this.clone()).unwrap(),
feature = "web_sys" => this.dyn_ref::<InputElement>().unwrap(),
};
let is_file = input
.get_attribute("type")
.map(|value| value.eq_ignore_ascii_case("file"))
.unwrap_or(false);
if is_file {
let files: FileList = cfg_match! {
feature = "std_web" => js!( return @{input}.files; ).try_into().unwrap(),
feature = "web_sys" => input.files().unwrap(),
};
ChangeData::Files(files)
} else {
cfg_match! {
feature = "std_web" => ChangeData::Value(input.raw_value()),
feature = "web_sys" => ChangeData::Value(input.value()),
}
}
}
"TEXTAREA" => {
let tae = cfg_match! {
feature = "std_web" => TextAreaElement::try_from(this.clone()).unwrap(),
feature = "web_sys" => this.dyn_ref::<TextAreaElement>().unwrap(),
};
ChangeData::Value(tae.value())
}
"SELECT" => {
let se = cfg_match! {
feature = "std_web" => SelectElement::try_from(this.clone()).unwrap(),
feature = "web_sys" => this.dyn_ref::<SelectElement>().unwrap().clone(),
};
ChangeData::Select(se)
}
_ => {
panic!("only an InputElement, TextAreaElement or SelectElement can have an onchange event listener");
}
}
}
#[cfg(feature = "std_web")]
#[derive(Debug)]
pub struct EventListener(Option<EventListenerHandle>);
#[cfg(feature = "std_web")]
impl Drop for EventListener {
fn drop(&mut self) {
if let Some(event) = self.0.take() {
event.remove()
}
}
}