@@ -72,6 +72,106 @@ function isColorToken(token: string): boolean {
7272// <length-percentage> or <angle-percentage>: `10%`, `0`, `12px`, `2em`, `-5%`, `30deg`, `.25turn`
7373const POSITION_RE = / ^ [ - + ] ? \d * \. ? \d + (?: % | [ a - z ] + ) ? $ / i
7474const PCT_RE = / ^ [ - + ] ? \d * \. ? \d + % ? $ /
75+ // an `at` offset: the position controls hold percentages only (`0` is mapped to `0%` first)
76+ const POS_PCT_RE = / ^ [ - + ] ? \d * \. ? \d + % $ /
77+ const LENGTH_RE = / ^ [ - + ] ? \d * \. ? \d + (?: p x | r ? e m | r ? e x | r ? c h | r ? l h | [ s l d ] ? v (?: w | h | i | b | m i n | m a x ) | c q (?: w | h | i | b | m i n | m a x ) | c m | m m | q | i n | p t | p c ) $ / i
78+
79+ // A conic stop position given as an angle (`90deg`, `.25turn`, `100grad`, `1.5708rad`)
80+ const ANGLE_RE = / ^ ( [ - + ] ? \d * \. ? \d + ) ( d e g | t u r n | g r a d | r a d ) $ / i
81+ const TURN_UNITS : Record < string , number > = { deg : 360 , turn : 1 , grad : 400 , rad : 2 * Math . PI }
82+
83+ // In a conic gradient a percentage is a fraction of the full turn, so `90deg` is exactly `25%`.
84+ // The stop store is percentage-based: converting keeps the stop where it was instead of
85+ // dropping it and letting the stop auto-distribute (which turns a hard-edged pie into a blend).
86+ function angleToTurnPct ( token : string ) : string | null {
87+ const m = ANGLE_RE . exec ( token . trim ( ) )
88+ if ( ! m ) return null
89+ const pct = ( parseFloat ( m [ 1 ] ) / TURN_UNITS [ m [ 2 ] . toLowerCase ( ) ] ) * 100
90+ return parseFloat ( pct . toFixed ( 4 ) ) + '%'
91+ }
92+
93+ const H_KW : Record < string , number > = { left : 0 , center : 50 , right : 100 }
94+ const V_KW : Record < string , number > = { top : 0 , center : 50 , bottom : 100 }
95+
96+ // CSS <position> after `at` (radial/conic): 1-4 values, keywords in either order
97+ // (`center top`, `left top`), keyword + offset pairs (`right 10% bottom 20%`).
98+ // Two keywords become a named position the UI offers (`top`, `top left`); anything with an
99+ // offset becomes x/y percentages. A 1-2 value length offset (`20px 30%`) centers only that axis.
100+ // Returns {} for anything else the percentage-based controls can't hold (invalid CSS, px offsets
101+ // in a 3-4 value position), which leaves the position at center.
102+ export function parsePosition ( input : string ) : { namedPosition ?: string ; position ?: { x : string ; y : string } } {
103+ const toks = input . trim ( ) . toLowerCase ( ) . split ( / \s + / ) . filter ( Boolean ) . map ( t => ( t === '0' ? '0%' : t ) )
104+ const isKw = ( t : string ) => t in H_KW || t in V_KW
105+ // the position stores are percentages (the output appends `%`), so only % or 0 offsets fit;
106+ // a unitless number (`left 30`) is invalid CSS, not 30%
107+ const isVal = ( t : string ) => POS_PCT_RE . test ( t )
108+ // a length offset (`20px`) is valid CSS the controls can't hold
109+ const isLen = ( t : string ) => LENGTH_RE . test ( t )
110+ // gradient <position> has no 3-value form (`left 10% top` is a background-position only)
111+ if ( ! toks . length || toks . length === 3 || toks . length > 4 ) return { }
112+ if ( ! toks . every ( t => isKw ( t ) || isVal ( t ) || ( toks . length <= 2 && isLen ( t ) ) ) ) return { }
113+ const pct = ( n : number ) => parseFloat ( n . toFixed ( 4 ) ) + '%'
114+ // 1-2 values: a length axis falls back to center, the other axis is kept (`20px 30%` = 50% 30%)
115+ if ( toks . length <= 2 && toks . some ( isLen ) ) {
116+ if ( toks . every ( t => isLen ( t ) || t === 'center' ) ) return { }
117+ const [ a , b = 'center' ] = toks
118+ if ( a in V_KW && ! ( a in H_KW ) ) return { }
119+ if ( b in H_KW && ! ( b in V_KW ) ) return { }
120+ const axis = ( t : string , kw : Record < string , number > ) => ( isLen ( t ) ? '50%' : t in kw ? pct ( kw [ t ] ) : t )
121+ return { position : { x : axis ( a , H_KW ) , y : axis ( b , V_KW ) } }
122+ }
123+
124+ if ( toks . every ( isKw ) ) {
125+ if ( toks . length > 2 ) return { }
126+ let h = 'center' , v = 'center'
127+ for ( const t of toks ) {
128+ if ( t === 'left' || t === 'right' ) { if ( h !== 'center' ) return { } ; h = t }
129+ else if ( t === 'top' || t === 'bottom' ) { if ( v !== 'center' ) return { } ; v = t }
130+ }
131+ const name = [ v , h ] . filter ( k => k !== 'center' ) . join ( ' ' )
132+ return { namedPosition : name || 'center' }
133+ }
134+
135+ if ( toks . length <= 2 ) {
136+ // 1-2 values with an offset: first is x, second is y (`30%`, `left 20%`, `25% top`)
137+ const [ a , b = 'center' ] = toks
138+ if ( a in V_KW && ! ( a in H_KW ) ) return { }
139+ if ( b in H_KW && ! ( b in V_KW ) ) return { }
140+ return {
141+ position : {
142+ x : a in H_KW ? pct ( H_KW [ a ] ) : a ,
143+ y : b in V_KW ? pct ( V_KW [ b ] ) : b ,
144+ } ,
145+ }
146+ }
147+
148+ // 3-4 values: keyword [offset] keyword [offset], axes given by the keywords
149+ const pairs : Array < { kw : string ; off : string | null } > = [ ]
150+ for ( let i = 0 ; i < toks . length ; i ++ ) {
151+ const kw = toks [ i ]
152+ if ( ! isKw ( kw ) ) return { }
153+ const off = isVal ( toks [ i + 1 ] ?? '' ) ? toks [ ++ i ] : null
154+ if ( off && kw === 'center' ) return { }
155+ pairs . push ( { kw, off } )
156+ }
157+ if ( pairs . length !== 2 ) return { }
158+ let x : string | null = null , y : string | null = null
159+ // `right 10%` is 10% in from the right edge
160+ const resolve = ( off : string | null , far : boolean ) =>
161+ off == null ? pct ( far ? 100 : 0 ) : pct ( far ? 100 - parseFloat ( off ) : parseFloat ( off ) )
162+ const assign = ( axis : 'x' | 'y' , v : string ) => {
163+ if ( axis === 'x' ) { if ( x != null ) return false ; x = v } else { if ( y != null ) return false ; y = v }
164+ return true
165+ }
166+ let centers = 0
167+ for ( const p of pairs ) {
168+ if ( p . kw === 'center' ) { centers ++ ; continue }
169+ const axis = p . kw === 'left' || p . kw === 'right' ? 'x' : 'y'
170+ if ( ! assign ( axis , resolve ( p . off , p . kw === 'right' || p . kw === 'bottom' ) ) ) return { }
171+ }
172+ for ( ; centers > 0 ; centers -- ) if ( ! assign ( x == null ? 'x' : 'y' , pct ( 50 ) ) ) return { }
173+ return x != null && y != null ? { position : { x, y } } : { }
174+ }
75175
76176// The color at the start of a stop segment (`red 10px`, `oklch(70% .2 40) 5%`), or null.
77177// Prelude segments (`to right`, `circle at 25% 25%`, `from 45deg in oklch`) never start with one.
@@ -204,19 +304,7 @@ export function parseGradient(input: string): ParsedGradient {
204304
205305 function extractPosition ( src : string ) : { namedPosition ?: string | null ; position ?: { x :string | null ; y :string | null } } {
206306 const posMatch = src . match ( / \b a t \s + ( .+ ) $ / i)
207- if ( ! posMatch ) return { }
208- const rhs = posMatch [ 1 ] . trim ( )
209- // try named positions
210- const named = [ 'center' , 'top left' , 'top' , 'top right' , 'right' , 'bottom right' , 'bottom' , 'bottom left' , 'left' , 'top center' , 'bottom center' , 'left center' , 'right center' ]
211- for ( const n of named ) {
212- if ( rhs . toLowerCase ( ) . startsWith ( n ) ) return { namedPosition : n }
213- }
214- // split into two tokens for x/y
215- const parts = rhs . split ( / \s + / )
216- if ( parts . length >= 2 ) {
217- return { position : { x : parts [ 0 ] , y : parts [ 1 ] } }
218- }
219- return { }
307+ return posMatch ? parsePosition ( posMatch [ 1 ] ) : { }
220308 }
221309
222310 if ( type === 'linear' ) {
@@ -267,15 +355,18 @@ export function parseGradient(input: string): ParsedGradient {
267355 }
268356
269357 // Build stops/hints from remaining segments
358+ // (a repeating gradient keeps its raw units: its tile *is* the stop positions)
359+ const turnStops = type === 'conic' && ! repeating
270360 const stops : ParsedGradient [ 'stops' ] = [ ]
271361 for ( let i = firstColorIndex ; i < segments . length ; i ++ ) {
272362 const seg = segments [ i ] . trim ( )
273363 if ( ! seg ) continue
274364
275365 // hint: a bare percentage value
276- if ( PCT_RE . test ( seg ) || ( repeating && POSITION_RE . test ( seg ) ) ) {
366+ const segTurnPct = turnStops ? angleToTurnPct ( seg ) : null
367+ if ( segTurnPct || PCT_RE . test ( seg ) || ( repeating && POSITION_RE . test ( seg ) ) ) {
277368 // percentages are stored unitless, lengths keep their unit (`10px`)
278- const pct = seg . trim ( ) . replace ( / % $ / , '' )
369+ const pct = ( segTurnPct ?? seg . trim ( ) ) . replace ( / % $ / , '' )
279370 stops . push ( { kind : 'hint' , auto : null , percentage : pct } )
280371 continue
281372 }
@@ -325,7 +416,10 @@ export function parseGradient(input: string): ParsedGradient {
325416 // Percentages and unitless numbers. Lengths/angles (px, em, deg...) are left unset for
326417 // normal gradients so the UI can auto-distribute them, but a repeating gradient's tile
327418 // *is* its stop positions (`#000 0 10px, #fff 10px 20px`): drop them and it stops repeating.
328- if ( PCT_RE . test ( t ) || ( repeating && POSITION_RE . test ( t ) ) ) {
419+ const turnPct = turnStops ? angleToTurnPct ( t ) : null
420+ if ( turnPct ) {
421+ posTokens . push ( turnPct )
422+ } else if ( PCT_RE . test ( t ) || ( repeating && POSITION_RE . test ( t ) ) ) {
329423 posTokens . push ( t )
330424 }
331425 }
0 commit comments