mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-21 10:01:42 +01:00
A script whose base isn't simple prints as `〖a^b〗^c` in linear text (port of
wordcraft#6084), and LaTeX export copied the brackets into the .tex, where the
exporter's preamble can't set them. They are now a TeX group, `{a^b}^c`; one
without its partner is dropped. Also pins that `〖a+b〗 ^2` keeps the delimiter.
Port of the review fix on wordcraft#6084.
82 lines
3.8 KiB
Rust
82 lines
3.8 KiB
Rust
//! A script whose base is itself a script survives linear text (#6075). `to_linear` puts a base that
|
||
//! isn't simple in invisible brackets, `〖a^b〗^c`, as Word linearizes it, and the parser treats
|
||
//! `〖…〗` as a script base as grouping only, as Word builds it: no delimiter is left.
|
||
|
||
use craft_math::math::{MNode, MRun, ScriptKind, parse_linear, to_linear};
|
||
|
||
fn a(t: &str) -> Vec<MNode> {
|
||
vec![MNode::Run(MRun::new(t))]
|
||
}
|
||
|
||
fn sup(base: Vec<MNode>, s: Vec<MNode>) -> MNode {
|
||
MNode::Script { kind: ScriptKind::Sup, base, sub: Vec::new(), sup: s }
|
||
}
|
||
|
||
#[test]
|
||
fn a_script_on_a_script_prints_back_the_same() {
|
||
// sSup(sSup(a, b), c), as Word files hold it.
|
||
let left = vec![sup(vec![sup(a("a"), a("b"))], a("c"))];
|
||
assert_eq!(to_linear(&left), "〖a^b〗^c");
|
||
assert_eq!(parse_linear(&to_linear(&left)), left);
|
||
// Subscripts and a longer base too.
|
||
let sub =
|
||
vec![MNode::Script { kind: ScriptKind::Sub, base: vec![sup(a("x"), a("2")), MNode::Run(MRun::new("+y"))], sub: a("i"), sup: Vec::new() }];
|
||
assert_eq!(parse_linear(&to_linear(&sub)), sub, "{}", to_linear(&sub));
|
||
}
|
||
|
||
#[test]
|
||
fn invisible_brackets_only_group_a_script_base() {
|
||
assert_eq!(parse_linear("〖a+b〗^2"), vec![sup(a("a+b"), a("2"))]);
|
||
// Visible brackets stay.
|
||
let [MNode::Script { base, .. }] = &parse_linear("(a+b)^2")[..] else { panic!() };
|
||
assert!(matches!(&base[..], [MNode::Delim { beg: Some('('), .. }]), "{base:?}");
|
||
}
|
||
|
||
#[test]
|
||
fn half_typed_scripts_print_back_the_same() {
|
||
for s in ["x^2^", "x_i_", "(a^b)^c", "a^b"] {
|
||
let n = parse_linear(s);
|
||
assert_eq!(parse_linear(&to_linear(&n)), n, "{s} → {}", to_linear(&n));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn an_invisible_delimiter_as_the_base_is_kept() {
|
||
// A base that is itself an invisible delimiter (LaTeX `\left.a\right.^2` makes one) is written
|
||
// `〖〖a〗〗^2`: the outer pair only groups, the inner one reads back as that delimiter.
|
||
let invisible = MNode::Delim { beg: None, end: None, sep: None, grow: true, shp_match: false, elems: vec![a("a")] };
|
||
let n = vec![sup(vec![invisible], a("2"))];
|
||
assert_eq!(parse_linear(&to_linear(&n)), n, "{}", to_linear(&n));
|
||
let latex = craft_math::math_latex::parse_latex(r"\left.a\right.^2");
|
||
assert!(matches!(&latex[..], [MNode::Script { base, .. }] if matches!(&base[..], [MNode::Delim { beg: None, end: None, .. }])), "{latex:?}");
|
||
assert_eq!(parse_linear(&to_linear(&latex)), latex, "{}", to_linear(&latex));
|
||
}
|
||
|
||
#[test]
|
||
fn an_operator_as_the_base_prints_back_the_same() {
|
||
// A script after an operator reads as a pre-script, so an operator base is grouped: `〖⊗〗_i`.
|
||
for op in ["⊗", "×", "∗", "*", "<", "&", "#"] {
|
||
let n = vec![MNode::Script { kind: ScriptKind::Sub, base: a(op), sub: a("i"), sup: Vec::new() }];
|
||
assert_eq!(parse_linear(&to_linear(&n)), n, "{op}: {}", to_linear(&n));
|
||
let n = vec![sup(a(op), a("2"))];
|
||
assert_eq!(parse_linear(&to_linear(&n)), n, "{op}: {}", to_linear(&n));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn an_explicit_delimiter_as_the_base_is_kept() {
|
||
// `├a┤` is an explicit (invisible) delimiter, not a grouping `〖a〗`: it stays the base.
|
||
let [MNode::Script { base, sup: two, .. }] = &parse_linear("├a┤^2")[..] else { panic!() };
|
||
assert_eq!(two, &a("2"));
|
||
assert!(matches!(&base[..], [MNode::Delim { beg: None, end: None, elems, .. }] if elems == &vec![a("a")]), "{base:?}");
|
||
// Grouping still leaves none.
|
||
assert_eq!(parse_linear("〖a〗^2"), vec![sup(a("a"), a("2"))]);
|
||
}
|
||
|
||
#[test]
|
||
fn invisible_brackets_before_a_space_stay_a_delimiter() {
|
||
// Only `〗` right before `^`/`_` makes them a script base; after a space they are kept as typed.
|
||
let n = parse_linear("〖a+b〗 ^2");
|
||
assert!(matches!(&n[..], [MNode::Delim { beg: None, end: None, .. }, ..]), "{n:?}");
|
||
}
|