mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
* Add craft-wordmodel: WordCraft's document model without its editing code WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX, RTF, ODT and other text-format crates can read into and write from one model. Module paths, type names and serde attributes are unchanged: an app that re-exports the crate keeps its native file format. Left in the app: range editing (edit.rs), IME text and equation editing. Not model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs. * craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs The DOCX reader and writer walk an equation's runs. The walk and the child accessors it uses were in WordCraft's equation editor (math_edit.rs), which stays in the app; they are structure, not editing. * craft-wordmodel: keep the document operations (edit.rs) with the model The DOCX and RTF readers use them: alternative-format chunks are merged into a document with adopt_blocks, and a reader formats ranges with format_range and inserts objects. They operate on the document, not on a view, so they belong beside it. The tests that drove them come back with them; the paragraph walks stay in walk.rs and edit re-exports them. base64 0.23, as WordCraft and craft-offcrypto use.
207 lines
9.2 KiB
Rust
207 lines
9.2 KiB
Rust
//! Legacy form fields (`FORMCHECKBOX`, `FORMTEXT`, `FORMDROPDOWN`): what their `w:ffData` says,
|
|
//! read from the raw XML a [`crate::InlineObject::Field`] keeps in its `form` (ECMA-376 Part 1
|
|
//! §17.16.17). Only what layout and PDF export need; the XML itself stays the source of truth
|
|
//! for saving.
|
|
|
|
/// Most drop-down entries kept (Word allows 25).
|
|
pub const MAX_ENTRIES: usize = 64;
|
|
|
|
/// A legacy form field's kind and state.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum LegacyForm {
|
|
/// A check box: `checked` is its state (`w:checked`, else `w:default`), `size` its explicit
|
|
/// size in points (`w:size`), `None` when it follows the text (`w:sizeAuto`).
|
|
CheckBox { name: String, checked: bool, size: Option<f32> },
|
|
/// A text field; its text is the field's result.
|
|
Text { name: String, max_len: Option<u32> },
|
|
/// A drop-down list showing entry `selected` (`w:result`, else `w:default`, else the first).
|
|
DropDown { name: String, entries: Vec<String>, selected: usize },
|
|
}
|
|
|
|
impl LegacyForm {
|
|
/// The field's definition, or `None` when `ff_data` holds none of the three.
|
|
pub fn parse(ff_data: &str) -> Option<LegacyForm> {
|
|
let name = attr_of(ff_data, "w:name").unwrap_or_default();
|
|
if let Some(cb) = element(ff_data, "w:checkBox") {
|
|
let default = attr_of(cb, "w:default").is_some_and(|v| on(&v));
|
|
// `<w:checked/>` with no value means checked.
|
|
let checked = match tag(cb, "w:checked") {
|
|
Some(t) => attr_in(t).is_none_or(|v| on(&v)),
|
|
None => default,
|
|
};
|
|
let size =
|
|
attr_of(cb, "w:size").and_then(|v| v.trim().parse::<f32>().ok()).filter(|v| v.is_finite() && *v > 0.0).map(|v| (v / 2.0).min(1584.0));
|
|
return Some(LegacyForm::CheckBox { name, checked, size });
|
|
}
|
|
if let Some(dd) = element(ff_data, "w:ddList") {
|
|
let entries: Vec<String> = tags(dd, "w:listEntry").filter_map(attr_in).take(MAX_ENTRIES).collect();
|
|
let pick = |n: &str| attr_of(dd, n).and_then(|v| v.trim().parse::<usize>().ok());
|
|
let selected = pick("w:result").or_else(|| pick("w:default")).filter(|i| *i < entries.len()).unwrap_or(0);
|
|
return Some(LegacyForm::DropDown { name, entries, selected });
|
|
}
|
|
if ff_data.contains("<w:textInput") {
|
|
let max_len =
|
|
element(ff_data, "w:textInput").and_then(|t| attr_of(t, "w:maxLength")).and_then(|v| v.trim().parse::<u32>().ok()).filter(|n| *n > 0);
|
|
return Some(LegacyForm::Text { name, max_len });
|
|
}
|
|
None
|
|
}
|
|
|
|
/// The text a drop-down shows (its selected entry); `None` for other kinds or an empty list.
|
|
pub fn shown_text(&self) -> Option<&str> {
|
|
match self {
|
|
LegacyForm::DropDown { entries, selected, .. } => entries.get(*selected).map(String::as_str),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The field's bookmark-style name (`w:name`), possibly empty.
|
|
pub fn name(&self) -> &str {
|
|
match self {
|
|
LegacyForm::CheckBox { name, .. } | LegacyForm::Text { name, .. } | LegacyForm::DropDown { name, .. } => name,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn on(v: &str) -> bool {
|
|
!matches!(v.trim(), "0" | "false" | "off")
|
|
}
|
|
|
|
/// The element `name` in `xml` with its content: from its start tag to its end tag, or the
|
|
/// self-closing tag alone.
|
|
fn element<'a>(xml: &'a str, name: &str) -> Option<&'a str> {
|
|
let open = tag(xml, name)?;
|
|
let start = xml.find(open)?;
|
|
if open.ends_with("/>") {
|
|
return Some(open);
|
|
}
|
|
let close = format!("</{name}>");
|
|
let end = xml.get(start..)?.find(&close)? + start + close.len();
|
|
xml.get(start..end)
|
|
}
|
|
|
|
/// The first start (or empty) tag `<name ...>` in `xml`.
|
|
fn tag<'a>(xml: &'a str, name: &str) -> Option<&'a str> {
|
|
tags(xml, name).next()
|
|
}
|
|
|
|
/// Every start (or empty) tag `<name ...>` in `xml`, in order.
|
|
fn tags<'a>(xml: &'a str, name: &str) -> impl Iterator<Item = &'a str> + use<'a> {
|
|
let pat = format!("<{name}");
|
|
let mut from = 0;
|
|
std::iter::from_fn(move || {
|
|
loop {
|
|
let at = from + xml.get(from..)?.find(&pat)?;
|
|
let after = at + pat.len();
|
|
from = after;
|
|
// `<w:checkBox` must not match `<w:checkBoxes`: the name ends at a space, `/` or `>`.
|
|
if xml.get(after..).and_then(|s| s.chars().next()).is_some_and(|c| c == ' ' || c == '/' || c == '>') {
|
|
let end = after + xml.get(after..)?.find('>')? + 1;
|
|
return xml.get(at..end);
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/// The `w:val` of the first tag `name` in `xml`.
|
|
fn attr_of(xml: &str, name: &str) -> Option<String> {
|
|
tag(xml, name).and_then(attr_in)
|
|
}
|
|
|
|
/// The `w:val` of a single tag, XML entities resolved.
|
|
fn attr_in(tag: &str) -> Option<String> {
|
|
let at = tag.find("w:val=")? + "w:val=".len();
|
|
let quote = tag.get(at..)?.chars().next()?;
|
|
if quote != '"' && quote != '\'' {
|
|
return None;
|
|
}
|
|
let rest = tag.get(at + 1..)?;
|
|
let end = rest.find(quote)?;
|
|
Some(unescape(rest.get(..end)?))
|
|
}
|
|
|
|
fn unescape(mut s: &str) -> String {
|
|
let mut out = String::with_capacity(s.len());
|
|
while let Some((text, rest)) = s.split_once('&') {
|
|
out.push_str(text);
|
|
if let Some(end) = rest.find(['&', ';'])
|
|
&& rest.as_bytes()[end] == b';'
|
|
{
|
|
let entity = &rest[..end];
|
|
let decoded = match entity {
|
|
"lt" => Some('<'),
|
|
"gt" => Some('>'),
|
|
"quot" => Some('"'),
|
|
"apos" => Some('\''),
|
|
"amp" => Some('&'),
|
|
_ => entity.strip_prefix('#').and_then(|n| {
|
|
let value = if let Some(hex) = n.strip_prefix('x') {
|
|
(!hex.is_empty() && hex.bytes().all(|b| b.is_ascii_hexdigit())).then(|| u32::from_str_radix(hex, 16).ok()).flatten()
|
|
} else {
|
|
(!n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())).then(|| n.parse::<u32>().ok()).flatten()
|
|
}?;
|
|
char::from_u32(value)
|
|
.filter(|c| matches!(*c, '\t' | '\n' | '\r' | '\u{20}'..='\u{d7ff}' | '\u{e000}'..='\u{fffd}' | '\u{10000}'..='\u{10ffff}'))
|
|
}),
|
|
};
|
|
if let Some(c) = decoded {
|
|
out.push(c);
|
|
s = &rest[end + 1..];
|
|
continue;
|
|
}
|
|
}
|
|
out.push('&');
|
|
s = rest;
|
|
}
|
|
out.push_str(s);
|
|
out
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn drop_down_entities_decode_once_including_xml_whitespace() {
|
|
let xml = r#"<w:ffData><w:name w:val="Name"/><w:ddList><w:listEntry w:val="A B	C D😀&#10;<>"'"/></w:ddList></w:ffData>"#;
|
|
let f = LegacyForm::parse(xml).unwrap();
|
|
assert_eq!(f.name(), "Name");
|
|
assert_eq!(f.shown_text(), Some("A\nB\tC\rD😀 <>\"'"));
|
|
let malformed = r#"<w:ffData><w:ddList><w:listEntry w:val="���&#-1;&#xZZ;&unknown;&"/></w:ddList></w:ffData>"#;
|
|
assert_eq!(LegacyForm::parse(malformed).unwrap().shown_text(), Some("���&#-1;&#xZZ;&unknown;&"));
|
|
}
|
|
|
|
#[test]
|
|
fn check_boxes_text_fields_and_drop_downs() {
|
|
let cb = |inner: &str| format!(r#"<w:ffData><w:name w:val="Box1"/><w:enabled/><w:checkBox>{inner}</w:checkBox></w:ffData>"#);
|
|
assert_eq!(
|
|
LegacyForm::parse(&cb(r#"<w:sizeAuto/><w:default w:val="0"/>"#)),
|
|
Some(LegacyForm::CheckBox { name: "Box1".into(), checked: false, size: None })
|
|
);
|
|
assert_eq!(
|
|
LegacyForm::parse(&cb(r#"<w:sizeAuto/><w:default w:val="1"/>"#)).map(|f| matches!(f, LegacyForm::CheckBox { checked: true, .. })),
|
|
Some(true)
|
|
);
|
|
// `w:checked` wins over `w:default`; without a value it means checked.
|
|
assert!(matches!(
|
|
LegacyForm::parse(&cb(r#"<w:default w:val="1"/><w:checked w:val="0"/>"#)),
|
|
Some(LegacyForm::CheckBox { checked: false, .. })
|
|
));
|
|
assert!(matches!(LegacyForm::parse(&cb(r#"<w:default w:val="0"/><w:checked/>"#)), Some(LegacyForm::CheckBox { checked: true, .. })));
|
|
assert!(matches!(
|
|
LegacyForm::parse(&cb(r#"<w:size w:val="30"/><w:default w:val="0"/>"#)),
|
|
Some(LegacyForm::CheckBox { size: Some(15.0), .. })
|
|
));
|
|
let dd = r#"<w:ffData><w:name w:val="List1"/><w:ddList><w:result w:val="1"/><w:listEntry w:val="Alpha"/><w:listEntry w:val="B & C"/></w:ddList></w:ffData>"#;
|
|
let f = LegacyForm::parse(dd).unwrap();
|
|
assert_eq!(f.shown_text(), Some("B & C"));
|
|
assert_eq!(f.name(), "List1");
|
|
let dd0 = r#"<w:ffData><w:ddList><w:listEntry w:val="Only"/></w:ddList></w:ffData>"#;
|
|
assert_eq!(LegacyForm::parse(dd0).and_then(|f| f.shown_text().map(str::to_string)).as_deref(), Some("Only"));
|
|
let tx = r#"<w:ffData><w:name w:val="Name1"/><w:textInput><w:maxLength w:val="40"/></w:textInput></w:ffData>"#;
|
|
assert_eq!(LegacyForm::parse(tx), Some(LegacyForm::Text { name: "Name1".into(), max_len: Some(40) }));
|
|
assert_eq!(LegacyForm::parse(""), None);
|
|
assert_eq!(LegacyForm::parse("<w:ffData><w:name w:val=\"x\"/></w:ffData>"), None);
|
|
}
|
|
}
|