mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
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.
23 lines
706 B
TOML
23 lines
706 B
TOML
[package]
|
|
name = "craft-math"
|
|
description = "Equations as one tree (the Office Math object model) with its syntaxes: OMML, MathML, LaTeX math and the UnicodeMath linear format, read and written."
|
|
version.workspace = true
|
|
edition.workspace = true
|
|
license.workspace = true
|
|
rust-version.workspace = true
|
|
repository.workspace = true
|
|
|
|
[lints]
|
|
workspace = true
|
|
|
|
[dependencies]
|
|
craft-xml = { workspace = true }
|
|
# A conforming XML parser (names, namespaces, characters), for checking kept OMML before it is
|
|
# written verbatim; already in the lock as a dependency of other crates.
|
|
roxmltree = "0.21"
|
|
serde = { workspace = true }
|
|
|
|
[dev-dependencies]
|
|
serde_json = { workspace = true }
|
|
proptest = { workspace = true }
|