textlane-html 0.5.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
use std::borrow::Cow;

use textlane_core::Error;
use textlane_core::hast::{Element, Node, PropertyItem, PropertyValue};

use crate::helpers::omission::{Siblings, is_closing_omitted, is_opening_omitted};
use crate::helpers::property::find_property;
use crate::helpers::schema::{
    BOOLEAN, COMMA_SEPARATED, HTML, Info, OVERLOADED_BOOLEAN, SVG,
};
use crate::models::options::Stringify;

// html-void-elements
pub(crate) const VOID_ELEMENTS: [&str; 20] = [
    "area", "base", "basefont", "bgsound", "br", "col", "command", "embed", "frame",
    "hr", "image", "img", "input", "keygen", "link", "meta", "param", "source", "track",
    "wbr",
];

// What to escape in attribute names, unquoted, single- and double-quoted values: indexed
// by whether parse errors are not allowed, then whether dangerous characters are not
const NAME: [[&str; 2]; 2] = [
    ["\t\n\x0C\r &/=>", "\t\n\x0C\r \"&'/=>`"],
    ["\0\t\n\x0C\r \"&'/<=>", "\0\t\n\x0C\r \"&'/<=>`"],
];
const UNQUOTED: [[&str; 2]; 2] = [
    ["\t\n\x0C\r &>", "\0\t\n\x0C\r \"&'<=>`"],
    ["\0\t\n\x0C\r \"&'<=>`", "\0\t\n\x0C\r \"&'<=>`"],
];
const SINGLE: [[&str; 2]; 2] = [["&'", "\"&'`"], ["\0&'", "\0\"&'`"]];
const DOUBLE: [[&str; 2]; 2] = [["\"&", "\"&'`"], ["\0\"&", "\0\"&'`"]];

/// Serialize hast as HTML, as hast-util-to-html does.
///
/// Walks with an explicit stack instead of recursing, so a deeply nested tree cannot
/// overflow the call stack.
pub fn stringify_hast(node: &Node<'_>, options: &Stringify) -> Result<String, Error> {
    let quote = match options.quote.as_deref() {
        None | Some("" | "\"") => '"',
        Some("'") => '\'',
        Some(quote) => {
            return Err(Error::new(
                "invalid-options",
                format!("Invalid quote `{quote}`, expected `'` or `\"`"),
            ));
        }
    };
    let mut state = State {
        options,
        output: String::new(),
        schema: if options.space.as_deref() == Some("svg") {
            &SVG
        } else {
            &HTML
        },
        quote,
        alternative: if quote == '"' { '\'' } else { '"' },
    };
    let mut stack = vec![];
    state.open(node, 0, None, &mut stack)?;
    while let Some(step) = stack.last_mut() {
        match step {
            Step::Children { siblings, next } => {
                let siblings = *siblings;
                let index = *next;
                match siblings.children.get(index) {
                    Some(child) => {
                        *next += 1;
                        state.open(child, index, Some(siblings), &mut stack)?;
                    }
                    None => {
                        stack.pop();
                    }
                }
            }
            Step::Close(_) => {
                if let Some(Step::Close(close)) = stack.pop() {
                    state.close(close);
                }
            }
        }
    }
    Ok(state.output)
}

struct State<'o> {
    options: &'o Stringify,
    output: String,
    schema: &'static [Info],
    quote: char,
    alternative: char,
}

enum Step<'n, 'a> {
    Children {
        siblings: Siblings<'n, 'a>,
        next: usize,
    },
    Close(Close<'n, 'a>),
}

struct Close<'n, 'a> {
    element: &'n Element<'a>,
    index: usize,
    parent: Option<Siblings<'n, 'a>>,
    schema: &'static [Info],
    omit: bool,
    self_closing: bool,
    // Where the content starts, to tell an empty element, which closes itself
    content_start: usize,
    wrote_start_tag: bool,
    tight_slash: bool,
}

fn is_svg(schema: &'static [Info]) -> bool {
    std::ptr::eq(schema, &SVG[..])
}

impl<'o> State<'o> {
    fn open<'n, 'a>(
        &mut self,
        node: &'n Node<'a>,
        index: usize,
        parent: Option<Siblings<'n, 'a>>,
        stack: &mut Vec<Step<'n, 'a>>,
    ) -> Result<(), Error> {
        match node {
            Node::Root(root) => {
                stack.push(Step::Children {
                    siblings: Siblings {
                        tag_name: None,
                        children: &root.children,
                    },
                    next: 0,
                });
            }
            Node::Element(element) => self.open_element(element, index, parent, stack),
            Node::Text(text) => self.write_text(text.value, parent),
            Node::Raw(raw) => {
                // Unlike upstream, raw is escaped even in `script` and `style` unless allowed:
                // its value is HTML from elsewhere, which could close the element early
                if self.options.allow_dangerous_html {
                    self.output.push_str(raw.value);
                } else {
                    push_escaped(&mut self.output, raw.value, "<&");
                }
            }
            Node::Comment(comment) => self.write_comment(comment.value),
            Node::Doctype(_) => {
                self.output.push_str("<!");
                self.output.push_str(if self.options.upper_doctype {
                    "DOCTYPE"
                } else {
                    "doctype"
                });
                if !self.options.tight_doctype {
                    self.output.push(' ');
                }
                self.output.push_str("html>");
            }
            Node::Unknown(unknown) => {
                return Err(Error::new(
                    "unknown-node",
                    format!("Cannot compile unknown node `{}`", unknown.kind),
                ));
            }
            Node::MdxJsxFlowElement(_)
            | Node::MdxJsxTextElement(_)
            | Node::MdxFlowExpression(_)
            | Node::MdxTextExpression(_)
            | Node::MdxjsEsm(_) => {
                let kind = match node {
                    Node::MdxJsxFlowElement(_) => "mdxJsxFlowElement",
                    Node::MdxJsxTextElement(_) => "mdxJsxTextElement",
                    Node::MdxFlowExpression(_) => "mdxFlowExpression",
                    Node::MdxTextExpression(_) => "mdxTextExpression",
                    _ => "mdxjsEsm",
                };
                return Err(Error::new(
                    "unknown-node",
                    format!("Cannot compile unknown node `{kind}`"),
                ));
            }
        }
        Ok(())
    }

    fn open_element<'n, 'a>(
        &mut self,
        element: &'n Element<'a>,
        index: usize,
        parent: Option<Siblings<'n, 'a>>,
        stack: &mut Vec<Step<'n, 'a>>,
    ) {
        let schema = self.schema;
        let svg = is_svg(schema);
        let omit = !svg && self.options.omit_optional_tags;
        let self_closing = if svg {
            self.options.close_empty_elements
        } else {
            let name = if element
                .tag_name
                .bytes()
                .any(|byte| byte.is_ascii_uppercase() || !byte.is_ascii())
            {
                Cow::Owned(element.tag_name.to_lowercase())
            } else {
                Cow::Borrowed(element.tag_name)
            };
            match &self.options.voids {
                Some(voids) => voids.iter().any(|void| *void == name),
                None => VOID_ELEMENTS.contains(&&*name),
            }
        };
        if !svg && element.tag_name == "svg" {
            self.schema = &SVG;
        }
        let start = self.output.len();
        self.output.push('<');
        self.output.push_str(element.tag_name);
        let attributes = self.output.len();
        self.write_attributes(element);
        let mut wrote_start_tag = false;
        let mut tight_slash = false;
        if self.output.len() == attributes
            && omit
            && is_opening_omitted(element, index, parent)
        {
            self.output.truncate(start);
        } else {
            let last = self.output[attributes..].chars().next_back();
            tight_slash = self.options.tight_self_closing
                && last != Some('/')
                && last.is_none_or(|last| last == '"' || last == '\'');
            self.output.push('>');
            wrote_start_tag = true;
        }
        let children = match element.content {
            Some(content) if !svg && element.tag_name == "template" => &content.children,
            _ => &element.children,
        };
        stack.push(Step::Close(Close {
            element,
            index,
            parent,
            schema,
            omit,
            self_closing,
            content_start: self.output.len(),
            wrote_start_tag,
            tight_slash,
        }));
        stack.push(Step::Children {
            siblings: Siblings {
                tag_name: Some(element.tag_name),
                children,
            },
            next: 0,
        });
    }

    fn close(&mut self, close: Close) {
        let self_closing = close.self_closing && self.output.len() == close.content_start;
        if self_closing
            && close.wrote_start_tag
            && (is_svg(close.schema) || self.options.close_self_closing)
        {
            self.output.pop();
            if !close.tight_slash {
                self.output.push(' ');
            }
            self.output.push_str("/>");
        }
        self.schema = close.schema;
        if !self_closing
            && (!close.omit
                || !is_closing_omitted(close.element, close.index, close.parent))
        {
            self.output.push_str("</");
            self.output.push_str(close.element.tag_name);
            self.output.push('>');
        }
    }

    fn write_text(&mut self, value: &str, parent: Option<Siblings>) {
        if parent
            .and_then(|parent| parent.tag_name)
            .is_some_and(|name| name == "script" || name == "style")
        {
            self.output.push_str(value);
        } else {
            push_escaped(&mut self.output, value, "<&");
        }
    }

    fn write_comment(&mut self, value: &str) {
        if self.options.bogus_comments {
            self.output.push_str("<?");
            push_escaped(&mut self.output, value, ">");
            self.output.push('>');
            return;
        }
        self.output.push_str("<!--");
        // `/^>|^->|<!--|-->|--!>|<!-$/g`
        let mut rest = value;
        let mut at_start = true;
        while !rest.is_empty() {
            let found = if at_start && rest.starts_with('>') {
                Some(1)
            } else if at_start && rest.starts_with("->") {
                Some(2)
            } else if rest.starts_with("<!--") || rest.starts_with("--!>") {
                Some(4)
            } else if rest.starts_with("-->") || rest == "<!-" {
                Some(3)
            } else {
                None
            };
            match found {
                Some(size) => {
                    push_escaped(&mut self.output, &rest[..size], "<>");
                    rest = &rest[size..];
                }
                None => {
                    let character = rest.chars().next().unwrap_or_default();
                    self.output.push(character);
                    rest = &rest[character.len_utf8()..];
                }
            }
            at_start = false;
        }
        self.output.push_str("-->");
    }

    // Each attribute after a space, or right after a quoted one with `tight_attributes`
    fn write_attributes(&mut self, element: &Element) {
        let mut tight = false;
        for property in element.properties.iter() {
            if matches!(property.value, PropertyValue::Null) {
                continue;
            }
            let mark = self.output.len();
            if !tight {
                self.output.push(' ');
            }
            let value = self.output.len();
            self.write_attribute(property.name, &property.value);
            if self.output.len() == value {
                self.output.truncate(mark);
                continue;
            }
            tight = self.options.tight_attributes
                && matches!(self.output.as_bytes().last(), Some(b'"' | b'\''));
        }
    }

    fn write_attribute(&mut self, key: &str, value: &PropertyValue) {
        let found = find_property(self.schema, key);
        let x = usize::from(!(self.options.allow_parse_errors && !is_svg(self.schema)));
        let y = usize::from(!self.options.allow_dangerous_characters);
        let attribute = found.attribute.as_ref();
        let is_bool = found.kinds & (BOOLEAN | OVERLOADED_BOOLEAN) != 0;
        let value: Value = match value {
            PropertyValue::String(text)
                if found.kinds & OVERLOADED_BOOLEAN != 0
                    && (*text == attribute || text.is_empty()) =>
            {
                Value::Boolean(true)
            }
            PropertyValue::String(text)
                if is_bool && (*text == attribute || text.is_empty()) =>
            {
                Value::Boolean(!text.is_empty())
            }
            PropertyValue::Number(number) if is_bool => {
                Value::Boolean(*number != 0.0 && !number.is_nan())
            }
            PropertyValue::List(_) if is_bool => Value::Boolean(true),
            PropertyValue::Null => return,
            PropertyValue::Boolean(boolean) => Value::Boolean(*boolean),
            PropertyValue::Number(number) if number.is_nan() => return,
            PropertyValue::Number(number) => {
                Value::Text(Cow::Owned(write_js_number(*number)))
            }
            PropertyValue::String(text) => Value::Text(Cow::Borrowed(text)),
            PropertyValue::List(items) => {
                let items: Vec<String> = items
                    .iter()
                    .map(|item| match item {
                        PropertyItem::Number(number) => write_js_number(*number),
                        PropertyItem::String(text) => (*text).to_string(),
                    })
                    .collect();
                Value::Text(Cow::Owned(if found.kinds & COMMA_SEPARATED != 0 {
                    join_commas(&items, !self.options.tight_comma_separated_lists)
                } else {
                    items.join(" ").trim_matches(is_js_space).to_string()
                }))
            }
        };
        let value = match value {
            Value::Boolean(false) => return,
            Value::Boolean(true) => {
                push_escaped(&mut self.output, attribute, NAME[x][y]);
                return;
            }
            Value::Text(value) => value,
        };
        push_escaped(&mut self.output, attribute, NAME[x][y]);
        if self.options.collapse_empty_attributes && value.is_empty() {
            return;
        }
        if self.options.prefer_unquoted && !has_any(&value, UNQUOTED[x][y]) {
            if !value.is_empty() {
                self.output.push('=');
                self.output.push_str(&value);
            }
            return;
        }
        let mut quote = self.quote;
        if self.options.quote_smart
            && value.matches(quote).count() > value.matches(self.alternative).count()
        {
            quote = self.alternative;
        }
        let subset = if quote == '\'' {
            SINGLE[x][y]
        } else {
            DOUBLE[x][y]
        };
        self.output.push('=');
        self.output.push(quote);
        push_escaped(&mut self.output, &value, subset);
        self.output.push(quote);
    }
}

enum Value<'v> {
    Boolean(bool),
    Text(Cow<'v, str>),
}

// comma-separated-tokens' `stringify`: a trailing empty item keeps its comma
fn join_commas(items: &[String], pad_left: bool) -> String {
    let separator = if pad_left { ", " } else { "," };
    let mut joined = items.join(separator);
    if items.last().is_some_and(String::is_empty) {
        joined.push_str(separator);
    }
    joined.trim_matches(is_js_space).to_string()
}

fn is_js_space(character: char) -> bool {
    character.is_whitespace() || character == '\u{FEFF}'
}

fn has_any(value: &str, subset: &str) -> bool {
    find_any(value.as_bytes(), subset.as_bytes(), 0).is_some()
}

// stringify-entities with a subset: each listed character as a hexadecimal reference. The
// subsets are ASCII, so a byte never matches inside a multibyte character
fn push_escaped(output: &mut String, value: &str, subset: &str) {
    let bytes = value.as_bytes();
    let mut start = 0;
    while let Some(index) = find_any(bytes, subset.as_bytes(), start) {
        let byte = bytes[index];
        output.push_str(&value[start..index]);
        output.push_str("&#x");
        if byte >= 16 {
            output.push(char::from(HEX[usize::from(byte >> 4)]));
        }
        output.push(char::from(HEX[usize::from(byte & 15)]));
        output.push(';');
        start = index + 1;
    }
    output.push_str(&value[start..]);
}

fn find_any(bytes: &[u8], subset: &[u8], from: usize) -> Option<usize> {
    let rest = &bytes[from..];
    let found = match *subset {
        [] => None,
        [a] => memchr::memchr(a, rest),
        [a, b] => memchr::memchr2(a, b, rest),
        [a, b, c] => memchr::memchr3(a, b, c, rest),
        _ => {
            let mut mask = [false; 256];
            for &byte in subset {
                mask[usize::from(byte)] = true;
            }
            rest.iter().position(|&byte| mask[usize::from(byte)])
        }
    };
    found.map(|index| from + index)
}

const HEX: &[u8; 16] = b"0123456789ABCDEF";

/// A number as JavaScript's `String(number)` writes it.
pub(crate) fn write_js_number(number: f64) -> String {
    if number.is_infinite() {
        return if number > 0.0 {
            "Infinity"
        } else {
            "-Infinity"
        }
        .to_string();
    }
    if number == 0.0 {
        return "0".to_string();
    }
    let magnitude = number.abs();
    if (1e-6..1e21).contains(&magnitude) {
        return format!("{number}");
    }
    // JavaScript writes the shortest digits with an exponent: `1e+21`, `1.5e-7`
    let formatted = format!("{number:e}");
    match formatted.split_once('e') {
        Some((mantissa, exponent)) if !exponent.starts_with('-') => {
            format!("{mantissa}e+{exponent}")
        }
        _ => formatted,
    }
}

#[cfg(test)]
#[path = "stringify_unit.rs"]
mod unit;