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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Utilities that provide limited access to nodes

use rustc_hash::FxHashSet;

use crate::{
    node::{ElementNode, FromAnyValue, NodeType, OwnedAttributeView},
    prelude::AttributeName,
    tags::TagName,
    NodeId,
};

/// A view into a [NodeType] with a mask that determines what is visible.
#[derive(Debug)]
pub struct NodeView<'a, V: FromAnyValue = ()> {
    id: NodeId,
    inner: &'a NodeType<V>,
    mask: &'a NodeMask,
    height: u16,
}

impl<'a, V: FromAnyValue> NodeView<'a, V> {
    /// Create a new NodeView from a VNode, and mask.
    pub fn new(id: NodeId, node: &'a NodeType<V>, view: &'a NodeMask, height: u16) -> Self {
        Self {
            inner: node,
            mask: view,
            id,
            height,
        }
    }

    pub fn node_type(&self) -> &'a NodeType<V> {
        self.inner
    }

    /// Get the node id of the node
    pub fn node_id(&self) -> NodeId {
        self.id
    }

    /// Get the node height
    pub fn height(&self) -> u16 {
        self.height
    }

    /// Get the tag of the node if the tag is enabled in the mask
    pub fn tag(&self) -> Option<&'a TagName> {
        self.mask
            .tag
            .then_some(match &self.inner {
                NodeType::Element(ElementNode { tag, .. }) => Some(tag),
                _ => None,
            })
            .flatten()
    }

    /// Get any attributes that are enabled in the mask
    pub fn attributes<'b>(
        &'b self,
    ) -> Option<impl Iterator<Item = OwnedAttributeView<'a, V>> + 'b> {
        match &self.inner {
            NodeType::Element(ElementNode { attributes, .. }) => Some(
                attributes
                    .iter()
                    .filter(move |(attr, _)| self.mask.attritutes.contains(attr))
                    .map(|(attr, val)| OwnedAttributeView {
                        attribute: attr,
                        value: val,
                    }),
            ),
            _ => None,
        }
    }

    /// Get the text if it is enabled in the mask
    pub fn text(&self) -> Option<&str> {
        self.mask.text.then_some(self.inner.text()).flatten()
    }
}

/// A mask that contains a list of attributes that are visible.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum AttributeMask {
    /// All attributes are visible
    All,
    /// Only the given attributes are visible
    Some(FxHashSet<AttributeName>),
}

impl AttributeMask {
    /// Check if the mask contains the given attribute
    pub fn contains(&self, attr: &AttributeName) -> bool {
        match self {
            AttributeMask::All => true,
            AttributeMask::Some(attrs) => attrs.contains(attr),
        }
    }

    /// Combine two attribute masks
    pub fn union(&self, other: &Self) -> Self {
        match (self, other) {
            (AttributeMask::Some(s), AttributeMask::Some(o)) => {
                AttributeMask::Some(s.union(o).cloned().collect())
            }
            _ => AttributeMask::All,
        }
    }

    /// Check if two attribute masks overlap
    fn overlaps(&self, other: &Self) -> bool {
        match (self, other) {
            (AttributeMask::All, AttributeMask::Some(attrs)) => !attrs.is_empty(),
            (AttributeMask::Some(attrs), AttributeMask::All) => !attrs.is_empty(),
            (AttributeMask::Some(attrs1), AttributeMask::Some(attrs2)) => {
                !attrs1.is_disjoint(attrs2)
            }
            _ => true,
        }
    }
}

impl Default for AttributeMask {
    fn default() -> Self {
        AttributeMask::Some(FxHashSet::default())
    }
}

/// A mask that limits what parts of a node a dependency can see.
#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct NodeMask {
    attritutes: AttributeMask,
    tag: bool,
    text: bool,
    listeners: bool,
}

impl NodeMask {
    /// Check if two masks overlap
    pub fn overlaps(&self, other: &Self) -> bool {
        (self.tag && other.tag)
            || self.attritutes.overlaps(&other.attritutes)
            || (self.text && other.text)
            || (self.listeners && other.listeners)
    }

    /// Combine two node masks
    pub fn union(&self, other: &Self) -> Self {
        Self {
            attritutes: self.attritutes.union(&other.attritutes),
            tag: self.tag | other.tag,
            text: self.text | other.text,
            listeners: self.listeners | other.listeners,
        }
    }

    /// Allow the mask to view the given attributes
    pub fn add_attributes(&mut self, attributes: AttributeMask) {
        self.attritutes = self.attritutes.union(&attributes);
    }

    /// Get the mask for the attributes
    pub fn attributes(&self) -> &AttributeMask {
        &self.attritutes
    }

    /// Set the mask to view the tag
    pub fn set_tag(&mut self) {
        self.tag = true;
    }

    /// Get the mask for the tag
    pub fn tag(&self) -> bool {
        self.tag
    }

    /// Set the mask to view the text
    pub fn set_text(&mut self) {
        self.text = true;
    }

    /// Get the mask for the text
    pub fn text(&self) -> bool {
        self.text
    }

    /// Set the mask to view the listeners
    pub fn set_listeners(&mut self) {
        self.listeners = true;
    }

    /// Get the mask for the listeners
    pub fn listeners(&self) -> bool {
        self.listeners
    }
}

/// A builder for a mask that controls what attributes are visible.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum AttributeMaskBuilder<'a> {
    /// All attributes are visible
    All,
    /// Only the given attributes are visible
    Some(&'a [AttributeName]),
}

impl Default for AttributeMaskBuilder<'_> {
    fn default() -> Self {
        AttributeMaskBuilder::Some(&[])
    }
}

/// A mask that limits what parts of a node a dependency can see.
#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct NodeMaskBuilder<'a> {
    attritutes: AttributeMaskBuilder<'a>,
    tag: bool,
    text: bool,
    listeners: bool,
}

impl<'a> NodeMaskBuilder<'a> {
    /// A node mask with no parts visible.
    pub const NONE: Self = Self::new();
    /// A node mask with every part visible.
    pub const ALL: Self = Self::new()
        .with_attrs(AttributeMaskBuilder::All)
        .with_text()
        .with_tag()
        .with_listeners();

    /// Create a empty node mask
    pub const fn new() -> Self {
        Self {
            attritutes: AttributeMaskBuilder::Some(&[]),
            tag: false,
            text: false,
            listeners: false,
        }
    }

    /// Allow the mask to view the given attributes
    pub const fn with_attrs(mut self, attritutes: AttributeMaskBuilder<'a>) -> Self {
        self.attritutes = attritutes;
        self
    }

    /// Allow the mask to view the tag
    pub const fn with_tag(mut self) -> Self {
        self.tag = true;
        self
    }

    /// Allow the mask to view the text
    pub const fn with_text(mut self) -> Self {
        self.text = true;
        self
    }

    /// Allow the mask to view the listeners
    pub const fn with_listeners(mut self) -> Self {
        self.listeners = true;
        self
    }

    /// Build the mask
    pub fn build(self) -> NodeMask {
        NodeMask {
            attritutes: match self.attritutes {
                AttributeMaskBuilder::All => AttributeMask::All,
                AttributeMaskBuilder::Some(attrs) => {
                    AttributeMask::Some(FxHashSet::from_iter(attrs.iter().copied()))
                }
            },
            tag: self.tag,
            text: self.text,
            listeners: self.listeners,
        }
    }
}