typst_assets/
html.rs

1//! Data describing HTML elements and attributes and how the attributes
2//! map to Typst types.
3
4/// Details about an HTML element.
5#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
6pub struct ElemInfo {
7    /// The element's tag name.
8    pub name: &'static str,
9    /// A description for the element.
10    pub docs: &'static str,
11    /// Indices of the element's attributes in `ATTRS`.
12    pub attrs: &'static [u8],
13}
14
15impl ElemInfo {
16    /// Creates element information from its parts.
17    ///
18    /// The `attrs` slice consists of indices pointing into `generated::ATTRS`.
19    /// It must be sorted by index (and, by extension, also by name of the
20    /// pointed-to attributes because the attributes themselves are sorted).
21    pub const fn new(name: &'static str, docs: &'static str, attrs: &'static [u8]) -> Self {
22        Self { name, docs, attrs }
23    }
24
25    /// Iterates over all attributes an element of this type can have
26    /// (both specific and global).
27    pub fn attributes(&self) -> impl Iterator<Item = &'static AttrInfo> {
28        self.attrs
29            .iter()
30            .map(|&i| &ATTRS[usize::from(i)])
31            .chain(&ATTRS[..ATTRS_GLOBAL])
32    }
33
34    /// Provides metadata for an attribute with the given name if it exists for
35    /// this element. The attribute may be specific or global.
36    pub fn get_attr(&self, name: &str) -> Option<&'static AttrInfo> {
37        self.get_specific_attr(name)
38            .or_else(|| self.get_global_attr(name))
39            .map(|i| &ATTRS[i])
40    }
41
42    /// Tries to locate the index of a specific attribute in `ATTRS`.
43    fn get_specific_attr(&self, name: &str) -> Option<usize> {
44        self.attrs
45            .binary_search_by_key(&name, |&i| ATTRS[usize::from(i)].name)
46            .map(|k| usize::from(self.attrs[k]))
47            .ok()
48    }
49
50    /// Tries to locate the index of a global attribute in `ATTRS`.
51    fn get_global_attr(&self, name: &str) -> Option<usize> {
52        ATTRS[..ATTRS_GLOBAL]
53            .binary_search_by_key(&name, |attr| attr.name)
54            .ok()
55    }
56}
57
58/// Details about an HTML attribute.
59#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
60pub struct AttrInfo {
61    /// The attribute's name.
62    pub name: &'static str,
63    /// A description for the attribute.
64    pub docs: &'static str,
65    /// Type information for the attribute.
66    pub ty: Type,
67}
68
69impl AttrInfo {
70    /// Creates attribute information from its parts.
71    pub const fn new(name: &'static str, docs: &'static str, ty: Type) -> Self {
72        Self { name, docs, ty }
73    }
74}
75
76/// Defines how an attribute maps to Typst types.
77///
78/// Each variant's documentation describes which Typst type is accepted and into
79/// what kind of HTML attribute string it is converted.
80#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
81pub enum Type {
82    /// `bool` → attribute is present or absent.
83    Presence,
84    /// `none` → `"none"`.
85    None,
86    /// `none` → `""`.
87    NoneEmpty,
88    /// `none` → `"undefined"`.
89    NoneUndefined,
90    /// `auto` → `"auto"`.
91    Auto,
92    /// `boolean` → `"true"` or `"false"`.
93    TrueFalse,
94    /// `boolean` → `"yes"` or `"no"`.
95    YesNo,
96    /// `boolean` → `"on"` or `"off"`.
97    OnOff,
98    /// `ltr` or `rtl` → `"ltr"` or `"rtl"`.
99    HorizontalDir,
100    /// `string` → string.
101    Str,
102    /// `char` → string.
103    Char,
104    /// `int` → stringified int.
105    Int,
106    /// `int >= 0` → stringified int.
107    NonNegativeInt,
108    /// `int > 0` → stringified int.
109    PositiveInt,
110    /// `float` → stringified float.
111    Float,
112    /// `float` > 0 → stringified float.
113    PositiveFloat,
114    /// `datetime` → stringified datetime.
115    Datetime,
116    /// `duration` → stringified duration.
117    Duration,
118    /// `color` → stringified CSS color.
119    Color,
120    /// `array (w, h)` of two `int >= 0` → `"{w}x{h}"`.
121    IconSize,
122    /// `dictionary` with keys `src` (string) and optionally `width` (int) or
123    /// `density` (positive float) → `"{src}"` plus optionally space-separated
124    /// `"{width}w"` or `{density}d`.
125    ImageCandidate,
126    /// `dictionary` with keys `condition` (string) and `size` (length) →
127    /// `"({condition}) {size}"`
128    SourceSize,
129    /// For `Strings(a, b)`, any of the strings in `ATTR_STRINGS[a, b]` →
130    /// string.
131    Strings(usize, usize),
132    /// Any of the listed types → the respective output.
133    Union(&'static [Type]),
134    /// An array of the listed type, or, if the bool is true, the listed type
135    /// itself as a shorthand → a string containing the respective outputs
136    /// separated by the char.
137    List(&'static Type, char, bool),
138}
139
140#[path = "../files/html/data.rs"]
141mod data;
142
143pub use data::*;