Skip to content

Commit

Permalink
Parse z-index property and fix serialization
Browse files Browse the repository at this point in the history
Fixes #170
  • Loading branch information
devongovett committed May 10, 2022
1 parent 0d7e4c6 commit 839bcd1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18269,4 +18269,13 @@ mod tests {
r#".foo{background-image:url(0123abcd)}"#,
);
}

#[test]
fn test_zindex() {
minify_test(".foo { z-index: 2 }", ".foo{z-index:2}");
minify_test(".foo { z-index: -2 }", ".foo{z-index:-2}");
minify_test(".foo { z-index: 999999 }", ".foo{z-index:999999}");
minify_test(".foo { z-index: 9999999 }", ".foo{z-index:9999999}");
minify_test(".foo { z-index: -9999999 }", ".foo{z-index:-9999999}");
}
}
3 changes: 3 additions & 0 deletions src/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,9 @@ define_properties! {
// https://drafts.fxtf.org/filter-effects-1/
"filter": Filter(FilterList<'i>, VendorPrefix) / WebKit,
"backdrop-filter": BackdropFilter(FilterList<'i>, VendorPrefix) / WebKit,

// https://drafts.csswg.org/css2/
"z-index": ZIndex(position::ZIndex),
}

impl<'i, T: smallvec::Array<Item = V>, V: Parse<'i>> Parse<'i> for SmallVec<T> {
Expand Down
35 changes: 34 additions & 1 deletion src/properties/position.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! The CSS position property.
//! CSS properties related to positioning.

use super::Property;
use crate::context::PropertyHandlerContext;
Expand All @@ -8,6 +8,7 @@ use crate::prefixes::Feature;
use crate::printer::Printer;
use crate::targets::Browsers;
use crate::traits::{Parse, PropertyHandler, ToCss};
use crate::values::number::CSSInteger;
use crate::vendor_prefix::VendorPrefix;
use cssparser::*;

Expand Down Expand Up @@ -62,6 +63,38 @@ impl ToCss for Position {
}
}

/// A value for the [z-index](https://drafts.csswg.org/css2/#z-index) property.
#[derive(Debug, Clone, PartialEq)]
pub enum ZIndex {
/// The `auto` keyword.
Auto,
/// An integer value.
Integer(CSSInteger),
}

impl<'i> Parse<'i> for ZIndex {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if let Ok(value) = input.expect_integer() {
return Ok(ZIndex::Integer(value));
}

input.expect_ident_matching("auto")?;
Ok(ZIndex::Auto)
}
}

impl ToCss for ZIndex {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match self {
ZIndex::Auto => dest.write_str("auto"),
ZIndex::Integer(value) => value.to_css(dest),
}
}
}

#[derive(Default)]
pub(crate) struct PositionHandler {
targets: Option<Browsers>,
Expand Down
24 changes: 3 additions & 21 deletions src/values/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,18 @@ impl ToCss for CSSNumber {
where
W: std::fmt::Write,
{
use cssparser::ToCss;
let number = *self;
let int_value = if number.fract() == 0.0 {
Some(number as i32)
} else {
None
};
let tok = Token::Number {
has_sign: number < 0.0,
value: number,
int_value,
};
if number != 0.0 && number.abs() < 1.0 {
let mut s = String::new();
tok.to_css(&mut s)?;
cssparser::ToCss::to_css(self, &mut s)?;
if number < 0.0 {
dest.write_char('-')?;
dest.write_str(s.trim_start_matches("-0"))
} else {
dest.write_str(s.trim_start_matches('0'))
}
} else {
tok.to_css(dest)?;
cssparser::ToCss::to_css(self, dest)?;
Ok(())
}
}
Expand Down Expand Up @@ -91,14 +80,7 @@ impl ToCss for CSSInteger {
where
W: std::fmt::Write,
{
use cssparser::ToCss;
let integer = *self;
let tok = Token::Number {
has_sign: integer < 0,
value: integer as f32,
int_value: Some(integer),
};
tok.to_css(dest)?;
cssparser::ToCss::to_css(self, dest)?;
Ok(())
}
}

0 comments on commit 839bcd1

Please sign in to comment.