textlane-html 0.7.0

Textlane's HTML plugins: parsing, transforms, sanitizing and serializing
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
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
use std::borrow::Cow;

use textlane_core::mdast::{self, AlignKind};
use textlane_core::{Allocator, ArenaVec, Position, UnistPosition};

// Building recurses once per level; past the red zone the stack grows on the heap
const STACK_RED_ZONE: usize = 64 * 1024;
const STACK_GROWTH: usize = 1024 * 1024;

/// An index into the `Store`.
pub(crate) type Id = usize;

/// The mdast a conversion builds, kept flat until it is done: handlers rewrap, split and
/// edit nodes, and remember facts about each subtree that mdast has no field for.
#[derive(Default)]
pub(crate) struct Store<'a> {
    pub(crate) items: Vec<Item<'a>>,
}

pub(crate) struct Item<'a> {
    pub(crate) kind: Kind<'a>,
    pub(crate) children: Vec<Id>,
    pub(crate) position: Option<Position<'a>>,
    /// Whether a descendant is not phrasing: hast-util-to-mdast's `wrapNeeded` of the
    /// children, kept so nested links and deletes are not searched again at each level.
    pub(crate) flow: bool,
    /// Whether the node or a descendant is a link.
    pub(crate) link: bool,
}

#[derive(Clone)]
pub(crate) enum Kind<'a> {
    Root,
    Paragraph,
    Heading(u8),
    ThematicBreak,
    Text(Text<'a>),
    Emphasis,
    Strong,
    Break,
    Code {
        lang: Option<&'a str>,
        meta: Option<&'a str>,
        value: &'a str,
    },
    InlineCode(&'a str),
    Math(&'a str),
    InlineMath(&'a str),
    Blockquote,
    List {
        ordered: bool,
        start: Option<u32>,
        spread: bool,
    },
    ListItem {
        spread: bool,
        checked: Option<bool>,
    },
    Link {
        url: &'a str,
        title: Option<&'a str>,
    },
    Image {
        url: &'a str,
        title: Option<&'a str>,
        alt: &'a str,
    },
    Html(&'a str),
    Delete,
    Table(Vec<AlignKind>),
    TableRow,
    /// A cell, with the row and column span the table handler reads off it.
    TableCell(Option<(usize, usize)>),
    /// Flow content already wrapped, spliced into its parent: re-wrapping it at each
    /// ancestor, as upstream's arrays are, is quadratic in how deep flow containers nest.
    Fragment,
}

/// Text, with the quotes `<q>` adds around it kept apart, so nesting them stays linear.
#[derive(Clone)]
pub(crate) struct Text<'a> {
    pub(crate) value: &'a str,
    /// Opening quotes, innermost first.
    pub(crate) open: Vec<char>,
    pub(crate) close: String,
}

/// What a handler returns: nothing, one node, or several.
pub(crate) enum Converted {
    Nothing,
    One(Id),
    Many(Vec<Id>),
}

impl<'a> Text<'a> {
    pub(crate) fn new(value: &'a str) -> Self {
        Self {
            value,
            open: vec![],
            close: String::new(),
        }
    }

    fn write(&self, output: &mut String) {
        output.extend(self.open.iter().rev());
        output.push_str(self.value);
        output.push_str(&self.close);
    }

    fn is_plain(&self) -> bool {
        self.open.is_empty() && self.close.is_empty()
    }

    /// Whether it is only HTML whitespace, as hast-util-whitespace checks.
    pub(crate) fn is_whitespace(&self) -> bool {
        let blank =
            |character: char| matches!(character, ' ' | '\t' | '\n' | '\u{C}' | '\r');
        self.open.iter().all(|character| blank(*character))
            && self.value.chars().all(blank)
            && self.close.chars().all(blank)
    }
}

impl<'a> Kind<'a> {
    /// An empty list item, as list handlers make one.
    pub(crate) fn list_item() -> Self {
        Kind::ListItem {
            spread: false,
            checked: None,
        }
    }

    /// Whether mdast-util-phrasing counts the node as phrasing content.
    pub(crate) fn is_phrasing(&self) -> bool {
        matches!(
            self,
            Kind::Break
                | Kind::Delete
                | Kind::Emphasis
                | Kind::Image { .. }
                | Kind::InlineCode(_)
                | Kind::InlineMath(_)
                | Kind::Link { .. }
                | Kind::Strong
                | Kind::Text(_)
        )
    }

    /// Whether the node has children.
    pub(crate) fn is_parent(&self) -> bool {
        !matches!(
            self,
            Kind::Text(_)
                | Kind::ThematicBreak
                | Kind::Break
                | Kind::Code { .. }
                | Kind::InlineCode(_)
                | Kind::Math(_)
                | Kind::InlineMath(_)
                | Kind::Image { .. }
                | Kind::Html(_)
        )
    }
}

impl<'a> Store<'a> {
    pub(crate) fn add(
        &mut self,
        kind: Kind<'a>,
        children: Vec<Id>,
        position: Option<Position<'a>>,
    ) -> Id {
        self.items.push(Item {
            kind,
            children: vec![],
            position,
            flow: false,
            link: false,
        });
        let id = self.items.len() - 1;
        self.set_children(id, children);
        id
    }

    /// A text node.
    pub(crate) fn add_text(
        &mut self,
        value: &'a str,
        position: Option<Position<'a>>,
    ) -> Id {
        self.add(Kind::Text(Text::new(value)), vec![], position)
    }

    pub(crate) fn set_children(&mut self, id: Id, children: Vec<Id>) {
        let flow = self.is_wrap_needed(&children);
        let link = children.iter().any(|child| self.items[*child].link);
        let item = &mut self.items[id];
        item.flow = flow;
        item.link = link || matches!(item.kind, Kind::Link { .. });
        item.children = children;
    }

    /// Nodes with every fragment among them replaced by its content.
    pub(crate) fn expand<'i>(&self, nodes: &'i [Id]) -> Cow<'i, [Id]> {
        let is_fragment = |node: &Id| matches!(self.items[*node].kind, Kind::Fragment);
        if !nodes.iter().any(is_fragment) {
            return Cow::Borrowed(nodes);
        }
        let mut expanded = vec![];
        let mut stack: Vec<&[Id]> = vec![nodes];
        while let Some(nodes) = stack.last_mut() {
            let Some((node, rest)) = nodes.split_first() else {
                stack.pop();
                continue;
            };
            *nodes = rest;
            if is_fragment(node) {
                stack.push(&self.items[*node].children);
            } else {
                expanded.push(*node);
            }
        }
        Cow::Owned(expanded)
    }

    pub(crate) fn take_children(&mut self, id: Id) -> Vec<Id> {
        std::mem::take(&mut self.items[id].children)
    }

    /// A copy of a node without its children, as `structuredClone` makes one.
    pub(crate) fn clone_without_children(&mut self, id: Id, children: Vec<Id>) -> Id {
        let item = &self.items[id];
        let (kind, position) = (item.kind.clone(), item.position);
        self.add(kind, children, position)
    }

    /// Whether some node is not phrasing or holds one that is not, as `wrapNeeded` checks.
    pub(crate) fn is_wrap_needed(&self, nodes: &[Id]) -> bool {
        nodes.iter().any(|node| {
            let item = &self.items[*node];
            !item.kind.is_phrasing() || item.flow
        })
    }

    /// Whether several list items are and one of them is spread, as `listItemsSpread` checks.
    pub(crate) fn is_spread(&self, items: &[Id]) -> bool {
        items.len() > 1
            && items.iter().any(|item| {
                matches!(self.items[*item].kind, Kind::ListItem { spread: true, .. })
            })
    }

    pub(crate) fn is_phrasing(&self, id: Id) -> bool {
        self.items[id].kind.is_phrasing()
    }

    /// The text in nodes, as mdast-util-to-string reads it.
    pub(crate) fn read_string(&self, nodes: &[Id]) -> String {
        let mut output = String::new();
        for node in nodes {
            self.write_string(*node, &mut output);
        }
        output
    }

    fn write_string(&self, id: Id, output: &mut String) {
        let item = &self.items[id];
        match &item.kind {
            Kind::Text(text) => text.write(output),
            Kind::Code { value, .. }
            | Kind::InlineCode(value)
            | Kind::Math(value)
            | Kind::InlineMath(value)
            | Kind::Html(value) => {
                output.push_str(value);
            }
            Kind::Image { alt, .. } if !alt.is_empty() => output.push_str(alt),
            _ => stacker::maybe_grow(STACK_RED_ZONE, STACK_GROWTH, || {
                for child in &item.children {
                    self.write_string(*child, output);
                }
            }),
        }
    }

    /// Turn a node into mdast, merging and trimming text as hast-util-to-mdast's last pass
    /// does.
    pub(crate) fn build(&self, id: Id, alloc: &'a Allocator) -> mdast::Node<'a> {
        stacker::maybe_grow(STACK_RED_ZONE, STACK_GROWTH, || self.build_node(id, alloc))
    }

    fn build_node(&self, id: Id, alloc: &'a Allocator) -> mdast::Node<'a> {
        let item = &self.items[id];
        let position = item.position;
        let children = || self.build_children(&item.kind, &item.children, alloc);
        match &item.kind {
            Kind::Root => mdast::Node::Root(mdast::Root {
                children: children(),
                position,
                data: None,
            }),
            Kind::Paragraph => mdast::Node::Paragraph(mdast::Paragraph {
                children: children(),
                position,
                data: None,
            }),
            Kind::Heading(depth) => mdast::Node::Heading(mdast::Heading {
                depth: *depth,
                children: children(),
                position,
                data: None,
            }),
            Kind::ThematicBreak => mdast::Node::ThematicBreak(mdast::ThematicBreak {
                position,
                data: None,
            }),
            Kind::Text(text) => {
                let value = if text.is_plain() {
                    text.value
                } else {
                    let mut value = String::new();
                    text.write(&mut value);
                    alloc.alloc_str(&value)
                };
                mdast::Node::Text(mdast::Text {
                    value,
                    position,
                    data: None,
                })
            }
            Kind::Emphasis => mdast::Node::Emphasis(mdast::Emphasis {
                children: children(),
                position,
                data: None,
            }),
            Kind::Strong => mdast::Node::Strong(mdast::Strong {
                children: children(),
                position,
                data: None,
            }),
            Kind::Break => mdast::Node::Break(mdast::Break {
                position,
                data: None,
            }),
            Kind::Code { lang, meta, value } => mdast::Node::Code(mdast::Code {
                lang: *lang,
                meta: *meta,
                value,
                position,
                data: None,
            }),
            Kind::Math(value) => mdast::Node::Math(mdast::Math {
                meta: None,
                value,
                position,
                data: None,
            }),
            Kind::InlineMath(value) => mdast::Node::InlineMath(mdast::InlineMath {
                value,
                position,
                data: None,
            }),
            Kind::InlineCode(value) => mdast::Node::InlineCode(mdast::InlineCode {
                value,
                position,
                data: None,
            }),
            Kind::Blockquote => mdast::Node::Blockquote(mdast::Blockquote {
                children: children(),
                position,
                data: None,
            }),
            Kind::List {
                ordered,
                start,
                spread,
            } => mdast::Node::List(mdast::List {
                ordered: *ordered,
                start: *start,
                spread: Some(*spread),
                children: children(),
                position,
                data: None,
            }),
            Kind::ListItem { spread, checked } => {
                mdast::Node::ListItem(mdast::ListItem {
                    spread: Some(*spread),
                    checked: *checked,
                    children: children(),
                    position,
                    data: None,
                })
            }
            Kind::Link { url, title } => mdast::Node::Link(mdast::Link {
                url,
                title: *title,
                children: children(),
                position,
                data: None,
            }),
            Kind::Image { url, title, alt } => mdast::Node::Image(mdast::Image {
                url,
                title: *title,
                alt: Some(alt),
                position,
                data: None,
            }),
            Kind::Html(value) => mdast::Node::Html(mdast::Html {
                value,
                position,
                data: None,
            }),
            Kind::Delete => mdast::Node::Delete(mdast::Delete {
                children: children(),
                position,
                data: None,
            }),
            Kind::Table(align) => {
                let mut kinds = ArenaVec::with_capacity_in(align.len(), alloc);
                kinds.extend(align.iter().copied());
                mdast::Node::Table(mdast::Table {
                    align: kinds,
                    children: children(),
                    position,
                    data: None,
                })
            }
            Kind::TableRow => mdast::Node::TableRow(mdast::TableRow {
                children: children(),
                position,
                data: None,
            }),
            Kind::TableCell(_) => mdast::Node::TableCell(mdast::TableCell {
                children: children(),
                position,
                data: None,
            }),
            Kind::Fragment => mdast::Node::Root(mdast::Root {
                children: children(),
                position,
                data: None,
            }),
        }
    }

    // Adjacent texts merge into one; the first line ending of each loses the spaces and
    // tabs around it, and in headings, paragraphs and roots texts are trimmed at the edges
    // and dropped when that empties them. Each text is added and reprocessed in turn, as
    // `visit` does, which comes to processing each run of texts once it ends.
    fn build_children(
        &self,
        parent: &Kind<'a>,
        children: &[Id],
        alloc: &'a Allocator,
    ) -> ArenaVec<'a, mdast::Node<'a>> {
        let trims = matches!(parent, Kind::Heading(_) | Kind::Paragraph | Kind::Root);
        let children = self.expand(children);
        let children = &children[..];
        let mut nodes = ArenaVec::with_capacity_in(children.len(), alloc);
        let mut run: Option<Run<'_, 'a>> = None;
        for (index, child) in children.iter().enumerate() {
            let item = &self.items[*child];
            let Kind::Text(text) = &item.kind else {
                if let Some(run) = run.take() {
                    run.finish(trims, false, &mut nodes, alloc);
                }
                nodes.push(self.build(*child, alloc));
                continue;
            };
            let last = index == children.len() - 1;
            let current = run.get_or_insert_with(|| Run {
                texts: vec![],
                index: nodes.len(),
                position: item.position,
                blank: true,
                empty: true,
            });
            if !current.texts.is_empty()
                && let (Some(start), Some(end)) = (current.position, item.position)
            {
                current.position = merge_positions(start, end, alloc);
            }
            current.add(text);
            // At the start, a run of only spaces and tabs is trimmed away before the next
            // text can join it; elsewhere only an empty run is dropped
            let dropped = if trims && current.index == 0 {
                current.blank
            } else {
                current.empty
            };
            if dropped {
                run = None;
            } else if last && let Some(run) = run.take() {
                run.finish(trims, true, &mut nodes, alloc);
            }
        }
        if let Some(run) = run.take() {
            run.finish(trims, false, &mut nodes, alloc);
        }
        nodes
    }
}

// Texts that merge into one
struct Run<'t, 'a> {
    texts: Vec<&'t Text<'a>>,
    index: usize,
    position: Option<Position<'a>>,
    blank: bool,
    empty: bool,
}

impl<'t, 'a> Run<'t, 'a> {
    fn add(&mut self, text: &'t Text<'a>) {
        let blank = |character: char| character == ' ' || character == '\t';
        self.blank = self.blank
            && text.open.iter().all(|character| blank(*character))
            && text.value.chars().all(blank)
            && text.close.chars().all(blank);
        self.empty = self.empty
            && text.open.is_empty()
            && text.value.is_empty()
            && text.close.is_empty();
        self.texts.push(text);
    }

    fn finish(
        self,
        trims: bool,
        last: bool,
        nodes: &mut ArenaVec<'a, mdast::Node<'a>>,
        alloc: &'a Allocator,
    ) {
        let value = match self.texts[..] {
            [text] if text.is_plain() => Cow::Borrowed(text.value),
            _ => {
                let mut output = String::new();
                for text in &self.texts {
                    text.write(&mut output);
                }
                Cow::Owned(output)
            }
        };
        let mut value = collapse_first_line_ending(value);
        if trims {
            if self.index == 0 {
                value = trim_cow(value, |text| text.trim_start_matches([' ', '\t']));
            }
            if last {
                value = trim_cow(value, |text| text.trim_end_matches([' ', '\t']));
            }
        }
        if value.is_empty() {
            return;
        }
        let value = match value {
            Cow::Borrowed(value) => value,
            Cow::Owned(value) => alloc.alloc_str(&value),
        };
        nodes.push(mdast::Node::Text(mdast::Text {
            value,
            position: self.position,
            data: None,
        }));
    }
}

// `value.replace(/[\t ]*(\r?\n|\r)[\t ]*/, '$1')`: only the first line ending
fn collapse_first_line_ending(value: Cow<'_, str>) -> Cow<'_, str> {
    let bytes = value.as_bytes();
    let Some(ending) = bytes.iter().position(|byte| matches!(byte, b'\n' | b'\r')) else {
        return value;
    };
    let is_space = |byte: &u8| matches!(byte, b' ' | b'\t');
    let start = ending
        - bytes[..ending]
            .iter()
            .rev()
            .take_while(|byte| is_space(byte))
            .count();
    let ending_end = if bytes[ending] == b'\r' && bytes.get(ending + 1) == Some(&b'\n') {
        ending + 2
    } else {
        ending + 1
    };
    let end = ending_end
        + bytes[ending_end..]
            .iter()
            .take_while(|byte| is_space(byte))
            .count();
    if start == ending && end == ending_end {
        return value;
    }
    let mut output = String::with_capacity(value.len());
    output.push_str(&value[..start]);
    output.push_str(&value[ending..ending_end]);
    output.push_str(&value[end..]);
    Cow::Owned(output)
}

fn trim_cow<'v>(value: Cow<'v, str>, trim: impl Fn(&str) -> &str) -> Cow<'v, str> {
    match value {
        Cow::Borrowed(value) => Cow::Borrowed(trim(value)),
        Cow::Owned(value) => Cow::Owned(trim(&value).to_string()),
    }
}

// The span from one text's start to another's end; a unist position joins only another
fn merge_positions<'a>(
    start: Position<'a>,
    end: Position<'a>,
    alloc: &'a Allocator,
) -> Option<Position<'a>> {
    match (start, end) {
        (Position::Span { start, .. }, Position::Span { end, .. }) => {
            Some(Position::span(start, end))
        }
        (Position::Unist(start), Position::Unist(end)) => {
            Some(Position::Unist(alloc.alloc(UnistPosition {
                start: start.start,
                end: end.end,
            })))
        }
        _ => Some(start),
    }
}