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
use std::collections::btree_map::Entry;
use std::collections::BTreeSet;
use std::collections::VecDeque;
use std::ops::Range;

use crate::let_else::assume;
use crate::offset::NoopOffset;
use crate::offset::Offset;
use crate::token::{Doctype, EndTag, StartTag, Token};
use crate::Emitter;
use crate::Error;

/// An [`Emitter`] implementation that yields [`Token`].
pub struct BasicEmitter<O = NoopOffset> {
    current_token: Option<Token>,
    current_attribute_name: String,
    current_attr_internal: crate::token::AttrInternal,
    seen_attributes: BTreeSet<String>,
    emitted_tokens: VecDeque<Token>,
    errors: VecDeque<(Error, Range<O>)>,
    attr_name_span: Range<O>,
}

impl<O: Default> Default for BasicEmitter<O> {
    fn default() -> Self {
        BasicEmitter {
            current_token: None,
            current_attribute_name: String::new(),
            current_attr_internal: Default::default(),
            seen_attributes: BTreeSet::new(),
            emitted_tokens: VecDeque::new(),
            errors: VecDeque::new(),
            attr_name_span: Default::default(),
        }
    }
}

impl<O> BasicEmitter<O> {
    /// Removes all encountered tokenizer errors and returns them as an iterator.
    pub fn drain_errors(&mut self) -> impl Iterator<Item = (Error, Range<O>)> + '_ {
        self.errors.drain(0..)
    }
}

impl<O> Iterator for BasicEmitter<O> {
    type Item = Token;

    fn next(&mut self) -> Option<Self::Item> {
        self.emitted_tokens.pop_back()
    }
}

#[allow(unused_variables)]
impl<O: Offset> Emitter<O> for BasicEmitter<O> {
    fn report_error(&mut self, error: Error, span: Range<O>) {
        self.errors.push_back((error, span));
    }

    fn emit_char(&mut self, c: char, span: Range<O>) {
        self.emit_token(Token::Char(c));
    }

    fn emit_eof(&mut self, offset: O) {
        self.emit_token(Token::EndOfFile);
    }

    fn init_start_tag(&mut self, tag_offset: O, name_offset: O) {
        self.current_token = Some(Token::StartTag(StartTag {
            self_closing: false,
            name: String::new(),
            attributes: Default::default(),
        }));
    }

    fn init_end_tag(&mut self, tag_offset: O, name_offset: O) {
        self.current_token = Some(Token::EndTag(EndTag {
            name: String::new(),
        }));
        self.seen_attributes.clear();
    }

    fn push_tag_name(&mut self, s: &str) {
        assume!(
            Some(Token::StartTag(StartTag { name, .. }) | Token::EndTag(EndTag { name, .. })),
            &mut self.current_token
        );
        name.push_str(s);
    }

    fn init_attribute_name(&mut self, offset: O) {
        self.flush_current_attribute();
        self.attr_name_span.start = offset;
    }

    fn push_attribute_name(&mut self, s: &str) {
        self.current_attribute_name.push_str(s);
    }

    fn terminate_attribute_name(&mut self, offset: O) {
        self.attr_name_span.end = offset;
    }

    fn push_attribute_value(&mut self, s: &str) {
        self.current_attr_internal.value.push_str(s);
    }

    fn set_self_closing(&mut self, slash_span: Range<O>) {
        let token = self.current_token.as_mut().unwrap();

        match token {
            Token::StartTag(tag) => {
                tag.self_closing = true;
            }
            Token::EndTag(_) => {
                self.report_error(Error::EndTagWithTrailingSolidus, slash_span);
            }
            other => debug_assert!(false, "unexpected current_token: {other:?}"),
        }
    }

    fn emit_current_tag(&mut self, offset: O) {
        self.flush_current_attribute();
        let mut token = self.current_token.take().unwrap();
        match &mut token {
            Token::EndTag(_) => {
                if !self.seen_attributes.is_empty() {
                    self.report_error(Error::EndTagWithAttributes, self.attr_name_span.clone());
                }
                self.seen_attributes.clear();
            }
            Token::StartTag(_) => {}
            other => {
                debug_assert!(false, "unexpected current_token: {other:?}");
                return;
            }
        }
        self.emit_token(token);
    }

    fn init_comment(&mut self, data_start_offset: O) {
        self.current_token = Some(Token::Comment(String::new()));
    }

    fn push_comment(&mut self, s: &str) {
        assume!(Some(Token::Comment(data)), &mut self.current_token);
        data.push_str(s);
    }

    fn emit_current_comment(&mut self, data_end_offset: O) {
        let token = self.current_token.take().unwrap();
        self.emit_token(token);
    }

    fn init_doctype(&mut self, offset: O) {
        self.current_token = Some(Token::Doctype(Doctype {
            name: None,
            force_quirks: false,
            public_id: None,
            system_id: None,
        }));
    }

    fn init_doctype_name(&mut self, offset: O) {
        assume!(Some(Token::Doctype(doctype)), &mut self.current_token);
        doctype.name = Some("".into());
    }

    fn push_doctype_name(&mut self, s: &str) {
        assume!(
            Some(Token::Doctype(Doctype {
                name: Some(name),
                ..
            })),
            &mut self.current_token
        );
        name.push_str(s);
    }

    fn init_doctype_public_id(&mut self, offset: O) {
        assume!(Some(Token::Doctype(doctype)), &mut self.current_token);
        doctype.public_id = Some("".to_owned());
    }

    fn push_doctype_public_id(&mut self, s: &str) {
        assume!(
            Some(Token::Doctype(Doctype {
                public_id: Some(public_id),
                ..
            })),
            &mut self.current_token
        );
        public_id.push_str(s);
    }

    fn init_doctype_system_id(&mut self, offset: O) {
        assume!(Some(Token::Doctype(doctype)), &mut self.current_token);
        doctype.system_id = Some("".to_owned());
    }

    fn push_doctype_system_id(&mut self, s: &str) {
        assume!(
            Some(Token::Doctype(Doctype {
                system_id: Some(id),
                ..
            })),
            &mut self.current_token
        );
        id.push_str(s);
    }

    fn set_force_quirks(&mut self) {
        assume!(Some(Token::Doctype(doctype)), &mut self.current_token);
        doctype.force_quirks = true;
    }

    fn emit_current_doctype(&mut self, offset: O) {
        let token = self.current_token.take().unwrap();
        self.emit_token(token);
    }
}

impl<O> BasicEmitter<O> {
    fn emit_token(&mut self, token: Token) {
        self.emitted_tokens.push_front(token);
    }

    fn flush_current_attribute(&mut self)
    where
        O: Offset,
    {
        if self.current_attribute_name.is_empty() {
            return;
        }
        let name = std::mem::take(&mut self.current_attribute_name);
        let attr_internal = std::mem::take(&mut self.current_attr_internal);

        match &mut self.current_token {
            Some(Token::StartTag(tag)) => match tag.attributes.inner.entry(name) {
                Entry::Vacant(vacant) => {
                    vacant.insert(attr_internal);
                }
                Entry::Occupied(_) => {
                    self.report_error(Error::DuplicateAttribute, self.attr_name_span.clone());
                }
            },
            Some(Token::EndTag(_)) => {
                if !self.seen_attributes.insert(name) {
                    self.report_error(Error::DuplicateAttribute, self.attr_name_span.clone());
                }
            }
            other => debug_assert!(false, "unexpected current_token: {other:?}"),
        }
    }
}