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
280
281
282
283
284
285
286
use std::collections::HashMap;

use super::html_scanner::TokenType;

#[derive(Debug, Clone)]
pub struct Node {
    /// It's None only when new
    pub tag: Option<String>,
    pub start: usize,
    pub end: usize,
    pub children: Vec<Node>,
    /// Whether part of end tag exists
    pub closed: bool,
    /// It's None only when new, it larger than end of start tag
    pub start_tag_end: Option<usize>,
    /// It's None only when it's self-closing tag or it miss part of end tag, it equals start of end tag
    pub end_tag_start: Option<usize>,
    pub attributes: HashMap<String, NodeAttribute>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct NodeAttribute {
    /// include quote
    pub value: Option<String>,
    /// start offset of attribute name
    pub offset: usize,
}

impl NodeAttribute {
    pub fn new(value: Option<String>, offset: usize) -> NodeAttribute {
        NodeAttribute { value, offset }
    }
}

impl Node {
    pub fn new(start: usize, end: usize, children: Vec<Node>) -> Node {
        Node {
            tag: None,
            start,
            end,
            children,
            closed: false,
            start_tag_end: None,
            end_tag_start: None,
            attributes: HashMap::new(),
        }
    }

    pub fn attribute_names(&self) -> Vec<&String> {
        self.attributes.keys().collect()
    }

    pub fn attribute_names_by_order(&self) -> Vec<&String> {
        let mut attributes = self.attribute_names();
        attributes.sort_by(|a, b| {
            let a = self.attributes.get(*a).unwrap().offset;
            let b = self.attributes.get(*b).unwrap().offset;
            a.cmp(&b)
        });
        attributes
    }

    pub fn is_self_closing(&self) -> bool {
        self.end_tag_start.is_none()
    }

    pub fn is_same_tag(&self, tag_in_lowercase: Option<&str>) -> bool {
        if self.tag.is_none() {
            tag_in_lowercase.is_none()
        } else {
            let tag: &str = &self.tag.as_ref().unwrap();
            tag_in_lowercase.is_some_and(|tag_in_lowercase| {
                tag.len() == tag_in_lowercase.len() && tag.to_lowercase() == tag_in_lowercase
            })
        }
    }

    pub fn first_child(&self) -> Option<&Node> {
        Some(self.children.first()?)
    }

    pub fn last_child(&self) -> Option<&Node> {
        Some(self.children.last()?)
    }

    pub fn find_node_before<'a>(
        node: &'a Node,
        offset: usize,
        parent_list: &mut Vec<&'a Node>,
    ) -> &'a Node {
        let mut idx = node.children.len();
        for (i, child) in node.children.iter().enumerate() {
            if offset <= child.start {
                idx = i;
                break;
            }
        }
        if idx > 0 {
            let child = &node.children[idx - 1];
            if offset > child.start {
                if offset < child.end {
                    parent_list.push(&node);
                    return Node::find_node_before(child, offset, parent_list);
                }
                if let Some(last_child) = child.last_child() {
                    if last_child.end == child.end {
                        parent_list.push(&node);
                        return Node::find_node_before(child, offset, parent_list);
                    }
                }
                parent_list.push(&node);
                return child;
            }
        }
        node
    }

    pub fn find_node_at<'a>(
        node: &'a Node,
        offset: usize,
        parent_list: &mut Vec<&'a Node>,
    ) -> &'a Node {
        let mut idx = node.children.len();
        for (i, child) in node.children.iter().enumerate() {
            if offset < child.start {
                idx = i;
                break;
            }
        }

        if idx > 0 {
            let child = &node.children[idx - 1];
            if offset >= child.start && offset < child.end {
                parent_list.push(&node);
                return Node::find_node_at(child, offset, parent_list);
            }
        }
        node
    }

    /// Find TokenType in node at offset
    ///
    /// it return StartTagOpen, StartTag, StartTagClose, StartTagSelfClose, Content, EndTagOpen, EndTag, EndTagClose, Unknown
    ///
    /// if offset in children, then it's Content
    /// if offset outside of node then it's Unknown
    pub fn find_token_type_in_node(node: &Node, offset: usize) -> TokenType {
        if node.start > offset || node.end <= offset {
            return TokenType::Unknown;
        }
        let tag = node.tag.as_ref().unwrap();
        if node.start == offset {
            return TokenType::StartTagOpen;
        }
        if offset < node.start + 1 + tag.len() {
            return TokenType::StartTag;
        }
        let start_tag_end = *node.start_tag_end.as_ref().unwrap();
        if offset >= start_tag_end {
            if let Some(end_tag_start) = node.end_tag_start {
                if offset < end_tag_start {
                    return TokenType::Content;
                } else if offset == end_tag_start || offset == end_tag_start + 1 {
                    return TokenType::EndTagOpen;
                } else if offset < node.end - 1 {
                    return TokenType::EndTag;
                } else {
                    return TokenType::EndTagClose;
                }
            } else if start_tag_end == node.end {
                if offset >= node.end - 2 {
                    return TokenType::StartTagSelfClose;
                }
            }
        } else {
            if start_tag_end == node.end {
                if offset >= start_tag_end - 2 {
                    return TokenType::StartTagSelfClose;
                }
            } else {
                if offset >= start_tag_end - 1 {
                    return TokenType::StartTagClose;
                }
            }
        }
        TokenType::Unknown
    }
}

/// A tree of nodes for an HTML document
///
/// There is no reference to the parent node in the Node.
/// The associated functions `find_node_before` and `find_node_at` keep a record of all parents of the target node.
/// To get the parent node of the target node, you can like this:
///
/// ```rust
/// use html_languageservice::{parse_html_document, HTMLDataManager};
///
/// let html_document = parse_html_document("<div><h1>title</h1></div>", "html", &HTMLDataManager::default());
///
/// let mut parent_list = vec![];
/// let node = html_document.find_node_at(9, &mut parent_list);
/// assert_eq!(node.unwrap().tag, Some("h1".to_string()));
///
/// let parent = parent_list.pop();
/// assert_eq!(parent.unwrap().tag, Some("div".to_string()));
///
/// let parent = parent_list.pop();
/// assert!(parent.is_none());
/// ```
///
/// If 'parent' is 'None', then its parent node is HTMLDocument.
#[derive(Clone)]
pub struct HTMLDocument {
    pub roots: Vec<Node>,
}

impl HTMLDocument {
    /// Find the node before the node where the given 'offset' is located
    ///
    /// `parent_list` is a list of parent nodes and the previous node is the parent node of the latter node.
    /// If you don't care about the parent node, you can use `&mut vec![]`.
    pub fn find_node_before<'a>(
        &'a self,
        offset: usize,
        parent_list: &mut Vec<&'a Node>,
    ) -> Option<&'a Node> {
        let mut idx = self.roots.len();
        for (i, child) in self.roots.iter().enumerate() {
            if offset <= child.start {
                idx = i;
                break;
            }
        }
        if idx > 0 {
            let child = &self.roots[idx - 1];
            if offset > child.start {
                if offset < child.end {
                    return Some(Node::find_node_before(child, offset, parent_list));
                }
                if let Some(last_child) = child.last_child() {
                    if last_child.end == child.end {
                        return Some(Node::find_node_before(child, offset, parent_list));
                    }
                }
                return Some(child);
            }
        }
        None
    }

    /// Find the node at the given 'offset' location
    ///
    /// `parent_list` is a list where the previous node is the parent node of the latter node.
    /// If you don't care about the parent node, you can use `&mut vec![]`.
    pub fn find_node_at<'a>(
        &'a self,
        offset: usize,
        parent_list: &mut Vec<&'a Node>,
    ) -> Option<&'a Node> {
        let mut idx = self.roots.len();
        for (i, child) in self.roots.iter().enumerate() {
            if offset < child.start {
                idx = i;
                break;
            }
        }

        if idx > 0 {
            let child = &self.roots[idx - 1];
            if offset >= child.start && offset < child.end {
                return Some(Node::find_node_at(child, offset, parent_list));
            }
        }
        None
    }

    pub fn find_root_at(&self, offset: usize) -> Option<&Node> {
        for root in &self.roots {
            if offset <= root.end {
                return Some(root);
            }
        }
        None
    }
}