Skip to content

Commit 839bcd1

Browse files
committed
Parse z-index property and fix serialization
Fixes parcel-bundler#170
1 parent 0d7e4c6 commit 839bcd1

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18269,4 +18269,13 @@ mod tests {
1826918269
r#".foo{background-image:url(0123abcd)}"#,
1827018270
);
1827118271
}
18272+
18273+
#[test]
18274+
fn test_zindex() {
18275+
minify_test(".foo { z-index: 2 }", ".foo{z-index:2}");
18276+
minify_test(".foo { z-index: -2 }", ".foo{z-index:-2}");
18277+
minify_test(".foo { z-index: 999999 }", ".foo{z-index:999999}");
18278+
minify_test(".foo { z-index: 9999999 }", ".foo{z-index:9999999}");
18279+
minify_test(".foo { z-index: -9999999 }", ".foo{z-index:-9999999}");
18280+
}
1827218281
}

src/properties/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,9 @@ define_properties! {
11331133
// https://drafts.fxtf.org/filter-effects-1/
11341134
"filter": Filter(FilterList<'i>, VendorPrefix) / WebKit,
11351135
"backdrop-filter": BackdropFilter(FilterList<'i>, VendorPrefix) / WebKit,
1136+
1137+
// https://drafts.csswg.org/css2/
1138+
"z-index": ZIndex(position::ZIndex),
11361139
}
11371140

11381141
impl<'i, T: smallvec::Array<Item = V>, V: Parse<'i>> Parse<'i> for SmallVec<T> {

src/properties/position.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! The CSS position property.
1+
//! CSS properties related to positioning.
22
33
use super::Property;
44
use crate::context::PropertyHandlerContext;
@@ -8,6 +8,7 @@ use crate::prefixes::Feature;
88
use crate::printer::Printer;
99
use crate::targets::Browsers;
1010
use crate::traits::{Parse, PropertyHandler, ToCss};
11+
use crate::values::number::CSSInteger;
1112
use crate::vendor_prefix::VendorPrefix;
1213
use cssparser::*;
1314

@@ -62,6 +63,38 @@ impl ToCss for Position {
6263
}
6364
}
6465

66+
/// A value for the [z-index](https://drafts.csswg.org/css2/#z-index) property.
67+
#[derive(Debug, Clone, PartialEq)]
68+
pub enum ZIndex {
69+
/// The `auto` keyword.
70+
Auto,
71+
/// An integer value.
72+
Integer(CSSInteger),
73+
}
74+
75+
impl<'i> Parse<'i> for ZIndex {
76+
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
77+
if let Ok(value) = input.expect_integer() {
78+
return Ok(ZIndex::Integer(value));
79+
}
80+
81+
input.expect_ident_matching("auto")?;
82+
Ok(ZIndex::Auto)
83+
}
84+
}
85+
86+
impl ToCss for ZIndex {
87+
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
88+
where
89+
W: std::fmt::Write,
90+
{
91+
match self {
92+
ZIndex::Auto => dest.write_str("auto"),
93+
ZIndex::Integer(value) => value.to_css(dest),
94+
}
95+
}
96+
}
97+
6598
#[derive(Default)]
6699
pub(crate) struct PositionHandler {
67100
targets: Option<Browsers>,

src/values/number.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,18 @@ impl ToCss for CSSNumber {
3232
where
3333
W: std::fmt::Write,
3434
{
35-
use cssparser::ToCss;
3635
let number = *self;
37-
let int_value = if number.fract() == 0.0 {
38-
Some(number as i32)
39-
} else {
40-
None
41-
};
42-
let tok = Token::Number {
43-
has_sign: number < 0.0,
44-
value: number,
45-
int_value,
46-
};
4736
if number != 0.0 && number.abs() < 1.0 {
4837
let mut s = String::new();
49-
tok.to_css(&mut s)?;
38+
cssparser::ToCss::to_css(self, &mut s)?;
5039
if number < 0.0 {
5140
dest.write_char('-')?;
5241
dest.write_str(s.trim_start_matches("-0"))
5342
} else {
5443
dest.write_str(s.trim_start_matches('0'))
5544
}
5645
} else {
57-
tok.to_css(dest)?;
46+
cssparser::ToCss::to_css(self, dest)?;
5847
Ok(())
5948
}
6049
}
@@ -91,14 +80,7 @@ impl ToCss for CSSInteger {
9180
where
9281
W: std::fmt::Write,
9382
{
94-
use cssparser::ToCss;
95-
let integer = *self;
96-
let tok = Token::Number {
97-
has_sign: integer < 0,
98-
value: integer as f32,
99-
int_value: Some(integer),
100-
};
101-
tok.to_css(dest)?;
83+
cssparser::ToCss::to_css(self, dest)?;
10284
Ok(())
10385
}
10486
}

0 commit comments

Comments
 (0)