Skip to content

Commit

Permalink
fix(postcss-convert-values): preserve percentage-only properties (#1212)
Browse files Browse the repository at this point in the history
Don't process properties that look like numbers, but only take percentage values
according to the spec.

Fix #1203
  • Loading branch information
ludofischer committed Oct 21, 2021
1 parent f199323 commit 8f34538
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
7 changes: 7 additions & 0 deletions packages/postcss-convert-values/src/__tests__/index.js
Expand Up @@ -420,4 +420,11 @@ test(
}
);

test(
'should not convert ascent and descent-override',
passthroughCSS(
'@font-face {descent-override:0%;ascent-override:0%;line-gap-override:0%;size-adjust:0%;font-stretch:0%}'
)
);

test('should use the postcss plugin api', usePostCSSPlugin(plugin()));
31 changes: 25 additions & 6 deletions packages/postcss-convert-values/src/index.js
Expand Up @@ -18,6 +18,23 @@ const LENGTH_UNITS = [
'pc',
'px',
];

// These properties only accept percentages, so no point in trying to transform
const notALength = new Set([
'descent-override',
'ascent-override',
'font-stretch',
'size-adjust',
'line-gap-override',
]);

// Can't change the unit on these properties when they're 0
const keepWhenZero = new Set([
'stroke-dashoffset',
'stroke-width',
'line-height',
]);

/*
* Numbers without digits after the dot are technically invalid,
* but in that case css-value-parser returns the dot as part of the unit,
Expand Down Expand Up @@ -71,7 +88,7 @@ function clampOpacity(node) {
}
}

function shouldKeepUnit(decl) {
function shouldKeepZeroUnit(decl) {
const { parent } = decl;
const lowerCasedProp = decl.prop.toLowerCase();
return (
Expand All @@ -81,15 +98,17 @@ function shouldKeepUnit(decl) {
parent.parent.name &&
parent.parent.name.toLowerCase() === 'keyframes' &&
lowerCasedProp === 'stroke-dasharray') ||
lowerCasedProp === 'stroke-dashoffset' ||
lowerCasedProp === 'stroke-width' ||
lowerCasedProp === 'line-height'
keepWhenZero.has(lowerCasedProp)
);
}

function transform(opts, decl) {
const lowerCasedProp = decl.prop.toLowerCase();
if (~lowerCasedProp.indexOf('flex') || lowerCasedProp.indexOf('--') === 0) {
if (
~lowerCasedProp.indexOf('flex') ||
lowerCasedProp.indexOf('--') === 0 ||
notALength.has(lowerCasedProp)
) {
return;
}

Expand All @@ -98,7 +117,7 @@ function transform(opts, decl) {
const lowerCasedValue = node.value.toLowerCase();

if (node.type === 'word') {
parseWord(node, opts, shouldKeepUnit(decl));
parseWord(node, opts, shouldKeepZeroUnit(decl));
if (
lowerCasedProp === 'opacity' ||
lowerCasedProp === 'shape-image-threshold'
Expand Down

0 comments on commit 8f34538

Please sign in to comment.