katex-rs 0.2.4

A Rust implementation of KaTeX - Fast math typesetting for anywhere, more than just the web.
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
//! MathML tree node definitions for MathML rendering
//!
//! These objects store data about MathML nodes. This is the MathML equivalent
//! of the types in dom_tree.rs. Since MathML handles its own rendering, and
//! since we're mainly using MathML to improve accessibility, we don't manage
//! any of the styling state that the plain DOM nodes do.

use crate::ParseError;
#[cfg(feature = "wasm")]
use crate::dom_tree::create_class;
use crate::tree::{DocumentFragment, VirtualNode};
use crate::units::make_em;
use crate::utils::escape_into;
#[cfg(feature = "wasm")]
use crate::web_context::WebContext;
use crate::{namespace::KeyMap, types::ClassList, types::CssStyle};
use bon::bon;
use core::fmt::{self, Debug, Write as _};
use strum::AsRefStr;
#[cfg(feature = "wasm")]
use wasm_bindgen::JsCast as _;
#[cfg(feature = "wasm")]
use wasm_bindgen::UnwrapThrowExt as _;
#[cfg(feature = "wasm")]
use web_sys;

fn map_fmt(result: fmt::Result) -> Result<(), ParseError> {
    result.map_err(ParseError::from)
}

#[cfg(feature = "wasm")]
fn set_attribute(element: &web_sys::Element, name: &str, value: &str) {
    element.set_attribute(name, value).unwrap_throw();
}

#[cfg(feature = "wasm")]
fn append_child(parent: &web_sys::Element, child: &web_sys::Node) {
    parent.append_child(child).unwrap_throw();
}

#[cfg(feature = "wasm")]
fn create_element(ctx: &WebContext, name: &str) -> web_sys::Element {
    ctx.document
        .create_element_ns(Some("http://www.w3.org/1998/Math/MathML"), name)
        .unwrap_throw()
}

/// MathML node types used in KaTeX
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsRefStr)]
#[strum(serialize_all = "lowercase")]
pub enum MathNodeType {
    /// `<math>` element
    Math,
    /// `<annotation>` element
    Annotation,
    /// `<semantics>` element
    Semantics,
    /// `<mtext>` element
    Mtext,
    /// `<mn>` element
    Mn,
    /// `<mo>` element
    Mo,
    /// `<mi>` element
    Mi,
    /// `<mspace>` element
    Mspace,
    /// `<mover>` element
    Mover,
    /// `<munder>` element
    Munder,
    /// `<munderover>` element
    Munderover,
    /// `<msup>` element
    Msup,
    /// `<msub>` element
    Msub,
    /// `<msubsup>` element
    Msubsup,
    /// `<mfrac>` element
    Mfrac,
    /// `<mroot>` element
    Mroot,
    /// `<msqrt>` element
    Msqrt,
    /// `<mtable>` element
    Mtable,
    /// `<mtr>` element
    Mtr,
    /// `<mtd>` element
    Mtd,
    /// `<mlabeledtr>` element
    Mlabeledtr,
    /// `<mrow>` element
    Mrow,
    /// `<menclose>` element
    Menclose,
    /// `<mstyle>` element
    Mstyle,
    /// `<mpadded>` element
    Mpadded,
    /// `<mphantom>` element
    Mphantom,
    /// `<mglyph>` element
    Mglyph,
}

/// Get the appropriate space character based on width
///
/// See <https://www.w3.org/TR/2000/WD-MathML2-20000328/chapter6.html>
/// for a table of space-like characters. We use Unicode representations
/// instead of &LongNames; as it's not clear how to make the latter via
/// document.createTextNode.
#[must_use]
pub fn get_space_character(width: f64) -> Option<String> {
    // Positive spaces
    if (0.05555..=0.05556).contains(&width) {
        Some("\u{200a}".to_owned()) // &VeryThinSpace;
    } else if (0.1666..=0.1667).contains(&width) {
        Some("\u{2009}".to_owned()) // &ThinSpace;
    } else if (0.2222..=0.2223).contains(&width) {
        Some("\u{2005}".to_owned()) // &MediumSpace;
    } else if (0.2777..=0.2778).contains(&width) {
        Some("\u{2005}\u{200a}".to_owned()) // &ThickSpace;
    }
    // Negative spaces - these include INVISIBLE SEPARATOR (U+2063) for proper negative spacing
    else if (-0.05556..=-0.05555).contains(&width) {
        Some("\u{200a}\u{2063}".to_owned()) // &NegativeVeryThinSpace;
    } else if (-0.1667..=-0.1666).contains(&width) {
        Some("\u{2009}\u{2063}".to_owned()) // &NegativeThinSpace;
    } else if (-0.2223..=-0.2222).contains(&width) {
        Some("\u{205f}\u{2063}".to_owned()) // &NegativeMediumSpace;
    } else if (-0.2778..=-0.2777).contains(&width) {
        Some("\u{2005}\u{2063}".to_owned()) // &NegativeThickSpace;
    } else {
        None
    }
}

// Clone implementation removed as we now use MathDomNodeEnum directly

/// MathML DOM node enum for type-safe node representation
#[derive(Clone)]
pub enum MathDomNode {
    /// MathML element node
    Math(MathNode),
    /// Text content node
    Text(TextNode),
    /// Space node
    Space(SpaceNode),
    /// Document fragment node
    Fragment(Box<MathDomFragment>),
}

/// Document fragment containing MathML DOM nodes
pub type MathDomFragment = DocumentFragment<MathDomNode>;

/// Make a MathDom Fragment
#[must_use]
pub fn make_fragment(children: Vec<MathDomNode>) -> MathDomFragment {
    MathDomFragment {
        children,
        classes: ClassList::Empty,
        depth: 0.0,
        height: 0.0,
        max_font_size: 0.0,
        style: CssStyle::default(),
    }
}

/// General purpose MathML node of any type
#[derive(Clone)]
pub struct MathNode {
    /// The type of MathML node
    pub node_type: MathNodeType,
    /// Attributes of the MathML node
    pub attributes: KeyMap<String, String>,
    /// Child nodes of the MathML node
    pub children: Vec<MathDomNode>,
    /// CSS classes applied to the MathML node
    pub classes: ClassList,
}

impl Debug for MathNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MathNode")
            .field("node_type", &self.node_type)
            .field("attributes", &self.attributes)
            .field(
                "children",
                &format_args!("{} children", self.children.len()),
            )
            .field("classes", &self.classes)
            .finish()
    }
}

#[bon]
impl MathNode {
    /// Create a new MathNode with the given type
    #[builder]
    pub fn new(
        /// Node type
        node_type: MathNodeType,
        /// Node attributes
        attributes: Option<KeyMap<String, String>>,
        /// Child nodes
        children: Option<Vec<MathDomNode>>,
        /// CSS classes
        classes: Option<ClassList>,
    ) -> Self {
        Self {
            node_type,
            attributes: attributes.unwrap_or_default(),
            children: children.unwrap_or_default(),
            classes: classes.unwrap_or_default(),
        }
    }

    /// Create a new MathNode with the given type and children
    #[must_use]
    pub fn with_children(node_type: MathNodeType, children: Vec<MathDomNode>) -> Self {
        Self {
            node_type,
            attributes: KeyMap::default(),
            children,
            classes: ClassList::Empty,
        }
    }

    /// Add a child to this node
    pub fn add_child(&mut self, child: MathDomNode) {
        self.children.push(child);
    }

    /// Set an attribute on this node
    pub fn set_attribute<K, V>(&mut self, key: K, value: V)
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.attributes.insert(key.into(), value.into());
    }

    fn to_text(&self) -> String {
        self.children.iter().map(MathDomNode::to_text).collect()
    }
}

impl VirtualNode for MathNode {
    fn write_markup(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), ParseError> {
        map_fmt(write!(fmt, "<{}", self.node_type.as_ref()))?;

        if !self.classes.is_empty() {
            map_fmt(fmt.write_str(" class=\""))?;
            let mut first = true;
            for class in &self.classes {
                if !first {
                    map_fmt(fmt.write_char(' '))?;
                }
                first = false;
                map_fmt(escape_into(fmt, class))?;
            }
            map_fmt(fmt.write_char('"'))?;
        }

        for (key, value) in &self.attributes {
            map_fmt(write!(fmt, " {key}=\""))?;
            map_fmt(escape_into(fmt, value))?;
            map_fmt(fmt.write_char('"'))?;
        }

        map_fmt(fmt.write_char('>'))?;

        for child in &self.children {
            child.write_markup(fmt)?;
        }

        map_fmt(write!(fmt, "</{}>", self.node_type.as_ref()))?;
        Ok(())
    }

    #[cfg(feature = "wasm")]
    fn to_node(&self, ctx: &WebContext) -> web_sys::Node {
        use wasm_bindgen::JsCast as _;

        let element = create_element(ctx, self.node_type.as_ref());

        // Set attributes
        for (key, value) in &self.attributes {
            set_attribute(&element, key, value);
        }

        // Set classes
        if !self.classes.is_empty() {
            let class_str = create_class(&self.classes);
            set_attribute(&element, "class", &class_str);
        }

        // Add children, combining consecutive TextNodes
        let mut i = 0;
        while i < self.children.len() {
            if let MathDomNode::Text(text_node) = &self.children[i] {
                // Check if next child is also a TextNode
                if i + 1 < self.children.len()
                    && let MathDomNode::Text(next_text_node) = &self.children[i + 1]
                {
                    // Combine them
                    let combined_text = format!("{}{}", text_node.text, next_text_node.text);
                    let text_node = ctx.document.create_text_node(&combined_text);
                    append_child(&element, &text_node);
                    i += 2; // Skip the next one
                    continue;
                }
            }
            // Normal case
            let child_node = self.children[i].to_node(ctx);
            append_child(&element, &child_node);
            i += 1;
        }

        element.unchecked_into::<web_sys::Node>()
    }
}

/// Text node for MathML content
#[derive(Debug, Clone)]
pub struct TextNode {
    /// The text content of the node
    pub text: String,
}

impl TextNode {
    fn to_text(&self) -> String {
        self.text.clone()
    }
}

impl VirtualNode for TextNode {
    fn write_markup(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), ParseError> {
        map_fmt(escape_into(fmt, &self.text))?;
        Ok(())
    }

    #[cfg(feature = "wasm")]
    fn to_node(&self, ctx: &WebContext) -> web_sys::Node {
        ctx.document
            .create_text_node(&self.text)
            .unchecked_into::<web_sys::Node>()
    }
}

/// Space node for MathML, may render as `<mspace>` or as text
#[derive(Debug, Clone)]
pub struct SpaceNode {
    /// The width of the space in em units
    pub width: f64,
    /// Optional character to represent the space, defaults to a regular space
    pub character: Option<String>,
}

impl SpaceNode {
    /// Create a new SpaceNode with the given width
    #[must_use]
    pub fn new(width: f64) -> Self {
        let character = get_space_character(width);
        Self { width, character }
    }

    fn to_text(&self) -> String {
        self.character.clone().unwrap_or_else(|| " ".to_owned())
    }
}

impl VirtualNode for SpaceNode {
    fn write_markup(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), ParseError> {
        if let Some(character) = &self.character {
            map_fmt(fmt.write_str("<mtext>"))?;
            map_fmt(escape_into(fmt, character))?;
            map_fmt(fmt.write_str("</mtext>"))?;
        } else {
            let width = make_em(self.width);
            map_fmt(fmt.write_str("<mspace width=\""))?;
            map_fmt(fmt.write_str(&width))?;
            map_fmt(fmt.write_str("\"/>"))?;
        }
        Ok(())
    }

    #[cfg(feature = "wasm")]
    fn to_node(&self, ctx: &WebContext) -> web_sys::Node {
        use wasm_bindgen::JsCast as _;

        self.character.as_ref().map_or_else(
            || {
                let element = create_element(ctx, "mspace");
                set_attribute(&element, "width", &make_em(self.width));
                element.unchecked_into::<web_sys::Node>()
            },
            |character| {
                ctx.document
                    .create_text_node(character)
                    .unchecked_into::<web_sys::Node>()
            },
        )
    }
}

impl MathDomNode {
    /// Convert the MathDomNode to plain text
    pub fn to_text(&self) -> String {
        match self {
            Self::Math(node) => node.to_text(),
            Self::Text(node) => node.to_text(),
            Self::Space(node) => node.to_text(),
            Self::Fragment(fragment) => fragment.children.iter().map(Self::to_text).collect(),
        }
    }
}

impl VirtualNode for MathDomNode {
    fn write_markup(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), ParseError> {
        match self {
            Self::Math(node) => node.write_markup(fmt),
            Self::Text(node) => node.write_markup(fmt),
            Self::Space(node) => node.write_markup(fmt),
            Self::Fragment(fragment) => fragment.write_markup(fmt),
        }
    }

    #[cfg(feature = "wasm")]
    fn to_node(&self, ctx: &WebContext) -> web_sys::Node {
        match self {
            Self::Math(node) => node.to_node(ctx),
            Self::Text(node) => node.to_node(ctx),
            Self::Space(node) => node.to_node(ctx),
            Self::Fragment(fragment) => fragment.to_node(ctx),
        }
    }
}

impl MathDomNode {
    /// Type-safe access to MathNode variant
    #[must_use]
    pub const fn as_math_node(&self) -> Option<&MathNode> {
        match self {
            Self::Math(node) => Some(node),
            _ => None,
        }
    }

    /// Type-safe access to TextNode variant
    #[must_use]
    pub const fn as_text_node(&self) -> Option<&TextNode> {
        match self {
            Self::Text(node) => Some(node),
            _ => None,
        }
    }

    /// Type-safe access to SpaceNode variant
    #[must_use]
    pub const fn as_space_node(&self) -> Option<&SpaceNode> {
        match self {
            Self::Space(node) => Some(node),
            _ => None,
        }
    }

    /// Mutable type-safe access to MathNode variant
    pub const fn as_math_node_mut(&mut self) -> Option<&mut MathNode> {
        match self {
            Self::Math(node) => Some(node),
            _ => None,
        }
    }

    /// Mutable type-safe access to TextNode variant
    pub const fn as_text_node_mut(&mut self) -> Option<&mut TextNode> {
        match self {
            Self::Text(node) => Some(node),
            _ => None,
        }
    }

    /// Mutable type-safe access to SpaceNode variant
    pub const fn as_space_node_mut(&mut self) -> Option<&mut SpaceNode> {
        match self {
            Self::Space(node) => Some(node),
            _ => None,
        }
    }

    /// Type-safe access to Fragment variant
    #[must_use]
    pub const fn as_fragment(&self) -> Option<&MathDomFragment> {
        match self {
            Self::Fragment(fragment) => Some(fragment),
            _ => None,
        }
    }

    /// Mutable type-safe access to Fragment variant
    pub const fn as_fragment_mut(&mut self) -> Option<&mut MathDomFragment> {
        match self {
            Self::Fragment(fragment) => Some(fragment),
            _ => None,
        }
    }
}

// From/Into trait implementations for compatibility
impl From<MathNode> for MathDomNode {
    fn from(node: MathNode) -> Self {
        Self::Math(node)
    }
}

impl From<TextNode> for MathDomNode {
    fn from(node: TextNode) -> Self {
        Self::Text(node)
    }
}

impl From<SpaceNode> for MathDomNode {
    fn from(node: SpaceNode) -> Self {
        Self::Space(node)
    }
}

impl From<MathDomFragment> for MathDomNode {
    fn from(fragment: MathDomFragment) -> Self {
        Self::Fragment(Box::new(fragment))
    }
}