devup-editor-html 1.0.17

HTML ↔ Document conversion + clipboard-mode support (tables, Notion heuristics, data-devup-props round-trip) for devup-editor
Documentation
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
//! Integration tests for `devup-editor-html`.
//!
//! Every test uses `SequentialIdGenerator` so block IDs are deterministic.
//! Round-trip tests don't require byte-perfect HTML preservation — they
//! check structural equivalence (block types + content text + marks).

use devup_editor_core::{
    Block, BlockId, Document, DocumentExport, DocumentImport, Mark, SequentialIdGenerator, TextSpan,
};
use devup_editor_html::Html;
use serde_json::{Map, Value};

// ── Helpers ──────────────────────────────────────────────────────

fn doc_with(blocks: Vec<Block>) -> Document {
    let mut doc = Document::new();
    for b in blocks {
        doc.push_root_block(b);
    }
    doc
}

fn styled_mark(mark_type: &str, style_key: &str, style_value: &str) -> Mark {
    let mut style = Map::new();
    style.insert(style_key.into(), Value::String(style_value.into()));
    let mut attrs = Map::new();
    attrs.insert("style".into(), Value::Object(style));
    Mark::with_attrs(mark_type, attrs)
}

fn block_types(doc: &Document) -> Vec<String> {
    doc.root_block_ids()
        .iter()
        .filter_map(|id| doc.get_block(id).map(|b| b.ty.clone()))
        .collect()
}

fn block_texts(doc: &Document) -> Vec<String> {
    doc.root_block_ids()
        .iter()
        .filter_map(|id| doc.get_block(id).map(devup_editor_core::Block::plain_text))
        .collect()
}

// ── Export tests ─────────────────────────────────────────────────

#[test]
fn export_heading_levels() {
    for level in 1u64..=6 {
        let mut b = Block::new(BlockId::new(format!("h{level}")), "heading");
        b.content = vec![TextSpan::plain(format!("Heading {level}"))];
        b.props.insert("level".into(), Value::from(level));
        let out = Html::export(&doc_with(vec![b])).unwrap();
        assert!(
            out.contains(&format!("<h{level}>Heading {level}</h{level}>")),
            "missing h{level}: {out}"
        );
    }
}

#[test]
fn export_paragraph_with_all_marks() {
    let mut b = Block::new_paragraph(BlockId::new("p"));
    b.content = vec![
        TextSpan::with_marks("bold", vec![Mark::bold()]),
        TextSpan::plain(" "),
        TextSpan::with_marks("italic", vec![Mark::italic()]),
        TextSpan::plain(" "),
        TextSpan::with_marks("code", vec![Mark::code()]),
        TextSpan::plain(" "),
        TextSpan::with_marks("under", vec![Mark::underline()]),
        TextSpan::plain(" "),
        TextSpan::with_marks("strike", vec![Mark::strike()]),
    ];
    let out = Html::export(&doc_with(vec![b])).unwrap();
    assert!(out.contains("<strong>bold</strong>"));
    assert!(out.contains("<em>italic</em>"));
    assert!(out.contains("<code>code</code>"));
    assert!(out.contains("<u>under</u>"));
    assert!(out.contains("<s>strike</s>"));
}

#[test]
fn export_color_and_highlight_produce_span_style() {
    let mut b = Block::new_paragraph(BlockId::new("p"));
    b.content = vec![TextSpan::with_marks(
        "rgb",
        vec![
            styled_mark("color", "color", "#ff0000"),
            styled_mark("highlight", "backgroundColor", "#fff000"),
        ],
    )];
    let out = Html::export(&doc_with(vec![b])).unwrap();
    assert!(out.contains("color:#ff0000"), "missing color style: {out}");
    assert!(
        out.contains("background-color:#fff000"),
        "missing highlight: {out}"
    );
}

#[test]
fn export_link_emits_a_with_rel() {
    let mut href_attrs = Map::new();
    href_attrs.insert("href".into(), Value::String("https://example.com".into()));
    let mut b = Block::new_paragraph(BlockId::new("p"));
    b.content = vec![TextSpan::with_marks(
        "click",
        vec![Mark::with_attrs("link", href_attrs)],
    )];
    let out = Html::export(&doc_with(vec![b])).unwrap();
    assert!(
        out.contains("<a href=\"https://example.com\" rel=\"noopener noreferrer\">click</a>"),
        "missing link: {out}"
    );
}

#[test]
fn export_link_strips_javascript_scheme() {
    let mut href_attrs = Map::new();
    href_attrs.insert("href".into(), Value::String("javascript:alert(1)".into()));
    let mut b = Block::new_paragraph(BlockId::new("p"));
    b.content = vec![TextSpan::with_marks(
        "evil",
        vec![Mark::with_attrs("link", href_attrs)],
    )];
    let out = Html::export(&doc_with(vec![b])).unwrap();
    assert!(
        !out.contains("javascript:"),
        "should drop unsafe href: {out}"
    );
    assert!(
        out.contains(">evil</"),
        "text content should still render: {out}"
    );
}

#[test]
fn export_todo_checked_and_unchecked() {
    let checked = Block::new_todo(BlockId::new("t1"), true);
    let mut unchecked = Block::new_todo(BlockId::new("t2"), false);
    unchecked.content = vec![TextSpan::plain("task")];

    let mut checked = checked;
    checked.content = vec![TextSpan::plain("done")];

    let out = Html::export(&doc_with(vec![checked, unchecked])).unwrap();
    // Todos round-trip through <p data-type="todo" data-checked="…">
    // so external clipboards see a plain paragraph but devup→devup
    // pastes re-parse them as todos.
    assert!(
        out.contains("<p data-type=\"todo\" data-checked=\"true\">"),
        "checked todo: {out}"
    );
    assert!(
        out.contains("<p data-type=\"todo\" data-checked=\"false\">"),
        "unchecked todo: {out}"
    );
    assert!(out.contains(">done</p>"));
    assert!(out.contains(">task</p>"));
}

#[test]
fn export_code_block_with_language() {
    let mut b = Block::new(BlockId::new("c"), "code");
    b.content = vec![TextSpan::plain("fn main() {}")];
    b.props
        .insert("language".into(), Value::String("rust".into()));
    let out = Html::export(&doc_with(vec![b])).unwrap();
    assert!(
        out.contains("<pre><code class=\"language-rust\">fn main() {}</code></pre>"),
        "got: {out}"
    );
}

#[test]
fn export_escapes_html_in_text() {
    let mut b = Block::new_paragraph(BlockId::new("p"));
    b.content = vec![TextSpan::plain("<script>alert(1)</script>")];
    let out = Html::export(&doc_with(vec![b])).unwrap();
    assert!(!out.contains("<script>"));
    assert!(out.contains("&lt;script&gt;"));
}

#[test]
fn export_empty_document_is_empty_string() {
    let out = Html::export(&Document::new()).unwrap();
    assert!(out.is_empty());
}

// ── Import tests ─────────────────────────────────────────────────

#[test]
fn import_h1_to_h6() {
    for level in 1u64..=6 {
        let mut id_gen = SequentialIdGenerator::new("t");
        let src = format!("<h{level}>Title</h{level}>");
        let doc = Html::import(src, &mut id_gen).unwrap();
        assert_eq!(block_types(&doc), vec!["heading".to_string()]);
        let block = doc.get_block(&BlockId::new("t-1")).unwrap();
        assert_eq!(
            block.props.get("level").and_then(Value::as_u64),
            Some(level),
            "h{level} produced wrong level"
        );
        assert_eq!(block.plain_text(), "Title");
    }
}

#[test]
fn import_todo_via_explicit_marker() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = r#"<ul data-devup-type="todo"><li><label><input type="checkbox" checked> done</label></li></ul>"#;
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["todo".to_string()]);
    let block = doc.get_block(&BlockId::new("t-1")).unwrap();
    assert_eq!(
        block.props.get("checked").and_then(Value::as_bool),
        Some(true)
    );
    assert!(block.plain_text().contains("done"));
}

#[test]
fn import_todo_via_checkbox_heuristic() {
    // Even without data-devup-type, presence of a checkbox in an <li>
    // should be treated as a todo item (matches TS clipboard importer).
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = r#"<ul><li><input type="checkbox"> open</li></ul>"#;
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["todo".to_string()]);
    let block = doc.get_block(&BlockId::new("t-1")).unwrap();
    assert_eq!(
        block.props.get("checked").and_then(Value::as_bool),
        Some(false)
    );
}

#[test]
fn import_ordered_vs_unordered_list() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = "<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    let types = block_types(&doc);
    assert_eq!(types, vec!["list", "list", "list"]);
    let styles: Vec<_> = doc
        .root_block_ids()
        .iter()
        .filter_map(|id| {
            doc.get_block(id).and_then(|b| {
                b.props
                    .get("style")
                    .and_then(Value::as_str)
                    .map(String::from)
            })
        })
        .collect();
    assert_eq!(styles, vec!["unordered", "unordered", "ordered"]);
}

#[test]
fn import_blockquote() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = "<blockquote>quoted</blockquote>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["quote".to_string()]);
    assert_eq!(block_texts(&doc), vec!["quoted".to_string()]);
}

#[test]
fn import_pre_code_with_language_class() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = "<pre><code class=\"language-python\">print('hi')\n</code></pre>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["code".to_string()]);
    let block = doc.get_block(&BlockId::new("t-1")).unwrap();
    assert_eq!(
        block.props.get("language").and_then(Value::as_str),
        Some("python")
    );
    assert!(block.plain_text().contains("print('hi')"));
}

#[test]
fn import_hr_produces_divider() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let doc = Html::import("<hr>".to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["divider".to_string()]);
}

#[test]
fn import_inline_marks_round_trip() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src =
        "<p><strong>bold</strong> <em>italic</em> <u>under</u> <s>strike</s> <code>c</code></p>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    let block = doc.get_block(&BlockId::new("t-1")).unwrap();

    let collect_marks = |mark_type: &str| -> Vec<&str> {
        block
            .content
            .iter()
            .filter(|s| s.has_mark(mark_type))
            .map(|s| s.text.as_str())
            .collect()
    };
    assert_eq!(collect_marks("bold"), vec!["bold"]);
    assert_eq!(collect_marks("italic"), vec!["italic"]);
    assert_eq!(collect_marks("underline"), vec!["under"]);
    assert_eq!(collect_marks("strike"), vec!["strike"]);
    assert_eq!(collect_marks("code"), vec!["c"]);
}

#[test]
fn import_span_color_and_highlight() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = r#"<p><span style="color:#ff0000;background-color:#fff000">rgb</span></p>"#;
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    let block = doc.get_block(&BlockId::new("t-1")).unwrap();
    let span = &block.content[0];
    assert!(span.has_mark("color"));
    assert!(span.has_mark("highlight"));
}

#[test]
fn import_link_captures_href() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = r#"<p><a href="https://example.com">click</a></p>"#;
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    let block = doc.get_block(&BlockId::new("t-1")).unwrap();
    let span = &block.content[0];
    let link_mark = span.marks.iter().find(|m| m.ty == "link").unwrap();
    assert_eq!(
        link_mark.attrs.get("href").and_then(Value::as_str),
        Some("https://example.com")
    );
}

#[test]
fn import_unknown_tag_flattens_to_paragraph() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = "<article>mystery content</article>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["paragraph".to_string()]);
    assert!(
        block_texts(&doc)[0].contains("mystery content"),
        "got: {:?}",
        block_texts(&doc)
    );
}

#[test]
fn import_transparent_containers_descend() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = "<div><section><p>inside nested wrappers</p></section></div>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert_eq!(block_types(&doc), vec!["paragraph".to_string()]);
    assert_eq!(block_texts(&doc)[0], "inside nested wrappers");
}

#[test]
fn import_details_becomes_toggle() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let src = "<details><summary>click me</summary><p>nested</p></details>";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    let types = block_types(&doc);
    // v1: toggle block + nested block both at root (see crate docs).
    assert!(types.contains(&"toggle".to_string()), "types: {types:?}");
}

#[test]
fn import_malformed_html_does_not_panic() {
    let mut id_gen = SequentialIdGenerator::new("t");
    // html5ever is tolerant — just make sure we don't panic on garbage.
    let src = "<p>unclosed <strong>text";
    let doc = Html::import(src.to_string(), &mut id_gen).unwrap();
    assert!(!block_types(&doc).is_empty());
}

#[test]
fn import_empty_document() {
    let mut id_gen = SequentialIdGenerator::new("t");
    let doc = Html::import(String::new(), &mut id_gen).unwrap();
    assert_eq!(doc.root_block_count(), 0);
}

// ── Round-trip ───────────────────────────────────────────────────

#[test]
fn roundtrip_preserves_block_types_and_text() {
    let mut doc = Document::new();

    let mut h1 = Block::new(BlockId::new("h"), "heading");
    h1.props.insert("level".into(), Value::from(1u64));
    h1.content = vec![TextSpan::plain("Title")];
    doc.push_root_block(h1);

    let mut p = Block::new_paragraph(BlockId::new("p"));
    p.content = vec![
        TextSpan::plain("Hello, "),
        TextSpan::with_marks("world", vec![Mark::bold()]),
        TextSpan::plain("."),
    ];
    doc.push_root_block(p);

    let mut li = Block::new(BlockId::new("l"), "list");
    li.props
        .insert("style".into(), Value::String("unordered".into()));
    li.content = vec![TextSpan::plain("item one")];
    doc.push_root_block(li);

    let exported = Html::export(&doc).unwrap();

    let mut id_gen = SequentialIdGenerator::new("r");
    let re = Html::import(exported, &mut id_gen).unwrap();

    assert_eq!(
        block_types(&re),
        vec!["heading", "paragraph", "list"],
        "block types drifted"
    );
    let texts = block_texts(&re);
    assert_eq!(texts[0], "Title");
    assert_eq!(texts[1], "Hello, world.");
    assert_eq!(texts[2], "item one");
}

// ──────────────────────────────────────────────────────────────────────
// Hardening: clipboard HTML is untrusted input. Importing pathological
// `style` values must NEVER panic the Rust engine — a panic inside
// WASM aborts the whole editor, which would be a paste-based DoS.
// ──────────────────────────────────────────────────────────────────────

/// Regression for the `from_f64(v).unwrap()` panic path in
/// `extract_row_props`. Several height strings hit f64 edge cases:
/// `"1e400px"` parses to `f64::INFINITY`, `"NaNpx"` to `NaN`, and a
/// negative height is semantically invalid. All three must be handled
/// without panicking; the row should just drop its `height` prop.
#[test]
fn import_tolerates_pathological_row_height_styles() {
    for raw_height in [
        // f64 parse-overflow → positive infinity
        "1e400px",
        // NaN literal
        "NaNpx",
        // Negative → semantically invalid for a row height
        "-48px",
        // Zero → same
        "0px",
        // Unparseable garbage
        "forty-eight-px",
        // Empty
        "",
    ] {
        let html = format!(
            r#"<table><tbody><tr style="height: {raw_height}"><td>cell</td></tr></tbody></table>"#
        );
        let mut id_gen = SequentialIdGenerator::new("p");
        // The whole point of the test: `Html::import` must return an
        // `Ok` (or a recoverable `Err`) — never panic.
        let result = Html::import(html, &mut id_gen);
        assert!(
            result.is_ok(),
            "import panicked or erred on row height {raw_height:?}"
        );
    }
}