f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! On/off values of WordprocessingML and OMML: the simple type `ST_OnOff` (ECMA-376 Part 1,
//! 22.9.2.7), whose values `1`, `true` and `on` turn a property on and `0`, `false` and `off`
//! turn it off. An on/off element, such as `<w:b/>`, is on unless its `val` turns it off; an
//! on/off attribute, such as `w:fldLock`, has the value that it states.
//!
//! Every reader of an on/off value in this crate calls this module. Compatibility helpers for
//! readers with slightly different legacy value sets are kept here as named functions.

use f_office_xml::{Document, NodeId};

use crate::ns::W;

/// The meaning of an `ST_OnOff` value: on for `1`, `true` and `on`, off for `0`, `false` and
/// `off`, and `None` for any other text, which the type does not hold.
#[must_use]
pub fn read(value: &str) -> Option<bool> {
    match value {
        "1" | "true" | "on" => Some(true),
        "0" | "false" | "off" => Some(false),
        _ => None,
    }
}

/// Whether the on/off element `node` is on: on without a `val` attribute in the namespace
/// `namespace`, off when the value turns it off, and on for any other value.
#[must_use]
pub fn element(doc: &Document, node: NodeId, namespace: &str) -> bool {
    doc.attribute(node, Some(namespace), "val")
        .is_none_or(|value| read(value) != Some(false))
}

/// Whether the WordprocessingML on/off element `node` is on, treating only `0` and `false` as
/// off (so `off` reads as on), matching section and settings readers.
#[must_use]
pub fn off_only_for_0_or_false(doc: &Document, node: NodeId) -> bool {
    !matches!(doc.attribute(node, Some(W), "val"), Some("0" | "false"))
}

/// Whether the WordprocessingML on/off element `node` is on, treating `0`, `false`, `off`, and
/// `none` as off, matching paragraph and run property readers.
#[must_use]
pub fn off_also_for_none(doc: &Document, node: NodeId) -> bool {
    !matches!(
        doc.attribute(node, Some(W), "val"),
        Some("0" | "false" | "off" | "none")
    )
}

/// Whether an on/off attribute value is on, treating only `1` and `true` as on (so `on` reads
/// as off), matching style `w:default` attributes.
#[must_use]
pub fn on_only_for_1_or_true(value: &str) -> bool {
    matches!(value, "1" | "true")
}

#[cfg(test)]
mod tests {
    use f_office_xml::{Document, Limits};

    use super::{element, off_also_for_none, off_only_for_0_or_false, on_only_for_1_or_true, read};
    use crate::ns::{M, W};

    /// The values of the tests: the six of `ST_OnOff`, one outside it, and `none`.
    const VALUES: [&str; 8] = ["1", "true", "on", "0", "false", "off", "yes", "none"];

    /// The first element of `<root>` that holds `child`, in the namespaces `w` and `m`.
    fn element_of(child: &str) -> (Document, f_office_xml::NodeId) {
        let xml = format!(r#"<root xmlns:w="{W}" xmlns:m="{M}">{child}</root>"#);
        let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
        let root = doc.root().unwrap();
        let node = doc.child_elements(root).next().unwrap();
        (doc, node)
    }

    #[test]
    fn reads_each_value_of_the_type() {
        let read_all = VALUES.map(read);
        let on = Some(true);
        let off = Some(false);
        assert_eq!(read_all, [on, on, on, off, off, off, None, None]);
    }

    #[test]
    fn an_element_is_on_unless_its_value_turns_it_off() {
        let (doc, node) = element_of("<w:b/>");
        assert!(element(&doc, node, W));
        let (doc, node) = element_of(r#"<m:show m:val="off"/>"#);
        assert!(!element(&doc, node, M));
        let on = VALUES.map(|value| {
            let (doc, node) = element_of(&format!(r#"<w:b w:val="{value}"/>"#));
            element(&doc, node, W)
        });
        assert_eq!(on, [true, true, true, false, false, false, true, true]);
    }

    #[test]
    fn keeps_the_older_sets_until_step_2() {
        let sets = VALUES.map(|value| {
            let (doc, node) = element_of(&format!(r#"<w:b w:val="{value}"/>"#));
            (
                off_only_for_0_or_false(&doc, node),
                off_also_for_none(&doc, node),
                on_only_for_1_or_true(value),
            )
        });
        let zero_or_false = sets.map(|s| s.0);
        let with_none = sets.map(|s| s.1);
        let one_or_true = sets.map(|s| s.2);
        assert_eq!(
            zero_or_false,
            [true, true, true, false, false, true, true, true]
        );
        assert_eq!(
            with_none,
            [true, true, true, false, false, false, true, false]
        );
        assert_eq!(
            one_or_true,
            [true, true, false, false, false, false, false, false]
        );
        let (doc, node) = element_of("<w:b/>");
        assert!(off_only_for_0_or_false(&doc, node));
        assert!(off_also_for_none(&doc, node));
    }
}