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: logical attribute calc css variable logic error #177

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions src/transformers/legacyLogicalProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function splitValues(
brackets += left - right;
}
if (brackets === 0) {
list.push(temp + item);
list.push(`${temp} ${item}`);
temp = '';
} else if (brackets > 0) {
temp += item;
temp += ` ${item}`;
Copy link
Contributor

@yoyo837 yoyo837 Mar 12, 2024

Choose a reason for hiding this comment

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

为啥不都用数组push, 然后最终join(' ')呢?字符串玩着玩着容易玩出这种问题。

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Member Author

Choose a reason for hiding this comment

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

感觉这段代码已经比较精简了,没有可操作空间了

Copy link
Contributor

Choose a reason for hiding this comment

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

  let temp: string[] = [];
  let brackets = 0;
  return [
    splitStyle.reduce<string[]>((list, item) => {
      if (item.includes('(') || item.includes(')')) {
        const left = item.split('(').length - 1;
        const right = item.split(')').length - 1;
        brackets += left - right;
      }
      if (brackets >= 0) {
        temp.push(item);
      }
      if (brackets === 0) {
        list.push(temp.join(' '));
        temp = [];
      }
      return list;
    }, []),
    !!importantCells,
  ];

Copy link
Member

Choose a reason for hiding this comment

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

@yoyo837 可以直接用 suggestion 能力。

图片

Copy link
Contributor

Choose a reason for hiding this comment

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

跨越很多行,还涉及一些未修改的行,这种情况我不会 suggestion

Copy link
Member Author

Choose a reason for hiding this comment

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

push 了,再 review 一下吧

}
return list;
}, []),
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/transform.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`transform > legacyLogicalProperties > split with calc() and var() 1`] = `".box{margin-top:calc(var(--a) + var(--b));margin-bottom:calc(2px + var(--c));margin-left:calc(2px + 1px);margin-right:3px;}.antd-issue-47652{padding-left:calc(8px - var(--c));padding-right:calc(8px - var(--c));}.antd-issue-47707{padding-left:calc(50% - calc(var(--c) / 2) - var(-c));padding-right:calc(50% - calc(var(--c) / 2) - var(-c));}"`;
19 changes: 19 additions & 0 deletions tests/transform.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ describe('transform', () => {
});

it('split with calc() and var()', () => {
const str47652 = 'calc(8px - var(--c))';
const str47707 = 'calc(50% - calc(var(--c) / 2) - var(-c))';

const { container } = render(
<Wrapper
css={{
Expand All @@ -169,6 +172,13 @@ describe('transform', () => {
marginInline: 'calc(2px + 1px)',
marginInlineEnd: '3px',
},
// https://github.com/ant-design/ant-design/issues/47652
'.antd-issue-47652': {
paddingInline: str47652,
},
'.antd-issue-47707': {
paddingInline: str47707,
}
}}
/>,
);
Expand All @@ -179,6 +189,15 @@ describe('transform', () => {
marginLeft: 'calc(2px + 1px)',
marginRight: '3px',
});

const styleText = document.head.querySelector('style')?.innerHTML;
expect(styleText).toMatchSnapshot();
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved

expect(styleText).toContain(`padding-left:${str47652}`);
expect(styleText).toContain(`padding-right:${str47652}`);

expect(styleText).toContain(`padding-left:${str47707}`);
expect(styleText).toContain(`padding-right:${str47707}`);
});
});

Expand Down
Loading