Skip to content

Commit

Permalink
additional calc() test cases #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierry Bela Nanga authored and Thierry Bela Nanga committed Dec 28, 2023
1 parent 6ad9bcc commit 84870f2
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 21.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
40 changes: 25 additions & 15 deletions dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,10 @@
case exports.EnumToken.PseudoClassFuncTokenType:
if (token.typ == exports.EnumToken.FunctionTokenType &&
token.val == 'calc' &&
token.chi.length == 1 && token.chi[0].typ != exports.EnumToken.BinaryExpressionTokenType) {
token.chi.length == 1 &&
token.chi[0].typ != exports.EnumToken.BinaryExpressionTokenType &&
token.chi[0].typ != exports.EnumToken.FractionTokenType &&
token.chi[0].val?.typ != exports.EnumToken.FractionTokenType) {
// calc(200px) => 200px
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '');
}
Expand Down Expand Up @@ -1028,19 +1031,26 @@
case exports.EnumToken.DimensionTokenType:
case exports.EnumToken.FrequencyTokenType:
case exports.EnumToken.ResolutionTokenType:
if (token.val.typ == exports.EnumToken.FractionTokenType) {
const result = renderToken(token.val, options, cache);
if (!('unit' in token)) {
return result;
}
if (!result.includes(' ')) {
return result + token.unit;
}
return `(${result})*1${token.unit}`;
}
let val = reduceNumber(token.val);
// if ((<FractionToken>token.val).typ == EnumToken.FractionTokenType) {
// const result: string = renderToken(<FractionToken>token.val, options, cache);
//
// if (!('unit' in token)) {
//
// return result;
// }
//
// return result.includes('/') ? result.replace('/', token.unit + '/') : result + token.unit;
//
// // if (!result.includes(' ')) {
// //
// // return result + token.unit;
// // }
//
// // return `(${result})*1${token.unit}`;
// }
let val = token.val.typ == exports.EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
let unit = token.unit;
if (token.typ == exports.EnumToken.AngleTokenType) {
if (token.typ == exports.EnumToken.AngleTokenType && !val.includes('/')) {
const angle = getAngle(token);
let v;
let value = val + unit;
Expand Down Expand Up @@ -1097,7 +1107,7 @@
}
return '0';
}
return val + unit;
return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
case exports.EnumToken.PercentageTokenType:
const perc = token.val.typ == exports.EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', '%/') : perc + '%');
Expand Down Expand Up @@ -2302,7 +2312,7 @@
return count;
}
function pushToken(token, hint) {
const result = { token, hint, position: { ...position }, bytesIn: ind };
const result = { token, hint, position: { ...position }, bytesIn: ind + 1 };
position.ind = ind;
position.lin = lin;
position.col = col == 0 ? 1 : col;
Expand Down
40 changes: 25 additions & 15 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,10 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
case exports.EnumToken.PseudoClassFuncTokenType:
if (token.typ == exports.EnumToken.FunctionTokenType &&
token.val == 'calc' &&
token.chi.length == 1 && token.chi[0].typ != exports.EnumToken.BinaryExpressionTokenType) {
token.chi.length == 1 &&
token.chi[0].typ != exports.EnumToken.BinaryExpressionTokenType &&
token.chi[0].typ != exports.EnumToken.FractionTokenType &&
token.chi[0].val?.typ != exports.EnumToken.FractionTokenType) {
// calc(200px) => 200px
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '');
}
Expand Down Expand Up @@ -1026,19 +1029,26 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
case exports.EnumToken.DimensionTokenType:
case exports.EnumToken.FrequencyTokenType:
case exports.EnumToken.ResolutionTokenType:
if (token.val.typ == exports.EnumToken.FractionTokenType) {
const result = renderToken(token.val, options, cache);
if (!('unit' in token)) {
return result;
}
if (!result.includes(' ')) {
return result + token.unit;
}
return `(${result})*1${token.unit}`;
}
let val = reduceNumber(token.val);
// if ((<FractionToken>token.val).typ == EnumToken.FractionTokenType) {
// const result: string = renderToken(<FractionToken>token.val, options, cache);
//
// if (!('unit' in token)) {
//
// return result;
// }
//
// return result.includes('/') ? result.replace('/', token.unit + '/') : result + token.unit;
//
// // if (!result.includes(' ')) {
// //
// // return result + token.unit;
// // }
//
// // return `(${result})*1${token.unit}`;
// }
let val = token.val.typ == exports.EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
let unit = token.unit;
if (token.typ == exports.EnumToken.AngleTokenType) {
if (token.typ == exports.EnumToken.AngleTokenType && !val.includes('/')) {
const angle = getAngle(token);
let v;
let value = val + unit;
Expand Down Expand Up @@ -1095,7 +1105,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
}
return '0';
}
return val + unit;
return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
case exports.EnumToken.PercentageTokenType:
const perc = token.val.typ == exports.EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', '%/') : perc + '%');
Expand Down Expand Up @@ -2300,7 +2310,7 @@ function* tokenize(stream) {
return count;
}
function pushToken(token, hint) {
const result = { token, hint, position: { ...position }, bytesIn: ind };
const result = { token, hint, position: { ...position }, bytesIn: ind + 1 };
position.ind = ind;
position.lin = lin;
position.col = col == 0 ? 1 : col;
Expand Down
1 change: 0 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ interface FractionToken extends BaseToken {
l: NumberToken;
r: NumberToken;
}

interface BinaryExpressionToken extends BaseToken {

typ: EnumToken.BinaryExpressionTokenType
Expand Down
2 changes: 1 addition & 1 deletion dist/lib/parser/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function* tokenize(stream) {
return count;
}
function pushToken(token, hint) {
const result = { token, hint, position: { ...position }, bytesIn: ind };
const result = { token, hint, position: { ...position }, bytesIn: ind + 1 };
position.ind = ind;
position.lin = lin;
position.col = col == 0 ? 1 : col;
Expand Down
38 changes: 24 additions & 14 deletions dist/lib/renderer/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
case EnumToken.PseudoClassFuncTokenType:
if (token.typ == EnumToken.FunctionTokenType &&
token.val == 'calc' &&
token.chi.length == 1 && token.chi[0].typ != EnumToken.BinaryExpressionTokenType) {
token.chi.length == 1 &&
token.chi[0].typ != EnumToken.BinaryExpressionTokenType &&
token.chi[0].typ != EnumToken.FractionTokenType &&
token.chi[0].val?.typ != EnumToken.FractionTokenType) {
// calc(200px) => 200px
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '');
}
Expand Down Expand Up @@ -351,19 +354,26 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
case EnumToken.DimensionTokenType:
case EnumToken.FrequencyTokenType:
case EnumToken.ResolutionTokenType:
if (token.val.typ == EnumToken.FractionTokenType) {
const result = renderToken(token.val, options, cache);
if (!('unit' in token)) {
return result;
}
if (!result.includes(' ')) {
return result + token.unit;
}
return `(${result})*1${token.unit}`;
}
let val = reduceNumber(token.val);
// if ((<FractionToken>token.val).typ == EnumToken.FractionTokenType) {
// const result: string = renderToken(<FractionToken>token.val, options, cache);
//
// if (!('unit' in token)) {
//
// return result;
// }
//
// return result.includes('/') ? result.replace('/', token.unit + '/') : result + token.unit;
//
// // if (!result.includes(' ')) {
// //
// // return result + token.unit;
// // }
//
// // return `(${result})*1${token.unit}`;
// }
let val = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
let unit = token.unit;
if (token.typ == EnumToken.AngleTokenType) {
if (token.typ == EnumToken.AngleTokenType && !val.includes('/')) {
const angle = getAngle(token);
let v;
let value = val + unit;
Expand Down Expand Up @@ -420,7 +430,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
}
return '0';
}
return val + unit;
return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
case EnumToken.PercentageTokenType:
const perc = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', '%/') : perc + '%');
Expand Down
9 changes: 5 additions & 4 deletions src/lib/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function doParse(iterator: string, options: ParserOptions = {}): Pr
};
}

async function parseNode(results: TokenizeResult[]) {
async function parseNode(results: TokenizeResult[]): Promise<AstRule | AstAtRule | null> {

let tokens: Token[] = results.map(mapToken);

Expand Down Expand Up @@ -362,7 +362,8 @@ export async function doParse(iterator: string, options: ParserOptions = {}): Pr
}
}

let t = renderToken(curr, {minify: false});
let t: string = renderToken(curr, {minify: false});

if (t == ',') {
acc.push([]);
} else {
Expand Down Expand Up @@ -430,7 +431,7 @@ export async function doParse(iterator: string, options: ParserOptions = {}): Pr

name = tokens;
}
const position = map.get(name[0]);
const position: Position = <Position> map.get(name[0]);

if (name.length > 0) {

Expand Down Expand Up @@ -496,7 +497,7 @@ export async function doParse(iterator: string, options: ParserOptions = {}): Pr
return node;
}

const iter = tokenize(iterator);
const iter: Generator<TokenizeResult> = tokenize(iterator);
let item: TokenizeResult;

while (item = iter.next().value) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function* tokenize(stream: string): Generator<TokenizeResult> {

function pushToken(token: string, hint?: TokenType): TokenizeResult {

const result = {token, hint, position: {...position}, bytesIn: ind};
const result = {token, hint, position: {...position}, bytesIn: ind + 1};

position.ind = ind;
position.lin = lin;
Expand Down
52 changes: 28 additions & 24 deletions src/lib/renderer/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
BinaryExpressionToken,
ColorToken, DimensionToken,
ErrorDescription, FractionToken,
Location,
Location, NumberToken,
Position,
RenderOptions,
RenderResult,
Expand Down Expand Up @@ -455,7 +455,10 @@ export function renderToken(token: Token, options: RenderOptions = {}, cache: {
if (
token.typ == EnumToken.FunctionTokenType &&
token.val == 'calc' &&
token.chi.length == 1 && token.chi[0].typ != EnumToken.BinaryExpressionTokenType) {
token.chi.length == 1 &&
token.chi[0].typ != EnumToken.BinaryExpressionTokenType &&
token.chi[0].typ != EnumToken.FractionTokenType &&
(<FractionToken>(<NumberToken>token.chi[0]).val)?.typ != EnumToken.FractionTokenType) {

// calc(200px) => 200px
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '')
Expand Down Expand Up @@ -548,31 +551,33 @@ export function renderToken(token: Token, options: RenderOptions = {}, cache: {
case EnumToken.FrequencyTokenType:
case EnumToken.ResolutionTokenType:

if ((<FractionToken>token.val).typ == EnumToken.FractionTokenType) {
const result: string = renderToken(<FractionToken>token.val, options, cache);

if (!('unit' in token)) {

return result;
}

if (!result.includes(' ')) {

return result + token.unit;
}

return `(${result})*1${token.unit}`;
}

let val: string = reduceNumber(<string | number>token.val);
// if ((<FractionToken>token.val).typ == EnumToken.FractionTokenType) {
// const result: string = renderToken(<FractionToken>token.val, options, cache);
//
// if (!('unit' in token)) {
//
// return result;
// }
//
// return result.includes('/') ? result.replace('/', token.unit + '/') : result + token.unit;
//
// // if (!result.includes(' ')) {
// //
// // return result + token.unit;
// // }
//
// // return `(${result})*1${token.unit}`;
// }

let val: string = (<FractionToken>token.val).typ == EnumToken.FractionTokenType ? renderToken(<FractionToken>token.val, options, cache) : reduceNumber(<string | number>token.val);
let unit: string = token.unit;

if (token.typ == EnumToken.AngleTokenType) {
if (token.typ == EnumToken.AngleTokenType && !val.includes('/')) {

const angle: number = getAngle(<AngleToken>token);

let v: string;
let value = val + unit;
let value: string = val + unit;

for (const u of ['turn', 'deg', 'rad', 'grad']) {

Expand Down Expand Up @@ -659,12 +664,11 @@ export function renderToken(token: Token, options: RenderOptions = {}, cache: {
return '0';
}

return val + unit;
return val.includes('/') ? val.replace('/', unit + '/') : val + unit;

case EnumToken.PercentageTokenType:

const perc: string = (<FractionToken>token.val).typ == EnumToken.FractionTokenType ? renderToken(<FractionToken>token.val, options, cache) : reduceNumber(<string
>token.val);
const perc: string = (<FractionToken>token.val).typ == EnumToken.FractionTokenType ? renderToken(<FractionToken>token.val, options, cache) : reduceNumber(<string>token.val);
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', '%/') : perc + '%');

case EnumToken.NumberTokenType:
Expand Down
Loading

0 comments on commit 84870f2

Please sign in to comment.