Skip to main content

parsec/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 James Conway
3
4//! An HTML parser that follows the WHATWG HTML Standard, written for
5//! Netweir and for speed.
6
7pub mod css;
8mod dom;
9mod edit;
10mod entities;
11mod pattern;
12mod serialize;
13mod shadow;
14mod tags;
15pub mod tokenizer;
16mod tree;
17mod unicode;
18
19pub use dom::{
20    AttrNs, Document, Kind, MAX_INPUT, NodeId, Ns, Quirks, ShadowRoot, ShadowRootMode,
21    SlotAssignment,
22};
23pub use edit::EditError;
24pub use serialize::SerializeOptions;
25pub use tree::{
26    Options, ParseError, Parser, SyntaxError, parse, parse_fragment, parse_fragment_with,
27    parse_with,
28};
29
30// A parsed document and a parsed selector can be read from many threads at
31// once (writing takes `&mut`, so one at a time). This fails the build if a
32// change ever makes either of them not Send or not Sync.
33const _: () = {
34    const fn shareable<T: Send + Sync>() {}
35    shareable::<Document>();
36    shareable::<css::SelectorList>();
37};
38
39/// The character(s) HTML's named reference `name` stands for, without the
40/// `&` and with or without the `;` as the table has it: `nbsp;` gives
41/// U+00A0.
42pub fn named_reference(name: &str) -> Option<&'static str> {
43    entities::ENTITIES
44        .binary_search_by(|(n, _)| n.as_bytes().cmp(name.as_bytes()))
45        .ok()
46        .map(|i| entities::ENTITIES[i].1)
47}
48
49/// The number [`Document::known_tag`] gives elements named `name`
50/// (lowercase), if the parser knows the tag.
51pub fn tag_number(name: &str) -> Option<u8> {
52    let tag = tags::Tag::from_name(name);
53    (tag != tags::Tag::Other).then_some(tag as u8)
54}
55
56/// Whether `name` (in any case) names an element of HTML, current or
57/// obsolete, or the root of SVG or MathML: `div`, `marquee`, `svg`; not
58/// `rss`.
59pub fn is_html_element(name: &str) -> bool {
60    const NAMES: &[&str] = &[
61        "a",
62        "abbr",
63        "acronym",
64        "address",
65        "applet",
66        "area",
67        "article",
68        "aside",
69        "audio",
70        "b",
71        "base",
72        "basefont",
73        "bdi",
74        "bdo",
75        "bgsound",
76        "big",
77        "blink",
78        "blockquote",
79        "body",
80        "br",
81        "button",
82        "canvas",
83        "caption",
84        "center",
85        "cite",
86        "code",
87        "col",
88        "colgroup",
89        "data",
90        "datalist",
91        "dd",
92        "del",
93        "details",
94        "dfn",
95        "dialog",
96        "dir",
97        "div",
98        "dl",
99        "dt",
100        "em",
101        "embed",
102        "fieldset",
103        "figcaption",
104        "figure",
105        "font",
106        "footer",
107        "form",
108        "frame",
109        "frameset",
110        "h1",
111        "h2",
112        "h3",
113        "h4",
114        "h5",
115        "h6",
116        "head",
117        "header",
118        "hgroup",
119        "hr",
120        "html",
121        "i",
122        "iframe",
123        "image",
124        "img",
125        "input",
126        "ins",
127        "isindex",
128        "kbd",
129        "keygen",
130        "label",
131        "legend",
132        "li",
133        "link",
134        "listing",
135        "main",
136        "map",
137        "mark",
138        "marquee",
139        "math",
140        "menu",
141        "menuitem",
142        "meta",
143        "meter",
144        "multicol",
145        "nav",
146        "nextid",
147        "nobr",
148        "noembed",
149        "noframes",
150        "noscript",
151        "object",
152        "ol",
153        "optgroup",
154        "option",
155        "output",
156        "p",
157        "param",
158        "picture",
159        "plaintext",
160        "pre",
161        "progress",
162        "q",
163        "rb",
164        "rp",
165        "rt",
166        "rtc",
167        "ruby",
168        "s",
169        "samp",
170        "script",
171        "search",
172        "section",
173        "select",
174        "selectedcontent",
175        "slot",
176        "small",
177        "source",
178        "spacer",
179        "span",
180        "strike",
181        "strong",
182        "style",
183        "sub",
184        "summary",
185        "sup",
186        "svg",
187        "table",
188        "tbody",
189        "td",
190        "template",
191        "textarea",
192        "tfoot",
193        "th",
194        "thead",
195        "time",
196        "title",
197        "tr",
198        "track",
199        "tt",
200        "u",
201        "ul",
202        "var",
203        "video",
204        "wbr",
205        "xmp",
206    ];
207    let lower = name.to_ascii_lowercase();
208    NAMES.binary_search(&lower.as_str()).is_ok()
209}
210
211#[cfg(test)]
212mod tests {
213    #[test]
214    fn named_references_and_html_element_names() {
215        assert_eq!(super::named_reference("nbsp;"), Some("\u{a0}"));
216        assert_eq!(super::named_reference("amp"), Some("&"));
217        assert_eq!(super::named_reference("nope;"), None);
218        assert!(super::is_html_element("DIV"));
219        assert!(super::is_html_element("marquee"));
220        assert!(!super::is_html_element("rss"));
221    }
222}