diff --git a/packages/style-value-parser/src/css-types/__tests__/basic-shape.js b/packages/style-value-parser/src/css-types/__tests__/basic-shape.js index a4f30c77f..06916a8a3 100644 --- a/packages/style-value-parser/src/css-types/__tests__/basic-shape.js +++ b/packages/style-value-parser/src/css-types/__tests__/basic-shape.js @@ -62,6 +62,23 @@ describe('Basic Shapes', () => { expect(() => Inset.parser.parseToEnd('inset(invalid)')).toThrow(); expect(() => Inset.parser.parseToEnd('inset(10px, invalid)')).toThrow(); }); + + it('should stringify the shortest valid form', () => { + const roundtrip = (css: string): string => + Inset.parser.parseToEnd(css).toString(); + // 1 value: all four sides equal. + expect(roundtrip('inset(10px)')).toBe('inset(10px)'); + // 2 values: vertical / horizontal. + expect(roundtrip('inset(10px 20px)')).toBe('inset(10px 20px)'); + // 3 values: top / horizontal / bottom (left === right). + expect(roundtrip('inset(10px 20px 30px)')).toBe('inset(10px 20px 30px)'); + // 4 values: all distinct (no trailing space before the paren). + expect(roundtrip('inset(10px 20px 30px 40px)')).toBe( + 'inset(10px 20px 30px 40px)', + ); + // round radius is preserved. + expect(roundtrip('inset(10px round 5px)')).toBe('inset(10px round 5px)'); + }); }); describe('Circle', () => { diff --git a/packages/style-value-parser/src/css-types/basic-shape.js b/packages/style-value-parser/src/css-types/basic-shape.js index e69bdc5a8..f6eb5cd86 100644 --- a/packages/style-value-parser/src/css-types/basic-shape.js +++ b/packages/style-value-parser/src/css-types/basic-shape.js @@ -41,24 +41,19 @@ export class Inset extends BasicShape { } // Stringify the shortest possible version of the inset toString(): string { - const { top, right, bottom, left, round } = this; + const { top, right, bottom, left } = this; const roundStr = this.round != null ? ` round ${this.round.toString()}` : ''; - if ( - top === right && - right === bottom && - bottom === left && - left === round - ) { + if (top === right && right === bottom && bottom === left && left === top) { return `inset(${top.toString()}${roundStr})`; } if (top === bottom && left === right) { return `inset(${top.toString()} ${right.toString()}${roundStr})`; } - if (top === bottom) { + if (left === right) { return `inset(${top.toString()} ${right.toString()} ${bottom.toString()}${roundStr})`; } - return `inset(${top.toString()} ${right.toString()} ${bottom.toString()} ${left.toString()} ${roundStr})`; + return `inset(${top.toString()} ${right.toString()} ${bottom.toString()} ${left.toString()}${roundStr})`; } static get parser(): TokenParser {