@@ -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 ;
5+ use super :: length:: { LengthPercentage , LengthValue } ;
66use super :: percentage:: Percentage ;
77
88/// https://www.w3.org/TR/css-backgrounds-3/#background-position
@@ -126,17 +126,25 @@ impl ToCss for Position {
126126 fn to_css < W > ( & self , dest : & mut Printer < W > ) -> std:: fmt:: Result where W : std:: fmt:: Write {
127127 match ( & self . x , & self . y ) {
128128 (
129- x_pos @ & HorizontalPosition :: Side ( _ , Some ( _) ) ,
129+ x_pos @ & HorizontalPosition :: Side ( side , Some ( _) ) ,
130130 & VerticalPosition :: Length ( ref y_lp) ,
131- ) => {
131+ ) if side != HorizontalPositionKeyword :: Left => {
132132 x_pos. to_css ( dest) ?;
133133 dest. write_str ( " top " ) ?;
134134 y_lp. to_css ( dest)
135135 } ,
136+ (
137+ x_pos @ & HorizontalPosition :: Side ( side, Some ( _) ) ,
138+ & VerticalPosition :: Center
139+ ) if side != HorizontalPositionKeyword :: Left => {
140+ // If there is a side keyword with an offset, "center" must be a keyword not a percentage.
141+ x_pos. to_css ( dest) ?;
142+ dest. write_str ( " center" )
143+ } ,
136144 (
137145 & HorizontalPosition :: Length ( ref x_lp) ,
138- y_pos @ & VerticalPosition :: Side ( _ , Some ( _) ) ,
139- ) => {
146+ y_pos @ & VerticalPosition :: Side ( side , Some ( _) ) ,
147+ ) if side != VerticalPositionKeyword :: Top => {
140148 dest. write_str ( "left " ) ?;
141149 x_lp. to_css ( dest) ?;
142150 dest. write_str ( " " ) ?;
@@ -153,7 +161,7 @@ impl ToCss for Position {
153161 & HorizontalPosition :: Side ( side, None ) ,
154162 & VerticalPosition :: Center ,
155163 ) => {
156- let p: Percentage = side. into ( ) ;
164+ let p: LengthPercentage = side. into ( ) ;
157165 p. to_css ( dest)
158166 } ,
159167 (
@@ -173,7 +181,7 @@ impl ToCss for Position {
173181 & HorizontalPosition :: Side ( side, None ) ,
174182 & VerticalPosition :: Length ( LengthPercentage :: Percentage ( Percentage ( y_lp) ) ) ,
175183 ) if y_lp == 0.5 => {
176- let p: Percentage = side. into ( ) ;
184+ let p: LengthPercentage = side. into ( ) ;
177185 p. to_css ( dest)
178186 } ,
179187 (
@@ -186,16 +194,50 @@ impl ToCss for Position {
186194 & HorizontalPosition :: Side ( x, None ) ,
187195 & VerticalPosition :: Side ( y, None )
188196 ) => {
189- let x: Percentage = x. into ( ) ;
190- let y: Percentage = y. into ( ) ;
197+ let x: LengthPercentage = x. into ( ) ;
198+ let y: LengthPercentage = y. into ( ) ;
191199 x. to_css ( dest) ?;
192200 dest. write_str ( " " ) ?;
193201 y. to_css ( dest)
194202 } ,
195203 ( x_pos, y_pos) => {
196- x_pos. to_css ( dest) ?;
197- dest. write_str ( " " ) ?;
198- y_pos. to_css ( dest)
204+ let zero = LengthPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ;
205+ let fifty = LengthPercentage :: Percentage ( Percentage ( 0.5 ) ) ;
206+ let x_len = match & x_pos {
207+ HorizontalPosition :: Side ( HorizontalPositionKeyword :: Left , len) => {
208+ if let Some ( len) = len {
209+ Some ( len)
210+ } else {
211+ Some ( & zero)
212+ }
213+ } ,
214+ HorizontalPosition :: Length ( len) => Some ( len) ,
215+ HorizontalPosition :: Center => Some ( & fifty) ,
216+ _ => None
217+ } ;
218+
219+ let y_len = match & y_pos {
220+ VerticalPosition :: Side ( VerticalPositionKeyword :: Top , len) => {
221+ if let Some ( len) = len {
222+ Some ( len)
223+ } else {
224+ Some ( & zero)
225+ }
226+ } ,
227+ VerticalPosition :: Length ( len) => Some ( len) ,
228+ VerticalPosition :: Center => Some ( & fifty) ,
229+ _ => None
230+ } ;
231+
232+ if let ( Some ( x) , Some ( y) ) = ( x_len, y_len) {
233+ x. to_css ( dest) ?;
234+ dest. write_str ( " " ) ?;
235+ y. to_css ( dest)
236+ } else {
237+ x_pos. to_css ( dest) ?;
238+ dest. write_str ( " " ) ?;
239+ y_pos. to_css ( dest)
240+ }
199241 } ,
200242 }
201243 }
@@ -256,11 +298,11 @@ enum_property!(HorizontalPositionKeyword,
256298 Right
257299) ;
258300
259- impl Into < Percentage > for HorizontalPositionKeyword {
260- fn into ( self ) -> Percentage {
301+ impl Into < LengthPercentage > for HorizontalPositionKeyword {
302+ fn into ( self ) -> LengthPercentage {
261303 match self {
262- HorizontalPositionKeyword :: Left => Percentage ( 0.0 ) ,
263- HorizontalPositionKeyword :: Right => Percentage ( 1.0 )
304+ HorizontalPositionKeyword :: Left => LengthPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ,
305+ HorizontalPositionKeyword :: Right => LengthPercentage :: Percentage ( Percentage ( 1.0 ) )
264306 }
265307 }
266308}
@@ -270,11 +312,11 @@ enum_property!(VerticalPositionKeyword,
270312 Bottom
271313) ;
272314
273- impl Into < Percentage > for VerticalPositionKeyword {
274- fn into ( self ) -> Percentage {
315+ impl Into < LengthPercentage > for VerticalPositionKeyword {
316+ fn into ( self ) -> LengthPercentage {
275317 match self {
276- VerticalPositionKeyword :: Top => Percentage ( 0.0 ) ,
277- VerticalPositionKeyword :: Bottom => Percentage ( 1.0 )
318+ VerticalPositionKeyword :: Top => LengthPercentage :: Dimension ( LengthValue :: Px ( 0.0 ) ) ,
319+ VerticalPositionKeyword :: Bottom => LengthPercentage :: Percentage ( Percentage ( 1.0 ) )
278320 }
279321 }
280322}
0 commit comments