Skip to main content

html_outliner/
sectioning_type.rs

1/// The kind of HTML structure represented by an outline structure node.
2#[derive(Debug, Copy, Clone, Eq, PartialEq)]
3pub enum SectioningType {
4    // ----- Sectioning Content -----
5    /// An `article` element.
6    Article,
7    /// An `aside` element.
8    Aside,
9    /// A `nav` element.
10    Nav,
11    /// A `section` element.
12    Section,
13
14    // ----- Element -----
15    /// A generic root used while flattening normal wrapper elements.
16    Root,
17    /// A `body` element.
18    Body,
19    /// An implied outline item created from a heading after the first heading in a section.
20    Heading,
21}
22
23impl SectioningType {
24    #[inline]
25    pub(crate) fn is_sectioning_content_type(&self) -> bool {
26        matches!(
27            self,
28            SectioningType::Article
29                | SectioningType::Aside
30                | SectioningType::Nav
31                | SectioningType::Section
32        )
33    }
34
35    #[inline]
36    pub(crate) fn is_heading(&self) -> bool {
37        matches!(self, SectioningType::Heading)
38    }
39
40    /// Returns the sectioning content type for the given tag name, or `None` when the tag is not sectioning content.
41    #[inline]
42    pub fn from_sectioning_content_tag<S: AsRef<str>>(s: S) -> Option<SectioningType> {
43        let s = s.as_ref();
44
45        match s {
46            "article" => Some(SectioningType::Article),
47            "aside" => Some(SectioningType::Aside),
48            "nav" => Some(SectioningType::Nav),
49            "section" => Some(SectioningType::Section),
50            _ => None,
51        }
52    }
53
54    /// Returns the lowercase HTML name of this sectioning type.
55    #[inline]
56    pub fn as_str(&self) -> &'static str {
57        match self {
58            SectioningType::Article => "article",
59            SectioningType::Aside => "aside",
60            SectioningType::Nav => "nav",
61            SectioningType::Section => "section",
62            SectioningType::Root => "root",
63            SectioningType::Body => "body",
64            SectioningType::Heading => "heading",
65        }
66    }
67}