aboutsummaryrefslogtreecommitdiff
path: root/html5lib_tests/src/lib.rs
blob: c473eb1793e6fc754017810a82a44832de1ae096 (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
use serde::{de, Deserialize};
use std::collections::BTreeMap;

pub fn parse_tests(
    reader: impl std::io::Read,
) -> Result<impl Iterator<Item = Test>, serde_json::Error> {
    let Tests { tests } = serde_json::from_reader(reader)?;
    Ok(tests.into_iter().map(undo_double_escaping))
}

#[derive(Debug, Deserialize)]
pub enum InitialState {
    #[serde(rename = "Data state")]
    Data,
    #[serde(rename = "PLAINTEXT state")]
    Plaintext,
    #[serde(rename = "RCDATA state")]
    Rcdata,
    #[serde(rename = "RAWTEXT state")]
    Rawtext,
    #[serde(rename = "Script data state")]
    ScriptData,
    #[serde(rename = "CDATA section state")]
    CdataSection,
}

fn initial_states_default() -> Vec<InitialState> {
    vec![InitialState::Data]
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Test {
    pub description: String,
    pub input: String,
    #[serde(default = "initial_states_default")]
    pub initial_states: Vec<InitialState>,
    #[serde(flatten)]
    pub output: Output,
    #[serde(default)]
    double_escaped: bool,
    #[serde(default)]
    pub last_start_tag: Option<String>,
}

#[derive(Deserialize, PartialEq, Eq, Debug)]
pub struct Output {
    #[serde(default)]
    pub errors: Vec<Error>,
    #[serde(rename = "output")]
    pub tokens: Vec<Token>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Token {
    Doctype {
        name: Option<String>,
        public_id: Option<String>,
        system_id: Option<String>,
        force_quirks: bool,
    },
    StartTag {
        name: String,
        attributes: BTreeMap<String, String>,
        self_closing: bool,
    },
    EndTag {
        name: String,
    },
    Comment(String),
    Character(String),
}

impl<'de> Deserialize<'de> for Token {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_seq(TokenVisitor)
    }
}

#[derive(Deserialize)]
enum TokenType {
    #[serde(rename = "DOCTYPE")]
    Doctype,
    StartTag,
    EndTag,
    Comment,
    Character,
}

struct TokenVisitor;

impl<'de> de::Visitor<'de> for TokenVisitor {
    type Value = Token;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("an array describing a token")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        let typ: TokenType = seq.next_element()?.ok_or(
            de::Error::custom(
                r#"expected first array element to be one of "DOCTYPE", "StartTag", "EndTag", "Comment" or "Character""#,
            )
        )?;

        Ok(match typ {
            TokenType::Doctype => Token::Doctype {
                name: seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("name"))?,
                public_id: seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("public_id"))?,
                system_id: seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("system_id"))?,
                force_quirks: !seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("correctness"))?,
            },
            TokenType::StartTag => Token::StartTag {
                name: seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("name"))?,
                attributes: seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("attributes"))?,
                self_closing: seq.next_element()?.unwrap_or_default(),
            },
            TokenType::EndTag => Token::EndTag {
                name: seq
                    .next_element()?
                    .ok_or(de::Error::missing_field("name"))?,
            },
            TokenType::Comment => Token::Comment(
                seq.next_element()?
                    .ok_or(de::Error::missing_field("data"))?,
            ),
            TokenType::Character => Token::Character(
                seq.next_element()?
                    .ok_or(de::Error::missing_field("data"))?,
            ),
        })
    }
}

#[derive(Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Error {
    pub code: String,
    // TODO: lineno and column?
}

#[derive(Deserialize)]
struct Tests {
    tests: Vec<Test>,
}

fn undo_double_escaping(mut test: Test) -> Test {
    if test.double_escaped {
        test.input = unescape(&test.input);

        test.output.tokens = test
            .output
            .tokens
            .into_iter()
            .map(|token| match token {
                Token::Character(x) => Token::Character(unescape(&x)),
                Token::Comment(x) => Token::Comment(unescape(&x)),
                token => token,
            })
            .collect();
    }

    test
}

/// Implements the escape sequences described in the tokenizer tests of html5lib-tests (and nothing
/// more)
fn unescape(data: &str) -> String {
    let mut stream = data.chars();
    let mut rv = String::new();

    loop {
        match stream.next() {
            Some('\\') => (),
            Some(x) => {
                rv.push(x);
                continue;
            }
            None => break,
        }

        match stream.next() {
            Some('u') => (),
            x => panic!("unexpected escape: {:?}", x),
        }

        let orig_len = rv.len();

        for _ in 0..4 {
            rv.push(match stream.next() {
                Some(x) => x,
                None => panic!("unexpected eof after \\u"),
            });
        }

        let c = u32::from_str_radix(&rv[orig_len..], 16).expect("failed to parse as hex");
        let c = char::from_u32(c).expect("bad character");
        rv.truncate(orig_len);
        rv.push(c);
    }

    rv
}