1// Copyright 2014-2017 The html5ever Project Developers. See the
2// COPYRIGHT file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use crate::interface::{LimitedQuirks, NoQuirks, Quirks, QuirksMode};
11use crate::tendril::StrTendril;
12use crate::tokenizer::Doctype;
13
14// These should all be lowercase, for ASCII-case-insensitive matching.
15static QUIRKY_PUBLIC_PREFIXES: &[&str] = &[
16 "-//advasoft ltd//dtd html 3.0 aswedit + extensions//",
17 "-//as//dtd html 3.0 aswedit + extensions//",
18 "-//ietf//dtd html 2.0 level 1//",
19 "-//ietf//dtd html 2.0 level 2//",
20 "-//ietf//dtd html 2.0 strict level 1//",
21 "-//ietf//dtd html 2.0 strict level 2//",
22 "-//ietf//dtd html 2.0 strict//",
23 "-//ietf//dtd html 2.0//",
24 "-//ietf//dtd html 2.1e//",
25 "-//ietf//dtd html 3.0//",
26 "-//ietf//dtd html 3.2 final//",
27 "-//ietf//dtd html 3.2//",
28 "-//ietf//dtd html 3//",
29 "-//ietf//dtd html level 0//",
30 "-//ietf//dtd html level 1//",
31 "-//ietf//dtd html level 2//",
32 "-//ietf//dtd html level 3//",
33 "-//ietf//dtd html strict level 0//",
34 "-//ietf//dtd html strict level 1//",
35 "-//ietf//dtd html strict level 2//",
36 "-//ietf//dtd html strict level 3//",
37 "-//ietf//dtd html strict//",
38 "-//ietf//dtd html//",
39 "-//metrius//dtd metrius presentational//",
40 "-//microsoft//dtd internet explorer 2.0 html strict//",
41 "-//microsoft//dtd internet explorer 2.0 html//",
42 "-//microsoft//dtd internet explorer 2.0 tables//",
43 "-//microsoft//dtd internet explorer 3.0 html strict//",
44 "-//microsoft//dtd internet explorer 3.0 html//",
45 "-//microsoft//dtd internet explorer 3.0 tables//",
46 "-//netscape comm. corp.//dtd html//",
47 "-//netscape comm. corp.//dtd strict html//",
48 "-//o'reilly and associates//dtd html 2.0//",
49 "-//o'reilly and associates//dtd html extended 1.0//",
50 "-//o'reilly and associates//dtd html extended relaxed 1.0//",
51 "-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//",
52 "-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//",
53 "-//spyglass//dtd html 2.0 extended//",
54 "-//sq//dtd html 2.0 hotmetal + extensions//",
55 "-//sun microsystems corp.//dtd hotjava html//",
56 "-//sun microsystems corp.//dtd hotjava strict html//",
57 "-//w3c//dtd html 3 1995-03-24//",
58 "-//w3c//dtd html 3.2 draft//",
59 "-//w3c//dtd html 3.2 final//",
60 "-//w3c//dtd html 3.2//",
61 "-//w3c//dtd html 3.2s draft//",
62 "-//w3c//dtd html 4.0 frameset//",
63 "-//w3c//dtd html 4.0 transitional//",
64 "-//w3c//dtd html experimental 19960712//",
65 "-//w3c//dtd html experimental 970421//",
66 "-//w3c//dtd w3 html//",
67 "-//w3o//dtd w3 html 3.0//",
68 "-//webtechs//dtd mozilla html 2.0//",
69 "-//webtechs//dtd mozilla html//",
70];
71
72static QUIRKY_PUBLIC_MATCHES: &[&str] = &[
73 "-//w3o//dtd w3 html strict 3.0//en//",
74 "-/w3c/dtd html 4.0 transitional/en",
75 "html",
76];
77
78static QUIRKY_SYSTEM_MATCHES: &[&str] =
79 &["http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"];
80
81static LIMITED_QUIRKY_PUBLIC_PREFIXES: &[&str] = &[
82 "-//w3c//dtd xhtml 1.0 frameset//",
83 "-//w3c//dtd xhtml 1.0 transitional//",
84];
85
86static HTML4_PUBLIC_PREFIXES: &[&str] = &[
87 "-//w3c//dtd html 4.01 frameset//",
88 "-//w3c//dtd html 4.01 transitional//",
89];
90
91pub(crate) fn doctype_error_and_quirks(
92 doctype: &Doctype,
93 iframe_srcdoc: bool,
94) -> (bool, QuirksMode) {
95 fn opt_string_as_slice(x: &Option<String>) -> Option<&str> {
96 x.as_deref()
97 }
98
99 fn opt_tendril_as_slice(x: &Option<StrTendril>) -> Option<&str> {
100 x.as_deref()
101 }
102
103 fn opt_to_ascii_lower(x: Option<&str>) -> Option<String> {
104 x.map(|y| y.to_ascii_lowercase())
105 }
106
107 let name = opt_tendril_as_slice(&doctype.name);
108 let public = opt_tendril_as_slice(&doctype.public_id);
109 let system = opt_tendril_as_slice(&doctype.system_id);
110
111 let err = !matches!(
112 (name, public, system),
113 (Some("html"), None, None)
114 | (Some("html"), None, Some("about:legacy-compat"))
115 | (Some("html"), Some("-//W3C//DTD HTML 4.0//EN"), None)
116 | (
117 Some("html"),
118 Some("-//W3C//DTD HTML 4.0//EN"),
119 Some("http://www.w3.org/TR/REC-html40/strict.dtd"),
120 )
121 | (Some("html"), Some("-//W3C//DTD HTML 4.01//EN"), None)
122 | (
123 Some("html"),
124 Some("-//W3C//DTD HTML 4.01//EN"),
125 Some("http://www.w3.org/TR/html4/strict.dtd"),
126 )
127 | (
128 Some("html"),
129 Some("-//W3C//DTD XHTML 1.0 Strict//EN"),
130 Some("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
131 )
132 | (
133 Some("html"),
134 Some("-//W3C//DTD XHTML 1.1//EN"),
135 Some("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"),
136 )
137 );
138
139 // FIXME: We could do something asymptotically faster here.
140 // But there aren't many strings, and this happens at most once per parse.
141 fn contains_pfx(haystack: &[&str], needle: &str) -> bool {
142 haystack.iter().any(|&x| needle.starts_with(x))
143 }
144
145 // Quirks-mode matches are case-insensitive.
146 let public = opt_to_ascii_lower(public);
147 let system = opt_to_ascii_lower(system);
148
149 let quirk = match (opt_string_as_slice(&public), opt_string_as_slice(&system)) {
150 _ if doctype.force_quirks => Quirks,
151 _ if name != Some("html") => Quirks,
152
153 _ if iframe_srcdoc => NoQuirks,
154
155 (Some(ref p), _) if QUIRKY_PUBLIC_MATCHES.contains(p) => Quirks,
156 (_, Some(ref s)) if QUIRKY_SYSTEM_MATCHES.contains(s) => Quirks,
157
158 (Some(p), _) if contains_pfx(QUIRKY_PUBLIC_PREFIXES, p) => Quirks,
159 (Some(p), _) if contains_pfx(LIMITED_QUIRKY_PUBLIC_PREFIXES, p) => LimitedQuirks,
160
161 (Some(p), s) if contains_pfx(HTML4_PUBLIC_PREFIXES, p) => match s {
162 None => Quirks,
163 Some(_) => LimitedQuirks,
164 },
165
166 _ => NoQuirks,
167 };
168
169 (err, quirk)
170}
171