aboutsummaryrefslogtreecommitdiff
path: root/src/tree_builder/data.rs
blob: 9d51a71097445e0cea6db358c1695231be46e099 (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
// Copyright 2014-2017 The html5ever Project Developers. See the
// COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::interface::{LimitedQuirks, NoQuirks, Quirks, QuirksMode};
use crate::tendril::StrTendril;
use crate::tokenizer::Doctype;

// These should all be lowercase, for ASCII-case-insensitive matching.
static QUIRKY_PUBLIC_PREFIXES: &'static [&'static str] = &[
    "-//advasoft ltd//dtd html 3.0 aswedit + extensions//",
    "-//as//dtd html 3.0 aswedit + extensions//",
    "-//ietf//dtd html 2.0 level 1//",
    "-//ietf//dtd html 2.0 level 2//",
    "-//ietf//dtd html 2.0 strict level 1//",
    "-//ietf//dtd html 2.0 strict level 2//",
    "-//ietf//dtd html 2.0 strict//",
    "-//ietf//dtd html 2.0//",
    "-//ietf//dtd html 2.1e//",
    "-//ietf//dtd html 3.0//",
    "-//ietf//dtd html 3.2 final//",
    "-//ietf//dtd html 3.2//",
    "-//ietf//dtd html 3//",
    "-//ietf//dtd html level 0//",
    "-//ietf//dtd html level 1//",
    "-//ietf//dtd html level 2//",
    "-//ietf//dtd html level 3//",
    "-//ietf//dtd html strict level 0//",
    "-//ietf//dtd html strict level 1//",
    "-//ietf//dtd html strict level 2//",
    "-//ietf//dtd html strict level 3//",
    "-//ietf//dtd html strict//",
    "-//ietf//dtd html//",
    "-//metrius//dtd metrius presentational//",
    "-//microsoft//dtd internet explorer 2.0 html strict//",
    "-//microsoft//dtd internet explorer 2.0 html//",
    "-//microsoft//dtd internet explorer 2.0 tables//",
    "-//microsoft//dtd internet explorer 3.0 html strict//",
    "-//microsoft//dtd internet explorer 3.0 html//",
    "-//microsoft//dtd internet explorer 3.0 tables//",
    "-//netscape comm. corp.//dtd html//",
    "-//netscape comm. corp.//dtd strict html//",
    "-//o'reilly and associates//dtd html 2.0//",
    "-//o'reilly and associates//dtd html extended 1.0//",
    "-//o'reilly and associates//dtd html extended relaxed 1.0//",
    "-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//",
    "-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//",
    "-//spyglass//dtd html 2.0 extended//",
    "-//sq//dtd html 2.0 hotmetal + extensions//",
    "-//sun microsystems corp.//dtd hotjava html//",
    "-//sun microsystems corp.//dtd hotjava strict html//",
    "-//w3c//dtd html 3 1995-03-24//",
    "-//w3c//dtd html 3.2 draft//",
    "-//w3c//dtd html 3.2 final//",
    "-//w3c//dtd html 3.2//",
    "-//w3c//dtd html 3.2s draft//",
    "-//w3c//dtd html 4.0 frameset//",
    "-//w3c//dtd html 4.0 transitional//",
    "-//w3c//dtd html experimental 19960712//",
    "-//w3c//dtd html experimental 970421//",
    "-//w3c//dtd w3 html//",
    "-//w3o//dtd w3 html 3.0//",
    "-//webtechs//dtd mozilla html 2.0//",
    "-//webtechs//dtd mozilla html//",
];

static QUIRKY_PUBLIC_MATCHES: &'static [&'static str] = &[
    "-//w3o//dtd w3 html strict 3.0//en//",
    "-/w3c/dtd html 4.0 transitional/en",
    "html",
];

static QUIRKY_SYSTEM_MATCHES: &'static [&'static str] =
    &["http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"];

static LIMITED_QUIRKY_PUBLIC_PREFIXES: &'static [&'static str] = &[
    "-//w3c//dtd xhtml 1.0 frameset//",
    "-//w3c//dtd xhtml 1.0 transitional//",
];

static HTML4_PUBLIC_PREFIXES: &'static [&'static str] = &[
    "-//w3c//dtd html 4.01 frameset//",
    "-//w3c//dtd html 4.01 transitional//",
];

pub fn doctype_error_and_quirks(doctype: &Doctype, iframe_srcdoc: bool) -> (bool, QuirksMode) {
    fn opt_string_as_slice<'t>(x: &'t Option<String>) -> Option<&'t str> {
        x.as_ref().map(|y| &y[..])
    }

    fn opt_tendril_as_slice<'t>(x: &'t Option<StrTendril>) -> Option<&'t str> {
        match *x {
            Some(ref t) => Some(t),
            None => None,
        }
    }

    fn opt_to_ascii_lower(x: Option<&str>) -> Option<String> {
        x.map(|y| y.to_ascii_lowercase())
    }

    let name = opt_tendril_as_slice(&doctype.name);
    let public = opt_tendril_as_slice(&doctype.public_id);
    let system = opt_tendril_as_slice(&doctype.system_id);

    let err = match (name, public, system) {
        (Some("html"), None, None) |
        (Some("html"), None, Some("about:legacy-compat")) |
        (Some("html"), Some("-//W3C//DTD HTML 4.0//EN"), None) |
        (
            Some("html"),
            Some("-//W3C//DTD HTML 4.0//EN"),
            Some("http://www.w3.org/TR/REC-html40/strict.dtd"),
        ) |
        (Some("html"), Some("-//W3C//DTD HTML 4.01//EN"), None) |
        (
            Some("html"),
            Some("-//W3C//DTD HTML 4.01//EN"),
            Some("http://www.w3.org/TR/html4/strict.dtd"),
        ) |
        (
            Some("html"),
            Some("-//W3C//DTD XHTML 1.0 Strict//EN"),
            Some("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
        ) |
        (
            Some("html"),
            Some("-//W3C//DTD XHTML 1.1//EN"),
            Some("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"),
        ) => false,

        _ => true,
    };

    // FIXME: We could do something asymptotically faster here.
    // But there aren't many strings, and this happens at most once per parse.
    fn contains_pfx(haystack: &[&str], needle: &str) -> bool {
        haystack.iter().any(|&x| needle.starts_with(x))
    }

    // Quirks-mode matches are case-insensitive.
    let public = opt_to_ascii_lower(public);
    let system = opt_to_ascii_lower(system);

    let quirk = match (opt_string_as_slice(&public), opt_string_as_slice(&system)) {
        _ if doctype.force_quirks => Quirks,
        _ if name != Some("html") => Quirks,

        _ if iframe_srcdoc => NoQuirks,

        (Some(ref p), _) if QUIRKY_PUBLIC_MATCHES.contains(p) => Quirks,
        (_, Some(ref s)) if QUIRKY_SYSTEM_MATCHES.contains(s) => Quirks,

        (Some(p), _) if contains_pfx(QUIRKY_PUBLIC_PREFIXES, p) => Quirks,
        (Some(p), _) if contains_pfx(LIMITED_QUIRKY_PUBLIC_PREFIXES, p) => LimitedQuirks,

        (Some(p), s) if contains_pfx(HTML4_PUBLIC_PREFIXES, p) => match s {
            None => Quirks,
            Some(_) => LimitedQuirks,
        },

        _ => NoQuirks,
    };

    (err, quirk)
}