DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Convert Text to Table: refuse text too wide or too big for a table
moppeeerb30f4edb5b93
diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rsindex 827384713..58a31e863 100644--- a/crates/engine/src/cmd/table.rs+++ b/crates/engine/src/cmd/table.rs@@ -569,6 +569,9 @@ fn para_direction(d: &Document, story: StoryId, offset: usize) -> designcraft_dopub const MAX_BODY_ROWS: u64 = 500;pub const MAX_COLUMNS: u64 = 200;pub const MAX_HEADER_FOOTER_ROWS: u64 = 100;+/// The most cells `table.convertFromText` makes: as many as the largest table `table.insert`+/// makes, however the rows and columns divide them (a long list can make a long table).+pub const MAX_CONVERTED_CELLS: u64 = MAX_BODY_ROWS * MAX_COLUMNS;/// A command's `tableStyle`: none for an absent one or [Basic Table] (a new table's look), else the/// resolved style and the cell styles it names. Anything but an existing style's name is an error.@@ -673,6 +676,14 @@ fn convert_from_text(s: &mut Session, p: &Value) -> Result<Value> {} else {text.split(row_sep.as_str()).map(|row| row.split(col_sep.as_str()).map(|c| c.trim().to_string()).collect()).collect()};+ // The table is as wide as its widest row; refuse what no table here could hold.+ let cols = data.iter().map(Vec::len).max().unwrap_or(1) as u64;+ if cols > MAX_COLUMNS {+ return Err(bad(ID, format!("the text makes {cols} columns; a table has at most {MAX_COLUMNS}")));+ }+ if cols * data.len() as u64 > MAX_CONVERTED_CELLS {+ return Err(bad(ID, format!("the text makes more than {MAX_CONVERTED_CELLS} cells")));+ }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.rsindex cb2519e1e..072e91f9a 100644--- a/crates/engine/src/tests_table.rs+++ b/crates/engine/src/tests_table.rs@@ -512,6 +512,15 @@ fn convert_text_to_table_with_indesigns_separators() {] {assert!(convert(&mut s, "p\tq", bad.clone()).is_err(), "{bad}");}+ // Text that would make too wide or too big a table is refused, and stays as it was.+ use crate::cmd::table::{MAX_COLUMNS, MAX_CONVERTED_CELLS};+ let wide = vec!["x"; MAX_COLUMNS as usize + 1].join("\t");+ assert!(convert(&mut s, &wide, json!({})).is_err(), "a row wider than a table can be");+ 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 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);// Back to text with a comma between columns and a tab between rows.let r = convert(&mut s, "Plum\t80%\nSunset\t45%", json!({})).unwrap();