From 43d74383a75fb502c19820b7a9c2bc388f2001c5 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:25:50 +0100 Subject: [PATCH 1/5] sheet.tabColor: preserve tab colors when input is invalid Reject missing or malformed color values before editing. Preserve valid RGB/theme colors and explicit No Color clearing, with regressions for retained workbook/history and successful redo. --- crates/engine/src/cmd/sheet/tabs.rs | 59 ++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/sheet/tabs.rs b/crates/engine/src/cmd/sheet/tabs.rs index 2c1657ef90..6512ea000a 100644 --- a/crates/engine/src/cmd/sheet/tabs.rs +++ b/crates/engine/src/cmd/sheet/tabs.rs @@ -229,9 +229,9 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result { pub(super) fn tab_color(s: &mut Session, p: &Json) -> Result { let i = target_sheet(s, p)?; - let c = crate::cmd::format::color_param(p.get("color")); + let c = crate::cmd::format::color_param(p.get("color")).ok_or_else(|| bad("sheet.tabColor", "missing or invalid `color`"))?; edit(s, |cx| { - cx.sheet_mut(i)?.tab_color = c.filter(|c| *c != gridcraft_model::Color::Auto); + cx.sheet_mut(i)?.tab_color = (c != gridcraft_model::Color::Auto).then_some(c); Ok(Json::Null) }) } @@ -294,3 +294,58 @@ pub(super) fn step_sheet(s: &mut Session, dir: isize) -> Result { // No visible sheet in this direction: keep the current sheet and its selection. ok() } + +#[cfg(test)] +mod tests { + use super::*; + use gridcraft_model::Color; + + #[test] + fn invalid_tab_colors_preserve_the_existing_color_and_history() { + let mut s = Session::new(); + s.new_workbook(); + s.execute("sheet.tabColor", json!({"color": "#FF0000"})).unwrap(); + s.execute("cell.set", json!({"cell": "C1", "input": "redo marker"})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + let (before, revision, undo, redo) = { + let doc = s.doc().unwrap(); + assert_eq!(doc.wb.active().unwrap().tab_color, Some(Color::Rgb(0xFF0000))); + assert!(!doc.undo.is_empty() && !doc.redo.is_empty()); + (doc.wb.clone(), doc.revision, doc.undo.len(), doc.redo.len()) + }; + for params in [ + json!({}), + json!({"color": "#GG0000"}), + json!({"color": ""}), + json!({"color": null}), + json!({"color": false}), + json!({"color": 7}), + json!({"color": []}), + json!({"color": {}}), + json!({"color": {"theme": "bad"}}), + ] { + let error = s.execute("sheet.tabColor", params.clone()).unwrap_err().to_string(); + assert!(error.contains("missing or invalid `color`"), "{params}: {error}"); + let doc = s.doc().unwrap(); + assert_eq!(*doc.wb, *before, "{params}"); + assert_eq!((doc.revision, doc.undo.len(), doc.redo.len()), (revision, undo, redo), "{params}"); + } + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().value(CellRef::new(0, 2)), gridcraft_core::Value::from("redo marker")); + } + + #[test] + fn tab_colors_still_accept_rgb_theme_and_explicit_clearing() { + let mut s = Session::new(); + s.new_workbook(); + for (color, expected) in [(json!("#00FF00"), Color::Rgb(0x00FF00)), (json!({"theme": 3, "tint": 0.4}), Color::Theme(3, 400))] { + s.execute("sheet.tabColor", json!({"color": color})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, Some(expected)); + } + for clear in ["none", "auto", "automatic", "NoNe"] { + s.execute("sheet.tabColor", json!({"color": "#FF0000"})).unwrap(); + s.execute("sheet.tabColor", json!({"color": clear})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, None, "{clear}"); + } + } +} From 5406b1d674e3a7c40150913ecdd2ef651fbe72bd Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:09:39 +0100 Subject: [PATCH 2/5] sheet.tabColor: preserve parameterless keytip clearing --- crates/engine/src/cmd/sheet/tabs.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/sheet/tabs.rs b/crates/engine/src/cmd/sheet/tabs.rs index 6512ea000a..01ab7c9d5f 100644 --- a/crates/engine/src/cmd/sheet/tabs.rs +++ b/crates/engine/src/cmd/sheet/tabs.rs @@ -229,7 +229,11 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result { pub(super) fn tab_color(s: &mut Session, p: &Json) -> Result { let i = target_sheet(s, p)?; - let c = crate::cmd::format::color_param(p.get("color")).ok_or_else(|| bad("sheet.tabColor", "missing or invalid `color`"))?; + // The Tab Color keytip omits color to clear it. + let c = match p.get("color") { + None => gridcraft_model::Color::Auto, + Some(value) => crate::cmd::format::color_param(Some(value)).ok_or_else(|| bad("sheet.tabColor", "invalid `color`"))?, + }; edit(s, |cx| { cx.sheet_mut(i)?.tab_color = (c != gridcraft_model::Color::Auto).then_some(c); Ok(Json::Null) @@ -314,7 +318,6 @@ mod tests { (doc.wb.clone(), doc.revision, doc.undo.len(), doc.redo.len()) }; for params in [ - json!({}), json!({"color": "#GG0000"}), json!({"color": ""}), json!({"color": null}), @@ -325,7 +328,7 @@ mod tests { json!({"color": {"theme": "bad"}}), ] { let error = s.execute("sheet.tabColor", params.clone()).unwrap_err().to_string(); - assert!(error.contains("missing or invalid `color`"), "{params}: {error}"); + assert!(error.contains("invalid `color`"), "{params}: {error}"); let doc = s.doc().unwrap(); assert_eq!(*doc.wb, *before, "{params}"); assert_eq!((doc.revision, doc.undo.len(), doc.redo.len()), (revision, undo, redo), "{params}"); @@ -335,7 +338,7 @@ mod tests { } #[test] - fn tab_colors_still_accept_rgb_theme_and_explicit_clearing() { + fn tab_colors_still_accept_rgb_theme_and_clearing() { let mut s = Session::new(); s.new_workbook(); for (color, expected) in [(json!("#00FF00"), Color::Rgb(0x00FF00)), (json!({"theme": 3, "tint": 0.4}), Color::Theme(3, 400))] { @@ -347,5 +350,10 @@ mod tests { s.execute("sheet.tabColor", json!({"color": clear})).unwrap(); assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, None, "{clear}"); } + s.execute("sheet.tabColor", json!({"color": "#FF0000"})).unwrap(); + s.journal.clear(); + s.execute("sheet.tabColor", json!({})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, None, "omitted color still clears the tab"); + assert!(s.journal.iter().any(|(id, _)| id == "sheet.tabColor"), "menu-style invocation is still journaled"); } } From 77cf0a05a406f0bb81a00ab83b38865f7637336e Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:23:35 +0100 Subject: [PATCH 3/5] Color: validate alpha digits before parsing RGB Reject malformed ARGB prefixes while preserving valid eight-digit colors. Add model regressions for invalid alpha, Unicode and signed input. --- crates/model/src/style.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/model/src/style.rs b/crates/model/src/style.rs index 9ad61abec0..37b932ff52 100644 --- a/crates/model/src/style.rs +++ b/crates/model/src/style.rs @@ -32,10 +32,11 @@ impl Color { } pub fn from_hex(s: &str) -> Option { let h = s.trim().trim_start_matches('#'); - let h = if h.len() == 8 { h.get(2..)? } else { h }; - if h.len() != 6 { + // Alpha is ignored for RGB colors, but its digits must still be valid hexadecimal. + if !matches!(h.len(), 6 | 8) || !h.bytes().all(|b| b.is_ascii_hexdigit()) { return None; } + let h = if h.len() == 8 { h.get(2..)? } else { h }; u32::from_str_radix(h, 16).ok().map(Color::Rgb) } /// Resolved RGB, `None` for automatic. @@ -543,6 +544,10 @@ mod tests { let th = Theme::default(); assert_eq!(Color::from_hex("#FF0000").ok_or("none")?.resolve(&th), Some([255, 0, 0])); assert_eq!(Color::from_hex("FF00FF00").ok_or("none")?.resolve(&th), Some([0, 255, 0])); + assert_eq!(Color::from_hex("#80FF0000"), Some(Color::Rgb(0xFF0000))); + for invalid in ["#GGFF0000", "#0GFF0000", "#éFF0000", "#+F0000"] { + assert_eq!(Color::from_hex(invalid), None, "{invalid}"); + } assert_eq!(Color::Theme(0, -500).resolve(&th), Some([128, 128, 128])); assert_eq!(Color::Theme(1, 500).resolve(&th), Some([128, 128, 128])); assert_eq!(Color::Auto.resolve(&th), None); From 0a91dcae0b01cfed8707af5ec0ab62339a82f2cf Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:25:16 +0100 Subject: [PATCH 4/5] sheet.tabColor: cover valid and invalid alpha prefixes --- crates/engine/src/cmd/sheet/tabs.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/sheet/tabs.rs b/crates/engine/src/cmd/sheet/tabs.rs index 01ab7c9d5f..fea943f4eb 100644 --- a/crates/engine/src/cmd/sheet/tabs.rs +++ b/crates/engine/src/cmd/sheet/tabs.rs @@ -319,6 +319,7 @@ mod tests { }; for params in [ json!({"color": "#GG0000"}), + json!({"color": "#GGFF0000"}), json!({"color": ""}), json!({"color": null}), json!({"color": false}), @@ -341,7 +342,11 @@ mod tests { fn tab_colors_still_accept_rgb_theme_and_clearing() { let mut s = Session::new(); s.new_workbook(); - for (color, expected) in [(json!("#00FF00"), Color::Rgb(0x00FF00)), (json!({"theme": 3, "tint": 0.4}), Color::Theme(3, 400))] { + for (color, expected) in [ + (json!("#00FF00"), Color::Rgb(0x00FF00)), + (json!("#80FF0000"), Color::Rgb(0xFF0000)), + (json!({"theme": 3, "tint": 0.4}), Color::Theme(3, 400)), + ] { s.execute("sheet.tabColor", json!({"color": color})).unwrap(); assert_eq!(s.doc().unwrap().wb.active().unwrap().tab_color, Some(expected)); } From f8713e6ed4ef16a4c39de392b8d251620967e9e9 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:33:09 +0100 Subject: [PATCH 5/5] Color: reject repeated hex prefixes Strip at most one # before validating RGB/ARGB digits. Extend colors() with repeated-prefix rejection and whitespace/lowercase acceptance. Validation: full style.rs isolated regression fails before fix (4 passed, 1 failed) and passes after (5 passed); isolated clippy -D warnings and repository rustfmt settings pass. Workspace gate attempted offline but blocked before compilation by uncached pinned egui Git source. --- crates/model/src/style.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/model/src/style.rs b/crates/model/src/style.rs index 37b932ff52..fb179161ab 100644 --- a/crates/model/src/style.rs +++ b/crates/model/src/style.rs @@ -31,7 +31,8 @@ impl Color { Color::Rgb(((r as u32) << 16) | ((g as u32) << 8) | b as u32) } pub fn from_hex(s: &str) -> Option { - let h = s.trim().trim_start_matches('#'); + let h = s.trim(); + let h = h.strip_prefix('#').unwrap_or(h); // Alpha is ignored for RGB colors, but its digits must still be valid hexadecimal. if !matches!(h.len(), 6 | 8) || !h.bytes().all(|b| b.is_ascii_hexdigit()) { return None; @@ -545,7 +546,9 @@ mod tests { assert_eq!(Color::from_hex("#FF0000").ok_or("none")?.resolve(&th), Some([255, 0, 0])); assert_eq!(Color::from_hex("FF00FF00").ok_or("none")?.resolve(&th), Some([0, 255, 0])); assert_eq!(Color::from_hex("#80FF0000"), Some(Color::Rgb(0xFF0000))); - for invalid in ["#GGFF0000", "#0GFF0000", "#éFF0000", "#+F0000"] { + assert_eq!(Color::from_hex(" ff0000 "), Some(Color::Rgb(0xFF0000))); + assert_eq!(Color::from_hex(" #80ff0000 "), Some(Color::Rgb(0xFF0000))); + for invalid in ["#GGFF0000", "#0GFF0000", "#éFF0000", "#+F0000", "##FF0000", "##80FF0000", "###FF0000"] { assert_eq!(Color::from_hex(invalid), None, "{invalid}"); } assert_eq!(Color::Theme(0, -500).resolve(&th), Some([128, 128, 128]));