Merge pull request #2582

This commit is contained in:
autocraft-bot[bot]
2026-12-04 14:48:40 +00:00
+220 -9
View File
@@ -247,15 +247,90 @@ fn neighbour(s: &mut Session, p: &Value, forward: bool) -> Result<Value> {
}
/// 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.
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();
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 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; 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;
}
// 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;
}
let words: Vec<&str> = stem.split_whitespace().collect();
if words.len() <= 3 {
return !words.is_empty();
}
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 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 {
let mut chars = word.chars();
let Some(first) = chars.next() else {
return false;
};
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()))
}
/// The name of the file a picture shape's picture came from, if it has one.
@@ -1102,8 +1177,144 @@ 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<u8> {
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",
r"C:\Users\Jane, Doe\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 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",
".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");
}
#[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");
}
}