@@ -39,17 +39,40 @@ type LinearGradientBackgroundImage = {
3939
4040// Radial Gradient
4141const RADIAL_SHAPE_REGEX = / ^ ( c i r c l e | e l l i p s e ) (?: \s | $ ) / i;
42- const RADIAL_SIZE_REGEX =
43- / ^ ( c l o s e s t - s i d e | c l o s e s t - c o r n e r | f a r t h e s t - s i d e | f a r t h e s t - c o r n e r | ( [ + - ] ? \d * \. ? \d + ) ( p x | % ) ? ) / i;
44- const RADIAL_POSITION_REGEX =
45- / ^ (?: a t \s + ) ? ( c e n t e r | t o p | r i g h t | b o t t o m | l e f t | ( [ + - ] ? \d * \. ? \d + ) ( p x | % ) ? ) (?: \s + ( c e n t e r | t o p | r i g h t | b o t t o m | l e f t | ( [ + - ] ? \d * \. ? \d + ) ( p x | % ) ? ) ) ? / i;
42+ const DEFAULT_RADIAL_SHAPE = 'ellipse' ;
43+ const DEFAULT_RADIAL_SIZE = {
44+ type : 'keyword' ,
45+ value : 'farthest-corner' ,
46+ } ;
47+ const DEFAULT_RADIAL_POSITION = {
48+ x : '50%' ,
49+ y : '50%' ,
50+ } ;
4651type RadialExtent =
47- 'closest-corner | closest-side | farthest-corner | farthest-side' ;
52+ | 'closest-corner'
53+ | 'closest-side'
54+ | 'farthest-corner'
55+ | 'farthest-side' ;
56+ type RadialGradientSize =
57+ | {
58+ type : 'keyword' ,
59+ value : RadialExtent ,
60+ }
61+ | {
62+ type : 'dimensions' ,
63+ x : string | number ,
64+ y : string | number ,
65+ } ;
66+ type RadialGradientShape = 'circle' | 'ellipse' ;
67+ type RadialGradientPosition = {
68+ x : number | string ,
69+ y : number | string ,
70+ } ;
4871type RadialGradientBackgroundImage = {
4972 type : 'radialGradient' ,
50- shape : 'circle' | 'ellipse' ,
51- size : RadialExtent | number | string ,
52- position : string ,
73+ shape : RadialGradientShape ,
74+ size : RadialGradientSize ,
75+ position : RadialGradientPosition ,
5376 colorStops : $ReadOnlyArray < {
5477 color : ColorStopColor ,
5578 position : ColorStopPosition ,
@@ -121,11 +144,57 @@ export default function processBackgroundImage(
121144 colorStops : processedColorStops ,
122145 } ) ;
123146 } else if ( bgImage . type === 'radialGradient' ) {
147+ let shape : RadialGradientShape = DEFAULT_RADIAL_SHAPE ;
148+ let size : RadialGradientSize = DEFAULT_RADIAL_SIZE ;
149+ let position : RadialGradientPosition = DEFAULT_RADIAL_POSITION ;
150+
151+ if (
152+ bgImage . shape != null &&
153+ ( bgImage . shape === 'circle' || bgImage . shape === 'ellipse' )
154+ ) {
155+ shape = bgImage . shape ;
156+ } else {
157+ // If the shape is invalid, return an empty array and do not apply any gradient. Same as web.
158+ return [ ] ;
159+ }
160+
161+ if ( bgImage . size != null ) {
162+ if (
163+ typeof bgImage . size === 'string' &&
164+ ( bgImage . size === 'closest-side' ||
165+ bgImage . size === 'closest-corner' ||
166+ bgImage . size === 'farthest-side' ||
167+ bgImage . size === 'farthest-corner' )
168+ ) {
169+ size = {
170+ type : 'keyword' ,
171+ value : bgImage . size ,
172+ } ;
173+ } else if (
174+ typeof bgImage . size === 'object' &&
175+ bgImage . size . x != null &&
176+ bgImage . size . y != null
177+ ) {
178+ size = {
179+ type : 'dimensions' ,
180+ x : bgImage . size . x ,
181+ y : bgImage . size . y ,
182+ } ;
183+ } else {
184+ // If the size is invalid, return an empty array and do not apply any gradient. Same as web.
185+ return [ ] ;
186+ }
187+ }
188+
189+ if ( bgImage . position != null ) {
190+ position = bgImage . position ;
191+ }
192+
124193 result = result . concat ( {
125194 type : 'radialGradient' ,
126- shape : 'circle' ,
127- size : 'closest-corner' ,
128- position : 'center' ,
195+ shape,
196+ size,
197+ position,
129198 colorStops : processedColorStops ,
130199 } ) ;
131200 }
@@ -225,14 +294,21 @@ function parseBackgroundImageCSSString(
225294}
226295
227296function parseRadialGradientCSSString (
228- cssString : string ,
229- ) : RadialGradientBackgroundImage {
297+ gradientContent : string ,
298+ ) : RadialGradientBackgroundImage | null {
299+ let shape = DEFAULT_RADIAL_SHAPE ;
300+ let size : RadialGradientSize = DEFAULT_RADIAL_SIZE ;
301+ let position : RadialGradientPosition = DEFAULT_RADIAL_POSITION ;
302+ const colorStops : Array < {
303+ color : ColorStopColor ,
304+ position : ColorStopPosition ,
305+ } > = [ ] ;
230306 return {
231307 type : 'radialGradient' ,
232- shape : 'circle' ,
233- size : 'closest-corner' ,
234- position : 'center' ,
235- colorStops : [ ] ,
308+ shape,
309+ size,
310+ position,
311+ colorStops,
236312 } ;
237313}
238314
0 commit comments