summaryrefslogtreecommitdiff
path: root/src/emitter.rs
blob: ff6e8631a83cf21b69a73533144c6d58ce884a0f (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
use std::collections::btree_map::Entry;
use std::collections::BTreeSet;
use std::collections::VecDeque;
use std::mem;
use std::ops::Range;

use crate::attr::AttrValueSyntax;
use crate::offset::NoopOffset;
use crate::offset::Offset;
use crate::Error;

/// An emitter is an object providing methods to the tokenizer to produce ("emit") tokens.
///
/// Domain-specific applications of the HTML tokenizer can manually implement this trait to
/// customize per-token allocations, or avoid them altogether.
///
/// An emitter is assumed to have these internal states:
///
/// * _current token_: Can be a tag, doctype or comment token. There's only one current token.
/// * _current attribute_: The currently processed HTML attribute, consisting of two strings for name and value.
///
/// The following methods are describing what kind of behavior the WHATWG spec expects, but that
/// doesn't mean you need to follow it. For example:
///
/// * If your usage of the tokenizer will ignore all errors, none of the error handling and
///   validation requirements apply to you. You can implement `emit_error` as noop and omit all
///   checks that would emit errors.
///
/// * If you don't care about attributes at all, you can make all related methods a noop.

#[allow(unused_variables)] // workaround for https://github.com/rust-lang/rust/issues/91074
pub trait Emitter<O> {
    /// The token type emitted by this emitter. This controls what type of values the [`Tokenizer`](crate::Tokenizer)
    /// yields when used as an iterator.
    type Token;

    /// The state machine has reached the end of the file. It will soon call `pop_token` for the
    /// last time.
    fn emit_eof(&mut self);

    /// A (probably recoverable) parsing error has occurred.
    fn emit_error(&mut self, error: Error, span: Range<O>);

    /// After every state change, the tokenizer calls this method to retrieve a new token that can
    /// be returned via the tokenizer's iterator interface.
    fn pop_token(&mut self) -> Option<Self::Token>;

    /// Emit a bunch of plain characters as character tokens.
    fn emit_string(&mut self, c: &str);

    /// Set the _current token_ to a start tag.
    fn init_start_tag(&mut self, offset: O);

    /// Set the _current token_ to an end tag.
    fn init_end_tag(&mut self, offset: O);

    /// Set the _current token_ to a comment.
    fn init_comment(&mut self, data_offset: O);

    /// Emit the _current token_, assuming it is a tag.
    ///
    /// Also get the current attribute and append it to the to-be-emitted tag. See docstring for
    /// [`Emitter::init_attribute_name`] for how duplicates should be handled.
    ///
    /// If an end tag is emitted with attributes, an [`Error::EndTagWithAttributes`]
    /// error should be emitted.
    ///
    /// If the current token is not a start/end tag, this method may panic.
    fn emit_current_tag(&mut self, offset: O);

    /// Emit the _current token_, assuming it is a comment.
    ///
    /// If the current token is not a comment, this method may panic.
    fn emit_current_comment(&mut self, offset: O);

    /// Emit the _current token_, assuming it is a doctype.
    ///
    /// If the current token is not a doctype, this method may panic.
    fn emit_current_doctype(&mut self, offset: O);

    /// Called after the last [`push_attribute_value`] call for an attribute value.
    ///
    /// [`push_attribute_value`]: Self::push_attribute_value
    fn terminate_attribute_value(&mut self, offset: O) {}

    /// Assuming the _current token_ is a start tag, set the self-closing flag.
    ///
    /// If the current token is not a start or end tag, this method may panic.
    ///
    /// If the current token is an end tag, the emitter should emit the
    /// [`Error::EndTagWithTrailingSolidus`] error.
    fn set_self_closing(&mut self, slash_offset: O);

    /// Assuming the _current token_ is a doctype, set its "force quirks" flag to true.
    ///
    /// If the current token is not a doctype, this method pay panic.
    fn set_force_quirks(&mut self);

    /// Assuming the _current token_ is a start/end tag, append a string to the current tag's name.
    ///
    /// If the current token is not a start or end tag, this method may panic.
    fn push_tag_name(&mut self, s: &str);

    /// Assuming the _current token_ is a comment, append a string to the comment's contents.
    ///
    /// If the current token is not a comment, this method may panic.
    fn push_comment(&mut self, s: &str);

    /// Assuming the _current token_ is a doctype, append a string to the doctype's name.
    ///
    /// If the current token is not a doctype, this method may panic.
    fn push_doctype_name(&mut self, s: &str);

    /// Set the _current token_ to a new doctype token:
    ///
    /// * the name should be empty
    /// * the "public identifier" should be null (different from empty)
    /// * the "system identifier" should be null (different from empty)
    /// * the "force quirks" flag should be `false`
    fn init_doctype(&mut self, offset: O);

    /// Set the _current attribute_ to a new one, starting with empty name and value strings.
    ///
    /// The old attribute, if any, should be put on the _current token_. If an attribute with that
    /// name already exists, WHATWG says the new one should be ignored and a
    /// [`Error::DuplicateAttribute`] error should be emitted.
    ///
    /// If the current token is no tag at all, this method may panic.
    fn init_attribute_name(&mut self, offset: O);

    /// Called before the first push_attribute_value call.
    ///
    /// If there is no current attribute, this method may panic.
    fn init_attribute_value(&mut self, syntax: AttrValueSyntax, offset: O) {}

    /// Append a string to the current attribute's name.
    ///
    /// If there is no current attribute, this method may panic.
    fn push_attribute_name(&mut self, s: &str);

    /// Append a string to the current attribute's value.
    ///
    /// If there is no current attribute, this method may panic.
    fn push_attribute_value(&mut self, s: &str);

    /// Assuming the _current token_ is a doctype, set its "public identifier" to the empty string.
    ///
    /// If the current token is not a doctype, this method may panic.
    fn init_doctype_public_id(&mut self, offset: O);

    /// Assuming the _current token_ is a doctype, set its "system identifier" to the empty string.
    ///
    /// If the current token is not a doctype, this method may panic.
    fn init_doctype_system_id(&mut self, offset: O);

    /// Assuming the _current token_ is a doctype, append a string to its "public identifier" to the given string.
    ///
    /// If the current token is not a doctype, this method may panic.
    fn push_doctype_public_id(&mut self, s: &str);

    /// Assuming the _current token_ is a doctype, append a string to its "system identifier" to the given string.
    ///
    /// If the current token is not a doctype, this method may panic.
    fn push_doctype_system_id(&mut self, s: &str);
}

/// The default implementation of [`Emitter`], used to produce tokens.
///
/// # Warning
///
/// * Using the DefaultEmitter without calling [`Tokenizer::set_state`]
///   results in wrong state transitions:
///
///   ```
///   # use html5tokenizer::{DefaultEmitter, Event, Tokenizer, Token};
///   let emitter = DefaultEmitter::default();
///   let html = "<script><b>";
///   let mut tokens = Tokenizer::new(html, emitter).flatten();
///   assert!(matches!(tokens.next().unwrap(), Event::Token(Token::StartTag(tag)) if tag.name == "script"));
///   assert!(matches!(tokens.next().unwrap(), Event::Token(Token::StartTag(tag)) if tag.name == "b"));
///   ```
///
/// [`Tokenizer::set_state`]: crate::Tokenizer::set_state
pub struct DefaultEmitter<O = NoopOffset> {
    current_characters: String,
    current_token: Option<Token<O>>,
    current_attribute: Option<(String, crate::attr::AttrInternal<O>)>,
    seen_attributes: BTreeSet<String>,
    emitted_tokens: VecDeque<Token<O>>,
    attr_in_end_tag_span: Option<Range<O>>,
}

impl<O> Default for DefaultEmitter<O> {
    fn default() -> Self {
        DefaultEmitter {
            current_characters: String::new(),
            current_token: None,
            current_attribute: None,
            seen_attributes: BTreeSet::new(),
            emitted_tokens: VecDeque::new(),
            attr_in_end_tag_span: None,
        }
    }
}

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

    fn flush_current_attribute(&mut self)
    where
        O: Offset,
    {
        if let Some((name, map_val)) = self.current_attribute.take() {
            match self.current_token {
                Some(Token::StartTag(ref mut tag)) => match tag.attributes.inner.entry(name) {
                    Entry::Vacant(vacant) => {
                        vacant.insert(map_val);
                    }
                    Entry::Occupied(entry) => {
                        let name_len = entry.key().len();
                        self.push_error(Error::DuplicateAttribute, map_val.name_span(name_len));
                    }
                },
                Some(Token::EndTag(_)) => {
                    let name_span = map_val.name_span(name.len());
                    self.attr_in_end_tag_span = Some(name_span.clone());
                    if !self.seen_attributes.insert(name) {
                        self.push_error(Error::DuplicateAttribute, name_span);
                    }
                }
                _ => {
                    debug_assert!(false);
                }
            }
        }
    }

    fn flush_current_characters(&mut self) {
        if self.current_characters.is_empty() {
            return;
        }

        let s = mem::take(&mut self.current_characters);
        self.emit_token(Token::String(s));
    }

    fn push_error(&mut self, error: Error, span: Range<O>) {
        // bypass character flushing in self.emit_token: we don't need the error location to be
        // that exact
        self.emitted_tokens.push_front(Token::Error { error, span });
    }
}

impl<O: Offset> Emitter<O> for DefaultEmitter<O> {
    type Token = Token<O>;

    fn emit_eof(&mut self) {
        self.flush_current_characters();
    }

    fn emit_error(&mut self, error: Error, span: Range<O>) {
        self.push_error(error, span);
    }

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

    fn emit_string(&mut self, s: &str) {
        self.current_characters.push_str(s);
    }

    fn init_start_tag(&mut self, offset: O) {
        self.current_token = Some(Token::StartTag(StartTag {
            span: offset..O::default(),
            self_closing: false,
            name: String::new(),
            attributes: Default::default(),
        }));
    }
    fn init_end_tag(&mut self, offset: O) {
        self.current_token = Some(Token::EndTag(EndTag {
            span: offset..O::default(),
            name: String::new(),
        }));
        self.seen_attributes.clear();
    }

    fn init_comment(&mut self, data_offset: O) {
        self.current_token = Some(Token::Comment(Comment {
            data: String::new(),
            data_offset,
        }));
    }
    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(tag) => {
                if !self.seen_attributes.is_empty() {
                    let span = self.attr_in_end_tag_span.take().unwrap();
                    self.push_error(Error::EndTagWithAttributes, span);
                }
                self.seen_attributes.clear();
                tag.span.end = offset;
            }
            Token::StartTag(tag) => {
                tag.span.end = offset;
            }
            _ => debug_assert!(false),
        }
        self.emit_token(token);
    }
    fn emit_current_comment(&mut self, _offset: O) {
        let comment = self.current_token.take().unwrap();
        debug_assert!(matches!(comment, Token::Comment(_)));
        self.emit_token(comment);
    }

    fn emit_current_doctype(&mut self, offset: O) {
        let Some(Token::Doctype(mut doctype)) = self.current_token.take() else {
            debug_assert!(false);
            return;
        };
        doctype.span.end = offset;
        self.emit_token(Token::Doctype(doctype));
    }

    fn set_self_closing(&mut self, slash_offset: O) {
        let tag = self.current_token.as_mut().unwrap();
        match tag {
            Token::StartTag(StartTag {
                ref mut self_closing,
                ..
            }) => {
                *self_closing = true;
            }
            Token::EndTag(_) => {
                self.emit_error(
                    Error::EndTagWithTrailingSolidus,
                    slash_offset..slash_offset + 1,
                );
            }
            _ => {
                debug_assert!(false);
            }
        }
    }
    fn set_force_quirks(&mut self) {
        match self.current_token {
            Some(Token::Doctype(ref mut doctype)) => doctype.force_quirks = true,
            _ => debug_assert!(false),
        }
    }
    fn push_tag_name(&mut self, s: &str) {
        match self.current_token {
            Some(Token::StartTag(StartTag { ref mut name, .. })) => {
                name.push_str(s);
            }
            Some(Token::EndTag(EndTag { ref mut name, .. })) => {
                name.push_str(s);
            }
            _ => debug_assert!(false),
        }
    }

    fn push_comment(&mut self, s: &str) {
        match self.current_token {
            Some(Token::Comment(Comment { ref mut data, .. })) => data.push_str(s),
            _ => debug_assert!(false),
        }
    }

    fn push_doctype_name(&mut self, s: &str) {
        match self.current_token {
            Some(Token::Doctype(ref mut doctype)) => doctype.name.push_str(s),
            _ => debug_assert!(false),
        }
    }
    fn init_doctype(&mut self, offset: O) {
        self.current_token = Some(Token::Doctype(Doctype {
            name: String::new(),
            force_quirks: false,
            public_id: None,
            system_id: None,
            span: offset..O::default(),
            public_id_offset: O::default(),
            system_id_offset: O::default(),
        }));
    }

    fn init_attribute_name(&mut self, offset: O) {
        self.flush_current_attribute();
        self.current_attribute = Some((
            String::new(),
            crate::attr::AttrInternal {
                name_offset: offset,
                value: String::new(),
                value_span: O::default()..O::default(),
                value_syntax: None,
            },
        ));
    }
    fn init_attribute_value(&mut self, syntax: AttrValueSyntax, offset: O) {
        let (_, current_attribute) = self.current_attribute.as_mut().unwrap();
        current_attribute.value_span.start = offset;
        current_attribute.value_syntax = Some(syntax);
    }

    fn push_attribute_name(&mut self, s: &str) {
        let current_attr = self.current_attribute.as_mut().unwrap();
        current_attr.0.push_str(s);
    }
    fn push_attribute_value(&mut self, s: &str) {
        let current_attr = self.current_attribute.as_mut().unwrap();
        current_attr.1.value.push_str(s);
    }

    fn terminate_attribute_value(&mut self, offset: O) {
        let current_attr = self.current_attribute.as_mut().unwrap();
        current_attr.1.value_span.end = offset;
    }

    fn init_doctype_public_id(&mut self, offset: O) {
        let Some(Token::Doctype(doctype)) = &mut self.current_token else {
            debug_assert!(false);
            return;
        };
        doctype.public_id = Some("".to_owned());
        doctype.public_id_offset = offset;
    }
    fn init_doctype_system_id(&mut self, offset: O) {
        let Some(Token::Doctype(doctype)) = &mut self.current_token else {
            debug_assert!(false);
            return;
        };
        doctype.system_id = Some("".to_owned());
        doctype.system_id_offset = offset;
    }
    fn push_doctype_public_id(&mut self, s: &str) {
        if let Some(Token::Doctype(Doctype {
            public_id: Some(ref mut id),
            ..
        })) = self.current_token
        {
            id.push_str(s);
        } else {
            debug_assert!(false);
        }
    }
    fn push_doctype_system_id(&mut self, s: &str) {
        if let Some(Token::Doctype(Doctype {
            system_id: Some(ref mut id),
            ..
        })) = self.current_token
        {
            id.push_str(s);
        } else {
            debug_assert!(false);
        }
    }
}

/// An HTML start tag, such as `<p>` or `<a>`.
#[derive(Debug, Eq, PartialEq)]
pub struct StartTag<O> {
    /// Whether this tag is self-closing. If it is self-closing, no following [`EndTag`] should be
    /// expected.
    pub self_closing: bool,

    /// The tag name.
    /// Uppercase ASCII characters (A-Z) have been converted to lowercase.
    pub name: String,

    /// A mapping for any HTML attributes this start tag may have.
    ///
    /// Duplicate attributes are ignored after the first one as per WHATWG spec. Implement your own
    /// [`Emitter`] to tweak this behavior.
    pub attributes: crate::attr::AttributeMap<O>,

    /// The source code span of the tag.
    pub span: Range<O>,
}

impl<O: Offset> StartTag<O> {
    /// Calculates the span for the tag name and returns it.
    pub fn name_span(&self) -> Range<O> {
        let start = self.span.start + b"<".len();
        start..start + self.name.len()
    }
}

/// An HTML end/close tag, such as `</p>` or `</a>`.
#[derive(Debug, Eq, PartialEq)]
pub struct EndTag<O> {
    /// The tag name.
    /// Uppercase ASCII characters (A-Z) have been converted to lowercase.
    pub name: String,

    /// The source code span of the tag.
    pub span: Range<O>,
}

impl<O: Offset> EndTag<O> {
    /// Calculates the span for the tag name and returns it.
    pub fn name_span(&self) -> Range<O> {
        let start = self.span.start + b"</".len();
        start..start + self.name.len()
    }
}

/// An HTML comment.
#[derive(PartialEq, Eq, Debug)]
pub struct Comment<O> {
    /// The text within the comment.
    pub data: String,
    /// The source offset of the comment data.
    pub data_offset: O,
}

impl<O: Offset> Comment<O> {
    /// Calculates the span for the comment data and returns it.
    pub fn data_span(&self) -> Range<O> {
        self.data_offset..self.data_offset + self.data.len()
    }
}

/// A doctype. Some examples:
///
/// * `<!DOCTYPE {name}>`
/// * `<!DOCTYPE {name} PUBLIC '{public_id}'>`
/// * `<!DOCTYPE {name} SYSTEM '{system_id}'>`
/// * `<!DOCTYPE {name} PUBLIC '{public_id}' '{system_id}'>`
#[derive(Debug, Eq, PartialEq)]
pub struct Doctype<O> {
    /// The [force-quirks flag].
    ///
    /// [force-quirks flag]: https://html.spec.whatwg.org/multipage/parsing.html#force-quirks-flag
    pub force_quirks: bool,

    /// The doctype's name. Uppercase ASCII characters (A-Z) have been
    /// converted to lowercase. For HTML documents this should be "html".
    pub name: String,

    /// The doctype's public identifier.
    pub public_id: Option<String>,

    /// The doctype's system identifier.
    pub system_id: Option<String>,

    /// The source code span of the doctype.
    pub span: Range<O>,

    /// The source offset of the public identifier.
    public_id_offset: O,

    /// The source offset of the system identifier.
    system_id_offset: O,
}

impl<O: Offset> Doctype<O> {
    /// Calculates the span of the public identifier and returns it.
    pub fn public_id_span(&self) -> Option<Range<O>> {
        let public_id = self.public_id.as_ref()?;
        Some(self.public_id_offset..self.public_id_offset + public_id.len())
    }

    /// Calculates the span of the system identifier and returns it.
    pub fn system_id_span(&self) -> Option<Range<O>> {
        let system_id = self.system_id.as_ref()?;
        Some(self.system_id_offset..self.system_id_offset + system_id.len())
    }
}

/// The token type used by default. You can define your own token type by implementing the
/// [`Emitter`] trait.
#[derive(Debug, Eq, PartialEq)]
pub enum Token<O> {
    /// An HTML start tag.
    StartTag(StartTag<O>),
    /// An HTML end tag.
    EndTag(EndTag<O>),
    /// A literal string. Character references have been resolved.
    String(String),
    /// An HTML comment.
    Comment(Comment<O>),
    /// An HTML doctype declaration.
    Doctype(Doctype<O>),
    /// An HTML parsing error.
    ///
    /// Can be skipped over, the tokenizer is supposed to recover from the error and continues with
    /// more tokens afterward.
    Error {
        /// What kind of error occurred.
        error: Error,
        /// The source code span of the error.
        span: Range<O>,
    },
}

/// The majority of our testing of the [`DefaultEmitter`] is done against the
/// html5lib-tests in the html5lib integration test. This module only tests
/// details that aren't present in the html5lib test data.
#[cfg(test)]
mod tests {
    use super::{DefaultEmitter, Token};
    use crate::{attr::AttrValueSyntax, Event, Tokenizer};

    #[test]
    fn test_attribute_value_syntax() {
        let mut tokenizer = Tokenizer::new(
            "<div empty unquoted=foo single-quoted='foo' double-quoted=\"foo\">",
            DefaultEmitter::default(),
        )
        .flatten();
        let Event::Token(Token::StartTag(start_tag)) = tokenizer.next().unwrap() else {
            panic!("expected start tag");
        };
        assert_eq!(
            start_tag.attributes.get("empty").unwrap().value_syntax(),
            None
        );
        assert_eq!(
            start_tag.attributes.get("unquoted").unwrap().value_syntax(),
            Some(AttrValueSyntax::Unquoted)
        );
        assert_eq!(
            start_tag
                .attributes
                .get("single-quoted")
                .unwrap()
                .value_syntax(),
            Some(AttrValueSyntax::SingleQuoted)
        );
        assert_eq!(
            start_tag
                .attributes
                .get("double-quoted")
                .unwrap()
                .value_syntax(),
            Some(AttrValueSyntax::DoubleQuoted)
        );
    }
}