From 6dfad5678fa49cdffd3992190d9b29bdb4107a1f Mon Sep 17 00:00:00 2001 From: MaheshDesu Date: Wed, 2 Dec 2026 23:43:06 +0530 Subject: [PATCH 1/4] fix: recognise a spaced picture file name after a PPTX round trip (#2514) Signed-off-by: MaheshDesu --- crates/engine/src/cmd/review.rs | 121 ++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index 67e99d273..b8d63223e 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -247,15 +247,49 @@ fn neighbour(s: &mut Session, p: &Value, forward: bool) -> Result { } /// Whether alternative text is a picture's file name and nothing else: one word ending in a -/// picture file's extension (`IMG_2041.png`, `C:\\photos\\a.JPG`), or the name of the file the -/// picture came from, which may have spaces (`Screenshot 2024 at 10.00.png`). +/// picture file's extension (`IMG_2041.png`, `C:\\photos\\a.JPG`), a spaced name with a file +/// name's shape (`Screenshot 2024 at 10.00.png`), or the exact name of the file the picture +/// came from, spaces and all. fn is_file_name(alt: &str, file: Option<&str>) -> bool { const PICTURE_EXTENSIONS: [&str; 15] = ["png", "jpg", "jpeg", "jpe", "gif", "bmp", "tif", "tiff", "webp", "svg", "emf", "wmf", "heic", "avif", "ico"]; let alt = alt.trim(); - let one_word = !alt.is_empty() && !alt.contains(char::is_whitespace); - (one_word && alt.rsplit_once('.').is_some_and(|(stem, ext)| !stem.is_empty() && PICTURE_EXTENSIONS.iter().any(|e| ext.eq_ignore_ascii_case(e)))) - || file.is_some_and(|f| !alt.is_empty() && f.trim().eq_ignore_ascii_case(alt)) + if alt.is_empty() { + return false; + } + if file.is_some_and(|f| f.trim().eq_ignore_ascii_case(alt)) { + return true; + } + let Some((stem, ext)) = alt.rsplit_once('.') else { + return false; + }; + if stem.is_empty() || !PICTURE_EXTENSIONS.iter().any(|e| ext.eq_ignore_ascii_case(e)) { + return false; + } + !alt.contains(char::is_whitespace) || is_spaced_file_name(stem) +} + +/// Whether a stem that ends in a picture extension has a file name's shape rather than a +/// sentence's. A stem of three words or fewer is a name. A longer stem must be mostly name +/// parts: digits, `_`, `-`, an internal `.`, or a capital letter. +fn is_spaced_file_name(stem: &str) -> bool { + // A path is a file name even when a folder has spaces. + if stem.contains(['/', '\\']) { + return true; + } + // Clause punctuation introduces a description: "A red square, from IMG_2041.png". + if stem.contains([',', ';', ':', '!', '?']) { + return false; + } + let words: Vec<&str> = stem.split_whitespace().collect(); + if words.len() <= 3 { + return !words.is_empty(); + } + let named = words + .iter() + .filter(|word| word.chars().any(|c| c.is_ascii_digit() || matches!(c, '_' | '-' | '.')) || word.chars().any(char::is_uppercase)) + .count(); + named * 2 > words.len() } /// The name of the file a picture shape's picture came from, if it has one. @@ -1102,8 +1136,81 @@ mod accessibility_contrast_tests { assert!(!is_file_name("Version 2.0", None), "a version number is not a picture's extension"); assert!(!is_file_name(".png", None)); assert!(!is_file_name("A red square, from IMG_2041.png", None)); - // The picture's own file name counts, spaces and all. + // A file name with spaces counts by its shape, with or without the picture's own name. assert!(is_file_name("Screenshot 2024 at 10.00.png", Some("Screenshot 2024 at 10.00.png"))); - assert!(!is_file_name("Screenshot 2024 at 10.00.png", None)); + assert!(is_file_name("Screenshot 2024 at 10.00.png", None), "a spaced name is recognised after the media item is renamed"); + } + + fn test_png() -> Vec { + vec![ + 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 8, 0, 0, 0, 8, 8, 2, 0, 0, 0, 75, 109, 41, 220, 0, 0, 0, 17, 73, + 68, 65, 84, 120, 156, 99, 184, 163, 161, 129, 21, 49, 12, 45, 9, 0, 123, 123, 75, 1, 251, 162, 81, 240, 0, 0, 0, 0, 73, 69, 78, 68, 174, + 66, 96, 130, + ] + } + + fn file_name_issue_count(s: &mut Session) -> usize { + s.execute("review.accessibility", &json!({})) + .unwrap() + .as_array() + .unwrap() + .iter() + .filter(|issue| issue["issue"] == "Alternative text is a file name") + .count() + } + + fn pptx_round_trip(s: &mut Session) { + let saved = s.execute("file.saveBytes", &json!({"format": "pptx"})).unwrap(); + s.execute("file.openBytes", &json!({"name": "deck.pptx", "data": saved["data"]})).unwrap(); + } + + #[test] + fn spaced_file_names_are_recognised_by_shape() { + for name in [ + "Screenshot 2024 at 10.00.png", + "rough draft preview.png", + "Screen Shot 2024-11-30 at 09.41.12.png", + r"C:\Users\Me\My Pictures\holiday.png", + ] { + assert!(is_file_name(name, None), "{name:?} is a file name"); + } + assert!( + is_file_name("Screenshot 2024 at 10.00.png", Some("image1.png")), + "the shape rule works when the media item was renamed by the round trip" + ); + for text in [ + "A red square, from IMG_2041.png", + "A red square from IMG_2041.png", + "Three markers in the upper corner of caption alt.png", + "The chart shows revenue in 2024.png", + "Version 2.0", + ".png", + "A red square", + ] { + assert!(!is_file_name(text, None), "{text:?} is not a file name"); + } + for name in ["IMG_2041.png", "photo.JPEG", r"C:\Users\me\Pictures\holiday.png"] { + assert!(is_file_name(name, None), "{name:?} is still a file name"); + } + assert!(is_file_name("Screenshot 2024 at 10.00.png", Some("Screenshot 2024 at 10.00.png"))); + } + + #[test] + fn a_spaced_picture_file_name_is_still_reported_after_a_pptx_round_trip() { + let png = test_png(); + let mut s = Session::with_new(); + s.execute("insert.picture", &json!({"data": crate::cmd::base64_encode(&png), "name": "Screenshot 2024 at 10.00.png"})).unwrap(); + assert_eq!(file_name_issue_count(&mut s), 1, "reported before the save"); + + pptx_round_trip(&mut s); + + let doc = s.execute("document.inspect", &json!({})).unwrap(); + assert!(doc["media"].as_array().unwrap().iter().any(|m| m["name"] == "image1.png"), "the round trip renamed the media item: {doc}"); + let slide = s.execute("slide.inspect", &json!({"index": 0})).unwrap(); + assert!( + slide["shapes"].as_array().unwrap().iter().any(|sh| sh["altText"] == "Screenshot 2024 at 10.00.png"), + "the alt text survives the round trip: {slide}" + ); + assert_eq!(file_name_issue_count(&mut s), 1, "reported after the save and reopen"); } } From d7702e7420d310cbec346747ecf08d37d0cd66a0 Mon Sep 17 00:00:00 2001 From: MaheshDesu Date: Wed, 2 Dec 2026 23:47:50 +0530 Subject: [PATCH 2/4] test: cover spaced file names across a PPTX round trip (#2514) Signed-off-by: MaheshDesu --- crates/engine/src/cmd/review.rs | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index b8d63223e..cd2a53fc2 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -1213,4 +1213,62 @@ mod accessibility_contrast_tests { ); assert_eq!(file_name_issue_count(&mut s), 1, "reported after the save and reopen"); } + + #[test] + fn a_description_that_mentions_a_file_is_not_reported_after_a_pptx_round_trip() { + let png = test_png(); + let mut s = Session::with_new(); + let id = s.execute("insert.picture", &json!({"data": crate::cmd::base64_encode(&png), "name": "IMG_2041.png"})).unwrap()["id"].clone(); + s.execute("shape.altText", &json!({"text": "A red square, from IMG_2041.png", "ids": [id]})).unwrap(); + assert_eq!(file_name_issue_count(&mut s), 0, "a description that mentions a file is clear"); + + pptx_round_trip(&mut s); + + let slide = s.execute("slide.inspect", &json!({"index": 0})).unwrap(); + assert!( + slide["shapes"].as_array().unwrap().iter().any(|sh| sh["altText"] == "A red square, from IMG_2041.png"), + "the description survives the round trip: {slide}" + ); + assert_eq!(file_name_issue_count(&mut s), 0, "the description stays clear after the round trip"); + } + + #[test] + fn a_decorative_picture_with_a_spaced_file_name_is_not_reported_after_a_pptx_round_trip() { + let png = test_png(); + let mut s = Session::with_new(); + let id = + s.execute("insert.picture", &json!({"data": crate::cmd::base64_encode(&png), "name": "Screenshot 2024 at 10.00.png"})).unwrap()["id"] + .clone(); + s.execute("shape.altText", &json!({"text": "Screenshot 2024 at 10.00.png", "decorative": true, "ids": [id]})).unwrap(); + assert_eq!(file_name_issue_count(&mut s), 0, "a decorative picture is exempt"); + + pptx_round_trip(&mut s); + + let slide = s.execute("slide.inspect", &json!({"index": 0})).unwrap(); + assert!( + slide["shapes"].as_array().unwrap().iter().any(|sh| sh["altText"] == "Screenshot 2024 at 10.00.png"), + "the alt text survives, so only decorative suppresses the finding: {slide}" + ); + assert_eq!(file_name_issue_count(&mut s), 0, "the decorative picture stays exempt after the round trip"); + } + + #[test] + fn one_word_names_and_paths_are_still_reported_after_a_pptx_round_trip() { + let png = test_png(); + let mut s = Session::with_new(); + let id = s.execute("insert.picture", &json!({"data": crate::cmd::base64_encode(&png), "name": "IMG_2041.png"})).unwrap()["id"].clone(); + assert_eq!(file_name_issue_count(&mut s), 1, "the inserted file name is reported"); + + s.execute("shape.altText", &json!({"text": r"C:\Users\me\Pictures\holiday.png", "ids": [id]})).unwrap(); + assert_eq!(file_name_issue_count(&mut s), 1, "a one-word path is reported"); + + pptx_round_trip(&mut s); + + let slide = s.execute("slide.inspect", &json!({"index": 0})).unwrap(); + assert!( + slide["shapes"].as_array().unwrap().iter().any(|sh| sh["altText"] == r"C:\Users\me\Pictures\holiday.png"), + "the path survives the round trip: {slide}" + ); + assert_eq!(file_name_issue_count(&mut s), 1, "the one-word path is still reported after the round trip"); + } } From 910f4e86d1bbfc527f69756628e1ae3a47a9ef39 Mon Sep 17 00:00:00 2001 From: MaheshDesu Date: Thu, 3 Dec 2026 07:21:26 +0530 Subject: [PATCH 3/4] fix(review): require a path shape or name parts, not title case (#2514) Signed-off-by: MaheshDesu --- crates/engine/src/cmd/review.rs | 45 ++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index cd2a53fc2..94f44369e 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -270,11 +270,13 @@ fn is_file_name(alt: &str, file: Option<&str>) -> bool { } /// Whether a stem that ends in a picture extension has a file name's shape rather than a -/// sentence's. A stem of three words or fewer is a name. A longer stem must be mostly name -/// parts: digits, `_`, `-`, an internal `.`, or a capital letter. +/// sentence's. A path is a name even when a folder has spaces. Otherwise a stem of three words or +/// fewer is a name, and a longer stem must have at least two name parts: digits, `_`, `-`, an +/// internal `.`, an uppercase after the first letter, or a multi-letter all-caps word. fn is_spaced_file_name(stem: &str) -> bool { - // A path is a file name even when a folder has spaces. - if stem.contains(['/', '\\']) { + // A path is a file name even when a folder has spaces; a slash inside a descriptive clause is + // not, so the clause check still follows for non-path stems. + if is_path_shaped(stem) { return true; } // Clause punctuation introduces a description: "A red square, from IMG_2041.png". @@ -285,11 +287,33 @@ fn is_spaced_file_name(stem: &str) -> bool { if words.len() <= 3 { return !words.is_empty(); } - let named = words - .iter() - .filter(|word| word.chars().any(|c| c.is_ascii_digit() || matches!(c, '_' | '-' | '.')) || word.chars().any(char::is_uppercase)) - .count(); - named * 2 > words.len() + words.iter().filter(|word| is_name_part(word)).count() >= 2 +} + +/// Whether the stem starts like a path: a root (`/`, `\`, `~`), a drive letter, or one unspaced +/// first segment followed by a separator. `"A red square, from photos/IMG_2041"` is a clause, not a +/// path, while `C:\Users\Jane, Doe\...` and `photos/IMG 2041` are paths. +fn is_path_shaped(stem: &str) -> bool { + let Some(at) = stem.find(['/', '\\']) else { + return false; + }; + let head = &stem[..at]; + head.is_empty() + || head == "~" + || (head.len() == 2 && head.as_bytes()[0].is_ascii_alphabetic() && head.as_bytes()[1] == b':') + || !head.contains(char::is_whitespace) +} + +/// Whether one word looks like part of a file name rather than ordinary prose: a digit, a +/// separator, an internal dot, an uppercase after the first letter, or a multi-letter all-caps word. +fn is_name_part(word: &str) -> bool { + let mut chars = word.chars(); + let Some(first) = chars.next() else { + return false; + }; + word.chars().any(|c| c.is_ascii_digit() || matches!(c, '_' | '-' | '.')) + || chars.any(char::is_uppercase) + || (word.len() > 1 && first.is_uppercase() && word.chars().all(|c| c.is_uppercase() || c.is_ascii_digit())) } /// The name of the file a picture shape's picture came from, if it has one. @@ -1171,6 +1195,7 @@ mod accessibility_contrast_tests { "rough draft preview.png", "Screen Shot 2024-11-30 at 09.41.12.png", r"C:\Users\Me\My Pictures\holiday.png", + r"C:\Users\Jane, Doe\Pictures\holiday.png", ] { assert!(is_file_name(name, None), "{name:?} is a file name"); } @@ -1180,7 +1205,9 @@ mod accessibility_contrast_tests { ); for text in [ "A red square, from IMG_2041.png", + "A red square, from photos/IMG_2041.png", "A red square from IMG_2041.png", + "A Red Square From IMG_2041.png", "Three markers in the upper corner of caption alt.png", "The chart shows revenue in 2024.png", "Version 2.0", From 10a319f002e46ecea700e66f7b67c671f9c480ff Mon Sep 17 00:00:00 2001 From: MaheshDesu Date: Fri, 4 Dec 2026 17:23:36 +0530 Subject: [PATCH 4/4] fix(review): keep embedded filenames and abbreviations out of the check (#2514) Signed-off-by: MaheshDesu --- crates/engine/src/cmd/review.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index 94f44369e..6d9a9881f 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -250,9 +250,9 @@ fn neighbour(s: &mut Session, p: &Value, forward: bool) -> Result { /// picture file's extension (`IMG_2041.png`, `C:\\photos\\a.JPG`), a spaced name with a file /// name's shape (`Screenshot 2024 at 10.00.png`), or the exact name of the file the picture /// came from, spaces and all. +const PICTURE_EXTENSIONS: [&str; 15] = ["png", "jpg", "jpeg", "jpe", "gif", "bmp", "tif", "tiff", "webp", "svg", "emf", "wmf", "heic", "avif", "ico"]; + fn is_file_name(alt: &str, file: Option<&str>) -> bool { - const PICTURE_EXTENSIONS: [&str; 15] = - ["png", "jpg", "jpeg", "jpe", "gif", "bmp", "tif", "tiff", "webp", "svg", "emf", "wmf", "heic", "avif", "ico"]; let alt = alt.trim(); if alt.is_empty() { return false; @@ -279,6 +279,10 @@ fn is_spaced_file_name(stem: &str) -> bool { if is_path_shaped(stem) { return true; } + // A description that mentions a file inside a sentence is prose, not a file name. + if mentions_a_file(stem) { + return false; + } // Clause punctuation introduces a description: "A red square, from IMG_2041.png". if stem.contains([',', ';', ':', '!', '?']) { return false; @@ -304,6 +308,17 @@ fn is_path_shaped(stem: &str) -> bool { || !head.contains(char::is_whitespace) } +/// Whether a word names a file inside a longer description: a picture extension follows its last +/// dot (`IMG_2041.png`). A trailing dot (`Dr.`) has no extension after it. +fn mentions_a_file(stem: &str) -> bool { + stem.split_whitespace().any(|word| { + let Some((before, ext)) = word.rsplit_once('.') else { + return false; + }; + !before.is_empty() && PICTURE_EXTENSIONS.iter().any(|e| ext.eq_ignore_ascii_case(e)) + }) +} + /// Whether one word looks like part of a file name rather than ordinary prose: a digit, a /// separator, an internal dot, an uppercase after the first letter, or a multi-letter all-caps word. fn is_name_part(word: &str) -> bool { @@ -311,7 +326,9 @@ fn is_name_part(word: &str) -> bool { let Some(first) = chars.next() else { return false; }; - word.chars().any(|c| c.is_ascii_digit() || matches!(c, '_' | '-' | '.')) + let internal_dot = word.split('.').count() > 1 && !word.starts_with('.') && !word.ends_with('.'); + word.chars().any(|c| c.is_ascii_digit() || matches!(c, '_' | '-')) + || internal_dot || chars.any(char::is_uppercase) || (word.len() > 1 && first.is_uppercase() && word.chars().all(|c| c.is_uppercase() || c.is_ascii_digit())) } @@ -1208,6 +1225,8 @@ mod accessibility_contrast_tests { "A red square, from photos/IMG_2041.png", "A red square from IMG_2041.png", "A Red Square From IMG_2041.png", + "A comparison of IMG_2041.png and IMG_2042.png", + "Portrait of Dr. A. Smith.png", "Three markers in the upper corner of caption alt.png", "The chart shows revenue in 2024.png", "Version 2.0",