Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration: #1679 table.convertFromText refuses more rows than a table holds

aiongge040354eb409
diff --git a/crates/engine/src/cmd/table/structure.rs b/crates/engine/src/cmd/table/structure.rs
index a358ee552..0eb0302df 100644
--- a/crates/engine/src/cmd/table/structure.rs
+++ b/crates/engine/src/cmd/table/structure.rs
@@ -135,6 +135,11 @@ pub(super) fn convert_from_text(s: &mut Session, p: &Value) -> Result<Value> {
if cols * data.len() as u64 > MAX_CONVERTED_CELLS {
return Err(bad(ID, format!("the text makes more than {MAX_CONVERTED_CELLS} cells")));
}
+ // `Table::new` would clamp the shape and drop the rows past it, with their text.
+ let (rows, _) = designcraft_doc::clamped_size(data.len(), cols as usize);
+ if rows < data.len() {
+ return Err(bad(ID, format!("the text makes {} rows; a table of {cols} columns has at most {rows}", data.len())));
+ }
let pf = st.paras[pa].clone();
let cf = st.format_after(a).clone();
let mut table = Table::from_strings(id, &data, width, &pf, &cf);
diff --git a/crates/engine/src/tests/table.rs b/crates/engine/src/tests/table.rs
index 2479ab459..c31a63dd4 100644
--- a/crates/engine/src/tests/table.rs
+++ b/crates/engine/src/tests/table.rs
@@ -586,6 +586,9 @@ fn convert_text_to_table_with_indesigns_separators() {
let long = vec!["a\tb"; MAX_CONVERTED_CELLS as usize / 2 + 1].join("\n");
assert!(convert(&mut s, &long, json!({})).is_err(), "more cells than a table can have");
assert_eq!(s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().text, format!("{long}\nAfter"));
+ let tall = vec!["a"; designcraft_doc::MAX_TABLE_ROWS + 1].join("\n");
+ assert!(convert(&mut s, &tall, json!({})).is_err(), "more rows than a table can have");
+ assert_eq!(s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().text, format!("{tall}\nAfter"), "no text is dropped");
let at_most = vec!["x"; MAX_COLUMNS as usize].join("\t");
assert!(convert(&mut s, &at_most, json!({})).is_ok(), "a row as wide as a table can be");
check(&s);