polished_css/selector/
mod.rs

1//! Patterns that match against elements in a tree, and as such form one of
2//! several technologies that can be used to select nodes in a document.
3//!
4//! ### Resources
5//!
6//! - [CSSWG specification](https://drafts.csswg.org/selectors/)
7//! - [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors)
8
9pub mod class;
10pub mod element;
11pub mod id;
12pub mod pseudo;
13
14pub use class::*;
15pub use element::*;
16pub use id::*;
17pub use pseudo::*;
18
19#[derive(Clone, Debug, PartialEq, strum_macros::EnumIs)]
20pub enum Selector {
21    Inlined,
22    Root,
23    Id(Id),
24    Element(Element),
25    Class(Class),
26}
27
28impl Default for Selector {
29    fn default() -> Self {
30        Self::Inlined
31    }
32}
33
34impl SelectorDisplay for Selector {
35    fn as_styles_content(&self) -> String {
36        match self {
37            Self::Inlined => String::new(),
38            Self::Root => String::from(":root"),
39            Self::Id(id) => id.as_styles_content(),
40            Self::Element(el) => el.as_styles_content(),
41            Self::Class(name) => name.as_styles_content(),
42        }
43    }
44
45    fn as_attribute_value(&self) -> String {
46        match self {
47            Self::Inlined => String::new(),
48            Self::Root => String::from(":root"),
49            Self::Id(id) => id.as_attribute_value(),
50            Self::Element(el) => el.as_attribute_value(),
51            Self::Class(name) => name.as_attribute_value(),
52        }
53    }
54}
55
56impl From<Id> for Selector {
57    fn from(value: Id) -> Self {
58        Self::Id(value)
59    }
60}
61
62impl From<Element> for Selector {
63    fn from(value: Element) -> Self {
64        Self::Element(value)
65    }
66}
67
68impl From<Class> for Selector {
69    fn from(value: Class) -> Self {
70        Self::Class(value)
71    }
72}
73
74pub trait SelectorDisplay {
75    fn as_attribute_value(&self) -> String;
76    fn as_styles_content(&self) -> String;
77}