@@ -190,47 +190,85 @@ function parseCSSLinearGradient(
190190 // If first part is not an angle/direction or a color stop, return an empty array and do not apply any gradient. Same as web.
191191 return [ ] ;
192192 }
193- colorStopRegex . lastIndex = 0 ;
194193
194+ const colorStopsString = parts . join ( ',' ) ;
195195 const colorStops = [ ] ;
196- const fullColorStopsStr = parts . join ( ',' ) ;
197- let colorStopMatch ;
198- while ( ( colorStopMatch = colorStopRegex . exec ( fullColorStopsStr ) ) ) {
199- const [ , color , position1 , position2 ] = colorStopMatch ;
200- const processedColor = processColor ( color . trim ( ) . toLowerCase ( ) ) ;
201- if ( processedColor == null ) {
202- // If a color is invalid, return an empty array and do not apply any gradient. Same as web.
196+ // split by comma, but not if it's inside a parentheses. e.g. red, rgba(0, 0, 0, 0.5), green => ["red", "rgba(0, 0, 0, 0.5)", "green"]
197+ const stops = colorStopsString . split ( / , (? ! [ ^ ( ] * \) ) / ) ;
198+ for ( const stop of stops ) {
199+ const trimmedStop = stop . trim ( ) . toLowerCase ( ) ;
200+ // Match function like pattern or single words
201+ const parts = trimmedStop . match ( / \S + \( [ ^ ) ] * \) | \S + / g) ;
202+ if ( parts == null ) {
203+ // If a color stop is invalid, return an empty array and do not apply any gradient. Same as web.
203204 return [ ] ;
204205 }
205-
206- if ( typeof position1 !== 'undefined' ) {
207- if ( position1 . endsWith ( '%' ) ) {
206+ // Case 1: [color, position, position]
207+ if ( parts . length === 3 ) {
208+ const color = parts [ 0 ] ;
209+ const position1 = parts [ 1 ] ;
210+ const position2 = parts [ 2 ] ;
211+ const processedColor = processColor ( color ) ;
212+ if ( processedColor == null ) {
213+ // If a color is invalid, return an empty array and do not apply any gradient. Same as web.
214+ return [ ] ;
215+ }
216+ if ( position1 . endsWith ( '%' ) && position2 . endsWith ( '%' ) ) {
208217 colorStops . push ( {
209218 color : processedColor ,
210219 position : parseFloat ( position1 ) / 100 ,
211220 } ) ;
221+ colorStops . push ( {
222+ color : processedColor ,
223+ position : parseFloat ( position2 ) / 100 ,
224+ } ) ;
212225 } else {
213226 // If a position is invalid, return an empty array and do not apply any gradient. Same as web.
214227 return [ ] ;
215228 }
216- } else {
217- colorStops . push ( {
218- color : processedColor ,
219- position : null ,
220- } ) ;
221229 }
222-
223- if ( typeof position2 !== 'undefined ') {
224- if ( position2 . endsWith ( '% ') ) {
230+ // Case 2: [color, position]
231+ else if ( parts . length === 2 ) {
232+ const color = parts [ 0 ] ;
233+ const position = parts [ 1 ] ;
234+ const processedColor = processColor ( color ) ;
235+ if ( processedColor == null ) {
236+ // If a color is invalid, return an empty array and do not apply any gradient. Same as web.
237+ return [ ] ;
238+ }
239+ if ( position . endsWith ( '% ') ) {
225240 colorStops . push ( {
226241 color : processedColor ,
227- position : parseFloat ( position2 ) / 100 ,
242+ position : parseFloat ( position ) / 100 ,
228243 } ) ;
229244 } else {
230245 // If a position is invalid, return an empty array and do not apply any gradient. Same as web.
231246 return [ ] ;
232247 }
233248 }
249+ // Case 3: [color]
250+ // Case 4: [position] => transition hint syntax
251+ else if ( parts . length === 1 ) {
252+ if ( parts [ 0 ] . endsWith ( '% ') ) {
253+ colorStops . push ( {
254+ color : null ,
255+ position : parseFloat ( parts [ 0 ] ) / 100 ,
256+ } ) ;
257+ } else {
258+ const processedColor = processColor ( parts [ 0 ] ) ;
259+ if ( processedColor == null ) {
260+ // If a color is invalid, return an empty array and do not apply any gradient. Same as web.
261+ return [ ] ;
262+ }
263+ colorStops . push ( {
264+ color : processedColor ,
265+ position : null ,
266+ } ) ;
267+ }
268+ } else {
269+ // If a color stop is invalid, return an empty array and do not apply any gradient. Same as web.
270+ return [ ] ;
271+ }
234272 }
235273
236274 const fixedColorStops = getFixedColorStops(colorStops);
0 commit comments