Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
13 changes: 4 additions & 9 deletions packages/style-value-parser/src/css-types/basic-shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Inset> {
Expand Down