Commit Graph
2 Commits
Author SHA1 Message Date
kiwi4boy 28bd7be180 craft-math, craft-docx: keep an equation's kept OMML only when it is one well-formed m:oMath (port of wordcraft#6081)
write_equation writes Math::omml verbatim into the host document once a check passes, and documents
saved as JSON carry that field, so a JSON → .docx conversion could write a document.xml that isn't
well-formed, or any WordprocessingML verbatim. craft-math's check was a strict craft-xml read of
text starting with <m:oMath, which already refuses unclosed and mismatched tags, unknown entities
and trailing markup, but let through: an m:oMathPara (nested in a second one), an m: bound to
another namespace, names that aren't XML names, undeclared prefixes, two attributes with one
expanded name, a < in an attribute value, ]]> in text, and characters XML 1.0 doesn't allow.

The kept XML is now written only when it starts with <m:oMath> (not m:oMathPara), passes the strict
read (which also bounds the nesting), and then parses with roxmltree, a conforming parser, as
exactly one element that is m:oMath by namespace. The canonical prefixes of kept OMML (m:, w:, mc:)
are declared for that parse, as craft-math already documents them and the .docx root declares
them; any other prefix is undeclared there. Otherwise the equation is written from its nodes.

In craft-docx, Flat OPC's one_element (the gate for inlining kept bytes as pkg:xmlData) now also
requires XML names and attribute values that unescape, as in the WordCraft PR.

Ported from storytold/wordcraft#6081 (issue wordcraft#6072). The check differs from the PR's in
where it lives: the PR combined flat::one_element, parse_strict and roxmltree in the DOCX writer,
over the OMML wrapped in every namespace document.xml declares. Here the writer is in craft-math,
which doesn't know the host's namespaces, so it declares the three canonical ones. Its tests go
into craft-docx's integration tests (tests/it/kept_omml_check.rs), with only the crate names
changed; all 22 malformed cases fail without this commit except the 11 craft-math already refused.
roxmltree 0.21 is already in the lock as a dependency of other crates.

Checked on the 72 .docx files with equations from earlier Word probes (488 equations with kept
OMML, 35 files saved by Word): the same equations are written back verbatim as before, except one
empty <m:oMath/> in a hand-made repro file, which is now written from its (empty) nodes.
2026-11-12 22:09:54 -05:00
Dylan M. Taylorandshuff57 85b4dabf0f Add craft-math: equations as one tree with OMML, MathML, LaTeX and UnicodeMath (#1013)
* Add craft-math: the equation tree with OMML, MathML, LaTeX and UnicodeMath

The math tree, LaTeX, linear-format, symbol and tree-walk modules come
from craft-wordmodel (storytold/craft-libs#963, WordCraft's crates/doc at
07f97737) unchanged, with the run colour (Rgb) and the symbol-font tables
they use. MathML is WordCraft's crates/formats/src/mathml.rs and OMML is
WordCraft's crates/docx/src/read/math.rs and write/math.rs (06c2ae6f /
07f97737), moved onto craft-xml. The HTML-specific MathML tests stay in
WordCraft; the rest moved with the code.

mathml::parse and omml::parse/read_str/write_str are new: string entry
points for callers that have no tree of their own.

* craft-math: read MathLive's and other spellings of LaTeX math commands

\abs{...} (an absolute value), \lvert \rvert \lVert \rVert, \nparallel,
\backprime, \infinity, \cross, \differentialD, \exponentialE and
\imaginaryI (MathLive's d, upright e and i), \emph, \textsf, \hbox,
\mathnormal and \underleftarrow / \underrightarrow /
\underleftrightarrow were unknown and read as literal text. They are read
as equaSHion's typed-input parser reads them (storytold/craft-libs#29).

* craft-math: equaSHion's equation corpus as test data

corpus/equations.tsv, corpus/latex.tsv and the OMML samples of
storytold/craft-libs#29 (7a6ef6b9), unmodified.

* craft-math: corpus round trips, GridCraft's OMML cases, entry points and limits tested; README

The corpus equations go through OMML, MathML and LaTeX and back; the OMML
samples are written back as kept and from the tree. GridCraft's OMML to
LaTeX test cases (crates/xlsx/src/omml.rs) give LaTeX that reads back the
same through the tree. omml::parse and read_str are tested against other
prefixes, the strict namespace, kept OMML, junk, the element cap and deep
nesting.

---------

Co-authored-by: shuff57 <62350898+shuff57@users.noreply.github.com>
2026-11-11 20:31:02 -05:00