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
mod attributes;
mod capturer;

use super::Mutations;

pub(super) use self::attributes::Attributes;
pub use self::attributes::{Attribute, AttributeNameError};
pub use self::capturer::*;

pub trait Serialize {
    fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8]));
}

macro_rules! impl_serialize {
    ($Token:ident) => {
        impl crate::rewritable_units::Serialize for $Token<'_> {
            #[inline]
            fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
                let Mutations {
                    content_before,
                    replacement,
                    content_after,
                    removed,
                    ..
                } = &self.mutations;

                if !content_before.is_empty() {
                    output_handler(content_before);
                }

                if !removed {
                    match self.raw() {
                        Some(raw) => output_handler(raw),
                        None => self.serialize_from_parts(output_handler),
                    }
                } else if !replacement.is_empty() {
                    output_handler(replacement);
                }

                if !content_after.is_empty() {
                    output_handler(content_after);
                }
            }
        }
    };
}

mod comment;
mod doctype;
mod end_tag;
mod start_tag;
mod text_chunk;

pub use self::comment::{Comment, CommentTextError};
pub use self::doctype::Doctype;
pub use self::end_tag::EndTag;
pub use self::start_tag::StartTag;
pub use self::text_chunk::TextChunk;

#[derive(Debug)]
pub enum Token<'i> {
    TextChunk(TextChunk<'i>),
    Comment(Comment<'i>),
    StartTag(StartTag<'i>),
    EndTag(EndTag<'i>),
    Doctype(Doctype<'i>),
}

impl Serialize for Token<'_> {
    #[inline]
    fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
        match self {
            Token::TextChunk(t) => t.to_bytes(output_handler),
            Token::Comment(t) => t.to_bytes(output_handler),
            Token::StartTag(t) => t.to_bytes(output_handler),
            Token::EndTag(t) => t.to_bytes(output_handler),
            Token::Doctype(t) => t.to_bytes(output_handler),
        }
    }
}