This commit is contained in:
bflatastic
2026-11-19 01:12:30 +07:00
2 changed files with 74 additions and 0 deletions
+43
View File
@@ -1960,6 +1960,20 @@ impl LibraryWalk {
}
}
/// Whether an XML node in `o` or the dictionaries it holds has a value or a type
/// (`xmlnode-nodevalue`, `xmlnode-nodetype`). `seen` counts the dictionaries looked at; past
/// [`MAX_LIBRARY_NODES`] of them, or deeper than [`MAX_DEPTH`], the library is taken to hold some.
fn holds_values(o: &Obj, seen: &mut usize, depth: usize) -> bool {
*seen = seen.saturating_add(1);
if *seen > MAX_LIBRARY_NODES || depth > MAX_DEPTH {
return true;
}
if o.ty == "XMLNode" && (o.get("xmlnode-nodevalue").is_some() || o.get("xmlnode-nodetype").is_some()) {
return true;
}
o.items().any(|c| holds_values(c, seen, depth + 1))
}
/// The library of the `variableSets` element `sets` (see [`variable_library`]).
fn read_variable_sets(sets: &Obj) -> VariableRead {
let mut walk = LibraryWalk::default();
@@ -1979,5 +1993,34 @@ fn read_variable_sets(sets: &Obj) -> VariableRead {
if walk.nodes > MAX_LIBRARY_NODES {
return VariableRead { variables: Variables::default(), bindings: vec![], bindings_skipped: 0, skipped: 1, present: true };
}
if vars == Variables::default() && walk.bindings.is_empty() && !holds_values(sets, &mut 0, 0) {
// The empty library a file can keep: nodes with names and nothing else (no value, no
// type). There is nothing in it to list, so nothing was left out.
walk.skipped = 0;
}
VariableRead { variables: vars, bindings: walk.bindings, bindings_skipped: walk.bindings_skipped, skipped: walk.skipped, present: true }
}
#[cfg(test)]
mod tests {
use super::*;
use std::rc::Rc;
/// An XML node holding `child` (when given) and nothing else: no value, no type.
fn node(child: Option<Obj>) -> Obj {
let children = Obj { ty: "Array".into(), content: child.map(|c| V::Obj(Rc::new(c))).into_iter().collect(), ..Obj::default() };
Obj { ty: "XMLNode".into(), entries: vec![(Some("xmlnode-children".into()), vec![V::Obj(Rc::new(children))])], ..Obj::default() }
}
/// An empty library is taken to hold values past the caps on what is looked at: nested deeper
/// than [`MAX_DEPTH`], or once [`MAX_LIBRARY_NODES`] dictionaries have been looked at.
#[test]
fn an_empty_library_past_the_caps_is_taken_to_hold_values() {
let shallow = node(Some(node(None)));
assert!(!holds_values(&shallow, &mut 0, 0));
let deep = (0..MAX_DEPTH).fold(node(None), |inner, _| node(Some(inner)));
assert!(holds_values(&deep, &mut 0, 0));
let mut seen = MAX_LIBRARY_NODES;
assert!(holds_values(&shallow, &mut seen, 0));
}
}
+31
View File
@@ -2507,6 +2507,37 @@ fn ai_variable_library_edge_cases() {
assert_eq!(notes, ["the file's variable library couldn't be read: the Variables panel doesn't list it"]);
}
/// The empty library a file can keep, its nodes named and nothing else (no value, no type, empty
/// attributes), has nothing to list: it comes in without a note. A node that has a value or a type
/// isn't empty, so the same skeleton with a value, or with a type, still gets the note.
#[test]
fn an_empty_variable_library_gives_no_note() {
// A node with a name and nothing else: its children, then its attributes, as `%_` lines.
fn bare(name: &str, attributes: &[&str], children: &[String]) -> String {
let close = |n: &str, key: &str| format!("{}%_; {key},\n", n.strip_suffix("%_;\n").unwrap_or(n));
let attributes: String = attributes.iter().map(|a| close(&bare(a, &[], &[]), &format!("({a}) "))).collect();
let children: String = children.iter().map(|c| close(c, "")).collect();
format!(
"%_/XMLNode :\n%_/Dictionary :\n{attributes}%_; (xmlnode-attributes) ,\n%_/Array :\n{children}%_; (xmlnode-children) ,\n%_({name}) /String (xmlnode-nodename) ,\n%_;\n"
)
}
let data_sets = bare("v:sampleDataSets", &["xmlns:v", "xmlns"], &[]);
let skeleton = bare("variableSets", &[], &[bare("variableSet", &["varSetName"], std::slice::from_ref(&data_sets))]);
let (vars, notes) = read_library(&skeleton);
assert_eq!(vars, vectorcraft_doc::Variables::default());
assert!(notes.is_empty(), "{notes:?}");
let unread = ["the file's variable library couldn't be read: the Variables panel doesn't list it"];
// The same skeleton but for one entry of the data sets node: a value, then a type.
let with = |entry: &str| {
let data_sets = data_sets.replace("(xmlnode-nodename) ,\n", &format!("(xmlnode-nodename) ,\n{entry}"));
bare("variableSets", &[], &[bare("variableSet", &["varSetName"], &[data_sets])])
};
for entry in ["%_(x) /String (xmlnode-nodevalue) ,\n", "%_1 /Int (xmlnode-nodetype) ,\n"] {
let (_, notes) = read_library(&with(entry));
assert_eq!(notes, unread, "{entry}");
}
}
/// When the layers can't be read, or reading stops before the library, [`variables`] still reads
/// it, for the file read from its PDF part.
#[test]