@@ -2,7 +2,7 @@ use cssparser::*;
22use crate :: traits:: { Parse , ToCss } ;
33use crate :: macros:: enum_property;
44use crate :: printer:: Printer ;
5- use super :: length:: { LengthPercentage , LengthValue } ;
5+ use super :: length:: { LengthPercentage } ;
66use super :: percentage:: Percentage ;
77use crate :: error:: { ParserError , PrinterError } ;
88
@@ -22,10 +22,11 @@ impl Position {
2222 }
2323
2424 pub fn is_center ( & self ) -> bool {
25- * self == Position :: center ( ) || * self == Position {
26- x : HorizontalPosition :: Length ( LengthPercentage :: Percentage ( Percentage ( 0.5 ) ) ) ,
27- y : VerticalPosition :: Length ( LengthPercentage :: Percentage ( Percentage ( 0.5 ) ) )
28- }
25+ self . x . is_center ( ) && self . y . is_center ( )
26+ }
27+
28+ pub fn is_zero ( & self ) -> bool {
29+ self . x . is_zero ( ) && self . y . is_zero ( )
2930 }
3031}
3132
@@ -136,8 +137,8 @@ impl ToCss for Position {
136137 } ,
137138 (
138139 x_pos @ & HorizontalPosition :: Side ( side, Some ( _) ) ,
139- & VerticalPosition :: Center
140- ) if side != HorizontalPositionKeyword :: Left => {
140+ y
141+ ) if side != HorizontalPositionKeyword :: Left && y . is_center ( ) => {
141142 // If there is a side keyword with an offset, "center" must be a keyword not a percentage.
142143 x_pos. to_css ( dest) ?;
143144 dest. write_str ( " center" )
@@ -151,44 +152,28 @@ impl ToCss for Position {
151152 dest. write_str ( " " ) ?;
152153 y_pos. to_css ( dest)
153154 } ,
154- (
155- & HorizontalPosition :: Length ( ref x_lp) ,
156- & VerticalPosition :: Center
157- ) => {
158- // `center` is assumed if omitted.
159- x_lp. to_css ( dest)
160- } ,
161- (
162- & HorizontalPosition :: Side ( side, None ) ,
163- & VerticalPosition :: Center ,
164- ) => {
165- let p: LengthPercentage = side. into ( ) ;
166- p. to_css ( dest)
167- } ,
168- (
169- & HorizontalPosition :: Center ,
170- y_pos @ & VerticalPosition :: Side ( _, None ) ,
171- ) => {
172- y_pos. to_css ( dest)
155+ ( x, y) if x. is_center ( ) && y. is_center ( ) => {
156+ // `center center` => 50%
157+ x. to_css ( dest)
173158 } ,
174159 (
175160 & HorizontalPosition :: Length ( ref x_lp) ,
176- & VerticalPosition :: Length ( LengthPercentage :: Percentage ( Percentage ( y_lp ) ) )
177- ) if y_lp == 0.5 => {
178- // 50% is equivalent to `center`, which may be omitted.
161+ y
162+ ) if y . is_center ( ) => {
163+ // `center` is assumed if omitted.
179164 x_lp. to_css ( dest)
180165 } ,
181166 (
182167 & HorizontalPosition :: Side ( side, None ) ,
183- & VerticalPosition :: Length ( LengthPercentage :: Percentage ( Percentage ( y_lp ) ) ) ,
184- ) if y_lp == 0.5 => {
168+ y ,
169+ ) if y . is_center ( ) => {
185170 let p: LengthPercentage = side. into ( ) ;
186171 p. to_css ( dest)
187172 } ,
188173 (
189- & HorizontalPosition :: Length ( LengthPercentage :: Percentage ( Percentage ( x_lp ) ) ) ,
174+ x ,
190175 y_pos @ & VerticalPosition :: Side ( _, None ) ,
191- ) if x_lp == 0.5 => {
176+ ) if x . is_center ( ) => {
192177 y_pos. to_css ( dest)
193178 } ,
194179 (
@@ -202,16 +187,21 @@ impl ToCss for Position {
202187 y. to_css ( dest)
203188 } ,
204189 ( x_pos, y_pos) => {
205- let zero = LengthPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ;
190+ let zero = LengthPercentage :: zero ( ) ;
206191 let fifty = LengthPercentage :: Percentage ( Percentage ( 0.5 ) ) ;
207192 let x_len = match & x_pos {
208193 HorizontalPosition :: Side ( HorizontalPositionKeyword :: Left , len) => {
209194 if let Some ( len) = len {
210- Some ( len)
195+ if * len == 0.0 {
196+ Some ( & zero)
197+ } else {
198+ Some ( len)
199+ }
211200 } else {
212201 Some ( & zero)
213202 }
214203 } ,
204+ HorizontalPosition :: Length ( len) if * len == 0.0 => Some ( & zero) ,
215205 HorizontalPosition :: Length ( len) => Some ( len) ,
216206 HorizontalPosition :: Center => Some ( & fifty) ,
217207 _ => None
@@ -220,11 +210,16 @@ impl ToCss for Position {
220210 let y_len = match & y_pos {
221211 VerticalPosition :: Side ( VerticalPositionKeyword :: Top , len) => {
222212 if let Some ( len) = len {
223- Some ( len)
213+ if * len == 0.0 {
214+ Some ( & zero)
215+ } else {
216+ Some ( len)
217+ }
224218 } else {
225219 Some ( & zero)
226220 }
227221 } ,
222+ VerticalPosition :: Length ( len) if * len == 0.0 => Some ( & zero) ,
228223 VerticalPosition :: Length ( len) => Some ( len) ,
229224 VerticalPosition :: Center => Some ( & fifty) ,
230225 _ => None
@@ -254,6 +249,20 @@ pub enum PositionComponent<S> {
254249 Side ( S , Option < LengthPercentage > ) ,
255250}
256251
252+ impl < S > PositionComponent < S > {
253+ fn is_center ( & self ) -> bool {
254+ match self {
255+ PositionComponent :: Center => true ,
256+ PositionComponent :: Length ( LengthPercentage :: Percentage ( Percentage ( p) ) ) => * p == 0.5 ,
257+ _ => false
258+ }
259+ }
260+
261+ fn is_zero ( & self ) -> bool {
262+ matches ! ( self , PositionComponent :: Length ( len) if * len == 0.0 )
263+ }
264+ }
265+
257266impl < S : Parse > Parse for PositionComponent < S > {
258267 fn parse < ' i , ' t > ( input : & mut Parser < ' i , ' t > ) -> Result < Self , ParseError < ' i , ParserError < ' i > > > {
259268 if input. try_parse ( |i| i. expect_ident_matching ( "center" ) ) . is_ok ( ) {
@@ -302,7 +311,7 @@ enum_property!(HorizontalPositionKeyword,
302311impl Into < LengthPercentage > for HorizontalPositionKeyword {
303312 fn into ( self ) -> LengthPercentage {
304313 match self {
305- HorizontalPositionKeyword :: Left => LengthPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ,
314+ HorizontalPositionKeyword :: Left => LengthPercentage :: zero ( ) ,
306315 HorizontalPositionKeyword :: Right => LengthPercentage :: Percentage ( Percentage ( 1.0 ) )
307316 }
308317 }
@@ -316,7 +325,7 @@ enum_property!(VerticalPositionKeyword,
316325impl Into < LengthPercentage > for VerticalPositionKeyword {
317326 fn into ( self ) -> LengthPercentage {
318327 match self {
319- VerticalPositionKeyword :: Top => LengthPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ,
328+ VerticalPositionKeyword :: Top => LengthPercentage :: zero ( ) ,
320329 VerticalPositionKeyword :: Bottom => LengthPercentage :: Percentage ( Percentage ( 1.0 ) )
321330 }
322331 }
0 commit comments