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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use super::*;
/// Implementation of `ElementExt` for `Element`.
///
/// Provides convenient methods for manipulating DOM element attributes and properties,
/// handling special cases for form elements like inputs, textareas, and selects.
impl ElementExt for Element {
/// Removes an attribute or property from the element.
///
/// Handles special cases for form element properties (`value`, `checked`, `disabled`,
/// `selected`, `readonly`, `multiple`) by setting them to their default values
/// rather than removing the attribute.
///
/// # Arguments
///
/// - `&str` - The name of the attribute or property to remove.
fn remove_attribute_or_property(&self, name: &str) {
if name == ATTR_VALUE {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_value(EMPTY_STRING);
return;
}
if let Some(textarea) = self.dyn_ref::<HtmlTextAreaElement>() {
textarea.set_value(EMPTY_STRING);
return;
}
if let Some(select) = self.dyn_ref::<HtmlSelectElement>() {
select.set_value(EMPTY_STRING);
return;
}
}
if name == ATTR_CHECKED
&& let Some(input) = self.dyn_ref::<HtmlInputElement>()
{
input.set_checked(false);
return;
}
if name == ATTR_DISABLED {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_disabled(false);
return;
}
if let Some(button) = self.dyn_ref::<HtmlButtonElement>() {
button.set_disabled(false);
return;
}
if let Some(select) = self.dyn_ref::<HtmlSelectElement>() {
select.set_disabled(false);
return;
}
if let Some(textarea) = self.dyn_ref::<HtmlTextAreaElement>() {
textarea.set_disabled(false);
return;
}
}
if name == ATTR_SELECTED
&& let Some(option) = self.dyn_ref::<HtmlOptionElement>()
{
option.set_selected(false);
return;
}
if name == ATTR_READONLY {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_read_only(false);
return;
}
if let Some(textarea) = self.dyn_ref::<HtmlTextAreaElement>() {
textarea.set_read_only(false);
return;
}
}
if name == ATTR_MULTIPLE {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_multiple(false);
return;
}
if let Some(select) = self.dyn_ref::<HtmlSelectElement>() {
select.set_multiple(false);
return;
}
}
let _: Result<(), JsValue> = self.remove_attribute(name);
}
/// Sets an attribute or property on the element.
///
/// Handles special cases for form element properties (`value`, `checked`, `disabled`,
/// `selected`, `readonly`, `multiple`) by setting the corresponding DOM property
/// rather than the HTML attribute, ensuring proper two-way binding behavior.
///
/// # Arguments
///
/// - `&str` - The name of the attribute or property to set.
/// - `&str` - The value to assign.
fn set_attribute_or_property(&self, name: &str, value: &str) {
if name == ATTR_VALUE {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_value(value);
return;
}
if let Some(textarea) = self.dyn_ref::<HtmlTextAreaElement>() {
textarea.set_value(value);
return;
}
if let Some(select) = self.dyn_ref::<HtmlSelectElement>() {
select.set_value(value);
return;
}
}
if name == ATTR_CHECKED
&& let Some(input) = self.dyn_ref::<HtmlInputElement>()
{
input.set_checked(value == BOOL_TRUE);
return;
}
if name == ATTR_DISABLED {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_disabled(value == BOOL_TRUE);
return;
}
if let Some(button) = self.dyn_ref::<HtmlButtonElement>() {
button.set_disabled(value == BOOL_TRUE);
return;
}
if let Some(select) = self.dyn_ref::<HtmlSelectElement>() {
select.set_disabled(value == BOOL_TRUE);
return;
}
if let Some(textarea) = self.dyn_ref::<HtmlTextAreaElement>() {
textarea.set_disabled(value == BOOL_TRUE);
return;
}
}
if name == ATTR_SELECTED
&& let Some(option) = self.dyn_ref::<HtmlOptionElement>()
{
option.set_selected(value == BOOL_TRUE);
return;
}
if name == ATTR_READONLY {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_read_only(value == BOOL_TRUE);
return;
}
if let Some(textarea) = self.dyn_ref::<HtmlTextAreaElement>() {
textarea.set_read_only(value == BOOL_TRUE);
return;
}
}
if name == ATTR_MULTIPLE {
if let Some(input) = self.dyn_ref::<HtmlInputElement>() {
input.set_multiple(value == BOOL_TRUE);
return;
}
if let Some(select) = self.dyn_ref::<HtmlSelectElement>() {
select.set_multiple(value == BOOL_TRUE);
return;
}
}
let _: Result<(), JsValue> = self.set_attribute(name, value);
}
/// Tracks a signal address on the element for cleanup purposes.
///
/// Resolves the element's `data-euv-id` (assigning a fresh one if
/// absent) and pushes `addr` into the Rust-side
/// [`crate::renderer::signal_addrs::SignalAddrs`] registry. Replaces
/// the previous `data-euv-signal-addrs` DOM attribute round-trip —
/// the cleanup path reads from the registry, never from the DOM.
///
/// # Arguments
///
/// - `usize` - The signal's inner address to track.
fn track_signal_addr(&self, addr: usize) {
let euv_id: usize = match self.get_attribute(DATA_EUV_ID) {
Some(id_str) => id_str.parse::<usize>().unwrap_or_else(|_| {
let new_id: usize = NEXT_EUV_ID.fetch_add(1, Ordering::Relaxed);
let _: Result<(), JsValue> = self.set_attribute(DATA_EUV_ID, &new_id.to_string());
new_id
}),
None => {
let new_id: usize = NEXT_EUV_ID.fetch_add(1, Ordering::Relaxed);
let _: Result<(), JsValue> = self.set_attribute(DATA_EUV_ID, &new_id.to_string());
new_id
}
};
SignalAddrs::push(euv_id, addr);
}
}