#3518: refuse whole-document language changes under protection

Protection leaves only its exceptions editable, so document scope is refused before anything changes. The post-check now compares the glossary, which no story-level check reached.
This commit is contained in:
Dylan M. Taylor
2026-11-15 15:08:58 -05:00
parent 2256cea114
commit 36a899eccf
4 changed files with 38 additions and 1 deletions
+3 -1
View File
@@ -431,7 +431,8 @@ enum HoleMode {
/// run formatting and paragraph formatting outside the ranges `author` may edit, in every story,
/// and the formatting of every table, row and cell that holds any of that protected content, and what
/// reaches the whole document: page setup, the document defaults and the existing styles
/// (applying a style may add one), the document's settings and its metadata.
/// (applying a style may add one), the glossary's building blocks, the document's settings and
/// its metadata.
pub(crate) fn outside_unchanged(before: &Document, after: &Document, author: &str) -> bool {
// Protection can't change mid-command (`protection_lifted` guards undo and redo).
let mode = if Some("forms") == before.settings.protection.as_deref() { HoleMode::Controls } else { HoleMode::Perms(author.into()) };
@@ -444,6 +445,7 @@ pub(crate) fn outside_unchanged(before: &Document, after: &Document, author: &st
&& before.settings == after.settings
&& before.core == after.core
&& before.custom_props == after.custom_props
&& before.glossary == after.glossary
&& comments_unchanged(before, after)
&& lists_unchanged(before, after)
&& sources_unchanged(before, after)
+17
View File
@@ -428,6 +428,23 @@ fn the_post_check_compares_the_settings_and_the_metadata() {
assert!(super::outside_unchanged(&before, &before.clone(), "someone"), "an unchanged document isn't unchanged");
}
/// The glossary's building blocks reach the whole document, so the post-check compares them.
#[test]
fn the_post_check_compares_the_glossary() {
use wordcraft_doc::{BuildingBlock, CharProps, Paragraph};
let mut before = Document::from_text("Some text.");
before.glossary.push(BuildingBlock {
name: "g".into(),
blocks: vec![wordcraft_doc::para_block(Paragraph::with_text("Block", Default::default()))],
..Default::default()
});
assert!(super::outside_unchanged(&before, &before.clone(), "someone"), "an unchanged glossary is unchanged");
let mut after = before.clone();
after.glossary[0].blocks[0] =
wordcraft_doc::para_block(Paragraph::with_text("Block", CharProps { lang: Some("fr-FR".into()), ..Default::default() }));
assert!(!super::outside_unchanged(&before, &after, "someone"), "a relanguaged building block is a change");
}
/// Read only, no exceptions: going to a header the document doesn't have doesn't create
/// one, and loading or filtering recipients doesn't rewrite the merge fields' shown results
/// (both used to change the document from a non-mutating command, #1327).
+4
View File
@@ -24,6 +24,10 @@ pub(super) fn language(s: &mut Session, v: &Value) -> CmdResult {
Some(Value::String(t)) if t == "document" => true,
Some(other) => return Err(CmdError::Params(format!("`scope` must be \"selection\" or \"document\", not {other}"))),
};
// Protection leaves only its exceptions editable, so the whole document can't be one.
if whole_document && let Some(mode) = s.doc.settings.protection.as_deref() {
return Err(CmdError::Disabled(format!("review.language: stop protection ({mode}) before changing the whole document's proofing language")));
}
let lang = match p::str(v, "lang") {
Some(t) => Some(
wordcraft_proof::lang::normalize_tag(t)
+14
View File
@@ -1004,3 +1004,17 @@ fn language_bad_scope_is_a_params_error() {
assert!(s.doc == before);
assert!(s.prefs.detect_language);
}
/// Document scope is refused under editing protection, before anything changes.
#[test]
fn language_document_scope_is_refused_under_protection() {
for mode in ["readOnly", "comments", "forms"] {
let mut s = multi_story_doc();
s.doc.settings.protection = Some(mode.into());
let before = s.doc.clone();
let e = s.run("review.language", &json!({"lang": "fr-FR", "detect": false, "scope": "document"})).unwrap_err();
assert!(matches!(e, CmdError::Disabled(_)), "{mode}: {e:?}");
assert!(s.doc == before, "{mode}: nothing changed");
assert!(s.prefs.detect_language, "{mode}: the detect preference is untouched");
}
}