Skip to content

Commit 656bd40

Browse files
committed
Parse grid line properties
1 parent 9b94237 commit 656bd40

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6264,6 +6264,22 @@ mod tests {
62646264
minify_test(".foo { grid: dense auto-flow / 30% }", ".foo{grid:auto-flow dense/30%}");
62656265
minify_test(".foo { grid: auto-flow 300px / repeat(3, [line1 line2 line3] 200px) }", ".foo{grid:auto-flow 300px/repeat(3,[line1 line2 line3]200px)}");
62666266
minify_test(".foo { grid: auto-flow dense 40% / [line1] minmax(20em, max-content) }", ".foo{grid:auto-flow dense 40%/[line1]minmax(20em,max-content)}");
6267+
6268+
minify_test(".foo { grid-row-start: auto }", ".foo{grid-row-start:auto}");
6269+
minify_test(".foo { grid-row-start: some-area }", ".foo{grid-row-start:some-area}");
6270+
minify_test(".foo { grid-row-start: 2 }", ".foo{grid-row-start:2}");
6271+
minify_test(".foo { grid-row-start: 2 some-line }", ".foo{grid-row-start:2 some-line}");
6272+
minify_test(".foo { grid-row-start: some-line 2 }", ".foo{grid-row-start:2 some-line}");
6273+
minify_test(".foo { grid-row-start: span 3 }", ".foo{grid-row-start:span 3}");
6274+
minify_test(".foo { grid-row-start: span some-line }", ".foo{grid-row-start:span some-line}");
6275+
minify_test(".foo { grid-row-start: span some-line 1 }", ".foo{grid-row-start:span some-line}");
6276+
minify_test(".foo { grid-row-start: span 1 some-line }", ".foo{grid-row-start:span some-line}");
6277+
minify_test(".foo { grid-row-start: span 5 some-line }", ".foo{grid-row-start:span 5 some-line}");
6278+
minify_test(".foo { grid-row-start: span some-line 5 }", ".foo{grid-row-start:span 5 some-line}");
6279+
6280+
minify_test(".foo { grid-row-end: span 1 some-line }", ".foo{grid-row-end:span some-line}");
6281+
minify_test(".foo { grid-column-start: span 1 some-line }", ".foo{grid-column-start:span some-line}");
6282+
minify_test(".foo { grid-column-end: span 1 some-line }", ".foo{grid-column-end:span some-line}");
62676283
}
62686284

62696285
#[test]

src/properties/grid.rs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::values::ident::CustomIdent;
1010
use smallvec::SmallVec;
1111
use crate::values::length::serialize_dimension;
1212
use bitflags::bitflags;
13+
use crate::values::number::serialize_integer;
1314

1415
/// https://drafts.csswg.org/css-grid-2/#track-sizing
1516
#[derive(Debug, Clone, PartialEq)]
@@ -739,6 +740,7 @@ impl ToCss for GridTemplate {
739740
}
740741

741742
bitflags! {
743+
/// https://drafts.csswg.org/css-grid-2/#grid-auto-flow-property
742744
pub struct GridAutoFlow: u8 {
743745
const Row = 0b00;
744746
const Column = 0b01;
@@ -828,6 +830,7 @@ impl ToCss for GridAutoFlow {
828830
}
829831
}
830832

833+
/// https://drafts.csswg.org/css-grid-2/#grid-shorthand
831834
#[derive(Debug, Clone, PartialEq)]
832835
pub struct Grid {
833836
rows: TrackSizing,
@@ -842,7 +845,6 @@ impl Parse for Grid {
842845
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
843846
// <'grid-template'>
844847
if let Ok(template) = input.try_parse(GridTemplate::parse) {
845-
println!("{:?}", template);
846848
Ok(Grid {
847849
rows: template.rows,
848850
columns: template.columns,
@@ -901,7 +903,6 @@ fn parse_grid_auto_flow<'i, 't>(input: &mut Parser<'i, 't>, flow: GridAutoFlow)
901903

902904
impl ToCss for Grid {
903905
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
904-
println!("{:?}", self);
905906
if self.areas != GridTemplateAreas::None ||
906907
(self.rows != TrackSizing::None && self.columns != TrackSizing::None) ||
907908
(self.areas == GridTemplateAreas::None && self.auto_rows == TrackSizeList::default() && self.auto_columns == TrackSizeList::default() && self.auto_flow == GridAutoFlow::default()) {
@@ -938,3 +939,88 @@ impl ToCss for Grid {
938939
Ok(())
939940
}
940941
}
942+
943+
/// https://drafts.csswg.org/css-grid-2/#typedef-grid-row-start-grid-line
944+
#[derive(Debug, Clone, PartialEq)]
945+
pub enum GridLine {
946+
Auto,
947+
Ident(CustomIdent),
948+
Line(i32, Option<CustomIdent>),
949+
Span(i32, Option<CustomIdent>)
950+
}
951+
952+
impl Parse for GridLine {
953+
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
954+
if input.try_parse(|input| input.expect_ident_matching("auto")).is_ok() {
955+
return Ok(GridLine::Auto)
956+
}
957+
958+
if input.try_parse(|input| input.expect_ident_matching("span")).is_ok() {
959+
// TODO: is calc() supported here??
960+
let (line_number, ident) = if let Ok(line_number) = input.try_parse(|input| input.expect_integer()) {
961+
let ident = input.try_parse(CustomIdent::parse).ok();
962+
(line_number, ident)
963+
} else if let Ok(ident) = input.try_parse(CustomIdent::parse) {
964+
let line_number = input.try_parse(|input| input.expect_integer()).unwrap_or(1);
965+
(line_number, Some(ident))
966+
} else {
967+
return Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
968+
};
969+
970+
if line_number == 0 {
971+
return Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
972+
}
973+
974+
return Ok(GridLine::Span(line_number, ident))
975+
}
976+
977+
if let Ok(line_number) = input.try_parse(|input| input.expect_integer()) {
978+
if line_number == 0 {
979+
return Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
980+
}
981+
let ident = input.try_parse(CustomIdent::parse).ok();
982+
return Ok(GridLine::Line(line_number, ident))
983+
}
984+
985+
let ident = CustomIdent::parse(input)?;
986+
if let Ok(line_number) = input.try_parse(|input| input.expect_integer()) {
987+
if line_number == 0 {
988+
return Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
989+
}
990+
return Ok(GridLine::Line(line_number, Some(ident)))
991+
}
992+
993+
Ok(GridLine::Ident(ident))
994+
}
995+
}
996+
997+
impl ToCss for GridLine {
998+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
999+
match self {
1000+
GridLine::Auto => dest.write_str("auto"),
1001+
GridLine::Ident(id) => id.to_css(dest),
1002+
GridLine::Line(line_number, id) => {
1003+
serialize_integer(*line_number, dest)?;
1004+
if let Some(id) = id {
1005+
dest.write_char(' ')?;
1006+
id.to_css(dest)?;
1007+
}
1008+
Ok(())
1009+
}
1010+
GridLine::Span(line_number, id) => {
1011+
dest.write_str("span ")?;
1012+
if *line_number != 1 || id.is_none() {
1013+
serialize_integer(*line_number, dest)?;
1014+
if id.is_some() {
1015+
dest.write_char(' ')?;
1016+
}
1017+
}
1018+
1019+
if let Some(id) = id {
1020+
id.to_css(dest)?;
1021+
}
1022+
Ok(())
1023+
}
1024+
}
1025+
}
1026+
}

src/properties/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ define_properties! {
415415
"grid-template": GridTemplate(GridTemplate),
416416
#[cfg(feature = "grid")]
417417
"grid": Grid(Grid),
418+
#[cfg(feature = "grid")]
419+
"grid-row-start": GridRowStart(GridLine),
420+
#[cfg(feature = "grid")]
421+
"grid-row-end": GridRowEnd(GridLine),
422+
#[cfg(feature = "grid")]
423+
"grid-column-start": GridColumnStart(GridLine),
424+
#[cfg(feature = "grid")]
425+
"grid-column-end": GridColumnEnd(GridLine),
418426

419427
"margin-top": MarginTop(LengthPercentageOrAuto),
420428
"margin-bottom": MarginBottom(LengthPercentageOrAuto),

src/values/number.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl std::convert::From<Calc<f32>> for f32 {
4242
}
4343
}
4444

45-
pub fn serialize_number<W>(number: f32, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
45+
pub(crate) fn serialize_number<W>(number: f32, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
4646
use cssparser::ToCss;
4747
let int_value = if number.fract() == 0.0 {
4848
Some(number as i32)
@@ -67,3 +67,13 @@ pub fn serialize_number<W>(number: f32, dest: &mut Printer<W>) -> std::fmt::Resu
6767
tok.to_css(dest)
6868
}
6969
}
70+
71+
pub(crate) fn serialize_integer<W>(integer: i32, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
72+
use cssparser::ToCss;
73+
let tok = Token::Number {
74+
has_sign: integer < 0,
75+
value: integer as f32,
76+
int_value: Some(integer)
77+
};
78+
tok.to_css(dest)
79+
}

0 commit comments

Comments
 (0)