diff --git a/README.md b/README.md index 252d3f986..e427a7830 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ rows are the enforced release-mode bounds plus one dated observation. | Measurement | Value | Version | Platform | Build mode | Input | Command | Statistic | Measured on | |---|---|---|---|---|---|---|---|---| -| Crates.io archive: rdocx | 4,105,912 compressed bytes, 54,118,469 member bytes, 37 members | 0.16.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rdocx` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | +| Crates.io archive: rdocx | 4,113,924 compressed bytes, 54,154,979 member bytes, 37 members | 0.16.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rdocx` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | | Large-document layout throughput | minimum 250 pages/s, observed 31,019.1 pages/s | rdocx 0.14.0 | macOS 26.6.2, Apple M5 Max, arm64 | release, one test thread | 1,000 one-page paragraphs with deterministic fonts | `cargo test -p rdocx --test regression_test --release a_thousand_page_document_paginates_and_renders_within_the_declared_limits -- --ignored --exact --nocapture --test-threads=1` | pages per wall-clock second | 2026-09-19 | | Large-document layout peak allocation | maximum 64 MiB, observed 29.03 MiB | rdocx 0.14.0 | macOS 26.6.2, Apple M5 Max, arm64 | release, one test thread | 1,000 one-page paragraphs with deterministic fonts | `cargo test -p rdocx --test regression_test --release a_thousand_page_document_paginates_and_renders_within_the_declared_limits -- --ignored --exact --nocapture --test-threads=1` | peak live allocation | 2026-09-19 | | Large-document PDF throughput | minimum 1,000 pages/s, observed 60,058.0 pages/s | rdocx 0.14.0 | macOS 26.6.2, Apple M5 Max, arm64 | release, one test thread | 1,000 deterministic layout pages | `cargo test -p rdocx --test regression_test --release a_thousand_page_document_paginates_and_renders_within_the_declared_limits -- --ignored --exact --nocapture --test-threads=1` | pages per wall-clock second | 2026-09-19 | diff --git a/crates/oxml-opc/README.md b/crates/oxml-opc/README.md index e9ef184b6..55ddb8ea2 100644 --- a/crates/oxml-opc/README.md +++ b/crates/oxml-opc/README.md @@ -18,7 +18,7 @@ The archive row is regenerated from the package that carries this README. | Measurement | Value | Version | Platform | Build mode | Input | Command | Statistic | Measured on | |---|---|---|---|---|---|---|---|---| -| Crates.io archive: oxml-opc | 117,614 compressed bytes, 477,731 member bytes, 12 members | 0.14.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `oxml-opc` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | +| Crates.io archive: oxml-opc | 118,426 compressed bytes, 480,988 member bytes, 12 members | 0.14.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `oxml-opc` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | ## Use it when diff --git a/crates/oxml-opc/src/package.rs b/crates/oxml-opc/src/package.rs index 4e51df36a..99b43d55a 100644 --- a/crates/oxml-opc/src/package.rs +++ b/crates/oxml-opc/src/package.rs @@ -122,17 +122,32 @@ impl OpcPackage { mut reader: R, limits: PackageReadLimits, ) -> Result { - if limits.max_entries != usize::MAX { - let entry_count = pre_index_entry_count(&mut reader)?; - if entry_count > limits.max_entries as u64 { - return Err(OpcError::PackageLimitExceeded { - kind: "entry count", - limit: limits.max_entries as u64, - }); - } + // Without an entry limit, an end record this scan cannot read is left + // to the ZIP reader, as before, and the repeated-name check is skipped. + let entry_count = match pre_index_entry_count(&mut reader) { + Ok(count) => Some(count), + Err(error) if limits.max_entries != usize::MAX => return Err(error), + Err(_) => None, + }; + if let Some(count) = entry_count + && count > limits.max_entries as u64 + { + return Err(OpcError::PackageLimitExceeded { + kind: "entry count", + limit: limits.max_entries as u64, + }); } let mut archive = ZipArchive::new(reader)?; + // The ZIP reader keeps the last of two entries that share a name, and + // other readers keep the first, so a package that holds a name twice + // is refused rather than read as one of its copies. + if let Some(count) = entry_count.filter(|count| *count > archive.len() as u64) { + let directory_start = archive.central_directory_start(); + let name = repeated_entry_name(&mut archive.into_inner(), directory_start, count) + .unwrap_or_else(|| "an entry stored twice".to_owned()); + return Err(OpcError::DuplicatePartName(name)); + } if archive.len() > limits.max_entries { return Err(OpcError::PackageLimitExceeded { kind: "entry count", @@ -987,6 +1002,31 @@ fn validate_zip_entry_identities( Ok(()) } +/// The first entry name the central directory at `start` holds twice, read +/// from its `count` file headers, compared as part names are. +fn repeated_entry_name(reader: &mut R, start: u64, count: u64) -> Option { + const CENTRAL_HEADER_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x01, 0x02]; + reader.seek(SeekFrom::Start(start)).ok()?; + let mut seen = HashSet::new(); + for _ in 0..count { + let mut header = [0; 46]; + reader.read_exact(&mut header).ok()?; + if header[..4] != CENTRAL_HEADER_SIGNATURE { + return None; + } + let mut name = vec![0; usize::from(read_u16(&header, 28)?)]; + reader.read_exact(&mut name).ok()?; + let skipped = i64::from(read_u16(&header, 30)?) + i64::from(read_u16(&header, 32)?); + reader.seek(SeekFrom::Current(skipped)).ok()?; + let name = normalize_part_name(&String::from_utf8_lossy(&name)); + let name = name.strip_prefix('/').unwrap_or(&name).to_owned(); + if !seen.insert(name.to_ascii_lowercase()) { + return Some(name); + } + } + None +} + fn pre_index_entry_count(reader: &mut R) -> Result { let file_len = reader.seek(SeekFrom::End(0))?; if file_len < EOCD_LEN { @@ -2745,6 +2785,41 @@ mod tests { assert!(package.parts.keys().all(|name| !name.contains(".."))); } + #[test] + fn issue435_an_entry_name_stored_twice_is_refused_naming_the_part() { + // The ZIP writer refuses a repeated name, so the second entry is + // written under another name of the same length and renamed. + let mut archive = package_zip(&[ + ("[Content_Types].xml", MINIMAL_CONTENT_TYPES), + ("word/document.xml", b""), + ("word/documenX.xml", b""), + ]) + .into_inner(); + replace_zip_name( + &mut archive, + b"word/documenX.xml", + b"word/document.xml", + true, + ); + for limits in [ + PackageReadLimits::UNBOUNDED, + PackageReadLimits { + max_entries: 10, + ..PackageReadLimits::UNBOUNDED + }, + ] { + let result = OpcPackage::from_reader_with_limits( + std::io::Cursor::new(archive.as_slice()), + limits, + ); + assert!( + matches!(result, Err(OpcError::DuplicatePartName(ref name)) if name == "word/document.xml"), + "{:?}", + result.map(|package| package.get_part("/word/document.xml").map(<[u8]>::to_vec)) + ); + } + } + #[test] fn duplicate_normalized_part_names_are_rejected() { for alias in [ diff --git a/crates/rdocx-cli/README.md b/crates/rdocx-cli/README.md index 422cdc0b7..26458d152 100644 --- a/crates/rdocx-cli/README.md +++ b/crates/rdocx-cli/README.md @@ -46,7 +46,7 @@ Signing publishes before reporting, so this error can leave a valid new output. | Measurement | Value | Version | Platform | Build mode | Input | Command | Statistic | Measured on | |---|---|---|---|---|---|---|---|---| -| Crates.io archive: rdocx-cli | 111,071 compressed bytes, 516,134 member bytes, 8 members | 0.16.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rdocx-cli` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | +| Crates.io archive: rdocx-cli | 111,494 compressed bytes, 517,637 member bytes, 8 members | 0.16.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rdocx-cli` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | ## Use it when @@ -154,6 +154,17 @@ stderr that names the part when it can, list no other story, and exit successfully. `text --json` then states `main` scope. `validate` reports that part as an error. +`validate` exits 1 on an error and names the part. It checks the package +(relationship targets, content types, a part stored twice), that every XML +part is well-formed XML 1.0, the style ids in use, and a subset of the +WordprocessingML schema in the body, header, footer, note and comment parts: +the child order and the allowed children of `w:body`, `w:p` and `w:r`, text +where only elements may stand, the `w:jc` and `w:color` values, the `w:id` and +`w:author` of a tracked change, extents, the required children of `m:rad`, +relationship ids that name no relationship, note references to no note, and +deleted text in `w:t`. It is not a full schema validator. Two bookmarks that +share an id are a warning. + `text --json` reports accepted-view paragraphs in source order. Each paragraph has a zero-based `body_index`, a typed zero-based path within that body item, its direct style and numbering, and accepted-view runs. Its `text` also holds diff --git a/crates/rdocx-cli/src/commands.rs b/crates/rdocx-cli/src/commands.rs index caac89444..5dd8221d3 100644 --- a/crates/rdocx-cli/src/commands.rs +++ b/crates/rdocx-cli/src/commands.rs @@ -2386,8 +2386,10 @@ fn validate_dpi(dpi: f64) -> Result<()> { /// /// Returns `Ok(false)` when a structural error was found, so the caller can /// exit non-zero — a validator that always succeeds cannot gate anything in CI. -/// A related part that is not well-formed XML and a paragraph, character, or -/// table style id that no style defines are structural errors too. +/// An XML part that is not well-formed XML 1.0, a paragraph, character, or +/// table style id that no style defines, a dangling relationship id or note +/// reference, and a fault of the WordprocessingML schema subset that +/// [`rdocx::validate_document_file`] checks are structural errors too. /// Advisory findings (empty paragraphs, missing metadata) are reported but do /// not affect the exit status. pub fn validate(file: &Path) -> Result { diff --git a/crates/rdocx-cli/tests/integration.rs b/crates/rdocx-cli/tests/integration.rs index 8cd57daff..66ef2b4b2 100644 --- a/crates/rdocx-cli/tests/integration.rs +++ b/crates/rdocx-cli/tests/integration.rs @@ -2038,12 +2038,12 @@ fn visible_nontext_content_and_empty_controls_keep_distinct_validation_warnings( ), ( "inserted-drawing", - "", + "", false, ), ( "moved-away-drawing", - "", + "", true, ), ( @@ -2066,7 +2066,7 @@ fn visible_nontext_content_and_empty_controls_keep_distinct_validation_warnings( ), ( "deleted", - "deleted", + "deleted", true, ), ( @@ -2092,8 +2092,17 @@ fn visible_nontext_content_and_empty_controls_keep_distinct_validation_warnings( rewrite_run(&input, "CONTENT", |_| fragment.to_owned()); let before = fs::read(&input).unwrap(); let output = cli(&["validate", path_text(&input)]); - assert_success(&output, label); - let report = String::from_utf8(output.stdout).unwrap(); + let report = String::from_utf8(output.stdout.clone()).unwrap(); + if matches!(label, "footnote" | "endnote") { + // The reference names a note the package does not hold. + assert_eq!(output.status.code(), Some(1), "{label}: {report}"); + assert!( + report.contains(&format!("names {label} 1, which no w:{label} defines")), + "{label}: {report}" + ); + } else { + assert_success(&output, label); + } assert_eq!( report.contains("1 empty paragraph(s) found"), empty, @@ -2210,17 +2219,17 @@ fn validation_qualified_display_content_and_field_controls_preserve_source() { ), ( "deleted-drawing", - "", + "", false, ), ( "moved-away", - "", + "", false, ), ( "inserted-drawing", - "", + "", true, ), ( @@ -2341,12 +2350,12 @@ fn validation_qualified_display_content_and_field_controls_preserve_source() { ), ( "inserted-loose-marker", - r#""#, + r#""#, false, ), ( "deleted-loose-marker", - r#""#, + r#""#, false, ), ( @@ -2456,7 +2465,7 @@ fn validation_preserves_recursive_block_control_and_table_paragraph_scope() { ); package.set_part( &part, - xml.replace("", &format!("{controls}")) + xml.replacen("", &format!("{control}")) + xml.replacen("{text}'.encode() + if item.filename == part: + assert run in data + data = data.replace( + run, + f''.encode() + run + + b'', + 1, + ) + zout.writestr(item, data) + document = rdocx.Document.from_bytes(output.getvalue()) + assert [paragraph.text for paragraph in document.paragraphs] == ['First heading', 'Some text'] + document.add_paragraph('New') + assert rdocx.Document.from_bytes(document.to_bytes()).paragraphs[-1].text == 'New' diff --git a/crates/rdocx/src/document.rs b/crates/rdocx/src/document.rs index 5065dcd8b..12b4125d7 100644 --- a/crates/rdocx/src/document.rs +++ b/crates/rdocx/src/document.rs @@ -5020,6 +5020,11 @@ pub(crate) struct DocumentIdentifiers { /// or ignore them, so a scan accepts them. A staged edit keeps the counts /// of the opened document and may not exceed them. repeated_drawing_ids: HashMap>, + /// The `w:bookmarkStart/@w:id` values the opened XML repeats across the + /// document, with their occurrence count. The id only pairs a start with + /// its end, and Word has saved files where two bookmarks share one, so a + /// scan accepts them. A staged edit may not exceed the opened counts. + repeated_bookmark_ids: HashMap, abstract_numbering_ids: HashSet, numbering_instance_ids: HashSet, part_names: HashSet, @@ -5086,6 +5091,7 @@ impl DocumentIdentifiers { comment_ids: HashSet::new(), drawing_ids: HashSet::new(), repeated_drawing_ids: HashMap::new(), + repeated_bookmark_ids: HashMap::new(), abstract_numbering_ids: HashSet::new(), numbering_instance_ids: HashSet::new(), part_names: package @@ -5313,7 +5319,9 @@ impl DocumentIdentifiers { *part_drawing_ids.entry(value).or_default() += 1; } IdentifierDefinition::Bookmark(value) => { - Self::insert_unique(&mut self.bookmark_ids, value, "bookmark")?; + if !self.bookmark_ids.insert(value) { + *self.repeated_bookmark_ids.entry(value).or_insert(1) += 1; + } } IdentifierDefinition::Comment(value) => { Self::insert_unique(&mut self.comment_ids, value, "comment")?; @@ -5482,6 +5490,26 @@ impl DocumentIdentifiers { } } + /// Refuse a `w:bookmarkStart/@w:id` occurrence that a staged edit added + /// to an id the document already uses. Repetitions the source had are kept. + fn reject_new_repeated_bookmark_ids(&self, source: &Self) -> Result<()> { + let mut added = self + .repeated_bookmark_ids + .iter() + .filter(|(id, count)| { + **count > source.repeated_bookmark_ids.get(id).copied().unwrap_or(1) + }) + .map(|(id, _)| *id) + .collect::>(); + added.sort_unstable(); + match added.first() { + Some(id) => Err(Error::Other(format!( + "duplicate bookmark id {id} in authored XML" + ))), + None => Ok(()), + } + } + fn insert_unique( occupied: &mut HashSet, value: T, @@ -5937,6 +5965,8 @@ impl DocumentIdentifiers { fn reconcile_provenance(&mut self, source: &Self) { self.repeated_drawing_ids .clone_from(&source.repeated_drawing_ids); + self.repeated_bookmark_ids + .clone_from(&source.repeated_bookmark_ids); self.preserved_relationship_ids = intersect_relationship_registry( &source.preserved_relationship_ids, &self.relationship_ids, @@ -25149,6 +25179,9 @@ impl Document { reopened .identifiers .reject_new_repeated_drawing_ids(&provenance)?; + reopened + .identifiers + .reject_new_repeated_bookmark_ids(&provenance)?; reopened.identifiers.reconcile_provenance(&provenance); reopened.custom_properties_owned = custom_properties_owned; reopened.settings_owned = settings_owned; @@ -75177,7 +75210,7 @@ mod tests { } duplicated.prepare_staged_package().unwrap(); let header = - format!(r#""#); + format!(r#""#); duplicated .package .set_part("/word/header1.xml", header.into_bytes()); @@ -75200,13 +75233,10 @@ mod tests { Ok(_) => panic!("duplicate bookmark reopened on the old path"), }; assert_eq!(fast_error.to_string(), slow_error.to_string()); - assert!( - fast_error.to_string().contains("duplicate bookmark id 1"), - "{fast_error}" - ); - assert!( - !fast_error.to_string().contains("invalid bookmark id"), - "{fast_error}" + assert_eq!( + fast_error.to_string(), + "duplicate bookmark id 1 in authored XML", + "an edit may not add a bookmark id the document already uses" ); } @@ -75268,11 +75298,12 @@ mod tests { r#""#; let invalid_then_duplicate = r#""#; - let duplicate_message = "cannot scan identifiers in XML part /word/header1.xml: duplicate bookmark id 1 in imported or preserved XML"; + // A repeated bookmark id is accepted, so the invalid one is reported + // wherever it stands. let invalid_message = "cannot scan identifiers in XML part /word/header1.xml: invalid bookmark id x"; for (header, message) in [ - (duplicate_then_invalid, duplicate_message), + (duplicate_then_invalid, invalid_message), (invalid_then_duplicate, invalid_message), ] { let mut edited = package.clone(); @@ -75297,11 +75328,8 @@ mod tests { let mut second = package.clone(); second.set_part("/word/header2.xml", story(r#""#)); - let error = DocumentIdentifiers::scan(&second).unwrap_err(); - assert!( - matches!(error, Error::Other(ref text) if text == "cannot scan identifiers in XML part /word/header2.xml: duplicate bookmark id 2 in imported or preserved XML"), - "{error}" - ); + let scanned = DocumentIdentifiers::scan(&second).unwrap(); + assert_eq!(scanned.repeated_bookmark_ids, HashMap::from([(2, 2)])); let mut retyped = package.clone(); retyped @@ -79495,12 +79523,21 @@ fn document_style_references( fn document_word_value( reader: &NsReader<&[u8]>, element: &BytesStart<'_>, +) -> std::result::Result, String> { + document_word_attribute(reader, element, b"val") +} + +/// Return one `w:` attribute of a WordprocessingML element. +fn document_word_attribute( + reader: &NsReader<&[u8]>, + element: &BytesStart<'_>, + wanted: &[u8], ) -> std::result::Result, String> { for attribute in element.attributes() { let attribute = attribute.map_err(|error| error.to_string())?; let (namespace, local_name) = reader.resolver().resolve_attribute(attribute.key); if matches!(namespace, ResolveResult::Bound(Namespace(uri)) if uri == W_NS.as_bytes()) - && local_name.as_ref() == b"val" + && local_name.as_ref() == wanted { let value = attribute .decoded_and_normalized_value(XmlVersion::Implicit1_0, reader.decoder()) @@ -79511,6 +79548,541 @@ fn document_word_value( Ok(None) } +/// The WordprocessingML children `w:body`, `w:p` and `w:r` admit, from the +/// transitional schema (`CT_Body`, `CT_P`, `CT_R`). Children in another +/// namespace are not checked here. +const BODY_CHILDREN: &[&[u8]] = &[ + b"p", + b"tbl", + b"sdt", + b"customXml", + b"altChunk", + b"sectPr", + b"proofErr", + b"permStart", + b"permEnd", + b"bookmarkStart", + b"bookmarkEnd", + b"moveFromRangeStart", + b"moveFromRangeEnd", + b"moveToRangeStart", + b"moveToRangeEnd", + b"commentRangeStart", + b"commentRangeEnd", + b"customXmlInsRangeStart", + b"customXmlInsRangeEnd", + b"customXmlDelRangeStart", + b"customXmlDelRangeEnd", + b"customXmlMoveFromRangeStart", + b"customXmlMoveFromRangeEnd", + b"customXmlMoveToRangeStart", + b"customXmlMoveToRangeEnd", + b"ins", + b"del", + b"moveFrom", + b"moveTo", +]; +const PARAGRAPH_CHILDREN: &[&[u8]] = &[ + b"pPr", + b"r", + b"customXml", + b"smartTag", + b"sdt", + b"dir", + b"bdo", + b"fldSimple", + b"hyperlink", + b"subDoc", + b"proofErr", + b"permStart", + b"permEnd", + b"bookmarkStart", + b"bookmarkEnd", + b"moveFromRangeStart", + b"moveFromRangeEnd", + b"moveToRangeStart", + b"moveToRangeEnd", + b"commentRangeStart", + b"commentRangeEnd", + b"customXmlInsRangeStart", + b"customXmlInsRangeEnd", + b"customXmlDelRangeStart", + b"customXmlDelRangeEnd", + b"customXmlMoveFromRangeStart", + b"customXmlMoveFromRangeEnd", + b"customXmlMoveToRangeStart", + b"customXmlMoveToRangeEnd", + b"ins", + b"del", + b"moveFrom", + b"moveTo", +]; +const RUN_CHILDREN: &[&[u8]] = &[ + b"rPr", + b"br", + b"t", + b"contentPart", + b"delText", + b"instrText", + b"delInstrText", + b"noBreakHyphen", + b"softHyphen", + b"dayShort", + b"monthShort", + b"yearShort", + b"dayLong", + b"monthLong", + b"yearLong", + b"annotationRef", + b"footnoteRef", + b"endnoteRef", + b"separator", + b"continuationSeparator", + b"sym", + b"pgNum", + b"cr", + b"tab", + b"object", + b"pict", + b"fldChar", + b"ruby", + b"footnoteReference", + b"endnoteReference", + b"commentReference", + b"drawing", + b"ptab", + b"lastRenderedPageBreak", +]; +/// WordprocessingML elements whose content is elements only, so that text +/// directly inside them is not part of the document. +const ELEMENT_ONLY: &[&[u8]] = &[ + b"body", + b"p", + b"r", + b"tbl", + b"tr", + b"tc", + b"pPr", + b"rPr", + b"ins", + b"del", + b"moveFrom", + b"moveTo", + b"hyperlink", + b"smartTag", + b"sdt", + b"sdtContent", + b"customXml", + b"fldSimple", +]; +/// `ST_Jc` and `ST_JcTable`, transitional values included. +const PARAGRAPH_JC: &[&str] = &[ + "start", + "center", + "end", + "both", + "mediumKashida", + "distribute", + "numTab", + "highKashida", + "lowKashida", + "thaiDistribute", + "left", + "right", +]; +const TABLE_JC: &[&str] = &["center", "end", "left", "right", "start"]; +/// The largest `ST_PositiveCoordinate`. +const MAX_POSITIVE_COORDINATE: i64 = 27_273_042_316_900; +/// At most this many schema findings are listed for one part. +const MAX_STORY_FINDINGS: usize = 10; + +/// What [`document_story_findings`] needs from outside the part. +struct StoryScanContext<'a> { + relationships: Option<&'a oxml_opc::relationship::Relationships>, + footnote_ids: &'a HashSet, + endnote_ids: &'a HashSet, +} + +#[derive(Default)] +struct StoryScanFrame { + namespace: SchemaNamespace, + local: Vec, + word_child: bool, + section_seen: bool, + math_children: Vec>, +} + +#[derive(Clone, Copy, Default, PartialEq, Eq)] +enum SchemaNamespace { + Word, + Math, + WordDrawing, + DrawingMain, + #[default] + Other, +} + +/// Check one well-formed story part against a subset of the +/// WordprocessingML schema and against the package: child order and +/// allowed children of `w:body`, `w:p` and `w:r`, text where only elements +/// may stand, the values of `w:jc` and `w:color`, the required `w:id` and +/// `w:author` of a tracked change, extents, the required children of +/// `m:rad`, relationship ids, note references, and deleted text in `w:t`. +/// Returns the findings, then the `w:bookmarkStart` ids in the part. +fn document_story_findings( + part_name: &str, + xml: &[u8], + context: &StoryScanContext<'_>, +) -> (Vec, Vec) { + let mut reader = NsReader::from_reader(xml); + let mut stack: Vec = Vec::new(); + let mut findings = Vec::new(); + let mut bookmarks = Vec::new(); + let mut buffer = Vec::new(); + let found = |findings: &mut Vec, position: u64, detail: String| { + findings.push(format!("part {part_name}: {detail} (at byte {position})")); + }; + loop { + let position = reader.buffer_position(); + let Ok((namespace, event)) = reader.read_resolved_event_into(&mut buffer) else { + break; + }; + let namespace = match namespace { + ResolveResult::Bound(Namespace(uri)) if uri == W_NS.as_bytes() => SchemaNamespace::Word, + ResolveResult::Bound(Namespace(uri)) if uri == M_NS.as_bytes() => SchemaNamespace::Math, + ResolveResult::Bound(Namespace(uri)) if uri == drawing_ns::WP.as_bytes() => { + SchemaNamespace::WordDrawing + } + ResolveResult::Bound(Namespace(uri)) if uri == drawing_ns::A.as_bytes() => { + SchemaNamespace::DrawingMain + } + _ => SchemaNamespace::Other, + }; + match event { + Event::Start(ref element) | Event::Empty(ref element) => { + let local = element.local_name().as_ref().to_vec(); + let word = namespace == SchemaNamespace::Word; + let name = String::from_utf8_lossy(&local).into_owned(); + if let Some(parent) = stack.last_mut() { + if parent.namespace == SchemaNamespace::Word && word { + let allowed = match parent.local.as_slice() { + b"body" => Some(BODY_CHILDREN), + b"p" => Some(PARAGRAPH_CHILDREN), + b"r" => Some(RUN_CHILDREN), + _ => None, + }; + if let Some(allowed) = allowed + && !allowed.contains(&local.as_slice()) + { + let parent = String::from_utf8_lossy(&parent.local); + found( + &mut findings, + position, + format!("w:{name} is not allowed in w:{parent}"), + ); + } + let first = match parent.local.as_slice() { + b"p" => Some(&b"pPr"[..]), + b"r" => Some(&b"rPr"[..]), + b"tc" => Some(&b"tcPr"[..]), + _ => None, + }; + if first == Some(local.as_slice()) && parent.word_child { + let parent = String::from_utf8_lossy(&parent.local); + found( + &mut findings, + position, + format!("w:{name} is not the first child of w:{parent}"), + ); + } + if parent.local == b"body" && parent.section_seen { + found( + &mut findings, + position, + format!("w:{name} follows w:sectPr, the last child of w:body"), + ); + } + parent.word_child = true; + parent.section_seen |= parent.local == b"body" && local == b"sectPr"; + } + if parent.namespace == SchemaNamespace::Math + && namespace == SchemaNamespace::Math + { + parent.math_children.push(local.clone()); + } + } + let parent_local = stack + .last() + .filter(|parent| parent.namespace == SchemaNamespace::Word) + .map(|parent| parent.local.as_slice()); + let attribute = |namespace: Option<&str>, wanted: &[u8]| { + element.attributes().flatten().find_map(|attribute| { + let (bound, attribute_local) = + reader.resolver().resolve_attribute(attribute.key); + let matches = match namespace { + Some(uri) => matches!( + bound, + ResolveResult::Bound(Namespace(found)) if found == uri.as_bytes() + ), + None => matches!(bound, ResolveResult::Unbound), + }; + (matches && attribute_local.as_ref() == wanted).then(|| { + attribute + .decoded_and_normalized_value( + XmlVersion::Implicit1_0, + reader.decoder(), + ) + .map(Cow::into_owned) + .unwrap_or_default() + }) + }) + }; + if word { + match local.as_slice() { + b"jc" => { + let (allowed, kind) = + if matches!(parent_local, Some(b"tblPr" | b"trPr")) { + (TABLE_JC, "ST_JcTable") + } else { + (PARAGRAPH_JC, "ST_Jc") + }; + if let Some(value) = attribute(Some(W_NS), b"val") + && !allowed.contains(&value.as_str()) + { + found( + &mut findings, + position, + format!("w:jc w:val={value:?} is not an {kind} value"), + ); + } + } + b"color" => { + if let Some(value) = attribute(Some(W_NS), b"val") + && value != "auto" + && !(value.len() == 6 + && value.bytes().all(|byte| byte.is_ascii_hexdigit())) + { + found( + &mut findings, + position, + format!( + "w:color w:val={value:?} is not an ST_HexColor (auto or six hexadecimal digits)" + ), + ); + } + } + b"ins" | b"del" | b"moveFrom" | b"moveTo" => { + for required in [&b"id"[..], b"author"] { + if attribute(Some(W_NS), required).is_none() { + let required = String::from_utf8_lossy(required); + found( + &mut findings, + position, + format!( + "w:{name} has no w:{required}, which a tracked change requires" + ), + ); + } + } + } + b"t" | b"instrText" => { + let change = stack.iter().rev().find(|frame| { + frame.namespace == SchemaNamespace::Word + && matches!( + frame.local.as_slice(), + b"ins" | b"del" | b"moveFrom" | b"moveTo" + ) + }); + if change.is_some_and(|frame| frame.local == b"del") { + let deleted = if local == b"t" { + "delText" + } else { + "delInstrText" + }; + found( + &mut findings, + position, + format!( + "w:{name} inside w:del, where deleted text is w:{deleted}" + ), + ); + } + } + b"footnoteReference" | b"endnoteReference" => { + let (ids, kind) = if local == b"footnoteReference" { + (context.footnote_ids, "footnote") + } else { + (context.endnote_ids, "endnote") + }; + if let Some(id) = attribute(Some(W_NS), b"id") + && !ids.contains(&id) + { + found( + &mut findings, + position, + format!( + "w:{name} names {kind} {id}, which no w:{kind} defines" + ), + ); + } + } + b"bookmarkStart" => bookmarks.extend(attribute(Some(W_NS), b"id")), + _ => {} + } + } + let extent = (namespace == SchemaNamespace::WordDrawing && local == b"extent") + || (namespace == SchemaNamespace::DrawingMain && local == b"ext"); + if extent { + for axis in [&b"cx"[..], b"cy"] { + if let Some(value) = attribute(None, axis) + && !value + .parse::() + .is_ok_and(|value| (0..=MAX_POSITIVE_COORDINATE).contains(&value)) + { + let prefix = if namespace == SchemaNamespace::WordDrawing { + "wp" + } else { + "a" + }; + let axis = String::from_utf8_lossy(axis); + found( + &mut findings, + position, + format!( + "{prefix}:{name} {axis}={value:?} is not an ST_PositiveCoordinate" + ), + ); + } + } + } + for attribute in element.attributes().flatten() { + let (bound, attribute_local) = + reader.resolver().resolve_attribute(attribute.key); + if !matches!(bound, ResolveResult::Bound(Namespace(uri)) if uri == drawing_ns::R.as_bytes()) + { + continue; + } + let id = attribute + .decoded_and_normalized_value(XmlVersion::Implicit1_0, reader.decoder()) + .map(Cow::into_owned) + .unwrap_or_default(); + if context + .relationships + .is_none_or(|relationships| relationships.get_by_id(&id).is_none()) + { + let attribute_local = String::from_utf8_lossy(attribute_local.as_ref()); + found( + &mut findings, + position, + format!( + "r:{attribute_local}={id:?} on {name} names no relationship of the part" + ), + ); + } + } + if matches!(event, Event::Start(_)) { + stack.push(StoryScanFrame { + namespace, + local, + ..StoryScanFrame::default() + }); + } else if namespace == SchemaNamespace::Math && local == b"rad" { + found( + &mut findings, + position, + "m:rad has no m:deg and no m:e".into(), + ); + } + } + Event::End(_) => { + if let Some(frame) = stack.pop() + && frame.namespace == SchemaNamespace::Math + && frame.local == b"rad" + { + for required in [&b"deg"[..], b"e"] { + if !frame.math_children.iter().any(|child| child == required) { + let required = String::from_utf8_lossy(required); + found( + &mut findings, + position, + format!("m:rad has no m:{required}, which the schema requires"), + ); + } + } + } + } + Event::Text(ref text) + if stack.last().is_some_and(|frame| { + frame.namespace == SchemaNamespace::Word + && ELEMENT_ONLY.contains(&frame.local.as_slice()) + }) && !text.iter().all(u8::is_ascii_whitespace) => + { + let parent = String::from_utf8_lossy(&stack.last().expect("checked").local); + found( + &mut findings, + position, + format!("text directly inside w:{parent}, which holds elements only"), + ); + } + Event::Eof => break, + _ => {} + } + buffer.clear(); + } + if findings.len() > MAX_STORY_FINDINGS { + let more = findings.len() - MAX_STORY_FINDINGS; + findings.truncate(MAX_STORY_FINDINGS); + findings.push(format!("part {part_name}: {more} more schema finding(s)")); + } + (findings, bookmarks) +} + +/// The `w:id` of every `w:footnote` or `w:endnote` in a notes part. +fn document_note_ids(xml: &[u8], note: &[u8]) -> HashSet { + let mut reader = NsReader::from_reader(xml); + let mut ids = HashSet::new(); + let mut buffer = Vec::new(); + while let Ok((namespace, event)) = reader.read_resolved_event_into(&mut buffer) { + match event { + Event::Start(ref element) | Event::Empty(ref element) + if matches!(namespace, ResolveResult::Bound(Namespace(uri)) if uri == W_NS.as_bytes()) + && element.local_name().as_ref() == note => + { + if let Ok(Some(id)) = document_word_attribute(&reader, element, b"id") { + ids.insert(id); + } + } + Event::Eof => break, + _ => {} + } + buffer.clear(); + } + ids +} + +/// A literal character XML 1.0 forbids, or another strict XML 1.0 lexical +/// fault that a namespace-aware parser accepts. A UTF-16 part is not +/// checked. +fn document_xml_lexical_fault(xml: &[u8]) -> Option { + if xml.starts_with(&[0xFF, 0xFE]) || xml.starts_with(&[0xFE, 0xFF]) { + return None; + } + let error = oxml_core::xml::validate_strict_xml_1_0(xml).err()?; + let text = String::from_utf8_lossy(xml); + if let Some((offset, character)) = text + .char_indices() + .find(|(_, character)| !oxml_core::xml::is_xml_1_0_character(*character)) + { + let line = text[..offset].matches('\n').count() + 1; + let column = offset - text[..offset].rfind('\n').map_or(0, |at| at + 1) + 1; + return Some(format!( + "it holds U+{:04X}, a character XML 1.0 forbids, at line {line}, column {column}", + u32::from(character) + )); + } + Some(format!("{error:?}")) +} + /// Validate serialized document bytes without changing a live document. /// Non-package input and unexplained document-open failures remain errors. pub fn validate_document_bytes(bytes: &[u8]) -> Result { @@ -79626,6 +80198,8 @@ fn validate_document_package(package: OpcPackage) -> Result { // Every XML part the main document relates to must be well formed. The // style ids that its story parts name are checked once the document opens. let mut style_references = Vec::new(); + let mut checked_parts = HashSet::new(); + let mut story_parts = Vec::new(); if let Some(doc_part) = package.main_document_part() { let mut parts = vec![(doc_part.clone(), true)]; for rel in package @@ -79652,20 +80226,97 @@ fn validate_document_package(package: OpcPackage) -> Result { let Some(xml) = package.get_part(&part_name).filter(|_| is_xml) else { continue; }; - match document_style_references(xml) { + checked_parts.insert(part_name.clone()); + let references = document_style_references(xml) + .and_then(|references| document_xml_lexical_fault(xml).map_or(Ok(references), Err)); + match references { Err(detail) => { errors.push(format!("part {part_name} is not well-formed XML: {detail}")); } - Ok(references) if story => style_references.extend( - references - .into_iter() - .map(|(kind, style_id)| (kind, style_id, part_name.clone())), - ), + Ok(references) if story => { + style_references.extend( + references + .into_iter() + .map(|(kind, style_id)| (kind, style_id, part_name.clone())), + ); + story_parts.push(part_name); + } Ok(_) => {} } } } + // Every other XML part must be well formed too, the document properties + // among them: a consumer may refuse the package for any one of them. + let mut other_xml_parts = package + .parts + .iter() + .filter(|(part_name, _)| { + !checked_parts.contains(*part_name) + && package + .content_types + .content_type_for(part_name) + .is_some_and(|content_type| { + content_type.ends_with("+xml") || content_type.ends_with("/xml") + }) + }) + .collect::>(); + other_xml_parts.sort(); + for (part_name, xml) in other_xml_parts { + if let Some(detail) = document_style_references(xml) + .err() + .or_else(|| document_xml_lexical_fault(xml)) + { + errors.push(format!("part {part_name} is not well-formed XML: {detail}")); + } + } + + // A subset of the schema and the references between the story parts. + let notes_part = |relationship_type: &str, note: &[u8]| { + let doc_part = package.main_document_part()?; + let relationship = package + .get_part_rels(&doc_part)? + .items + .iter() + .find(|relationship| { + relationship.rel_type == relationship_type && relationship_is_internal(relationship) + })?; + let target = oxml_opc::OpcPackage::resolve_rel_target(&doc_part, &relationship.target); + Some(document_note_ids(package.get_part(&target)?, note)) + }; + let footnote_ids = notes_part(rel_types::FOOTNOTES, b"footnote").unwrap_or_default(); + let endnote_ids = notes_part(rel_types::ENDNOTES, b"endnote").unwrap_or_default(); + let mut bookmark_parts = BTreeMap::>::new(); + for part_name in &story_parts { + let Some(xml) = package.get_part(part_name) else { + continue; + }; + let context = StoryScanContext { + relationships: package.get_part_rels(part_name), + footnote_ids: &footnote_ids, + endnote_ids: &endnote_ids, + }; + let (findings, bookmarks) = document_story_findings(part_name, xml, &context); + errors.extend(findings); + for id in bookmarks { + bookmark_parts + .entry(id) + .or_default() + .push(part_name.clone()); + } + } + // Word has saved bookmarks that share an id, which only pairs a start + // with its end, and opens such files, so a shared id is a warning. + for (id, parts) in bookmark_parts.iter().filter(|(_, parts)| parts.len() > 1) { + let mut names = parts.clone(); + names.dedup(); + warnings.push(format!( + "bookmark id {id} is used by {} bookmarks, in {}", + parts.len(), + names.join(", ") + )); + } + // A malformed part can keep the document from opening. The errors found // so far explain why, so they are reported together with the failure. let doc = match Document::from_package(package) { diff --git a/crates/rdocx/tests/regression_test.rs b/crates/rdocx/tests/regression_test.rs index d683e25d5..814617c73 100644 --- a/crates/rdocx/tests/regression_test.rs +++ b/crates/rdocx/tests/regression_test.rs @@ -39276,8 +39276,11 @@ fn imported_and_preserved_collisions_fail_before_mutation() { ); assert_ne!(xml, collided, "second bookmark id must be present"); package.set_part("/word/document.xml", collided.into_bytes()); - let error = f249_open_error("bookmark", package); - assert!(error.contains("duplicate bookmark id 0"), "{error}"); + let mut output = std::io::Cursor::new(Vec::new()); + package.write_to(&mut output).unwrap(); + // Two bookmarks may share an id, as Word has saved them. It opens, and + // `issue435_bookmarks_sharing_an_id_open_and_render` covers it. + Document::from_bytes(&output.into_inner()).unwrap(); let mut package = f249_package(&bytes); let xml = String::from_utf8(package.get_part("/word/comments.xml").unwrap().to_vec()).unwrap(); @@ -408406,3 +408409,289 @@ fn body_range_across_table_includes_cell_text() { } assert!(failures.is_empty(), "{failures:#?}"); } + +/// Rewrites one entry of a package with `edit`, with no check of the result, +/// so that a test can store a fault the package writer refuses. +fn issue435_edit(bytes: &[u8], part: &str, edit: impl Fn(&str) -> String) -> Vec { + use std::io::{Read, Write}; + let mut source = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut output = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let mut edited = false; + for index in 0..source.len() { + let mut entry = source.by_index(index).unwrap(); + let name = entry.name().unwrap().into_owned(); + let mut data = Vec::new(); + entry.read_to_end(&mut data).unwrap(); + if name == part { + let changed = edit(std::str::from_utf8(&data).unwrap()); + assert_ne!(changed.as_bytes(), data, "{part} must change"); + data = changed.into_bytes(); + edited = true; + } + output + .start_file(name, zip::write::SimpleFileOptions::default()) + .unwrap(); + output.write_all(&data).unwrap(); + } + assert!(edited, "{part} must be in the package"); + output.finish().unwrap().into_inner() +} + +fn issue435_errors(bytes: &[u8]) -> Vec { + rdocx::validate_document_bytes(bytes) + .unwrap() + .findings + .into_iter() + .filter(|finding| finding.severity == rdocx::ValidationSeverity::Error) + .map(|finding| finding.message) + .collect() +} + +fn issue435_base() -> Vec { + let mut document = Document::new(); + document.add_paragraph("Hello"); + document.add_paragraph("World"); + document.to_bytes().unwrap() +} + +const ISSUE435_PICTURE: &str = concat!( + r#""#, + r#""#, + r#""#, + r#""#, + r#""#, + r#""#, +); + +#[test] +fn issue435_validate_reports_wordprocessingml_schema_faults() { + let base = issue435_base(); + assert_eq!(issue435_errors(&base), Vec::::new()); + let run = "Hello"; + let extent = ISSUE435_PICTURE.replace(r#"r:embed="rId999""#, ""); + let radical = concat!( + r#""#, + r#"x"#, + ); + let cases = [ + ( + format!(r#"{run}"#), + "w:pPr is not the first child of w:p", + ), + ( + "Hello".to_owned(), + "w:rPr is not the first child of w:r", + ), + (format!("{run}"), "w:bogus is not allowed in w:p"), + ( + format!(r#"{run}"#), + r#"w:jc w:val="middle" is not an ST_Jc value"#, + ), + ( + r##"Hello"##.to_owned(), + r##"w:color w:val="#FF0000" is not an ST_HexColor"##, + ), + ("Hello".to_owned(), "text directly inside w:r"), + ( + format!(r#"{run}"#), + "w:ins has no w:id", + ), + ( + extent.replace(r#"cx="914400""#, r#"cx="-914400""#), + r#"wp:extent cx="-914400" is not an ST_PositiveCoordinate"#, + ), + (format!("{run}{radical}"), "m:rad has no m:deg"), + ]; + for (replacement, expected) in cases { + let bytes = issue435_edit(&base, "word/document.xml", |xml| { + xml.replacen(run, &replacement, 1) + }); + let errors = issue435_errors(&bytes); + assert!( + errors + .iter() + .any(|error| error.starts_with("part /word/document.xml: ") + && error.contains(expected)), + "{expected}: {errors:?}" + ); + } + let bytes = issue435_edit(&base, "word/document.xml", |xml| { + xml.replacen( + "", + "after", + 1, + ) + }); + let errors = issue435_errors(&bytes); + assert!( + errors + .iter() + .any(|error| error.contains("w:p follows w:sectPr, the last child of w:body")), + "{errors:?}" + ); +} + +#[test] +fn issue435_validate_reports_dangling_references() { + let mut original = Document::new(); + original.add_paragraph("Pay within thirty days."); + let mut edited = Document::new(); + edited.add_paragraph("Pay within sixty days."); + original + .compare(&edited, "Reviewer", "2026-10-30T12:00:00Z") + .unwrap(); + original.set_header("Header text"); + let base = original.to_bytes().unwrap(); + assert_eq!(issue435_errors(&base), Vec::::new()); + let header = issue435_edit(&base, "word/document.xml", |xml| { + let start = xml.find("{ISSUE435_PICTURE}").unwrap(); + format!( + "{}{}", + &xml[..start], + &xml[start + "".len()..] + ) + }), + "w:t inside w:del, where deleted text is w:delText", + ), + ]; + for (bytes, expected) in cases { + let errors = issue435_errors(&bytes); + assert!( + errors + .iter() + .any(|error| error.starts_with("part /word/document.xml: ") + && error.contains(expected)), + "{expected}: {errors:?}" + ); + } +} + +#[test] +fn issue435_validate_reports_a_malformed_document_properties_part() { + let base = issue435_base(); + let bytes = issue435_edit(&base, "docProps/app.xml", |xml| { + xml.replacen( + "", + "Smith & Sons", + 1, + ) + }); + let errors = issue435_errors(&bytes); + assert!( + errors + .iter() + .any(|error| error.starts_with("part /docProps/app.xml is not well-formed XML")), + "{errors:?}" + ); +} + +#[test] +fn issue435_validate_reports_a_character_xml_forbids() { + let base = issue435_base(); + let bytes = issue435_edit(&base, "word/document.xml", |xml| { + xml.replacen(">Hello<", ">He\u{1}llo<", 1) + }); + let errors = issue435_errors(&bytes); + assert!( + errors.iter().any(|error| error.starts_with( + "part /word/document.xml is not well-formed XML: it holds U+0001, a character XML 1.0 forbids" + )), + "{errors:?}" + ); +} + +#[test] +fn issue435_bookmarks_sharing_an_id_open_and_render() { + let mut document = Document::new(); + document.add_paragraph("First heading"); + document.add_paragraph("Some text"); + document.set_header("Header text"); + let base = document.to_bytes().unwrap(); + let bookmark = |xml: &str, text: &str, name: &str| { + let run = format!("{text}"); + assert!(xml.contains(&run), "{text}"); + xml.replacen( + &run, + &format!( + r#"{run}"# + ), + 1, + ) + }; + let same_part = issue435_edit(&base, "word/document.xml", |xml| { + bookmark( + &bookmark(xml, "First heading", "_Toc1"), + "Some text", + "_GoBack", + ) + }); + let body = issue435_edit(&base, "word/document.xml", |xml| { + bookmark(xml, "First heading", "_Toc1") + }); + let mut archive = zip::ZipArchive::new(std::io::Cursor::new(base.as_slice())).unwrap(); + let header_part = (0..archive.len()) + .map(|index| { + archive + .by_index(index) + .unwrap() + .name() + .unwrap() + .into_owned() + }) + .find(|name| name.starts_with("word/header")) + .unwrap(); + let across_parts = issue435_edit(&body, &header_part, |xml| { + bookmark(xml, "Header text", "_GoBack") + }); + for bytes in [same_part, across_parts] { + let mut opened = Document::from_bytes(&bytes).unwrap(); + assert!(opened.text().contains("Some text")); + assert!(!opened.layout().unwrap().layout.pages.is_empty()); + let report = rdocx::validate_document_bytes(&bytes).unwrap(); + assert!(report.is_valid(), "{:?}", report.findings); + assert!( + report.findings.iter().any(|finding| finding + .message + .starts_with("bookmark id 2 is used by 2 bookmarks")), + "{:?}", + report.findings + ); + opened.add_paragraph("New"); + let reopened = Document::from_bytes(&opened.to_bytes().unwrap()).unwrap(); + assert!(reopened.text().contains("New")); + } +} diff --git a/crates/rpptx-cli/README.md b/crates/rpptx-cli/README.md index beb5734f2..81f6400f6 100644 --- a/crates/rpptx-cli/README.md +++ b/crates/rpptx-cli/README.md @@ -16,6 +16,17 @@ replaces text, and produces deterministic fixed output. - Scriptable output covers slide order, notes, comments, recursive group content, relationship-backed media, and deterministic rendering diagnostics. +## Validation + +`rpptx validate` exits 1 on an issue and names the part. It reads the package +as stored, which a save keeps for every part it does not change, and reports a +part stored twice, an XML part that is not well-formed XML 1.0, a slide, +layout, master or notes part without its own content type, `presentation.xml` +children out of the schema's order, unknown shape tree children, a `p:cNvPr` +without its `id` or `name`, and a table row with another number of cells than +the grid has columns. It then checks the deck as rpptx reads it: shape and +slide ids, relationships, media, placeholders, and the layout and theme +relationships. It is not a full schema validator. ## Package signatures @@ -39,7 +50,7 @@ Signing publishes before reporting, so this error can leave a valid new output. | Measurement | Value | Version | Platform | Build mode | Input | Command | Statistic | Measured on | |---|---|---|---|---|---|---|---|---| -| Crates.io archive: rpptx-cli | 67,536 compressed bytes, 310,592 member bytes, 8 members | 0.14.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rpptx-cli` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | +| Crates.io archive: rpptx-cli | 67,795 compressed bytes, 311,234 member bytes, 8 members | 0.14.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rpptx-cli` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | ## Use it when diff --git a/crates/rpptx-cli/src/commands.rs b/crates/rpptx-cli/src/commands.rs index f88078e46..cf454ee13 100644 --- a/crates/rpptx-cli/src/commands.rs +++ b/crates/rpptx-cli/src/commands.rs @@ -663,8 +663,7 @@ pub fn replace( } pub fn validate(file: &Path) -> Result { - let presentation = Presentation::open(file)?; - let issues = presentation.validate(); + let issues = rpptx::validate_presentation_file(file)?; for issue in &issues { eprintln!("{issue:?}"); } diff --git a/crates/rpptx-py/tests/test_documented_examples.py b/crates/rpptx-py/tests/test_documented_examples.py index b41ad6999..6b4631239 100644 --- a/crates/rpptx-py/tests/test_documented_examples.py +++ b/crates/rpptx-py/tests/test_documented_examples.py @@ -12510,3 +12510,21 @@ def test_issue389_empty_body_removal_has_no_ghost_owner_or_resurrection(): assert len(fresh.text_frame.paragraphs) == 1 with pytest.raises(rpptx.StaleElementError): _ = held_body.text + + +def test_issue435_add_textbox_and_add_shape_refuse_a_negative_size(): + import rpptx + from rpptx.enum.shapes import MSO_SHAPE + from rpptx.util import Inches + + deck = rpptx.Presentation() + slide = deck.slides.add_slide(deck.slide_layouts[6]) + before = deck.to_bytes() + with pytest.raises(rpptx.RpptxError, match="must not be negative"): + slide.shapes.add_textbox(Inches(1), Inches(1), -Inches(3), Inches(1)) + with pytest.raises(rpptx.RpptxError, match="must not be negative"): + slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(1), Inches(3), -Inches(1)) + assert deck.to_bytes() == before + assert len(slide.shapes) == 0 + line = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(1), Inches(3), 0) + assert line.height == 0 diff --git a/crates/rpptx/README.md b/crates/rpptx/README.md index 638cb9631..de2f3723f 100644 --- a/crates/rpptx/README.md +++ b/crates/rpptx/README.md @@ -28,7 +28,7 @@ presentation, notes, handout, PDF, and animation outputs. | Measurement | Value | Version | Platform | Build mode | Input | Command | Statistic | Measured on | |---|---|---|---|---|---|---|---|---| -| Crates.io archive: rpptx | 762,699 compressed bytes, 4,061,273 member bytes, 16 members | 0.14.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rpptx` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | +| Crates.io archive: rpptx | 768,276 compressed bytes, 4,084,821 member bytes, 16 members | 0.14.4 | macOS 26.6.2, Apple M5 Max, arm64 | `cargo publish --workspace --dry-run --allow-dirty --locked` | Tracked `rpptx` package inventory | `python3 scripts/readme_doctests.py --record-measurements` | gzip archive bytes, tar member bytes, tar member count | 2026-10-28 | ## Use it when diff --git a/crates/rpptx/src/lib.rs b/crates/rpptx/src/lib.rs index e70798f9e..7c81dc9fc 100644 --- a/crates/rpptx/src/lib.rs +++ b/crates/rpptx/src/lib.rs @@ -20750,6 +20750,362 @@ fn relationship_requires_xml_reference(relationship_type: &str) -> bool { || relationship_type.ends_with("/diagramData") } +/// Validates a presentation file: the package as stored, which a save keeps +/// for every part it does not change, then [`Presentation::validate`]. +/// +/// The stored package is checked for XML parts that are not well-formed, +/// slide parts whose content type is not their own, `presentation.xml` +/// children out of the schema's order, unknown shape tree children, a +/// `p:cNvPr` without its required `id` or `name`, and table rows that hold +/// another number of cells than the grid has columns. A package that does +/// not open returns its error, unless these checks explain it. +pub fn validate_presentation_file(path: impl AsRef) -> Result> { + let bytes = std::fs::read(path).map_err(OpcError::from)?; + let package = OpcPackage::from_reader(Cursor::new(bytes.as_slice()))?; + let mut issues = stored_package_issues(&package); + match Presentation::from_read_package(package) { + Ok(presentation) => { + for issue in presentation.validate() { + if !issues.contains(&issue) { + issues.push(issue); + } + } + Ok(issues) + } + Err(_) if !issues.is_empty() => Ok(issues), + Err(error) => Err(error), + } +} + +/// The content type each PresentationML relationship's target must have. +const STORED_PART_CONTENT_TYPES: [(&str, &str); 6] = [ + (rel_types::SLIDE, content_types::SLIDE), + (rel_types::SLIDE_LAYOUT, content_types::SLIDE_LAYOUT), + (rel_types::SLIDE_MASTER, content_types::SLIDE_MASTER), + (rel_types::NOTES_SLIDE, content_types::NOTES_SLIDE), + (rel_types::NOTES_MASTER, content_types::NOTES_MASTER), + (rel_types::HANDOUT_MASTER, content_types::HANDOUT_MASTER), +]; + +/// The `p:` children of `p:presentation`, in `CT_Presentation` order. +const PRESENTATION_CHILD_ORDER: [&str; 15] = [ + "sldMasterIdLst", + "notesMasterIdLst", + "handoutMasterIdLst", + "sldIdLst", + "sldSz", + "notesSz", + "smartTags", + "embeddedFontLst", + "custShowLst", + "photoAlbum", + "custDataLst", + "kinsoku", + "defaultTextStyle", + "modifyVerifier", + "extLst", +]; + +/// The `p:` children a shape tree or a group admits (`CT_GroupShape`). +const SHAPE_TREE_CHILDREN: [&str; 9] = [ + "nvGrpSpPr", + "grpSpPr", + "sp", + "grpSp", + "graphicFrame", + "cxnSp", + "pic", + "contentPart", + "extLst", +]; + +fn stored_package_issues(package: &OpcPackage) -> Vec { + let mut issues = Vec::new(); + let mut part_names = package.parts.keys().collect::>(); + part_names.sort_unstable(); + let mut malformed = HashSet::new(); + for part_name in part_names { + let is_xml = package + .content_types + .content_type_for(part_name) + .is_some_and(|content_type| content_type.to_ascii_lowercase().ends_with("xml")); + let Some(xml) = package.get_part(part_name).filter(|_| is_xml) else { + continue; + }; + if let Some(message) = stored_xml_fault(xml) { + malformed.insert(part_name.as_str()); + issues.push(ValidationIssue::InvalidXmlChild { + part: part_name.clone(), + path: "/".to_owned(), + message: format!("the part is not well-formed XML: {message}"), + }); + } + } + + let mut slide_parts = Vec::new(); + let mut owners = package.part_rels.iter().collect::>(); + owners.sort_unstable_by_key(|(owner, _)| owner.as_str()); + for (owner, relationships) in owners { + for relationship in &relationships.items { + let Some((_, expected)) = STORED_PART_CONTENT_TYPES + .iter() + .find(|(rel_type, _)| relationship.rel_type == *rel_type) + else { + continue; + }; + if relationship_is_external(relationship) { + continue; + } + let target = OpcPackage::resolve_rel_target(owner, &relationship.target); + if package.get_part(&target).is_none() { + continue; + } + if package.content_types.content_type_for(&target) != Some(*expected) { + let issue = ValidationIssue::MissingContentTypeOverride { + part: target.clone(), + }; + if !issues.contains(&issue) { + issues.push(issue); + } + } + if !slide_parts.contains(&target) { + slide_parts.push(target); + } + } + } + + if let Some(part) = package + .main_document_part() + .filter(|part| !malformed.contains(part.as_str())) + && let Some(xml) = package.get_part(&part) + { + stored_presentation_order(&part, xml, &mut issues); + } + slide_parts.sort_unstable(); + for part in slide_parts { + if let Some(xml) = package + .get_part(&part) + .filter(|_| !malformed.contains(part.as_str())) + { + stored_slide_part_issues(&part, xml, &mut issues); + } + } + issues +} + +/// Why a stored XML part is not well-formed, if it is not: a parser error, +/// more or fewer than one root element, an element left open, or a literal +/// character XML 1.0 forbids. A UTF-16 part is only parsed. +fn stored_xml_fault(xml: &[u8]) -> Option { + let mut reader = quick_xml::Reader::from_reader(xml); + let mut depth = 0usize; + let mut roots = 0usize; + loop { + match reader.read_event() { + Err(error) => return Some(format!("{error} at byte {}", reader.error_position())), + Ok(XmlEvent::Start(_)) => { + roots += usize::from(depth == 0); + depth += 1; + } + Ok(XmlEvent::Empty(_)) => roots += usize::from(depth == 0), + Ok(XmlEvent::End(_)) => depth = depth.saturating_sub(1), + Ok(XmlEvent::Eof) => break, + Ok(_) => {} + } + } + if roots != 1 { + return Some(format!("the part has {roots} root elements")); + } + if depth != 0 { + return Some(format!("the part ends inside {depth} unclosed element(s)")); + } + if xml.starts_with(&[0xFF, 0xFE]) || xml.starts_with(&[0xFE, 0xFF]) { + return None; + } + let error = oxml_core::xml::validate_strict_xml_1_0(xml).err()?; + let text = String::from_utf8_lossy(xml); + Some( + match text + .char_indices() + .find(|(_, character)| !oxml_core::xml::is_xml_1_0_character(*character)) + { + Some((offset, character)) => format!( + "it holds U+{:04X}, a character XML 1.0 forbids, at byte {offset}", + u32::from(character) + ), + None => format!("{error:?}"), + }, + ) +} + +/// Reports `p:presentation` children out of the `CT_Presentation` order. +fn stored_presentation_order(part: &str, xml: &[u8], issues: &mut Vec) { + let mut reader = NsReader::from_reader(xml); + let mut depth = 0usize; + let mut last: Option<(usize, String)> = None; + let mut buffer = Vec::new(); + while let Ok((namespace, event)) = reader.read_resolved_event_into(&mut buffer) { + let is_presentation = matches!(namespace, XmlResolveResult::Bound(XmlNamespace(uri)) if uri == P_NS.as_bytes()); + match event { + XmlEvent::Start(ref element) | XmlEvent::Empty(ref element) => { + if depth == 1 && is_presentation { + let local = String::from_utf8_lossy(element.local_name().as_ref()).into_owned(); + match PRESENTATION_CHILD_ORDER + .iter() + .position(|name| *name == local) + { + Some(rank) => { + if let Some((last_rank, last_name)) = &last + && rank < *last_rank + { + issues.push(ValidationIssue::InvalidXmlChild { + part: part.to_owned(), + path: "/p:presentation".to_owned(), + message: format!( + "p:{local} follows p:{last_name}, out of the schema's order" + ), + }); + } + last = Some((rank, local)); + } + None => issues.push(ValidationIssue::InvalidXmlChild { + part: part.to_owned(), + path: "/p:presentation".to_owned(), + message: format!("p:{local} is not allowed in p:presentation"), + }), + } + } + if matches!(event, XmlEvent::Start(_)) { + depth += 1; + } + } + XmlEvent::End(_) => depth = depth.saturating_sub(1), + XmlEvent::Eof => break, + _ => {} + } + buffer.clear(); + } +} + +/// One open element of a stored slide part. +struct StoredElement { + name: String, + presentation: bool, + /// For an `a:tbl`, the `a:gridCol` count and the rows read so far. + table: Option<(usize, usize)>, + /// For an `a:tr`, the grid columns its `a:tc` cover so far, each cell + /// counting its `gridSpan`. + cells: usize, +} + +/// Reports unknown shape tree children, a `p:cNvPr` without its `id` or +/// `name`, and a table row whose cell count differs from the grid's column +/// count, in one stored slide, layout, master or notes part. +fn stored_slide_part_issues(part: &str, xml: &[u8], issues: &mut Vec) { + let mut stack: Vec = Vec::new(); + let mut reader = NsReader::from_reader(xml); + let mut buffer = Vec::new(); + let path = |stack: &[StoredElement]| { + stack + .iter() + .map(|element| format!("/{}", element.name)) + .collect::() + }; + let mut issue = |stack: &[StoredElement], message: String| { + issues.push(ValidationIssue::InvalidXmlChild { + part: part.to_owned(), + path: path(stack), + message, + }); + }; + while let Ok((namespace, event)) = reader.read_resolved_event_into(&mut buffer) { + let presentation = matches!(namespace, XmlResolveResult::Bound(XmlNamespace(uri)) if uri == P_NS.as_bytes()); + let drawing = matches!(namespace, XmlResolveResult::Bound(XmlNamespace(uri)) if uri == A_NS.as_bytes()); + match event { + XmlEvent::Start(ref element) | XmlEvent::Empty(ref element) => { + let local = String::from_utf8_lossy(element.local_name().as_ref()).into_owned(); + let name = String::from_utf8_lossy(element.name().as_ref()).into_owned(); + let in_shape_tree = stack.last().is_some_and(|parent| { + parent.presentation && matches!(local_part(&parent.name), "spTree" | "grpSp") + }); + if presentation && in_shape_tree && !SHAPE_TREE_CHILDREN.contains(&local.as_str()) { + issue(&stack, format!("{name} is not allowed in a shape tree")); + } + if presentation && local == "cNvPr" { + for required in ["id", "name"] { + if !element + .attributes() + .flatten() + .any(|attribute| attribute.key.as_ref() == required.as_bytes()) + { + issue( + &stack, + format!("{name} has no {required}, which the schema requires"), + ); + } + } + } + let table = stack.iter_mut().rev().find_map(|open| open.table.as_mut()); + match (drawing, local.as_str(), table) { + (true, "gridCol", Some(table)) => table.0 += 1, + (true, "tr", Some(table)) => table.1 += 1, + _ => {} + } + if drawing + && local == "tc" + && let Some(row) = stack + .last_mut() + .filter(|open| local_part(&open.name) == "tr") + { + // LibreOffice writes a spanning cell without the hMerge + // cells that follow it, so a cell covers its gridSpan. + let span = element + .attributes() + .flatten() + .find(|attribute| attribute.key.as_ref() == b"gridSpan") + .and_then(|attribute| { + std::str::from_utf8(&attribute.value).ok()?.parse().ok() + }) + .filter(|span: &usize| *span > 0) + .unwrap_or(1); + row.cells += span; + } + if matches!(event, XmlEvent::Start(_)) { + stack.push(StoredElement { + name, + presentation, + table: (drawing && local == "tbl").then_some((0, 0)), + cells: 0, + }); + } + } + XmlEvent::End(_) => { + if let Some(row) = stack.pop().filter(|open| local_part(&open.name) == "tr") + && let Some((columns, rows)) = stack.iter().rev().find_map(|open| open.table) + && row.cells < columns + { + issue( + &stack, + format!( + "table row {rows} covers {} of its {columns} a:gridCol", + row.cells + ), + ); + } + } + XmlEvent::Eof => break, + _ => {} + } + buffer.clear(); + } +} + +fn local_part(qualified: &str) -> &str { + qualified + .rsplit_once(':') + .map_or(qualified, |(_, local)| local) +} + fn part_has_content_type(package: &OpcPackage, part_name: &str) -> bool { package.content_types.content_type_for(part_name).is_some() || part_name.rsplit_once('.').is_some_and(|(_, extension)| { @@ -22354,7 +22710,7 @@ fn textbox_member(id: u32, left: Emu, top: Emu, width: Emu, height: Emu) -> Resu CT_Shape::new_textbox( id, &format!("TextBox {id}"), - positioned_transform(left, top, width, height), + sized_transform("add textbox", left, top, width, height)?, ) .map(ShapeTreeChild::Shape) .map_err(|error| invalid_shape_construction("add textbox", error)) @@ -22372,7 +22728,7 @@ fn preset_member( id, &format!("Shape {id}"), preset, - positioned_transform(left, top, width, height), + sized_transform("add shape", left, top, width, height)?, ) .map_err(|error| invalid_shape_construction("add shape", error))?; shape @@ -22393,7 +22749,7 @@ fn unstyled_preset_member( id, &format!("Shape {id}"), preset, - positioned_transform(left, top, width, height), + sized_transform("add shape", left, top, width, height)?, ) .map(ShapeTreeChild::Shape) .map_err(|error| invalid_shape_construction("add shape", error)) @@ -22733,6 +23089,27 @@ impl ConnectorType { } } +/// The transform of a new shape, refusing a negative width or height, which +/// `a:ext` cannot carry (`ST_PositiveCoordinate`), as a new table refuses it. +fn sized_transform( + operation: &'static str, + left: Emu, + top: Emu, + width: Emu, + height: Emu, +) -> Result { + if width.0 < 0 || height.0 < 0 { + return Err(Error::InvalidShapeMutation { + operation, + message: format!( + "shape width and height must not be negative, got {} by {} EMU", + width.0, height.0 + ), + }); + } + Ok(positioned_transform(left, top, width, height)) +} + fn positioned_transform(left: Emu, top: Emu, width: Emu, height: Emu) -> CT_Transform2D { let mut transform = CT_Transform2D::default(); transform.offset = Some(CT_Point2D { x: left, y: top }); diff --git a/crates/rpptx/tests/integration.rs b/crates/rpptx/tests/integration.rs index ce2255ee8..d16f06351 100644 --- a/crates/rpptx/tests/integration.rs +++ b/crates/rpptx/tests/integration.rs @@ -37182,3 +37182,239 @@ fn native_scaled_preset_paints_all_expected_corners_at_parallel_phase() { ); } } + +/// Rewrites one entry of a deck with `edit`, with no check of the result, +/// so that a test can store a fault the package writer refuses. +fn issue435_edit(bytes: &[u8], part: &str, edit: impl Fn(&str) -> String) -> Vec { + use std::io::{Read, Write}; + let mut source = zip::ZipArchive::new(Cursor::new(bytes)).unwrap(); + let mut output = zip::ZipWriter::new(Cursor::new(Vec::new())); + let mut edited = false; + for index in 0..source.len() { + let mut entry = source.by_index(index).unwrap(); + let name = entry.name().unwrap().into_owned(); + let mut data = Vec::new(); + entry.read_to_end(&mut data).unwrap(); + if name == part { + let changed = edit(std::str::from_utf8(&data).unwrap()); + assert_ne!(changed.as_bytes(), data, "{part} must change"); + data = changed.into_bytes(); + edited = true; + } + output + .start_file(name, zip::write::SimpleFileOptions::default()) + .unwrap(); + output.write_all(&data).unwrap(); + } + assert!(edited, "{part} must be in the deck"); + output.finish().unwrap().into_inner() +} + +/// The issues `validate_presentation_file` reports, as their debug text. +fn issue435_issues(bytes: &[u8]) -> Vec { + let path = std::env::temp_dir().join(format!( + "rpptx-issue435-{}-{}.pptx", + std::process::id(), + bytes.len() + )); + fs::write(&path, bytes).unwrap(); + let issues = rpptx::validate_presentation_file(&path); + fs::remove_file(&path).unwrap(); + issues + .unwrap() + .iter() + .map(|issue| format!("{issue:?}")) + .collect() +} + +fn issue435_deck() -> Vec { + use rpptx::{Emu, Presentation}; + let mut deck = Presentation::new().unwrap(); + deck.add_slide(5).unwrap(); + let mut slide = deck.slide_mut(0).unwrap(); + slide + .add_textbox(Emu(0), Emu(0), Emu(3_000_000), Emu(500_000)) + .unwrap() + .set_text("Hello") + .unwrap(); + slide + .add_table(2, 2, Emu(0), Emu(1_000_000), Emu(3_000_000), Emu(900_000)) + .unwrap(); + deck.set_notes_text(0, "A note").unwrap(); + deck.to_bytes().unwrap() +} + +#[test] +fn issue435_validate_presentation_file_reports_stored_faults() { + let base = issue435_deck(); + assert_eq!(issue435_issues(&base), Vec::::new()); + let slide = "ppt/slides/slide1.xml"; + let presentation = "ppt/presentation.xml"; + /// Moves the element that starts with `second` in front of `first`. + fn move_before(xml: &str, first: &str, second: &str) -> String { + let start = xml.find(second).unwrap(); + let end = if xml[start..].starts_with(&format!("{second}>")) { + let close = format!("", &second[1..]); + start + xml[start..].find(&close).unwrap() + close.len() + } else { + start + xml[start..].find("/>").unwrap() + 2 + }; + let element = &xml[start..end]; + let without = format!("{}{}", &xml[..start], &xml[end..]); + let at = without.find(first).unwrap(); + format!("{}{element}{}", &without[..at], &without[at..]) + } + /// The part, its edit, and the issue text expected. + type Case = (&'static str, fn(&str) -> String, &'static str); + let cases: [Case; 6] = [ + ( + slide, + |xml| xml.replacen("", "", 1), + "p:bogus is not allowed in a shape tree", + ), + ( + slide, + |xml| xml.replacen(r#" name="TextBox"#, r#" title="TextBox"#, 1), + "p:cNvPr has no name, which the schema requires", + ), + ( + presentation, + |xml| move_before(xml, "").unwrap() + 2; + format!("{}{}", &xml[..start], &xml[end..]) + }, + r#"MissingContentTypeOverride { part: "/ppt/slides/slide1.xml" }"#, + ), + ( + slide, + |xml| { + let row = xml.find("").unwrap(); + let end = cell + xml[cell..].find("").unwrap() + "".len(); + format!("{}{}", &xml[..cell], &xml[end..]) + }, + "table row 1 covers 1 of its 2 a:gridCol", + ), + ]; + for (part, edit, expected) in cases { + let issues = issue435_issues(&issue435_edit(&base, part, edit)); + let named = if part.starts_with('[') { slide } else { part }; + assert!( + issues + .iter() + .any(|issue| issue.contains(expected) + && issue.contains(&format!(r#"part: "/{named}""#))), + "{expected}: {issues:?}" + ); + } +} + +#[test] +fn issue435_a_spanned_row_without_hmerge_cells_covers_its_grid() { + // LibreOffice writes a cell with gridSpan and leaves out the hMerge cells + // after it, so the row holds fewer a:tc than the grid has columns. + let base = issue435_deck(); + let slide = "ppt/slides/slide1.xml"; + let spanned = issue435_edit(&base, slide, |xml| { + let row = xml.find("").unwrap(); + let second = cell + xml[cell..].find("").unwrap() + "".len(); + let end = second + xml[second..].find("").unwrap() + "".len(); + format!( + "{}{}{}", + &xml[..cell], + &xml[cell + "".len()..second], + &xml[end..] + ) + }); + let issues = issue435_issues(&spanned); + assert!( + !issues.iter().any(|issue| issue.contains("a:gridCol")), + "{issues:?}" + ); +} + +#[test] +fn issue435_validate_presentation_file_reports_a_malformed_document_properties_part() { + let bytes = issue435_edit(&issue435_deck(), "docProps/app.xml", |xml| { + xml.replacen( + "", + "Smith & Sons", + 1, + ) + }); + let issues = issue435_issues(&bytes); + assert!( + issues + .iter() + .any(|issue| issue.contains(r#"part: "/docProps/app.xml""#) + && issue.contains("the part is not well-formed XML")), + "{issues:?}" + ); +} + +#[test] +fn issue435_validate_presentation_file_reports_a_character_xml_forbids() { + let bytes = issue435_edit(&issue435_deck(), "ppt/slides/slide1.xml", |xml| { + xml.replacen(">Hello<", ">He\u{1}llo<", 1) + }); + let issues = issue435_issues(&bytes); + assert!( + issues + .iter() + .any(|issue| issue.contains(r#"part: "/ppt/slides/slide1.xml""#) + && issue.contains("it holds U+0001, a character XML 1.0 forbids")), + "{issues:?}" + ); +} + +#[test] +fn issue435_new_shapes_refuse_a_negative_size() { + use rpptx::{Emu, Presentation}; + let mut deck = Presentation::new().unwrap(); + deck.add_slide(6).unwrap(); + let before = deck.to_bytes().unwrap(); + let mut slide = deck.slide_mut(0).unwrap(); + let error = slide + .add_textbox(Emu(0), Emu(0), Emu(-2_743_200), Emu(914_400)) + .err() + .unwrap(); + assert!( + error + .to_string() + .contains("shape width and height must not be negative"), + "{error}" + ); + let error = slide + .add_shape("rect", Emu(0), Emu(0), Emu(2_743_200), Emu(-914_400)) + .err() + .unwrap(); + assert!( + error + .to_string() + .contains("shape width and height must not be negative"), + "{error}" + ); + let mut shapes = deck.shapes_mut(0, &[]).unwrap(); + assert!(shapes.add_textbox(Emu(0), Emu(0), Emu(-1), Emu(1)).is_err()); + // A zero extent stays allowed, as the schema allows it. + shapes + .add_shape("line", Emu(0), Emu(0), Emu(914_400), Emu(0)) + .unwrap(); + assert_ne!(deck.to_bytes().unwrap(), before); + let reopened = Presentation::from_bytes(&deck.to_bytes().unwrap()).unwrap(); + assert!(reopened.validate().is_empty()); +} diff --git a/scripts/readme_doctests.py b/scripts/readme_doctests.py index bafa5ed5b..6209bbfc6 100644 --- a/scripts/readme_doctests.py +++ b/scripts/readme_doctests.py @@ -399,19 +399,19 @@ class ReadmeCase: "oxml-drawing": (226_588, 1_494_418, 24), "oxml-layout": (4_904_118, 9_841_237, 60), "oxml-media": (12_258, 51_016, 6), - "oxml-opc": (117_614, 477_731, 12), + "oxml-opc": (118_426, 480_988, 12), "oxml-pdf": (86_558, 406_477, 14), "oxml-sml": (24_918, 115_416, 6), - "rdocx": (4_105_912, 54_118_469, 37), - "rdocx-cli": (111_071, 516_134, 8), + "rdocx": (4_113_924, 54_154_979, 37), + "rdocx-cli": (111_494, 517_637, 8), "rdocx-html": (27_460, 109_636, 11), "rdocx-layout": (358_235, 1_964_578, 15), "rdocx-opc": (3_906, 10_535, 6), "rdocx-oxml": (565_559, 3_484_893, 32), "rdocx-pdf": (8_125, 26_782, 6), - "rpptx": (762_699, 4_061_273, 16), + "rpptx": (768_276, 4_084_821, 16), "rpptx-chart": (6_842, 21_809, 6), - "rpptx-cli": (67_536, 310_592, 8), + "rpptx-cli": (67_795, 311_234, 8), "rpptx-layout": (103_940, 594_082, 11), "rpptx-oxml": (218_670, 1_386_209, 20), "rpptx-render": (86_981, 477_031, 8),