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
// Copyright 2017-2022 The MathJax Consortium
// SPDX-License-Identifier: Apache-2.0
// Pinned mathjax-go tex/commands.go: normalizeStackBase, overSet,
// underSet, and overUnderSet. These commands construct authored stacks;
// unlike stackrel/stackbin, they do not expand through a mathop macro.
use super::*;
// parseArgument returns its single child directly; a multi-child argument
// retains the inferred row. The Rust child parser initially returns Sequence.
fn argument_node(atom: Atom) -> Atom {
match atom {
Atom::Sequence(mut children) if children.len() == 1 => children.pop().unwrap(),
atom => atom,
}
}
impl Parser {
pub(super) fn stack_annotation(&mut self, name: &str) -> Option<Atom> {
let first = argument_node(self.padded_argument(name)?);
if cd::first_error(&first).is_some() {
return Some(first);
}
let second = if name == "overunderset" {
let child = argument_node(self.padded_argument(name)?);
if cd::first_error(&child).is_some() {
return Some(child);
}
Some(Box::new(child))
} else {
None
};
let mut base = argument_node(self.padded_argument(name)?);
if cd::first_error(&base).is_some() {
return Some(base);
}
check_movable_limits(&mut base);
// Rust retains the primary under/over family for movable scripts,
// so normalizeStackBase needs no side-to-stack representation change.
// UnderSet/OverUnderSet explicitly disable stack accents. OverSet
// leaves the stack unset but disables a direct mo annotation's accent;
// an embellished wrapper still inherits its deeper operator's flag.
let direct_mo = match &first {
Atom::RawOperator { .. } | Atom::OperatorToken(_) | Atom::HorizontalMark { .. } => true,
Atom::Glyph(code) | Atom::FontGlyph { original: code, .. } => {
mathml_node_kind(*code) == "mo"
}
_ => false,
};
let accent = (name != "overset" || direct_mo).then_some(accents::Spec {
under: false,
enabled: false,
mathsize: 1.,
});
let (sub, sup) = if name == "underset" {
(Some(Box::new(first)), None)
} else {
(second, Some(Box::new(first)))
};
Some(Atom::Scripts {
base: Box::new(base),
sub,
sup,
limits: true,
accent,
eager: false,
leading_space: 0.,
})
}
}
#[cfg(test)]
mod contracts {
use super::*;
#[test]
fn annotations_keep_authored_slots_and_follow_nested_movable_cores() {
// commands.go + script_attachment.go: an authored annotation is the
// base of a new side script, while occupied lower slots still fault.
for (source, annotation) in [
(r"\overset{a}{x}^b", "mover"),
(r"\underset{a}{x}^b", "munder"),
] {
let svg = render(source).unwrap();
assert!(svg.contains("data-mml-node=\"msup\""), "{source}");
assert!(
svg.contains(&format!("data-mml-node=\"{annotation}\"")),
"{source}"
);
}
for source in [r"\underset{a}{x}_b", r"\overunderset{a}{c}{x}_b"] {
assert_eq!(
render(source).unwrap(),
cd::render_error("Double subscripts: use braces to clarify")
);
}
{
let source = r"\overunderset{a}{c}{x}^b";
assert_eq!(
render(source).unwrap(),
cd::render_error("Double exponent: use braces to clarify")
);
}
// svg/munderover.go movableLimitCore follows the nested script's
// operator attribute. The outer authored mover retains its kind but
// places its annotation to the right in inline style.
let svg = render(r"\textstyle \overset{a}{\sum_i^n}").unwrap();
assert!(svg.contains("viewBox=\"0 -828.5 2020.3 1121.6\""));
assert!(svg.contains("data-mml-node=\"mover\""));
assert!(svg.contains("data-mml-node=\"munderover\""));
}
}