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
use crate::scrape::ScrapedElement;
use crate::Result;
use convert_case::{Case, Casing};
use serde::{Deserialize, Serialize};

use super::{Attribute, AttributeType, ParsedCategory, ParsedRelationship};
use categories::parse_categories;

mod categories;

/// The parsed values converted from the raw spec
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedElement {
    pub tag_name: String,
    pub struct_name: String,
    pub submodule_name: String,
    pub mdn_link: String,
    pub has_global_attributes: bool,
    pub has_closing_tag: bool,
    pub attributes: Vec<Attribute>,
    pub dom_interface: String,
    pub content_categories: Vec<ParsedCategory>,
    pub permitted_content: Vec<ParsedRelationship>,
    pub permitted_parents: Vec<ParsedRelationship>,
}

pub fn parse_elements(
    scraped_iter: impl Iterator<Item = Result<ScrapedElement>>,
) -> Result<Vec<ParsedElement>> {
    let mut scraped = vec![];
    for el in scraped_iter {
        let el = el?;
        scraped.push(el);
    }

    let mut tag_names = vec![];
    for scraped in scraped.iter().cloned() {
        // let struct_name = parse_struct_name(&scraped.tag_name);
        tag_names.push(scraped.tag_name.to_owned());
    }
    let mut output = vec![];
    for scraped in scraped {
        let tag_name = scraped.tag_name;
        let struct_name = parse_struct_name(&tag_name);
        let (has_global_attributes, attributes) = parse_attrs(scraped.content_attributes);
        let dom_interface = parse_dom_interface(&scraped.dom_interface);
        let mut permitted_parents = parse_relationships(&scraped.contexts, &tag_names);
        append_super_categories(&mut permitted_parents);
        output.push(ParsedElement {
            struct_name,
            dom_interface,
            has_closing_tag: parse_tags(scraped.tag_omission),
            attributes,
            has_global_attributes,
            submodule_name: parse_kinds(scraped.submodule_name),
            mdn_link: parse_mdn_link(&tag_name),
            content_categories: parse_content_categories(&scraped.categories),
            permitted_content: parse_relationships(&scraped.content_model, &tag_names),
            permitted_parents,
            tag_name,
        });
    }
    Ok(output)
}

fn parse_mdn_link(tag_name: &str) -> String {
    format!("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/{tag_name}")
}

pub fn parse_struct_name(tag_name: &str) -> String {
    match tag_name {
        "a" => "Anchor".to_owned(),
        "abbr" => "Abbreviation".to_owned(),
        "area" => "ImageMapArea".to_owned(),
        "b" => "Bold".to_owned(),
        "bdi" => "BidirectionalIsolate".to_owned(),
        "bdo" => "BidirectionalTextOverride".to_owned(),
        "bgsound" => "BackgroundSound".to_owned(),
        "blockquote" => "BlockQuote".to_owned(),
        "br" => "LineBreak".to_owned(),
        "col" => "TableColumn".to_owned(),
        "colgroup" => "TableColumnGroup".to_owned(),
        "datalist" => "DataList".to_owned(),
        "dd" => "DescriptionDetails".to_owned(),
        "del" => "DeletedText".to_owned(),
        "dfn" => "Definition".to_owned(),
        "dir" => "Directory".to_owned(),
        "div" => "Division".to_owned(),
        "dl" => "DescriptionList".to_owned(),
        "dt" => "DescriptionTerm".to_owned(),
        "em" => "Emphasis".to_owned(),
        "figcaption" => "FigureCaption".to_owned(),
        "hgroup" => "HeadingGroup".to_owned(),
        "h1" => "Heading1".to_owned(),
        "h2" => "Heading2".to_owned(),
        "h3" => "Heading3".to_owned(),
        "h4" => "Heading4".to_owned(),
        "h5" => "Heading5".to_owned(),
        "h6" => "Heading6".to_owned(),
        "hr" => "ThematicBreak".to_owned(),
        "i" => "Italic".to_owned(),
        "img" => "Image".to_owned(),
        "ins" => "InsertedText".to_owned(),
        "kbd" => "KeyboardInput".to_owned(),
        "li" => "ListItem".to_owned(),
        "map" => "ImageMap".to_owned(),
        "mark" => "MarkText".to_owned(),
        "nav" => "Navigation".to_owned(),
        "nobr" => "NonBreakingText".to_owned(),
        "noscript" => "NoScript".to_owned(),
        "ol" => "OrderedList".to_owned(),
        "optgroup" => "OptionGroup".to_owned(),
        "p" => "Paragraph".to_owned(),
        "pre" => "PreformattedText".to_owned(),
        "q" => "Quotation".to_owned(),
        "rp" => "RubyFallbackParenthesis".to_owned(),
        "rt" => "RubyText".to_owned(),
        "rtc" => "RubyTextContainer".to_owned(),
        "ruby" => "RubyAnnotation".to_owned(),
        "s" => "StrikeThrough".to_owned(),
        "samp" => "SampleOutput".to_owned(),
        "small" => "SideComment".to_owned(),
        "source" => "MediaSource".to_owned(),
        "span" => "Span".to_owned(),
        "sub" => "SubScript".to_owned(),
        "sup" => "SuperScript".to_owned(),
        "textarea" => "TextArea".to_owned(),
        "tbody" => "TableBody".to_owned(),
        "td" => "TableCell".to_owned(),
        "tfoot" => "TableFoot".to_owned(),
        "th" => "TableHeader".to_owned(),
        "thead" => "TableHead".to_owned(),
        "tr" => "TableRow".to_owned(),
        "track" => "TextTrack".to_owned(),
        "tt" => "TeletypeText".to_owned(),
        "u" => "Underline".to_owned(),
        "ul" => "UnorderedList".to_owned(),
        "var" => "Variable".to_owned(),
        "wbr" => "LineBreakOpportunity".to_owned(),
        other => other.to_case(Case::UpperCamel),
    }
}

fn parse_tags(input: Vec<String>) -> bool {
    let s = input.join("");
    match &*s {
        "Neither tag is omissible." | "" => true,
        "No end tag." => false,
        // NOTE: There are a bunch of conditional cases which allow omitting end tags
        // but for the sake of convenience we just don't bother with any of those.
        // That's mostly important for parsers, which we're not defining here.
        _ => true,
    }
}

fn parse_attrs(content_attributes: Vec<String>) -> (bool, Vec<Attribute>) {
    let mut has_global_attributes = false;
    let mut output = vec![];
    for s in content_attributes {
        if s == "Global attributes" {
            has_global_attributes = true;
            continue;
        } else if !s.contains("—") {
            continue;
        }
        let mut iter = s.split("—");
        let name = iter.next().unwrap().trim().to_owned();
        let description = iter.next().unwrap().trim().to_owned();

        // we skip over all the conditional comments for now.
        if name.contains(' ') {
            continue;
        }

        // Rename attributes which are labeled after keywords
        let field_name = super::normalize_field_name(&name);

        output.push(Attribute {
            ty: AttributeType::String,
            name,
            description,
            field_name,
        });
    }
    (has_global_attributes, output)
}

fn parse_kinds(kind: String) -> String {
    let s = match kind.as_str() {
        "the-root-element" => "root",
        "interactive-elements" => "interactive",
        "grouping-content" => "text",
        "text-level-semantics" => "text",
        "document-metadata" => "metadata",
        "embedded-content" => "embedded",
        "forms" => "forms",
        "tables" => "tables",
        "sections" => "sections",
        "edits" => "edits",
        "scripting-3" => "scripting",
        other => panic!("unknown category: {other}"),
    };
    s.to_owned()
}

fn parse_content_categories(categories: &[String]) -> Vec<ParsedCategory> {
    let mut cat_output = vec![];
    for line in categories {
        for line in parse_categories(line.as_str()) {
            match line.as_str() {
                "metadata" => cat_output.push(ParsedCategory::Metadata),
                "flow" => cat_output.push(ParsedCategory::Flow),
                "sectioning" => cat_output.push(ParsedCategory::Sectioning),
                "heading" => cat_output.push(ParsedCategory::Heading),
                "phrasing" => cat_output.push(ParsedCategory::Phrasing),
                "embedded" => cat_output.push(ParsedCategory::Embedded),
                "interactive" => cat_output.push(ParsedCategory::Interactive),
                "palpable" => cat_output.push(ParsedCategory::Palpable),
                "transparent" => cat_output.push(ParsedCategory::Transparent),
                "script-supporting" => cat_output.push(ParsedCategory::ScriptSupporting),
                other => eprintln!("unknown content kind: {other}"),
            }
        }
    }

    cat_output.dedup();
    cat_output.sort();
    cat_output
}

fn parse_relationships(categories: &[String], tag_names: &[String]) -> Vec<ParsedRelationship> {
    let mut cat_output = vec![];
    for line in categories {
        for line in parse_categories(line.as_str()) {
            match line.as_str() {
                "metadata" => cat_output.push(ParsedCategory::Metadata.into()),
                "flow" => cat_output.push(ParsedCategory::Flow.into()),
                "sectioning" => cat_output.push(ParsedCategory::Sectioning.into()),
                "heading" => cat_output.push(ParsedCategory::Heading.into()),
                "phrasing" => cat_output.push(ParsedCategory::Phrasing.into()),
                "embedded" => cat_output.push(ParsedCategory::Embedded.into()),
                "interactive" => cat_output.push(ParsedCategory::Interactive.into()),
                "palpable" => cat_output.push(ParsedCategory::Palpable.into()),
                "transparent" => cat_output.push(ParsedCategory::Transparent.into()),
                "script-supporting" => cat_output.push(ParsedCategory::ScriptSupporting.into()),
                tag_name => {
                    if tag_names.contains(&tag_name.to_owned()) {
                        cat_output.push(ParsedRelationship::Element(parse_struct_name(tag_name)));
                    } else if tag_name == "text" {
                        cat_output.push(ParsedRelationship::Element(parse_struct_name("Text")));
                    } else {
                        eprintln!("unknown tag name: {tag_name}");
                    }
                }
            }
        }
    }

    cat_output.sort();
    cat_output.dedup();
    cat_output
}

fn append_super_categories(cat_output: &mut Vec<ParsedRelationship>) {
    let mut additional_output = vec![];

    for relationship in &*cat_output {
        if let ParsedRelationship::Category(category) = relationship {
            match category {
                ParsedCategory::Heading
                | ParsedCategory::Sectioning
                | ParsedCategory::Phrasing
                | ParsedCategory::Interactive => {
                    additional_output.push(ParsedCategory::Flow.into());
                }
                ParsedCategory::Embedded => {
                    additional_output.push(ParsedCategory::Phrasing.into());
                    additional_output.push(ParsedCategory::Flow.into());
                }
                ParsedCategory::Transparent => {
                    // NOTE(yosh): Sure, why not.
                    additional_output.push(ParsedCategory::Flow.into());
                }
                _ => continue,
            }
        }
    }

    cat_output.append(&mut additional_output);
    cat_output.sort();
    cat_output.dedup();
}

/// Find out which WebIDL interface this element relies on.
fn parse_dom_interface(lines: &[String]) -> String {
    let line = lines.get(0).as_deref().unwrap().clone();

    if line.starts_with("Uses") {
        let line = line.strip_prefix("Uses").unwrap();
        let line = line.strip_suffix(".").unwrap();
        line.trim().to_owned()
    } else if line.starts_with("Use") {
        let line = line.strip_prefix("Use").unwrap();
        let line = line.strip_suffix(".").unwrap();
        line.trim().to_owned()
    } else {
        crate::utils::extract_webidl_name(&line).unwrap()
    }
}