Skip to main content

rumdl_lib/utils/
html_elements.rs

1//! Facts about HTML elements shared by the rules that read inline HTML.
2
3/// The void elements of the HTML standard, plus `param`, which browsers still
4/// parse as one. Each is complete in its start tag: it holds no content, and no
5/// later closing tag belongs to it.
6///
7/// Sorted, so a lowercase tag name can be binary searched.
8pub const VOID_ELEMENTS: &[&str] = &[
9    "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr",
10];
11
12/// Whether the lowercase tag `name` is a void element.
13pub fn is_void_element(name: &str) -> bool {
14    VOID_ELEMENTS.binary_search(&name).is_ok()
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn void_elements_are_sorted_for_binary_search() {
23        assert!(VOID_ELEMENTS.is_sorted(), "{VOID_ELEMENTS:?}");
24    }
25
26    #[test]
27    fn param_is_void_as_browsers_parse_it() {
28        assert!(is_void_element("param"));
29        assert!(is_void_element("br"));
30        assert!(!is_void_element("span"));
31    }
32}