1use super::*;
10
11fn builtin_member(it: &mut Interp, this: &Value, name: &str) -> Result<Value, Value> {
13 if let Some(idx) = this_dom_idx(this) {
14 return Ok(it.dom_builtin_get(idx, name));
15 }
16 let host = match this {
17 Value::Object(o) => match &o.borrow().kind {
18 ObjKind::HostRef(h) => Some(*h),
19 _ => None,
20 },
21 _ => None,
22 };
23 match host {
24 Some(h) => {
25 let v = it.dom_get_property(crate::os_lib::web_apis::interp::dom_props::DomDisp::Host(h), name);
26 if matches!(v, Value::Undefined) {
28 if let Value::Object(o) = this {
29 if let Some(own) = o.borrow().props.get(name).cloned() {
30 return Ok(own);
31 }
32 }
33 }
34 Ok(v)
35 }
36 None => Err(it.type_error(alloc::format!("Illegal invocation: {}", name))),
37 }
38}
39
40fn forward(it: &mut Interp, this: Value, a: &[Value], name: &str) -> Result<Value, Value> {
42 let f = builtin_member(it, &this, name)?;
43 if !matches!(&f, Value::Object(o) if o.borrow().is_callable()) {
44 return Err(it.type_error(alloc::format!("{} is not a function", name)));
45 }
46 it.call_value(&f, this, a)
47}
48
49macro_rules! forwarders {
50 ($($fname:ident => $key:literal),* $(,)?) => {
51 $(
52 fn $fname(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
53 forward(it, this, a, $key)
54 }
55 )*
56 const FORWARDS: &[(&str, NativeFn)] = &[$(($key, $fname)),*];
58 };
59}
60
61forwarders! {
62 f_append_child => "appendChild",
63 f_insert_before => "insertBefore",
64 f_remove_child => "removeChild",
65 f_replace_child => "replaceChild",
66 f_clone_node => "cloneNode",
67 f_contains => "contains",
68 f_has_child_nodes => "hasChildNodes",
69 f_get_root_node => "getRootNode",
70 f_is_equal_node => "isEqualNode",
71 f_compare_document_position => "compareDocumentPosition",
72 f_normalize => "normalize",
73 f_get_attribute => "getAttribute",
74 f_set_attribute => "setAttribute",
75 f_remove_attribute => "removeAttribute",
76 f_has_attribute => "hasAttribute",
77 f_toggle_attribute => "toggleAttribute",
78 f_get_attribute_names => "getAttributeNames",
79 f_has_attributes => "hasAttributes",
80 f_query_selector => "querySelector",
81 f_query_selector_all => "querySelectorAll",
82 f_get_element_by_id => "getElementById",
83 f_get_elements_by_tag_name => "getElementsByTagName",
84 f_get_elements_by_class_name => "getElementsByClassName",
85 f_closest => "closest",
86 f_matches => "matches",
87 f_get_bounding_client_rect => "getBoundingClientRect",
88 f_get_client_rects => "getClientRects",
89 f_attach_shadow => "attachShadow",
90 f_insert_adjacent_html => "insertAdjacentHTML",
91 f_insert_adjacent_element => "insertAdjacentElement",
92 f_insert_adjacent_text => "insertAdjacentText",
93 f_remove => "remove",
94 f_append => "append",
95 f_prepend => "prepend",
96 f_after => "after",
97 f_before => "before",
98 f_replace_with => "replaceWith",
99 f_replace_children => "replaceChildren",
100 f_scroll_into_view => "scrollIntoView",
101 f_focus => "focus",
102 f_blur => "blur",
103 f_click => "click",
104}
105
106fn forward_get(it: &mut Interp, this: Value, name: &str) -> Result<Value, Value> {
108 builtin_member(it, &this, name)
109}
110
111fn forward_set(it: &mut Interp, this: Value, a: &[Value], name: &str) -> Result<Value, Value> {
113 if let Some(idx) = this_dom_idx(&this) {
114 it.dom_builtin_set(idx, name, arg(a, 0));
115 return Ok(Value::Undefined);
116 }
117 let host = match &this {
118 Value::Object(o) => match &o.borrow().kind {
119 ObjKind::HostRef(h) => Some(*h),
120 _ => None,
121 },
122 _ => None,
123 };
124 match host {
125 Some(h) => {
126 it.dom_set_property(crate::os_lib::web_apis::interp::dom_props::DomDisp::Host(h), name, arg(a, 0));
127 Ok(Value::Undefined)
128 }
129 None => Err(it.type_error(alloc::format!("Illegal invocation: {}", name))),
130 }
131}
132
133macro_rules! forwarders_doc {
134 ($($fname:ident => $key:literal),* $(,)?) => {
135 $(
136 fn $fname(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
137 forward(it, this, a, $key)
138 }
139 )*
140 const DOC_FORWARDS: &[(&str, NativeFn)] = &[$(($key, $fname)),*];
142 };
143}
144
145macro_rules! accessors {
146 ($($node:literal $gname:ident $sname:ident => $key:literal $writable:literal),* $(,)?) => {
147 $(
148 fn $gname(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
149 forward_get(it, this, $key)
150 }
151 fn $sname(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
152 forward_set(it, this, a, $key)
153 }
154 )*
155 const ACCESSORS: &[(bool, &str, NativeFn, NativeFn, bool)] = &[$(($node, $key, $gname, $sname, $writable)),*];
157 };
158}
159
160accessors! {
164 true g_parent_node s_parent_node => "parentNode" false,
165 true g_parent_element s_parent_element => "parentElement" false,
166 true g_first_child s_first_child => "firstChild" false,
167 true g_last_child s_last_child => "lastChild" false,
168 true g_previous_sibling s_previous_sibling => "previousSibling" false,
169 true g_next_sibling s_next_sibling => "nextSibling" false,
170 true g_child_nodes s_child_nodes => "childNodes" false,
171 true g_is_connected s_is_connected => "isConnected" false,
172 true g_owner_document s_owner_document => "ownerDocument" false,
173 true g_node_type s_node_type => "nodeType" false,
174 true g_node_name s_node_name => "nodeName" false,
175 true g_text_content s_text_content => "textContent" true,
176 false g_children s_children => "children" false,
177 false g_first_element_child s_first_element_child => "firstElementChild" false,
178 false g_last_element_child s_last_element_child => "lastElementChild" false,
179 false g_previous_element_sibling s_previous_element_sibling => "previousElementSibling" false,
180 false g_next_element_sibling s_next_element_sibling => "nextElementSibling" false,
181 false g_child_element_count s_child_element_count => "childElementCount" false,
182 false g_inner_html s_inner_html => "innerHTML" true,
183 false g_outer_html s_outer_html => "outerHTML" true,
184 false g_class_name s_class_name => "className" true,
185 false g_id s_id => "id" true,
186 false g_slot s_slot => "slot" true,
187 false g_shadow_root s_shadow_root => "shadowRoot" false,
188 false g_assigned_slot s_assigned_slot => "assignedSlot" false,
189 false g_tag_name s_tag_name => "tagName" false,
190 false g_local_name s_local_name => "localName" false,
191}
192
193forwarders_doc! {
194 d_create_element => "createElement",
195 d_create_element_ns => "createElementNS",
196 d_create_text_node => "createTextNode",
197 d_create_comment => "createComment",
198 d_create_pi => "createProcessingInstruction",
199 d_create_cdata_section => "createCDATASection",
200 d_create_document_fragment => "createDocumentFragment",
201 d_import_node => "importNode",
202 d_adopt_node => "adoptNode",
203 d_get_element_by_id => "getElementById",
204 d_create_tree_walker => "createTreeWalker",
205 d_create_event => "createEvent",
206 d_create_range => "createRange",
207 d_element_from_point => "elementFromPoint",
208}
209
210const NODE_METHODS: &[&str] = &[
212 "appendChild",
213 "insertBefore",
214 "removeChild",
215 "replaceChild",
216 "cloneNode",
217 "contains",
218 "hasChildNodes",
219 "getRootNode",
220 "isEqualNode",
221 "compareDocumentPosition",
222 "normalize",
223];
224
225pub(crate) fn install(node_proto: &ObjRef, element_proto: &ObjRef) {
227 for (name, f) in FORWARDS {
228 let target = if NODE_METHODS.contains(name) { node_proto } else { element_proto };
229 let mut b = target.borrow_mut();
230 if !b.props.contains_key(name) {
231 let v = nv(name, *f);
232 if *name == "insertBefore" {
233 if let Value::Object(ref o) = v {
234 o.borrow_mut().props.insert("length".into(), Value::Number(2.0));
235 }
236 }
237 b.props.insert(String::from(*name).into(), v);
238 }
239 }
240 for (on_node, name, g, set, writable) in ACCESSORS {
241 let target = if *on_node { node_proto } else { element_proto };
242 let mut b = target.borrow_mut();
243 if !b.has_accessor(name) && !b.props.contains_key(name) {
244 b.insert_accessor(
245 String::from(*name),
246 Accessor { get: Some(nv(name, *g)), set: if *writable { Some(nv(name, *set)) } else { None } },
247 );
248 }
249 }
250 let unscopables = Obj::plain();
251 {
252 let mut u = unscopables.borrow_mut();
253 for name in ["before", "after", "replaceWith", "remove", "prepend", "append"] {
254 u.props.insert(String::from(name).into(), Value::Bool(true));
255 }
256 }
257 let mut eb = element_proto.borrow_mut();
258 eb.props.insert(String::from("Symbol(Symbol.unscopables)").into(), Value::Object(unscopables));
259 eb.insert_attr(
260 "Symbol(Symbol.unscopables)",
261 PropertyAttributes { writable: false, configurable: true, enumerable: false },
262 );
263}
264
265pub(crate) fn named_node_map_get(_it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
267 let name = arg(a, 0).to_js_string();
268 if let Value::Object(o) = &this {
269 if let Some(v @ Value::Object(_)) = o.borrow().props.get(&name) {
270 return Ok(v.clone());
271 }
272 }
273 Ok(Value::Null)
274}
275
276pub(crate) fn named_node_map_item(_it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
278 let i = arg(a, 0).to_number();
279 if let Value::Object(o) = &this {
280 if let ObjKind::Array(items) = &o.borrow().kind {
281 if i >= 0.0 {
282 if let Some(v) = items.get(i as usize) {
283 return Ok(v.clone());
284 }
285 }
286 }
287 }
288 Ok(Value::Null)
289}
290
291pub(crate) fn install_document(doc_proto: &ObjRef) {
293 let mut b = doc_proto.borrow_mut();
294 for (name, f) in DOC_FORWARDS {
295 if !b.props.contains_key(name) {
296 let v = nv(name, *f);
297 if *name == "insertBefore" {
298 if let Value::Object(ref o) = v {
299 o.borrow_mut().props.insert("length".into(), Value::Number(2.0));
300 }
301 }
302 b.props.insert(String::from(*name).into(), v);
303 }
304 }
305}
306
307pub(crate) fn install_fragment(df_proto: &ObjRef) {
310 const NAMES: &[&str] = &["querySelector", "querySelectorAll", "getElementById", "append", "prepend", "replaceChildren"];
311 let mut b = df_proto.borrow_mut();
312 for (name, f) in FORWARDS {
313 if NAMES.contains(name) && !b.props.contains_key(name) {
314 b.props.insert(String::from(*name).into(), nv(name, *f));
315 }
316 }
317 for (_, name, g, _, _) in ACCESSORS {
318 if matches!(*name, "children" | "firstElementChild" | "lastElementChild" | "childElementCount") && !b.has_accessor(name) {
319 b.insert_accessor(String::from(*name), Accessor { get: Some(nv(name, *g)), set: None });
320 }
321 }
322}