Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/css-calc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Unreleased (minor)

- Add support for `calc-mix()`
- Update `random()` to match the latest spec

### 3.2.1

Expand Down
2 changes: 1 addition & 1 deletion packages/css-calc/dist/index.mjs

Large diffs are not rendered by default.

43 changes: 34 additions & 9 deletions packages/css-calc/src/functions/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,15 @@ function random(randomNode: FunctionNode, globals: Globals, options: conversionO

function parseRandomValueSharing(fnNode: FunctionNode, nodes: Array<ComponentValue>, globals: Globals, options: conversionOptions): [RandomValueSharing, Array<ComponentValue>] | -1 {
const x: RandomValueSharing = {
isAuto: false,
dashedIdent: "",
fixed: -1,
elementShared: false,
elementScoped: false,
propertyScoped: false,
propertyIndexScoped: false,
};

let hasAutoKeyword = false;

const firstNode = nodes[0];
if (!isTokenNode(firstNode) || !isTokenIdent(firstNode.value)) {
return [x, nodes];
Expand All @@ -753,18 +756,36 @@ function parseRandomValueSharing(fnNode: FunctionNode, nodes: Array<ComponentVal
const token = node.value;
const tokenStr = token[4].value.toLowerCase();

if (tokenStr === 'element-shared') {
if (x.fixed !== -1) {
if (tokenStr === 'element-scoped') {
if (x.fixed !== -1 || hasAutoKeyword || x.elementScoped) {
return -1;
}

x.elementScoped = true;
continue;
}

if (tokenStr === 'property-scoped') {
if (x.fixed !== -1 || hasAutoKeyword || x.propertyScoped || x.propertyIndexScoped) {
return -1;
}

x.elementShared = true;
x.propertyScoped = true;
continue;
}

if (tokenStr === 'property-index-scoped') {
if (x.fixed !== -1 || hasAutoKeyword || x.propertyScoped || x.propertyIndexScoped) {
return -1;
}

x.propertyIndexScoped = true;
continue;
}

// fixed <number [0,1]>
if (tokenStr === 'fixed') {
if (x.elementShared || x.dashedIdent || x.isAuto) {
if (x.fixed !== -1 || hasAutoKeyword || x.dashedIdent || x.elementScoped || x.propertyScoped || x.propertyIndexScoped) {
return -1;
}

Expand Down Expand Up @@ -793,22 +814,26 @@ function parseRandomValueSharing(fnNode: FunctionNode, nodes: Array<ComponentVal
}

if (tokenStr === 'auto') {
if (x.fixed !== -1 || x.dashedIdent) {
if (x.fixed !== -1 || hasAutoKeyword || x.dashedIdent || x.elementScoped || x.propertyScoped || x.propertyIndexScoped) {
return -1;
}

x.isAuto = true;
x.elementScoped = true;
x.propertyIndexScoped = true;
hasAutoKeyword = true;
continue;
}

if (tokenStr.startsWith('--')) {
if (x.fixed !== -1 || x.isAuto) {
if (x.fixed !== -1 || hasAutoKeyword || x.dashedIdent) {
return -1;
}

x.dashedIdent = tokenStr;
continue;
}

return -1;
}

return -1;
Expand Down
11 changes: 7 additions & 4 deletions packages/css-calc/src/functions/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import type { conversionOptions } from '../options';
const NULL_CHAR = String.fromCodePoint(0x000);

export type RandomValueSharing = {
isAuto: boolean,
dashedIdent: string,
elementShared: boolean,
elementScoped: boolean,
propertyScoped: boolean,
propertyIndexScoped: boolean,
fixed: number,
};

Expand Down Expand Up @@ -71,8 +72,10 @@ export function solveRandom(randomNode: FunctionNode, randomValueSharing: Random
const rnd = randomValueSharing.fixed === -1 ? sfc32(
crc32(
[
randomValueSharing.dashedIdent ? randomValueSharing.dashedIdent : (`${options.randomCaching?.propertyName} ${options.randomCaching.propertyN++}`),
randomValueSharing.elementShared ? "" : options.randomCaching.elementID,
randomValueSharing.dashedIdent ? randomValueSharing.dashedIdent : "",
randomValueSharing.elementScoped ? options.randomCaching.elementID : "",
(randomValueSharing.propertyScoped || randomValueSharing.propertyIndexScoped) ? options.randomCaching.propertyName : "",
randomValueSharing.propertyIndexScoped ? options.randomCaching.propertyN : "",
options.randomCaching.documentID,
].join(NULL_CHAR),
),
Expand Down
Loading
Loading