web-api-cat 0.6.8

Bindings between boa-cat (JS engine) and the DOM (html-cat tree) plus fetch (net-cat). v0.6.8 lands parent-backref infrastructure: every element gets a hidden `__parent__` slot (Object when attached, Null when detached or root), every structural mutator (`appendChild` / `removeChild` / `insertBefore` / `replaceChild` / `innerHTML` setter / `cloneNode` deep-children) routes through shared `set_parent_backref` / `clear_parent_backref` helpers, and `parentElement` / `parentNode` accessor-getters plus `Element.remove()` ride on top. Seventh sub-crate of a Servo-replacement webview runtime targeting Tauri.
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Translate an `html-cat` document into a tree of boa-cat element
//! objects.

use std::collections::BTreeMap;

use boa_cat::Value;
use boa_cat::fuel::Fuel;
use boa_cat::heap::Heap;
use boa_cat::outcome::{EvalResult, Outcome};
use boa_cat::value::{Object, ObjectId};
use html_cat::{Document as HtmlDoc, Element as HtmlElement, Node as HtmlNode};

use crate::element;

/// Build the JS-side document object tree from `html_doc`.  Returns
/// `(document_value, root_element_value, heap)`.
#[must_use]
pub fn build(html_doc: &HtmlDoc, heap: Heap) -> (Value, Value, Heap) {
    let (root_value, heap) = build_element(html_doc.root(), heap);
    build_document_object(root_value, heap)
}

fn build_document_object(root_value: Value, heap: Heap) -> (Value, Value, Heap) {
    let body_value = object_id_of(&root_value)
        .and_then(|id| find_element_by_tag(id, "body", &heap))
        .unwrap_or(Value::Null);
    let mut props = BTreeMap::new();
    let _ = props.insert("documentElement".to_owned(), root_value.clone());
    let _ = props.insert("body".to_owned(), body_value);
    let _ = props.insert(
        "getElementById".to_owned(),
        Value::Native(get_element_by_id_impl),
    );
    let _ = props.insert(
        "querySelector".to_owned(),
        Value::Native(document_query_selector_impl),
    );
    let _ = props.insert(
        "createElement".to_owned(),
        Value::Native(create_element_impl),
    );
    let (doc_id, heap) = heap.alloc_object(Object::from_properties(props));
    let doc_value = Value::Object(doc_id);
    let heap = crate::cookie::install_cookie_accessor(&doc_value, heap);
    (doc_value, root_value, heap)
}

/// `document.createElement(tagName)` (v0.6.1): allocate a fresh
/// element-shaped object on the heap.  The new element has the
/// requested `tagName`, empty `id` / `className` / `textContent`,
/// an empty children array, an empty `__attributes` object, and
/// every method binding a build-time element carries (so the
/// caller can chain `setAttribute`, `appendChild`, etc. straight
/// away).
#[allow(clippy::needless_pass_by_value, clippy::unnecessary_wraps)]
fn create_element_impl(args: Vec<Value>, _this: Value, heap: Heap, fuel: Fuel) -> EvalResult {
    let tag = match args.first() {
        Some(Value::String(s)) => s.clone(),
        Some(
            Value::Undefined
            | Value::Null
            | Value::Boolean(_)
            | Value::Number(_)
            | Value::Object(_)
            | Value::Function(_)
            | Value::Native(_)
            | Value::Promise(_),
        )
        | None => String::new(),
    };
    let (element, heap) = build_blank_element(&tag, heap);
    Ok((Outcome::Normal(element), heap, fuel))
}

/// v0.6.1 helper: build an element with the supplied tagName but
/// otherwise-empty fields.  Used by both `createElement` and the
/// `appendChild` test setup; the rendered fields match what
/// [`build_element`] produces for an HTML-parsed element so
/// [`crate::extract::extract_document`] picks the new node up on
/// the next back-prop pass.
#[must_use]
pub fn build_blank_element(tag: &str, heap: Heap) -> (Value, Heap) {
    let attribute_pairs: Vec<(String, String)> = Vec::new();
    let (attributes_value, heap) = element::build_attributes_object(&attribute_pairs, heap);
    let (children_array_value, heap) = build_array_object(&[], heap);
    let mut props = BTreeMap::new();
    let _ = props.insert("tagName".to_owned(), Value::String(tag.to_owned()));
    let _ = props.insert("id".to_owned(), Value::String(String::new()));
    let _ = props.insert("className".to_owned(), Value::String(String::new()));
    let _ = props.insert("textContent".to_owned(), Value::String(String::new()));
    let _ = props.insert("children".to_owned(), children_array_value);
    let _ = props.insert("__attributes".to_owned(), attributes_value);
    let _ = props.insert("__parent__".to_owned(), Value::Null);
    let (style_value, heap) = build_empty_style_object(heap);
    let _ = props.insert("style".to_owned(), style_value);
    let _ = props.insert(
        "getAttribute".to_owned(),
        Value::Native(element::get_attribute_impl),
    );
    let _ = props.insert(
        "setAttribute".to_owned(),
        Value::Native(element::set_attribute_impl),
    );
    let _ = props.insert(
        "hasAttribute".to_owned(),
        Value::Native(element::has_attribute_impl),
    );
    let _ = props.insert(
        "querySelector".to_owned(),
        Value::Native(element::query_selector_impl),
    );
    let _ = props.insert(
        "appendChild".to_owned(),
        Value::Native(element::append_child_impl),
    );
    let _ = props.insert(
        "removeChild".to_owned(),
        Value::Native(element::remove_child_impl),
    );
    let _ = props.insert(
        "insertBefore".to_owned(),
        Value::Native(element::insert_before_impl),
    );
    let _ = props.insert(
        "replaceChild".to_owned(),
        Value::Native(element::replace_child_impl),
    );
    let _ = props.insert("cloneNode".to_owned(), Value::Native(clone_node_impl));
    let _ = props.insert("remove".to_owned(), Value::Native(element::remove_impl));
    let (id, heap) = heap.alloc_object(Object::from_properties(props));
    let heap = install_class_list(id, heap);
    let heap = install_parent_accessors(id, heap);
    let heap = crate::inner_html::install_inner_html_accessor(&Value::Object(id), heap);
    let heap = crate::inner_html::install_outer_html_accessor(&Value::Object(id), heap);
    (Value::Object(id), heap)
}

/// v0.6.8 helper: install `parentElement` and `parentNode` as
/// accessors with getters that read `this.__parent__`.  Same
/// underlying `NativeFn` (`element::parent_getter_impl`) -- we
/// don't yet distinguish elements from other node types, so both
/// accessors produce identical results.  Setter is omitted; the
/// engine reports the property as read-only.
fn install_parent_accessors(element_id: ObjectId, heap: Heap) -> Heap {
    let Some(element) = heap.object(element_id).cloned() else {
        return heap;
    };
    let parent_element_pair =
        boa_cat::value::AccessorPair::new(Some(Value::Native(element::parent_getter_impl)), None);
    let parent_node_pair =
        boa_cat::value::AccessorPair::new(Some(Value::Native(element::parent_getter_impl)), None);
    let updated = element
        .with_accessor("parentElement".to_owned(), parent_element_pair)
        .with_accessor("parentNode".to_owned(), parent_node_pair);
    heap.store_object(element_id, updated).unwrap_or_else(|h| h)
}

/// v0.6.4 helper: allocate a fresh empty Object to attach as
/// `element.style`.  Scripts read and write arbitrary keys
/// (`el.style.color = 'red'`) and the engine's normal property
/// storage handles the round-trip; no special methods needed.  The
/// inline `style="..."` attribute on parsed HTML is NOT parsed
/// into this object (it stays readable via `getAttribute('style')`);
/// real-world `CSSStyleDeclaration` parsing is deferred.  Back-prop
/// from this object into layout-cat is also deferred -- this v0
/// chunk targets script-compatibility, not visual styling.
fn build_empty_style_object(heap: Heap) -> (Value, Heap) {
    let (id, heap) = heap.alloc_object(Object::from_properties(BTreeMap::new()));
    (Value::Object(id), heap)
}

/// v0.6.3 helper: allocate a `classList` Object holding an
/// `__element__` backref to `element_id` and the four
/// `DOMTokenList` natives (`add`, `remove`, `contains`, `toggle`),
/// then attach it as `classList` on the element via
/// `store_object`.  Run after the element itself is allocated so
/// the backref can be materialised.
fn install_class_list(element_id: ObjectId, heap: Heap) -> Heap {
    let (class_list_value, heap) = build_class_list_object(element_id, heap);
    let Some(element) = heap.object(element_id).cloned() else {
        return heap;
    };
    let updated = element.with("classList".to_owned(), class_list_value);
    heap.store_object(element_id, updated).unwrap_or_else(|h| h)
}

fn build_class_list_object(element_id: ObjectId, heap: Heap) -> (Value, Heap) {
    let mut props = BTreeMap::new();
    let _ = props.insert("__element__".to_owned(), Value::Object(element_id));
    let _ = props.insert(
        "add".to_owned(),
        Value::Native(element::class_list_add_impl),
    );
    let _ = props.insert(
        "remove".to_owned(),
        Value::Native(element::class_list_remove_impl),
    );
    let _ = props.insert(
        "contains".to_owned(),
        Value::Native(element::class_list_contains_impl),
    );
    let _ = props.insert(
        "toggle".to_owned(),
        Value::Native(element::class_list_toggle_impl),
    );
    let (id, heap) = heap.alloc_object(Object::from_properties(props));
    (Value::Object(id), heap)
}

fn object_id_of(value: &Value) -> Option<ObjectId> {
    match value {
        Value::Object(id) => Some(*id),
        Value::Undefined
        | Value::Null
        | Value::Boolean(_)
        | Value::Number(_)
        | Value::String(_)
        | Value::Function(_)
        | Value::Native(_)
        | Value::Promise(_) => None,
    }
}

fn find_element_by_tag(node_id: ObjectId, tag: &str, heap: &Heap) -> Option<Value> {
    let object = heap.object(node_id)?;
    let tag_name = match object.get("tagName") {
        Some(Value::String(s)) => s.as_str(),
        Some(_) | None => "",
    };
    if tag_name.eq_ignore_ascii_case(tag) {
        Some(Value::Object(node_id))
    } else {
        let children_id = object.get("children").and_then(object_id_of)?;
        let children_obj = heap.object(children_id)?;
        let length = match children_obj.get("length") {
            Some(Value::Number(n)) if n.is_finite() && *n >= 0.0 => {
                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
                let len = *n as u32;
                len
            }
            Some(_) | None => 0,
        };
        (0..length).find_map(|i| {
            children_obj
                .get(&format!("{i}"))
                .and_then(object_id_of)
                .and_then(|child_id| find_element_by_tag(child_id, tag, heap))
        })
    }
}

fn build_element(html_element: &HtmlElement, heap: Heap) -> (Value, Heap) {
    let attribute_pairs: Vec<(String, String)> = html_element
        .attributes()
        .iter()
        .map(|a| (a.name().to_owned(), a.value().to_owned()))
        .collect();
    let id = lookup_attribute(&attribute_pairs, "id").unwrap_or_default();
    let class = lookup_attribute(&attribute_pairs, "class").unwrap_or_default();
    let text_content = collect_text(html_element.children());
    let (attributes_value, heap) = element::build_attributes_object(&attribute_pairs, heap);
    let (children_values, heap) = build_children(html_element.children(), heap);
    let (children_array_value, heap) = build_array_object(&children_values, heap);
    let mut props = BTreeMap::new();
    let _ = props.insert(
        "tagName".to_owned(),
        Value::String(html_element.name().to_owned()),
    );
    let _ = props.insert("id".to_owned(), Value::String(id));
    let _ = props.insert("className".to_owned(), Value::String(class));
    let _ = props.insert("textContent".to_owned(), Value::String(text_content));
    let _ = props.insert("children".to_owned(), children_array_value);
    let _ = props.insert("__attributes".to_owned(), attributes_value);
    let _ = props.insert("__parent__".to_owned(), Value::Null);
    let (style_value, heap) = build_empty_style_object(heap);
    let _ = props.insert("style".to_owned(), style_value);
    let _ = props.insert(
        "getAttribute".to_owned(),
        Value::Native(element::get_attribute_impl),
    );
    let _ = props.insert(
        "setAttribute".to_owned(),
        Value::Native(element::set_attribute_impl),
    );
    let _ = props.insert(
        "hasAttribute".to_owned(),
        Value::Native(element::has_attribute_impl),
    );
    let _ = props.insert(
        "querySelector".to_owned(),
        Value::Native(element::query_selector_impl),
    );
    let _ = props.insert(
        "appendChild".to_owned(),
        Value::Native(element::append_child_impl),
    );
    let _ = props.insert(
        "removeChild".to_owned(),
        Value::Native(element::remove_child_impl),
    );
    let _ = props.insert(
        "insertBefore".to_owned(),
        Value::Native(element::insert_before_impl),
    );
    let _ = props.insert(
        "replaceChild".to_owned(),
        Value::Native(element::replace_child_impl),
    );
    let _ = props.insert("cloneNode".to_owned(), Value::Native(clone_node_impl));
    let _ = props.insert("remove".to_owned(), Value::Native(element::remove_impl));
    let (id, heap) = heap.alloc_object(Object::from_properties(props));
    let heap = children_values.iter().fold(heap, |heap, child| {
        element::set_parent_backref(child, id, heap)
    });
    let heap = install_class_list(id, heap);
    let heap = install_parent_accessors(id, heap);
    let heap = crate::inner_html::install_inner_html_accessor(&Value::Object(id), heap);
    let heap = crate::inner_html::install_outer_html_accessor(&Value::Object(id), heap);
    (Value::Object(id), heap)
}

/// `Element.cloneNode(deep)` (v0.6.6): produce an independent copy
/// of `this`.  `deep === true` clones every descendant
/// recursively; the default (no argument or anything that isn't
/// the literal `true`) clones only the element itself with an
/// empty children array.  Returns `null` if `this` isn't an
/// Object.
///
/// The clone has fresh `ObjectId`s for the element, its
/// `__attributes`, its `style`, its `classList` (with a backref to
/// the new element id), and its children array.  Native-method
/// bindings (`getAttribute`, `appendChild`, ...) are copied by
/// value (function pointers, no per-instance state).  The
/// `innerHTML` accessor is freshly installed on the clone.  All of
/// that means mutating `clone.setAttribute(...)`,
/// `clone.classList.add(...)`, `clone.style.x = ...`, or
/// `clone.appendChild(...)` cannot bleed back into the original.
///
/// # Errors
///
/// Never returns `Err`; bad inputs yield `null`.
#[allow(clippy::needless_pass_by_value, clippy::unnecessary_wraps)]
fn clone_node_impl(args: Vec<Value>, this: Value, heap: Heap, fuel: Fuel) -> EvalResult {
    let deep = matches!(args.first(), Some(Value::Boolean(true)));
    let (clone_value, heap) = if let Some(id) = object_id_of(&this) {
        clone_element(id, deep, heap)
    } else {
        (Value::Null, heap)
    };
    Ok((Outcome::Normal(clone_value), heap, fuel))
}

fn clone_element(element_id: ObjectId, deep: bool, heap: Heap) -> (Value, Heap) {
    let Some(original) = heap.object(element_id).cloned() else {
        return (Value::Null, heap);
    };
    let attrs_id_opt = original.get("__attributes").and_then(object_id_of);
    let (cloned_attrs_id_opt, heap) = clone_object_option(attrs_id_opt, heap);
    let style_id_opt = original.get("style").and_then(object_id_of);
    let (cloned_style_id_opt, heap) = clone_object_option(style_id_opt, heap);
    let children_id_opt = original.get("children").and_then(object_id_of);
    let (new_children_values, heap) = if deep {
        if let Some(cid) = children_id_opt {
            clone_children_deep(cid, heap)
        } else {
            (Vec::new(), heap)
        }
    } else {
        (Vec::new(), heap)
    };
    let (new_children_array_value, heap) = build_array_object(&new_children_values, heap);
    let new_props: BTreeMap<String, Value> = original
        .properties()
        .iter()
        .filter(|(k, _)| {
            !matches!(
                k.as_str(),
                "__attributes" | "style" | "children" | "classList" | "__parent__"
            )
        })
        .map(|(k, v)| (k.clone(), v.clone()))
        .chain(cloned_attrs_id_opt.map(|id| ("__attributes".to_owned(), Value::Object(id))))
        .chain(cloned_style_id_opt.map(|id| ("style".to_owned(), Value::Object(id))))
        .chain(std::iter::once((
            "children".to_owned(),
            new_children_array_value,
        )))
        .chain(std::iter::once(("__parent__".to_owned(), Value::Null)))
        .collect();
    let (new_element_id, heap) = heap.alloc_object(Object::from_properties(new_props));
    let heap = new_children_values.iter().fold(heap, |heap, child| {
        element::set_parent_backref(child, new_element_id, heap)
    });
    let heap = install_class_list(new_element_id, heap);
    let heap = install_parent_accessors(new_element_id, heap);
    let heap = crate::inner_html::install_inner_html_accessor(&Value::Object(new_element_id), heap);
    let heap = crate::inner_html::install_outer_html_accessor(&Value::Object(new_element_id), heap);
    (Value::Object(new_element_id), heap)
}

fn clone_object_option(id_opt: Option<ObjectId>, heap: Heap) -> (Option<ObjectId>, Heap) {
    let Some(id) = id_opt else {
        return (None, heap);
    };
    let Some(obj) = heap.object(id).cloned() else {
        return (None, heap);
    };
    let (new_id, heap) = heap.alloc_object(obj);
    (Some(new_id), heap)
}

fn clone_children_deep(children_id: ObjectId, heap: Heap) -> (Vec<Value>, Heap) {
    let Some(children) = heap.object(children_id).cloned() else {
        return (Vec::new(), heap);
    };
    let length = match children.get("length") {
        Some(Value::Number(n)) if n.is_finite() && *n >= 0.0 => {
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            let len = *n as u32;
            len
        }
        Some(_) | None => 0,
    };
    (0..length).fold((Vec::new(), heap), |(acc, heap), i| {
        if let Some(child_id) = children.get(&format!("{i}")).and_then(object_id_of) {
            let (cloned, h) = clone_element(child_id, true, heap);
            let acc = acc.into_iter().chain(std::iter::once(cloned)).collect();
            (acc, h)
        } else {
            (acc, heap)
        }
    })
}

/// v0.6.5 helper used by [`crate::inner_html`]: parse `html` as a
/// fragment (wrapped in `<html><body>...</body></html>` so the
/// html-cat full-document parser handles it), locate the body
/// element, and run [`build_children`] over its child nodes.
/// Returns the produced child element values plus the updated
/// heap.  On parse failure returns an empty vector and the
/// unchanged heap so the caller can treat it as a no-op.
#[must_use]
pub fn parse_fragment_children(html: &str, heap: Heap) -> (Vec<Value>, Heap) {
    let wrapped = format!("<html><body>{html}</body></html>");
    let Ok(doc) = html_cat::parse(&wrapped) else {
        return (Vec::new(), heap);
    };
    let root = doc.root();
    let body = find_html_body(root).unwrap_or(root);
    build_children(body.children(), heap)
}

fn find_html_body(element: &HtmlElement) -> Option<&HtmlElement> {
    if element.name().eq_ignore_ascii_case("body") {
        Some(element)
    } else {
        element.children().iter().find_map(|c| match c {
            HtmlNode::Element(e) => find_html_body(e),
            HtmlNode::Text(_) | HtmlNode::Comment(_) => None,
        })
    }
}

fn build_children(children: &[HtmlNode], heap: Heap) -> (Vec<Value>, Heap) {
    children
        .iter()
        .fold((Vec::new(), heap), |(acc, heap), child| match child {
            HtmlNode::Element(e) => {
                let (value, heap) = build_element(e, heap);
                let extended: Vec<Value> = acc.into_iter().chain(std::iter::once(value)).collect();
                (extended, heap)
            }
            HtmlNode::Text(_) | HtmlNode::Comment(_) => (acc, heap),
        })
}

fn build_array_object(values: &[Value], heap: Heap) -> (Value, Heap) {
    let length = u32::try_from(values.len()).unwrap_or(u32::MAX);
    let map: BTreeMap<String, Value> = values
        .iter()
        .enumerate()
        .map(|(i, v)| (format!("{i}"), v.clone()))
        .chain(std::iter::once((
            "length".to_owned(),
            Value::Number(f64::from(length)),
        )))
        .collect();
    let (id, heap) = heap.alloc_object(Object::from_properties(map));
    (Value::Object(id), heap)
}

fn lookup_attribute(pairs: &[(String, String)], name: &str) -> Option<String> {
    pairs
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case(name))
        .map(|(_, v)| v.clone())
}

fn collect_text(children: &[HtmlNode]) -> String {
    children
        .iter()
        .fold(String::new(), |acc, child| match child {
            HtmlNode::Text(t) => format!("{acc}{}", t.content()),
            HtmlNode::Element(e) => format!("{acc}{}", collect_text(e.children())),
            HtmlNode::Comment(_) => acc,
        })
}

fn document_root_id(this: &Value, heap: &Heap) -> Option<ObjectId> {
    let document_id = object_id_of(this)?;
    let document = heap.object(document_id)?;
    document.get("documentElement").and_then(object_id_of)
}

#[allow(clippy::needless_pass_by_value)]
#[allow(clippy::unnecessary_wraps)]
fn get_element_by_id_impl(args: Vec<Value>, this: Value, heap: Heap, fuel: Fuel) -> EvalResult {
    let id_arg = match args.first() {
        Some(Value::String(s)) => s.clone(),
        Some(_) | None => String::new(),
    };
    let outcome = document_root_id(&this, &heap)
        .and_then(|root_id| element::find_by_id(root_id, &id_arg, &heap))
        .map_or(Outcome::Normal(Value::Null), Outcome::Normal);
    Ok((outcome, heap, fuel))
}

#[allow(clippy::needless_pass_by_value)]
#[allow(clippy::unnecessary_wraps)]
fn document_query_selector_impl(
    args: Vec<Value>,
    this: Value,
    heap: Heap,
    fuel: Fuel,
) -> EvalResult {
    let selector = match args.first() {
        Some(Value::String(s)) => s.clone(),
        Some(_) | None => String::new(),
    };
    let outcome = document_root_id(&this, &heap)
        .and_then(|root_id| element::find_first_descendant(root_id, &selector, &heap))
        .map_or(Outcome::Normal(Value::Null), Outcome::Normal);
    Ok((outcome, heap, fuel))
}