Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly parse multi-value shorthand with slash #859

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions packages/shared/__tests__/split-value-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ describe('Ensure CSS values are split correctly', () => {
splitValue('calc((100% - 50px) * 0.5) var(--rightpadding, 20px)'),
).toEqual(['calc((100% - 50px) * 0.5)', 'var(--rightpadding,20px)']);
});

test('Splits a string of values with slash notation appropriately.', () => {
expect(splitValue('1px/2px 3px 4px 5px')).toEqual([
'1px/2px',
'3px',
'4px',
'5px',
]);
});
});
Copy link
Contributor

@nmn nmn Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how border-radius values are expanded. The correct expansion should be:

Suggested change
test('Splits a string of values with slash notation appropriately.', () => {
expect(splitValue('1px/2px 3px 4px 5px')).toEqual([
'1px/2px',
'3px',
'4px',
'5px',
]);
});
});
test('Splits a string of values with slash notation appropriately.', () => {
expect(splitValue('1px/2px 3px 4px 5px')).toEqual([
'1px 2px',
'1px 3px',
'1px 4px',
'1px 5px',
]);
});
});

See https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius
which mentions:

border-radius: 4px 3px 6px / 2px 4px;

/* It is equivalent to: */
border-top-left-radius: 4px 2px;
border-top-right-radius: 3px 4px;
border-bottom-right-radius: 6px 2px;
border-bottom-left-radius: 3px 4px;

To further explain, a border-radius value can only contain a single / and it separates the "vertical" / "horizontal" border radii.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood the usage of / in border-radius. I will review the specification and correct the code accordingly.

44 changes: 42 additions & 2 deletions packages/shared/src/utils/split-css-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@ function printNode(node: PostCSSValueASTNode): string {
}
}

// Merges slash-separated values within nodes into single nodes.
function combineNodesWithSlash(nodes: PostCSSValueASTNode[]) {
const result = [];

for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];

if (node.type !== 'div' || node.value !== '/') {
result.push(node);
continue;
}

if (i === 0 || i === nodes.length - 1) {
result.push(node);
continue;
}

const prev = result.at(-1);
const next = nodes[i + 1];

if (!prev || !next || prev.type !== 'word' || next.type !== 'word') {
result.push(node);
continue;
}

result.pop();
const combinedNode = {
...prev,
value: prev.value + node.value + next.value,
sourceEndIndex: next.sourceEndIndex,
};
result.push(combinedNode);
i++;
}

return result;
}

// Using split(' ') Isn't enough because of values like calc.
export default function splitValue(
str: TStyleValue,
Expand All @@ -38,8 +76,10 @@ export default function splitValue(

const parsed = parser(str.trim());

const nodes = parsed.nodes
.filter((node) => node.type !== 'space' && node.type !== 'div')
const nodes = combineNodesWithSlash(
parsed.nodes.filter((node) => node.type !== 'space'),
)
.filter((node) => node.type !== 'div')
.map(printNode);

if (
Expand Down
Loading