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: maximum call caused by large base64 #2406

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions packages/style-unit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

## v3.0.6

- Fix: maximum call caused by large base64.

## v3.0.5

- Fix: fix transform rpx error in node env.
Expand Down
2 changes: 1 addition & 1 deletion packages/style-unit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "style-unit",
"version": "3.0.5",
"version": "3.0.6",
"description": "style-unit",
"license": "BSD-3-Clause",
"main": "lib/index.js",
Expand Down
5 changes: 5 additions & 0 deletions packages/style-unit/src/__tests__/style-unit.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ describe('Web style-unit', () => {
expect(convertUnit('375rpx 20px 375rpx', 'margin')).toEqual('100vw 20px 100vw');
expect(convertUnit('translateX(375rpx) translateY(375rpx)', 'transform')).toEqual('translateX(100vw) translateY(100vw)');
});

it('should not convert base64', () => {
const base64String = 'url(\'data:image/png;base64,iVBORw30rpx...)';
expect(convertUnit(base64String, 'backgroundImage')).toEqual(base64String);
});
});

describe('exported API', () => {
Expand Down
23 changes: 17 additions & 6 deletions packages/style-unit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,27 @@ export function setTargetPlatform(platform) {
targetPlatform = platform;
}

const _convertUnit = cached((value, prop, platform) => {
if (platform) {
setTargetPlatform(platform);
}
return isRpx(value) ? calcRpx(value) : value;
});

const REG_BASE64 = /data:image\/(png|jpg|jpeg|gif|svg|webp|bmp|dpg);base64,/;
function isBase64(str) {
// Maximal base64 string start with `url('data:image/jpeg;base64,` which contains 30 characters.
return typeof str === 'string' && REG_BASE64.test(str.substring(0, 30));
}

/**
* Convert rpx.
* @param value
* @param prop
* @param platform
* @return {String} Transformed value.
*/
export const convertUnit = cached((value, prop, platform) => {
if (platform) {
setTargetPlatform(platform);
}
return isRpx(value) ? calcRpx(value) : value;
});
export const convertUnit = (value, prop, platform) => {
// Do not to convert base64 value which may cause maximum error.
return isBase64(value) ? value : _convertUnit(value, prop, platform);
};