1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::marker::PhantomData;

use crate::{
    attributes::Attribute,
    markers::{Init, Uninit},
    Element, Elements,
};

use super::IntoElements;

/// The `maction` element accepts the global [`Attribute`]s as well as `selection` and
/// `actiontype`.
///
/// [`Attribute`]: crate::attributes::Attribute
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ActionAttr {
    /// The of the global [`Attribute`]s.
    Global(Attribute),

    /// The child element currently visible, only taken into account for `actiontype="toggle"` or
    /// non-standard actiontype values. The default value is `1`, which is the first child element.
    Selection(String),

    /// The action which specifies what happens for this element. Special behavior for the
    /// following values were implemented by some browsers:
    ///
    /// * `statusline`: If there is a click on the expression or the reader moves the pointer over
    ///   it, the message is sent to the browser's status line. The syntax is:
    ///
    ///   ```html
    ///   <maction actiontype="statusline"> expression message </maction>.
    ///   ```
    ///
    /// * `toggle`: When there is a click on the subexpression, the rendering alternates the
    ///    display of selected subexpressions. Therefore each click increments the selection value.
    ///    The syntax is:
    ///
    ///   ```html
    ///   <maction actiontype="toggle" selection="positive-integer">
    ///       expression1 expression2 expressionN
    ///   </maction>.
    ///   ```
    ActionType(String),
}

/// Historically, the `maction` element provides a mechanism for binding actions to expressions.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Action {
    content: Elements,
    attributes: Vec<ActionAttr>,
}

impl From<Elements> for Action {
    fn from(value: Elements) -> Self {
        Self {
            content: value,
            attributes: Default::default(),
        }
    }
}

impl Action {
    /// Create new `maction` element.
    pub fn with_mathml(math: impl IntoElements) -> Self {
        Self {
            content: math.into_elements(),
            attributes: Default::default(),
        }
    }

    /// Create a builder for [`Action`] element.
    pub fn builder() -> ActionBuilder<Uninit> {
        ActionBuilder::default()
    }

    /// Get a reference to the inner content of the [`Action`] element.
    pub fn content(&self) -> &[Element] {
        &self.content
    }

    /// Get a reference to all attributes of the [`Action`] element.
    pub fn attributes(&self) -> &[ActionAttr] {
        &self.attributes
    }
}

crate::element_from_type!(Action => Action);

/// Builder of the [`Action`] element.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ActionBuilder<T> {
    content: Option<Elements>,
    attributes: Vec<ActionAttr>,

    _marker: PhantomData<(T,)>,
}

impl<T> ActionBuilder<T> {
    /// Set the content of the [`Action`] element.
    pub fn content(self, content: impl IntoElements) -> ActionBuilder<Init> {
        ActionBuilder {
            content: Some(content.into_elements()),
            attributes: self.attributes,
            _marker: PhantomData,
        }
    }

    /// Add attributes.
    pub fn attr<I, A>(mut self, attributes: I) -> Self
    where
        I: IntoIterator<Item = A>,
        A: Into<ActionAttr>,
    {
        self.attributes
            .extend(attributes.into_iter().map(Into::into));

        self
    }
}

impl ActionBuilder<Init> {
    /// Build the [`Action`] element.
    pub fn build(self) -> Action {
        Action {
            content: self.content.expect("Content is guaranteed to be init."),
            attributes: self.attributes,
        }
    }
}