Skip to content

Commit

Permalink
fix: QRCode style.padding should work
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Nov 12, 2023
1 parent 6ba626b commit 3070be8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions components/qr-code/__tests__/index.test.tsx
Expand Up @@ -88,4 +88,17 @@ describe('QRCode test', () => {
);
errSpy.mockRestore();
});

it('style.padding should works', () => {
const { container: container1 } = render(<QRCode value="test" size={200} />);
expect(container1.querySelector('canvas')).toHaveStyle('width: 174px; height: 174px');
const { container: container2 } = render(
<QRCode value="test" size={200} style={{ padding: 0 }} />,
);
expect(container2.querySelector('canvas')).toHaveStyle('width: 200px; height: 200px');
const { container: container3 } = render(
<QRCode value="test" size={200} style={{ padding: '2px' }} />,
);
expect(container3.querySelector('canvas')).toHaveStyle('width: 198px; height: 198px');
});
});
12 changes: 11 additions & 1 deletion components/qr-code/index.tsx
Expand Up @@ -45,9 +45,19 @@ const QRCode: React.FC<QRCodeProps> = (props) => {
excavate: true,
};

const getQrCodePadding = (): number => {
if (typeof style?.padding === 'number') {
return style?.padding;
}
if (/\d+px/.test(style?.padding || '')) {

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '9'.
return parseInt(style?.padding || '', 10);
}
return (token.paddingSM + token.lineWidth) * 2;
};

const qrCodeProps = {
value,
size: size - (token.paddingSM + token.lineWidth) * 2,
size: size - getQrCodePadding(),
level: errorLevel,
bgColor,
fgColor: color,
Expand Down

0 comments on commit 3070be8

Please sign in to comment.