parcel_css/properties/
position.rs1use super::Property;
4use crate::context::PropertyHandlerContext;
5use crate::declaration::DeclarationList;
6use crate::error::{ParserError, PrinterError};
7use crate::prefixes::Feature;
8use crate::printer::Printer;
9use crate::targets::Browsers;
10use crate::traits::{Parse, PropertyHandler, ToCss};
11use crate::values::number::CSSInteger;
12use crate::vendor_prefix::VendorPrefix;
13use cssparser::*;
14
15#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(
18 feature = "serde",
19 derive(serde::Serialize, serde::Deserialize),
20 serde(tag = "type", content = "value", rename_all = "kebab-case")
21)]
22pub enum Position {
23 Static,
25 Relative,
27 Absolute,
29 Sticky(VendorPrefix),
31 Fixed,
33}
34
35impl<'i> Parse<'i> for Position {
36 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
37 let location = input.current_source_location();
38 let ident = input.expect_ident()?;
39 match_ignore_ascii_case! { &*ident,
40 "static" => Ok(Position::Static),
41 "relative" => Ok(Position::Relative),
42 "absolute" => Ok(Position::Absolute),
43 "fixed" => Ok(Position::Fixed),
44 "sticky" => Ok(Position::Sticky(VendorPrefix::None)),
45 "-webkit-sticky" => Ok(Position::Sticky(VendorPrefix::WebKit)),
46 _ => Err(location.new_unexpected_token_error(
47 cssparser::Token::Ident(ident.clone())
48 ))
49 }
50 }
51}
52
53impl ToCss for Position {
54 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
55 where
56 W: std::fmt::Write,
57 {
58 match self {
59 Position::Static => dest.write_str("static"),
60 Position::Relative => dest.write_str("relative"),
61 Position::Absolute => dest.write_str("absolute"),
62 Position::Fixed => dest.write_str("fixed"),
63 Position::Sticky(prefix) => {
64 prefix.to_css(dest)?;
65 dest.write_str("sticky")
66 }
67 }
68 }
69}
70
71#[derive(Debug, Clone, PartialEq)]
73#[cfg_attr(
74 feature = "serde",
75 derive(serde::Serialize, serde::Deserialize),
76 serde(tag = "type", content = "value", rename_all = "kebab-case")
77)]
78pub enum ZIndex {
79 Auto,
81 Integer(CSSInteger),
83}
84
85impl<'i> Parse<'i> for ZIndex {
86 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
87 if let Ok(value) = input.expect_integer() {
88 return Ok(ZIndex::Integer(value));
89 }
90
91 input.expect_ident_matching("auto")?;
92 Ok(ZIndex::Auto)
93 }
94}
95
96impl ToCss for ZIndex {
97 fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
98 where
99 W: std::fmt::Write,
100 {
101 match self {
102 ZIndex::Auto => dest.write_str("auto"),
103 ZIndex::Integer(value) => value.to_css(dest),
104 }
105 }
106}
107
108#[derive(Default)]
109pub(crate) struct PositionHandler {
110 targets: Option<Browsers>,
111 position: Option<Position>,
112}
113
114impl PositionHandler {
115 pub fn new(targets: Option<Browsers>) -> PositionHandler {
116 PositionHandler {
117 targets,
118 ..PositionHandler::default()
119 }
120 }
121}
122
123impl<'i> PropertyHandler<'i> for PositionHandler {
124 fn handle_property(
125 &mut self,
126 property: &Property<'i>,
127 _: &mut DeclarationList<'i>,
128 _: &mut PropertyHandlerContext<'i, '_>,
129 ) -> bool {
130 if let Property::Position(position) = property {
131 if let (Some(Position::Sticky(cur)), Position::Sticky(new)) = (&mut self.position, position) {
132 *cur |= *new;
133 } else {
134 self.position = Some(position.clone());
135 }
136
137 return true;
138 }
139
140 false
141 }
142
143 fn finalize(&mut self, dest: &mut DeclarationList, _: &mut PropertyHandlerContext<'i, '_>) {
144 if self.position.is_none() {
145 return;
146 }
147
148 if let Some(position) = std::mem::take(&mut self.position) {
149 match position {
150 Position::Sticky(mut prefix) => {
151 if prefix.contains(VendorPrefix::None) {
152 if let Some(targets) = self.targets {
153 prefix = Feature::Sticky.prefixes_for(targets)
154 }
155 }
156
157 if prefix.contains(VendorPrefix::WebKit) {
158 dest.push(Property::Position(Position::Sticky(VendorPrefix::WebKit)))
159 }
160
161 if prefix.contains(VendorPrefix::None) {
162 dest.push(Property::Position(Position::Sticky(VendorPrefix::None)))
163 }
164 }
165 _ => dest.push(Property::Position(position)),
166 }
167 }
168 }
169}