htui 0.2.0

A terminal HTML/CSS renderer with an embedded JavaScript engine — build interactive TUIs from HTML, CSS and JS
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Content natives: `value` / `textContent` / `innerHTML` / `nodeValue` /
//! `data` accessors and their shared write paths, plus the CharacterData
//! methods (`substringData` / `appendData` / `insertData` / `deleteData` /
//! `replaceData`, `length` and `Text.splitText`).

use boa_engine::{Context, JsNativeError, JsResult, JsValue, js_string};

use crate::tdom::node::{Node, NodeId, NodeType};

use super::{
    ScriptState, collect_text, mark_dirty, node_wrapper, this_node_id, with_state,
    wrapper_is_cached,
};

/// `el.value` getter — the DOM `value` attribute (the field's committed
/// content; the active edit buffer while focused is engine-side state).
/// `<output>` and `<textarea>` are special-cased: their `value` IDL reflects
/// the text content (a textarea's DOM value lives in its child text nodes,
/// not a `value` attribute).
pub(super) fn native_get_value(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let Some(rc) = with_state() else {
        return Ok(js_string!().into());
    };
    let st = rc.borrow();
    let value = st
        .arena
        .get(node_id)
        .map(|n| {
            if matches!(n.node_type, NodeType::Output | NodeType::Textarea) {
                n.text_content(&st.arena)
            } else {
                n.attributes.get("value").cloned().unwrap_or_default()
            }
        })
        .unwrap_or_default();
    Ok(js_string!(value).into())
}

/// `el.value` setter — write the DOM `value` attribute, marking the node dirty
/// so the next frame re-renders it. Equal values short-circuit (no re-render),
/// mirroring the `textContent` fast path. `<output>` and `<textarea>` are
/// special-cased: setting `value` replaces the element's text content (the
/// output / textarea IDL contract).
pub(super) fn native_set_value(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let value = args
        .first()
        .map(|v| v.to_string(context).map(|s| s.to_std_string_lossy()))
        .transpose()?
        .unwrap_or_default();
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    if st
        .arena
        .get(node_id)
        .is_some_and(|n| matches!(n.node_type, NodeType::Output | NodeType::Textarea))
    {
        return set_text_content(&mut st, node_id, &value);
    }
    let existing = st
        .arena
        .get(node_id)
        .and_then(|n| n.attributes.get("value"))
        .cloned()
        .unwrap_or_default();
    if existing == value {
        return Ok(JsValue::undefined());
    }
    if let Some(node) = st.arena.get_mut(node_id) {
        node.attributes.insert("value".to_string(), value);
        mark_dirty(&mut st, node_id);
    }
    Ok(JsValue::undefined())
}

pub(super) fn native_get_text(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let Some(rc) = with_state() else {
        return Ok(js_string!().into());
    };
    let st = rc.borrow();
    let text = collect_text(&st.arena, node_id);
    Ok(js_string!(text).into())
}

pub(super) fn native_set_text(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let text = args
        .first()
        .map(|v| v.to_string(context).map(|s| s.to_std_string_lossy()))
        .transpose()?
        .unwrap_or_default();
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    set_text_content(&mut st, node_id, &text)
}

/// Shared `textContent` write: replace the node's text, short-circuiting on
/// equal values and reusing a sole text child to avoid arena growth. Also the
/// backing store of the `<output>` `value` setter.
fn set_text_content(st: &mut ScriptState, node_id: NodeId, text: &str) -> JsResult<JsValue> {
    // Text / Comment nodes: `textContent` writes their `data` directly.
    let is_text_like = matches!(
        st.arena.get(node_id).map(|n| n.node_type),
        Some(NodeType::Text | NodeType::Comment)
    );
    if is_text_like {
        let existing = st
            .arena
            .get(node_id)
            .and_then(|n| n.data.clone())
            .unwrap_or_default();
        if existing == text {
            return Ok(JsValue::undefined());
        }
        if let Some(n) = st.arena.get_mut(node_id) {
            n.data = Some(text.to_string());
        }
        mark_dirty(st, node_id);
        return Ok(JsValue::undefined());
    }

    // Fast path: the node already holds exactly one text child — reuse it so
    // repeated `textContent` updates don't grow the arena (each grown node
    // makes the per-frame `RenderArena` clone progressively slower). When the
    // new text equals the existing one, skip entirely so no re-render is
    // scheduled (monitors that refresh identical values avoid full relayouts).
    let reuse_child = st
        .arena
        .get(node_id)
        .filter(|n| n.children.len() == 1)
        .and_then(|n| n.children.first().copied())
        .filter(|&c| matches!(st.arena.get(c).map(|n| n.node_type), Some(NodeType::Text)));
    if let Some(child_id) = reuse_child {
        let existing = st
            .arena
            .get(child_id)
            .and_then(|c| c.data.clone())
            .unwrap_or_default();
        if existing == text {
            return Ok(JsValue::undefined());
        }
        if let Some(c) = st.arena.get_mut(child_id) {
            c.data = Some(text.to_string());
        }
        mark_dirty(st, node_id);
        return Ok(JsValue::undefined());
    }

    // Structural change: detach all children and insert a fresh text node.
    let Some(node) = st.arena.get_mut(node_id) else {
        return Ok(JsValue::undefined());
    };
    let old_children = std::mem::take(&mut node.children);
    for child in old_children {
        if let Some(c) = st.arena.get_mut(child) {
            c.parent = None;
        }
    }
    let text_id = st.arena.insert(Node::text(text));
    if let Some(parent) = st.arena.get_mut(node_id) {
        parent.children.push(text_id);
    }
    if let Some(t) = st.arena.get_mut(text_id) {
        t.parent = Some(node_id);
    }
    mark_dirty(st, node_id);
    Ok(JsValue::undefined())
}

pub(super) fn native_get_inner_html(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let Some(rc) = with_state() else {
        return Ok(js_string!().into());
    };
    let st = rc.borrow();
    let html = crate::html::html_parse::serialize_inner_html(&st.arena, node_id);
    Ok(js_string!(html).into())
}

/// `element.outerHTML` getter — serialization including the element's own
/// tags (text / comment nodes fall back to their body, per the DOM
/// serializer's node form).
pub(super) fn native_get_outer_html(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let Some(rc) = with_state() else {
        return Ok(js_string!().into());
    };
    let st = rc.borrow();
    let html = crate::html::html_parse::serialize_outer_html(&st.arena, node_id);
    Ok(js_string!(html).into())
}

pub(super) fn native_set_inner_html(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let html = args
        .first()
        .map(|v| v.to_string(context).map(|s| s.to_std_string_lossy()))
        .transpose()?
        .unwrap_or_default();
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    {
        let mut st = rc.borrow_mut();
        set_inner_html(&mut st, node_id, &html, context)?;
    }
    // The fresh fragment's `on*` attributes wire their handlers now (browser
    // semantics) — compile the inserted subtree before returning to JS.
    crate::script::bindings::compile_attribute_listeners_for_subtree(context, &rc, node_id)?;
    Ok(JsValue::undefined())
}

/// Shared `innerHTML` write: parse `html` as a fragment in the context of
/// `node_id` and replace the node's children. `<style>` CSS is accumulated on
/// the state so the next render applies it; `<script>` blocks are skipped
/// (never executed, matching the browser's `innerHTML` semantics). Replaced
/// subtrees JS can no longer reach are freed from the arena (see
/// [`sweep_replaced_subtrees`]).
fn set_inner_html(
    st: &mut ScriptState,
    node_id: NodeId,
    html: &str,
    context: &mut Context,
) -> JsResult<JsValue> {
    // `innerHTML` is an Element-only property; non-elements are left alone.
    let is_element = st
        .arena
        .get(node_id)
        .is_some_and(|n| n.node_type_constant() == 1);
    if !is_element {
        return Ok(JsValue::undefined());
    }
    // Capture the children about to be replaced so the unreachable ones can
    // be freed after the new fragment is in place — without this, every
    // timer-driven `innerHTML` refresh (clock / monitor pages) leaks the
    // full replaced subtree into the slotmap.
    let replaced = st
        .arena
        .get(node_id)
        .map(|n| n.children.clone())
        .unwrap_or_default();
    let mut css_sources = Vec::new();
    crate::html::html_parse::parse_fragment_into(&mut st.arena, node_id, html, &mut css_sources);
    if !css_sources.is_empty() {
        // Host-keyed replacement semantics: rewriting a host's `innerHTML`
        // replaces its injected styles (the browser drops the host's old
        // `<style>` elements), so a timer that re-injects the same stylesheet
        // every tick is idempotent instead of accumulating an unbounded
        // history on the render side.
        st.dirty
            .injected_css
            .insert(node_id, css_sources.join("\n"));
    }
    sweep_replaced_subtrees(st, &replaced, context)?;
    mark_dirty(st, node_id);
    Ok(JsValue::undefined())
}

/// Free the replaced subtrees that JS can no longer reach.
///
/// Browser semantics: nodes dropped by an `innerHTML` write stay alive while
/// a script holds a reference to them, and are collected once unreachable.
/// The wrapper cache (`__htuiElements`, GC-rooted) is the conservative
/// reachability oracle: a wrapper exists exactly when JS has touched the
/// node, so a wrapped node — and its whole subtree, which stays attached to
/// it like a detached browser node — is kept, while unwrapped subtrees are
/// removed from the arena. Listeners imply a wrapper (they are installed on
/// one), so no listener registry cleanup is needed here.
fn sweep_replaced_subtrees(
    st: &mut ScriptState,
    roots: &[NodeId],
    context: &mut Context,
) -> JsResult<()> {
    for &root in roots {
        // Collect top-down; a wrapped node stops the descent (its whole
        // subtree stays attached to it).
        let mut doomed: Vec<NodeId> = Vec::new();
        let mut stack = vec![root];
        while let Some(id) = stack.pop() {
            if wrapper_is_cached(context, id)? {
                continue;
            }
            doomed.push(id);
            if let Some(node) = st.arena.get(id) {
                stack.extend(node.children.iter().copied());
            }
        }
        for id in doomed {
            st.dirty.injected_css.swap_remove(&id);
            // The node is leaving the arena for good — drop its attribute
            // handler slots so the registry cannot grow without bound.
            crate::script::bindings::clear_node_attribute_listeners(context, id)?;
            st.arena.remove(id);
        }
    }
    Ok(())
}

pub(super) fn native_get_node_value(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let Some(rc) = with_state() else {
        return Ok(JsValue::null());
    };
    let st = rc.borrow();
    let Some(node) = st.arena.get(node_id) else {
        return Ok(JsValue::null());
    };
    match node.node_type {
        NodeType::Text | NodeType::Comment => {
            Ok(js_string!(node.data.as_deref().unwrap_or("")).into())
        }
        _ => Ok(JsValue::null()),
    }
}

pub(super) fn native_set_node_value(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let value = args
        .first()
        .map(|v| v.to_string(context).map(|s| s.to_std_string_lossy()))
        .transpose()?
        .unwrap_or_default();
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    let Some(node) = st.arena.get_mut(node_id) else {
        return Ok(JsValue::undefined());
    };
    if matches!(node.node_type, NodeType::Text | NodeType::Comment) {
        node.data = Some(value);
        mark_dirty(&mut st, node_id);
    }
    Ok(JsValue::undefined())
}

pub(super) fn native_get_data(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let Some(rc) = with_state() else {
        return Ok(js_string!().into());
    };
    let st = rc.borrow();
    let data = st
        .arena
        .get(node_id)
        .and_then(|n| n.data.clone())
        .unwrap_or_default();
    Ok(js_string!(data).into())
}

pub(super) fn native_set_data(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let value = args
        .first()
        .map(|v| v.to_string(context).map(|s| s.to_std_string_lossy()))
        .transpose()?
        .unwrap_or_default();
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    if let Some(n) = st.arena.get_mut(node_id) {
        if n.data.as_deref() == Some(value.as_str()) {
            return Ok(JsValue::undefined());
        }
        n.data = Some(value);
        mark_dirty(&mut st, node_id);
    }
    Ok(JsValue::undefined())
}

// ============================================================
// CharacterData methods (substringData / insertData / …)
// ============================================================

/// Character-offset check shared by the CharacterData methods. Offsets count
/// Unicode scalar values (chars) rather than UTF-16 code units — the natural
/// unit for a char-grid renderer. `offset > length` is the DOM's
/// IndexSizeError, surfaced as a JS `RangeError`.
fn checked_char_offset(data: &str, offset: u32) -> JsResult<usize> {
    let len = data.chars().count();
    let offset = offset as usize;
    if offset > len {
        return Err(JsNativeError::range()
            .with_message(format!(
                "IndexSizeError: offset {offset} exceeds data length {len}"
            ))
            .into());
    }
    Ok(offset)
}

/// Replace `count` characters starting at `offset` with `insert` (the shared
/// core of insert / append / delete / replace: an empty `insert` deletes and
/// a zero `count` inserts).
fn replace_char_range(data: &str, offset: usize, count: u32, insert: &str) -> String {
    let mut chars: Vec<char> = data.chars().collect();
    let end = (offset + count as usize).min(chars.len());
    chars.splice(offset..end, insert.chars());
    chars.into_iter().collect()
}

/// Unsigned-argument read with the DOM's `unsigned long` conversion (negative
/// input wraps modulo 2³², matching browser behavior for e.g.
/// `substringData(-1, 1)`).
fn uint_arg(args: &[JsValue], index: usize, context: &mut Context) -> JsResult<u32> {
    match args.get(index) {
        Some(v) => v.to_u32(context),
        None => Ok(0),
    }
}

/// String-argument read (missing argument is the empty string, matching the
/// loose call style the other content natives tolerate).
fn string_arg(args: &[JsValue], index: usize, context: &mut Context) -> JsResult<String> {
    match args.get(index) {
        Some(v) => Ok(v.to_string(context)?.to_std_string_lossy()),
        None => Ok(String::new()),
    }
}

/// Read the `data` of a text-like node (empty for anything else).
fn text_data(arena: &crate::tdom::node::NodeArena, node_id: NodeId) -> String {
    arena
        .get(node_id)
        .and_then(|n| n.data.clone())
        .unwrap_or_default()
}

/// Write back the `data` of a text-like node, short-circuiting when the value
/// did not change (no dirty, no re-render — the same contract as the `data`
/// setter).
fn store_data(st: &mut ScriptState, node_id: NodeId, new: String) {
    let Some(node) = st.arena.get_mut(node_id) else {
        return;
    };
    let changed = node.data.as_deref() != Some(new.as_str());
    node.data = Some(new);
    if changed {
        mark_dirty(st, node_id);
    }
}

/// `CharacterData.substringData(offset, count)` — the `count` characters
/// starting at `offset`.
pub(super) fn native_substring_data(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let offset = uint_arg(args, 0, context)?;
    let count = uint_arg(args, 1, context)?;
    let Some(rc) = with_state() else {
        return Ok(js_string!().into());
    };
    let st = rc.borrow();
    let data = text_data(&st.arena, node_id);
    let offset = checked_char_offset(&data, offset)?;
    Ok(js_string!(
        data.chars()
            .skip(offset)
            .take(count as usize)
            .collect::<String>()
    )
    .into())
}

/// `CharacterData.appendData(text)` — add `text` at the end of `data`.
pub(super) fn native_append_data(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let text = string_arg(args, 0, context)?;
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    let data = text_data(&st.arena, node_id);
    let new = format!("{data}{text}");
    store_data(&mut st, node_id, new);
    Ok(JsValue::undefined())
}

/// `CharacterData.insertData(offset, text)` — insert `text` at `offset`.
pub(super) fn native_insert_data(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let offset = uint_arg(args, 0, context)?;
    let text = string_arg(args, 1, context)?;
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    let data = text_data(&st.arena, node_id);
    let offset = checked_char_offset(&data, offset)?;
    let new = replace_char_range(&data, offset, 0, &text);
    store_data(&mut st, node_id, new);
    Ok(JsValue::undefined())
}

/// `CharacterData.deleteData(offset, count)` — remove `count` characters
/// starting at `offset` (count clamps to the remaining length).
pub(super) fn native_delete_data(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let offset = uint_arg(args, 0, context)?;
    let count = uint_arg(args, 1, context)?;
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    let data = text_data(&st.arena, node_id);
    let offset = checked_char_offset(&data, offset)?;
    let new = replace_char_range(&data, offset, count, "");
    store_data(&mut st, node_id, new);
    Ok(JsValue::undefined())
}

/// `CharacterData.replaceData(offset, count, text)` — swap the `count`
/// characters starting at `offset` for `text`.
pub(super) fn native_replace_data(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let offset = uint_arg(args, 0, context)?;
    let count = uint_arg(args, 1, context)?;
    let text = string_arg(args, 2, context)?;
    let Some(rc) = with_state() else {
        return Ok(JsValue::undefined());
    };
    let mut st = rc.borrow_mut();
    let data = text_data(&st.arena, node_id);
    let offset = checked_char_offset(&data, offset)?;
    let new = replace_char_range(&data, offset, count, &text);
    store_data(&mut st, node_id, new);
    Ok(JsValue::undefined())
}

/// `CharacterData.length` — the number of characters in `data` (the same
/// char-counting contract as the CharacterData methods above).
pub(super) fn native_get_data_length(
    this: &JsValue,
    _args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let len = with_state()
        .map(|rc| {
            let st = rc.borrow();
            text_data(&st.arena, node_id).chars().count()
        })
        .unwrap_or(0);
    Ok(JsValue::from(len as u32))
}

/// `Text.splitText(offset)` — split the node at `offset` characters: this
/// node keeps the leading text and a fresh Text node holding the tail is
/// inserted as the next sibling (detached when the node has no parent).
/// Text nodes only — `Comment` has no `splitText` (browser behavior).
pub(super) fn native_split_text(
    this: &JsValue,
    args: &[JsValue],
    context: &mut Context,
) -> JsResult<JsValue> {
    let node_id = this_node_id(this, context)?;
    let offset = uint_arg(args, 0, context)?;
    let Some(rc) = with_state() else {
        return Ok(JsValue::null());
    };
    let mut st = rc.borrow_mut();
    let data = text_data(&st.arena, node_id);
    let offset = checked_char_offset(&data, offset)?;
    let head: String = data.chars().take(offset).collect();
    let tail: String = data.chars().skip(offset).collect();
    let parent = st.arena.get(node_id).and_then(|n| n.parent);
    store_data(&mut st, node_id, head);
    let tail_id = st.arena.insert(Node::text(&tail));
    if let Some(pid) = parent {
        if let Some(pnode) = st.arena.get_mut(pid) {
            let pos = pnode
                .children
                .iter()
                .position(|&c| c == node_id)
                .map_or(pnode.children.len(), |p| p + 1);
            pnode.children.insert(pos, tail_id);
        }
        if let Some(t) = st.arena.get_mut(tail_id) {
            t.parent = Some(pid);
        }
        mark_dirty(&mut st, pid);
    }
    drop(st);
    node_wrapper(context, tail_id)
}