transform-decl.js
1.58 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
let Declaration = require('../declaration')
class TransformDecl extends Declaration {
/**
* Recursively check all parents for @keyframes
*/
keyframeParents(decl) {
let { parent } = decl
while (parent) {
if (parent.type === 'atrule' && parent.name === 'keyframes') {
return true
}
;({ parent } = parent)
}
return false
}
/**
* Is transform contain 3D commands
*/
contain3d(decl) {
if (decl.prop === 'transform-origin') {
return false
}
for (let func of TransformDecl.functions3d) {
if (decl.value.includes(`${func}(`)) {
return true
}
}
return false
}
/**
* Replace rotateZ to rotate for IE 9
*/
set(decl, prefix) {
decl = super.set(decl, prefix)
if (prefix === '-ms-') {
decl.value = decl.value.replace(/rotatez/gi, 'rotate')
}
return decl
}
/**
* Don't add prefix for IE in keyframes
*/
insert(decl, prefix, prefixes) {
if (prefix === '-ms-') {
if (!this.contain3d(decl) && !this.keyframeParents(decl)) {
return super.insert(decl, prefix, prefixes)
}
} else if (prefix === '-o-') {
if (!this.contain3d(decl)) {
return super.insert(decl, prefix, prefixes)
}
} else {
return super.insert(decl, prefix, prefixes)
}
return undefined
}
}
TransformDecl.names = ['transform', 'transform-origin']
TransformDecl.functions3d = [
'matrix3d',
'translate3d',
'translateZ',
'scale3d',
'scaleZ',
'rotate3d',
'rotateX',
'rotateY',
'perspective'
]
module.exports = TransformDecl