mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
From the review of this series: - A base that is one number by the parser's rule (1,000 as well as 2.5 and .5) is written bare, 1,000^2 rather than 〖1,000〗^2. lin_base tested numbers with digits and points only, so a comma made it a multi-letter base. The rule is now is_number, the parser's number_len over the whole text; simple_operand, which fractions share, is unchanged. - A bare script followed by a comma and a digit is ended with a space: x^2 then ,3 was written x^2,3, and the number script read the ,3 back into the exponent. Only a comma before a digit gets one; x^2, y is written as before. - Kept OMML whose element name is followed by a tab, carriage return or line feed (all XML whitespace) is written back like one followed by a space, instead of from the nodes. Each new test fails without its fix.
110 lines
5.2 KiB
Rust
110 lines
5.2 KiB
Rust
//! An equation's kept source OMML is written back verbatim only when it is exactly one well-formed
|
|
//! `m:oMath` (#6072). `Math::omml` also comes from `.json` documents, so anything else must not
|
|
//! reach `document.xml`: it is written from the equation's nodes instead.
|
|
|
|
use craft_math::math::{MNode, MRun, Math};
|
|
use craft_wordmodel::para::{InlineObject, OBJ};
|
|
use craft_wordmodel::props::CharProps;
|
|
use craft_wordmodel::{Document, Paragraph, para_block};
|
|
|
|
/// Save a one-equation document whose equation keeps `omml` and has the nodes `NODES`.
|
|
fn save(omml: &str, display: bool) -> (String, Document) {
|
|
let math = Math { nodes: vec![MNode::Run(MRun::new("NODES"))], omml: omml.into(), ..Default::default() };
|
|
let mut p = Paragraph::new();
|
|
p.insert_object(0, InlineObject::Equation { linear: "NODES".into(), display, math }, &CharProps::default()).unwrap();
|
|
let mut doc = Document::new();
|
|
doc.body = vec![para_block(p)];
|
|
let bytes = craft_docx::write(&doc).unwrap();
|
|
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap();
|
|
let mut xml = String::new();
|
|
std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap();
|
|
// The saved file must open again (the main part is read strictly).
|
|
let back = craft_docx::read(&bytes).unwrap_or_else(|e| panic!("{omml:?} doesn't reopen: {e}"));
|
|
(xml, back)
|
|
}
|
|
|
|
/// The text of the reopened document's equations and paragraphs.
|
|
fn texts(doc: &Document) -> Vec<String> {
|
|
let mut out = Vec::new();
|
|
for b in &doc.body {
|
|
let Some(p) = b.as_para() else { continue };
|
|
for o in &p.objects {
|
|
if let InlineObject::Equation { math, .. } = o {
|
|
out.push(craft_math::math::to_linear(&math.nodes));
|
|
}
|
|
}
|
|
let text: String = p.text.chars().filter(|c| *c != OBJ).collect();
|
|
if !text.trim().is_empty() {
|
|
out.push(text);
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn well_formed_kept_omml_is_written_back_unchanged() {
|
|
let kept = "<m:oMath><m:r><m:t>KEPT</m:t></m:r></m:oMath>";
|
|
let (xml, back) = save(kept, false);
|
|
assert!(xml.contains(kept), "{xml}");
|
|
assert_eq!(texts(&back), vec!["KEPT".to_string()]);
|
|
}
|
|
|
|
#[test]
|
|
fn xml_whitespace_after_the_element_name_is_kept() {
|
|
// XML allows any of space, tab, carriage return and line feed between a name and `>`.
|
|
for ws in [" ", "\t", "\r", "\n", "\r\n "] {
|
|
let kept = format!("<m:oMath{ws}><m:r><m:t>KEPT</m:t></m:r></m:oMath>");
|
|
let (xml, back) = save(&kept, false);
|
|
assert!(xml.contains(&kept), "{ws:?}: {xml}");
|
|
assert_eq!(texts(&back), vec!["KEPT".to_string()], "{ws:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_kept_omml_is_written_from_the_nodes() {
|
|
for kept in [
|
|
// Unclosed, mismatched, an undefined entity.
|
|
"<m:oMath><m:r>",
|
|
"<m:oMath></m:r>",
|
|
"<m:oMath><m:r><m:t>&bad;</m:t></m:r></m:oMath>",
|
|
// Markup after the element, text after it, a second element.
|
|
"<m:oMath></m:oMath></w:p><w:p><w:r><w:t>INJECTED</w:t></w:r></w:p><w:p>",
|
|
"<m:oMath></m:oMath>INJECTED",
|
|
"<m:oMath></m:oMath><w:r><w:t>INJECTED</w:t></w:r>",
|
|
// Not an m:oMath.
|
|
"<m:r><m:t>INJECTED</m:t></m:r>",
|
|
// An m:oMath in another namespace, and an unknown entity in an attribute.
|
|
"<m:oMath xmlns:m=\"urn:other\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r bad=\"&missing;\"><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
// Names that aren't XML names, and prefixes that aren't declared.
|
|
"<m:oMath bad?=\"1\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r><m:t a:b:c=\"1\">INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r><m:t>INJECTED</m:t></m:r><m:r?/></m:oMath>",
|
|
"<m:oMath missing:x=\"1\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><missing:r><m:t>INJECTED</m:t></missing:r></m:oMath>",
|
|
// Two attributes with one expanded name (Namespaces in XML §6.3).
|
|
"<m:oMath xmlns:p=\"urn:x\" xmlns:q=\"urn:x\" p:a=\"1\" q:a=\"2\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
// A `<` in an attribute value, `]]>` in text.
|
|
"<m:oMath bad=\"<\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r><m:t>INJECTED]]></m:t></m:r></m:oMath>",
|
|
// Characters XML 1.0 doesn't allow, as references or as they are.
|
|
"<m:oMath bad=\"\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath bad=\"\u{1}\"><m:r><m:t>INJECTED</m:t></m:r></m:oMath>",
|
|
"<m:oMath><m:r><m:t>INJECTED\u{1}</m:t></m:r></m:oMath>",
|
|
] {
|
|
let (xml, back) = save(kept, false);
|
|
assert!(!xml.contains("INJECTED") && !xml.contains("&bad;"), "{kept:?} was written: {xml}");
|
|
assert_eq!(texts(&back), vec!["NODES".to_string()], "{kept:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_kept_omath_para_is_not_nested_in_another() {
|
|
let kept = "<m:oMathPara><m:oMath><m:r><m:t>P</m:t></m:r></m:oMath></m:oMathPara>";
|
|
let (xml, back) = save(kept, true);
|
|
assert!(!xml.contains("<m:oMathPara><m:oMathPara>"), "{xml}");
|
|
assert_eq!(texts(&back), vec!["NODES".to_string()]);
|
|
}
|