-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathcache-key.ts
54 lines (45 loc) · 1.33 KB
/
cache-key.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { AtRule, Container, Declaration, Document, Rule } from 'postcss';
import type { ContainerWithChildren } from 'postcss/lib/container';
const NULL_CHAR = String.fromCodePoint(0x000);
export function randomCacheKeyFromPostcssDeclaration(decl: Declaration): {
/**
* The name of the property the random function is used in.
*/
propertyName: string
/**
* N is the index of the random function among other random functions in the same property value.
*/
propertyN: number
/**
* An element ID identifying the element the style is being applied to.
*/
elementID: string
/**
* A document ID identifying the Document the styles are from.
*/
documentID: string
} {
let elementID = '';
let ancestor: Document | ContainerWithChildren | Container | undefined = decl.parent;
while (ancestor) {
switch (ancestor.type) {
case "rule":
elementID += NULL_CHAR + 'selector' + NULL_CHAR + (ancestor as Rule).selector + NULL_CHAR;
break;
case "atrule":
if ((ancestor as AtRule).name === 'scope') {
elementID += NULL_CHAR + 'prelude' + NULL_CHAR + (ancestor as AtRule).params + NULL_CHAR;
}
break;
default:
break;
}
ancestor = ancestor.parent
}
return {
propertyName: decl.prop,
propertyN: 0,
elementID: elementID,
documentID: decl.source?.input.css ?? decl.root().toString(),
}
}