Skip to content

Commit 6957184

Browse files
committed
fix stripes and add test
1 parent a8933c9 commit 6957184

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/lib/roundtrip.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ describe('round-trip: builder output parses back into app format', () => {
3232
expect(parsedClassic.stops.length).toBeGreaterThan(1)
3333
})
3434

35+
it('preserves explicit span positions for stripe-style presets', () => {
36+
// Match the structure of the "Stripes" preset as a single layer
37+
const stripes = makeLayer({
38+
type: 'linear',
39+
space: 'oklab',
40+
linear: { named_angle: 'to right top', angle: null },
41+
stops: [
42+
stop('#fff'), // leading white, no positions
43+
hint(0), // midpoint (auto-managed)
44+
stop('#000', 0, 20),
45+
hint(0),
46+
stop('#fff', 0, 40),
47+
hint(0),
48+
stop('#000', 0, 60),
49+
hint(0),
50+
stop('#fff', 0, 80),
51+
hint(0),
52+
stop('#000', 0, 100),
53+
],
54+
})
55+
56+
const { modern } = buildGradientStrings(stripes as any)
57+
58+
// The output should contain both start and end positions for each black/white span,
59+
// not collapse them down to a single 0% position.
60+
expect(modern.replace(/\s+/g, ' ')).toContain('#000 0% 20%')
61+
expect(modern.replace(/\s+/g, ' ')).toContain('#fff 0% 40%')
62+
expect(modern.replace(/\s+/g, ' ')).toContain('#000 0% 60%')
63+
expect(modern.replace(/\s+/g, ' ')).toContain('#fff 0% 80%')
64+
expect(modern.replace(/\s+/g, ' ')).toContain('#000 0% 100%')
65+
})
66+
3567
it('radial and conic builder outputs also parse successfully', () => {
3668
const radial = makeLayer({ type: 'radial', radial: { shape: 'circle', size: 'farthest-corner', named_position: 'center', position: { x: 50, y: 50 } } as any })
3769
const conic = makeLayer({ type: 'conic', conic: { angle: 0, named_position: 'center', position: { x: 50, y: 50 } } as any })

src/utils/gradientString.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,15 @@ function stopsToStrings(stops: any[], { convert_colors, new_lines }: { convert_c
106106
let p1: any = s.position1
107107
let p2: any = s.position2
108108

109-
// If positions equal computed auto position, omit them
109+
// Only ever suppress the "auto" value for the primary position.
110+
// Secondary positions are kept even when they equal the auto value so
111+
// that explicit spans from presets/URL imports (like the Stripes preset)
112+
// are preserved.
110113
if (p1 != null && s.auto != null && String(p1) == String(s.auto)) p1 = null
111-
if (p2 != null && s.auto != null && String(p2) == String(s.auto)) p2 = null
112114

113-
// Omit default endpoints regardless of whether value is in p1 or p2
115+
// Omit browser default *leading* endpoint only when explicitly authored as 0%.
114116
if (i === firstStopIdx) {
115117
if (isPctZero(p1)) p1 = null
116-
if (isPctZero(p2) && p1 == null) p2 = null
117-
}
118-
if (i === lastStopIdx) {
119-
if (isPctHundred(p2)) p2 = null
120-
if (isPctHundred(p1) && p2 == null) p1 = null
121118
}
122119

123120
const colorStr = maybeConvertColor(s.color, convert_colors)

0 commit comments

Comments
 (0)