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
mod display;

use crate::HtmlElement;
use std::fmt::{Display, Formatter};

#[derive(Clone, PartialEq, Eq)]
pub enum HtmlNode {
    /// A doctype.
    Doctype(DocType),
    /// A comment.
    Comment(String),
    /// Text.
    Text(String),
    /// An element.
    Element(HtmlElement),
    /// A processing instruction.
    ProcessingInstruction(ProcessingInstruction),
}

impl Display for HtmlNode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            HtmlNode::Doctype(_) => write!(f, "<!DOCTYPE html>"),
            HtmlNode::Comment(comment) => write!(f, "<!-- {} -->", comment),
            HtmlNode::Text(text) => write!(f, "{}", text),
            HtmlNode::Element(element) => write!(f, "{}", element),
            HtmlNode::ProcessingInstruction(pi) => write!(f, "<?{} {}?>", pi.target, pi.data),
        }
    }
}

impl Default for HtmlNode {
    fn default() -> Self {
        Self::Element(HtmlElement::default())
    }
}

/// A doctype.
#[derive(Clone, PartialEq, Eq)]
pub struct DocType {
    /// The doctype name.
    pub name: String,
    /// The doctype public ID.
    pub public_id: String,
    /// The doctype system ID.
    pub system_id: String,
}

/// HTML Processing Instruction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessingInstruction {
    /// The PI target.
    pub target: String,
    /// The PI data.
    pub data: String,
}