Merge pull request #2943 from dotCooCoo/fix/docx-hide-proofing-errors-next

craft-docx: read and write the hide spelling and grammar errors settings on next
This commit is contained in:
BFlat
2026-11-26 10:23:52 +07:00
committed by GitHub
5 changed files with 67 additions and 0 deletions
+2
View File
@@ -34,6 +34,8 @@ impl Reader<'_> {
"w:linkStyles" => s.link_styles = on_off(k),
"w:removePersonalInformation" => s.remove_personal_info = on_off(k),
"w:removeDateAndTime" => s.remove_date_time = on_off(k),
"w:hideSpellingErrors" => s.hide_spelling_errors = on_off(k),
"w:hideGrammaticalErrors" => s.hide_grammar_errors = on_off(k),
"w:compat" => {
s.allow_space_of_same_style_in_table = k.child("w:allowSpaceOfSameStyleInTable").is_some_and(on_off);
// Only Word's own setting: another `w:uri` names a different application's.
+7
View File
@@ -54,6 +54,13 @@ pub(super) fn settings_xml(doc: &Document, footnotes: bool, endnotes: bool, temp
if s.gutter_at_top {
w.empty("w:gutterAtTop", &[]);
}
// After `w:gutterAtTop` and before `w:proofState` in CT_Settings.
if s.hide_spelling_errors {
w.empty("w:hideSpellingErrors", &[]);
}
if s.hide_grammar_errors {
w.empty("w:hideGrammaticalErrors", &[]);
}
if let Some(id) = template_rel {
w.empty("w:attachedTemplate", &[("r:id", id)]);
}
+2
View File
@@ -20,6 +20,8 @@ const OWNED: &[&str] = &[
"w:displayBackgroundShape",
"w:mirrorMargins",
"w:gutterAtTop",
"w:hideSpellingErrors",
"w:hideGrammaticalErrors",
"w:attachedTemplate",
"w:linkStyles",
"w:mailMerge",
+47
View File
@@ -204,3 +204,50 @@ fn do_not_track_moves_round_trips() {
assert!(s.contains("<w:doNotTrackMoves/>"), "{s}");
assert!(!craft_docx::read(&craft_docx::write(&doc).unwrap()).unwrap().settings.track_moves);
}
/// Hide spelling errors and Hide grammar errors in this document only (`w:hideSpellingErrors`,
/// `w:hideGrammaticalErrors`) are read, written once each (before `w:proofState` in a merged
/// part), written in a generated part, and left out when they are turned off.
#[test]
fn hidden_proofing_errors_round_trip() {
let original = SETTINGS.replace("<w:proofState", r#"<w:hideSpellingErrors/><w:hideGrammaticalErrors w:val="1"/><w:proofState"#);
let mut doc = craft_docx::read(&with_settings(&original)).unwrap();
assert!(doc.settings.hide_spelling_errors && doc.settings.hide_grammar_errors);
let s = text(&save(&doc), "word/settings.xml");
assert_eq!(s.matches("hideSpellingErrors").count(), 1, "{s}");
assert_eq!(s.matches("hideGrammaticalErrors").count(), 1, "{s}");
assert!(pos(&s, "<w:hideSpellingErrors/>") < pos(&s, "<w:hideGrammaticalErrors/>"));
assert!(pos(&s, "<w:hideGrammaticalErrors/>") < pos(&s, "<w:proofState"), "{s}");
assert!(s.contains("ClauseSet"), "a merged part: {s}");
doc.settings.hide_spelling_errors = false;
doc.settings.hide_grammar_errors = false;
let s = text(&save(&doc), "word/settings.xml");
assert!(!s.contains("hideSpellingErrors") && !s.contains("hideGrammaticalErrors"), "{s}");
// Turned on in a part that lacks them: inserted once each, before `w:proofState`.
let mut doc = craft_docx::read(&with_settings(SETTINGS)).unwrap();
assert!(!doc.settings.hide_spelling_errors && !doc.settings.hide_grammar_errors);
doc.settings.hide_spelling_errors = true;
doc.settings.hide_grammar_errors = true;
let s = text(&save(&doc), "word/settings.xml");
assert_eq!(s.matches("hideSpellingErrors").count(), 1, "{s}");
assert_eq!(s.matches("hideGrammaticalErrors").count(), 1, "{s}");
assert!(pos(&s, "removeDateAndTime") < pos(&s, "<w:hideSpellingErrors/>"), "{s}");
assert!(pos(&s, "<w:hideGrammaticalErrors/>") < pos(&s, "<w:proofState"), "{s}");
// `w:val="false"` and `w:val="0"` show them.
let off = SETTINGS.replace("<w:proofState", r#"<w:hideSpellingErrors w:val="false"/><w:hideGrammaticalErrors w:val="0"/><w:proofState"#);
let read = craft_docx::read(&with_settings(&off)).unwrap();
assert!(!read.settings.hide_spelling_errors && !read.settings.hide_grammar_errors);
let s = text(&save(&read), "word/settings.xml");
assert!(!s.contains("hideSpellingErrors") && !s.contains("hideGrammaticalErrors"), "{s}");
// A document of our own: absent by default, written when on, and read back.
let mut doc = craft_wordmodel::Document::new();
assert!(!text(&save(&doc), "word/settings.xml").contains("hideSpellingErrors"));
doc.settings.hide_grammar_errors = true;
let s = text(&save(&doc), "word/settings.xml");
assert!(s.contains("<w:hideGrammaticalErrors/>") && !s.contains("hideSpellingErrors"), "{s}");
let back = craft_docx::read(&craft_docx::write(&doc).unwrap()).unwrap();
assert!(back.settings.hide_grammar_errors && !back.settings.hide_spelling_errors);
}
+9
View File
@@ -170,6 +170,13 @@ pub struct Settings {
/// comments and tracked changes are saved without `w:date`.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub remove_date_time: bool,
/// Hide spelling errors in this document only (`w:hideSpellingErrors`): no squiggles under
/// misspelled words. Spelling & Grammar (F7) still finds them.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub hide_spelling_errors: bool,
/// Hide grammar errors in this document only (`w:hideGrammaticalErrors`).
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub hide_grammar_errors: bool,
/// The document was opened from ODF: the standard's rules that differ from Word's apply as
/// it is laid out.
#[serde(skip_serializing_if = "Option::is_none")]
@@ -333,6 +340,8 @@ impl Default for Settings {
zoom: None,
remove_personal_info: false,
remove_date_time: false,
hide_spelling_errors: false,
hide_grammar_errors: false,
odf: None,
}
}