| 1 |
let Browsers = require('./browsers') |
| 2 |
let Prefixer = require('./prefixer') |
| 3 |
let utils = require('./utils') |
| 4 |
|
| 5 |
class Declaration extends Prefixer { |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
add(decl, prefix, prefixes, result) { |
| 10 |
let prefixed = this.prefixed(decl.prop, prefix) |
| 11 |
if ( |
| 12 |
this.isAlready(decl, prefixed) || |
| 13 |
this.otherPrefixes(decl.value, prefix) |
| 14 |
) { |
| 15 |
return undefined |
| 16 |
} |
| 17 |
return this.insert(decl, prefix, prefixes, result) |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
calcBefore(prefixes, decl, prefix = '') { |
| 24 |
let max = this.maxPrefixed(prefixes, decl) |
| 25 |
let diff = max - utils.removeNote(prefix).length |
| 26 |
|
| 27 |
let before = decl.raw('before') |
| 28 |
if (diff > 0) { |
| 29 |
before += Array(diff).fill(' ').join('') |
| 30 |
} |
| 31 |
|
| 32 |
return before |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
check() { |
| 39 |
return true |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
insert(decl, prefix, prefixes) { |
| 46 |
let cloned = this.set(this.clone(decl), prefix) |
| 47 |
if (!cloned) return undefined |
| 48 |
|
| 49 |
let already = decl.parent.some( |
| 50 |
i => i.prop === cloned.prop && i.value === cloned.value |
| 51 |
) |
| 52 |
if (already) { |
| 53 |
return undefined |
| 54 |
} |
| 55 |
|
| 56 |
if (this.needCascade(decl)) { |
| 57 |
cloned.raws.before = this.calcBefore(prefixes, decl, prefix) |
| 58 |
} |
| 59 |
return decl.parent.insertBefore(decl, cloned) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
isAlready(decl, prefixed) { |
| 66 |
let already = this.all.group(decl).up(i => i.prop === prefixed) |
| 67 |
if (!already) { |
| 68 |
already = this.all.group(decl).down(i => i.prop === prefixed) |
| 69 |
} |
| 70 |
return already |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
maxPrefixed(prefixes, decl) { |
| 77 |
if (decl._autoprefixerMax) { |
| 78 |
return decl._autoprefixerMax |
| 79 |
} |
| 80 |
|
| 81 |
let max = 0 |
| 82 |
for (let prefix of prefixes) { |
| 83 |
prefix = utils.removeNote(prefix) |
| 84 |
if (prefix.length > max) { |
| 85 |
max = prefix.length |
| 86 |
} |
| 87 |
} |
| 88 |
decl._autoprefixerMax = max |
| 89 |
|
| 90 |
return decl._autoprefixerMax |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
needCascade(decl) { |
| 97 |
if (!decl._autoprefixerCascade) { |
| 98 |
decl._autoprefixerCascade = |
| 99 |
this.all.options.cascade !== false && decl.raw('before').includes('\n') |
| 100 |
} |
| 101 |
return decl._autoprefixerCascade |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
normalize(prop) { |
| 108 |
return prop |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
old(prop, prefix) { |
| 115 |
return [this.prefixed(prop, prefix)] |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
otherPrefixes(value, prefix) { |
| 122 |
for (let other of Browsers.prefixes()) { |
| 123 |
if (other === prefix) { |
| 124 |
continue |
| 125 |
} |
| 126 |
if (value.includes(other)) { |
| 127 |
return value.replace(/var\([^)]+\)/, '').includes(other) |
| 128 |
} |
| 129 |
} |
| 130 |
return false |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
prefixed(prop, prefix) { |
| 137 |
return prefix + prop |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
process(decl, result) { |
| 144 |
if (!this.needCascade(decl)) { |
| 145 |
super.process(decl, result) |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
let prefixes = super.process(decl, result) |
| 150 |
|
| 151 |
if (!prefixes || !prefixes.length) { |
| 152 |
return |
| 153 |
} |
| 154 |
|
| 155 |
this.restoreBefore(decl) |
| 156 |
decl.raws.before = this.calcBefore(prefixes, decl) |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
restoreBefore(decl) { |
| 163 |
let lines = decl.raw('before').split('\n') |
| 164 |
let min = lines[lines.length - 1] |
| 165 |
|
| 166 |
this.all.group(decl).up(prefixed => { |
| 167 |
let array = prefixed.raw('before').split('\n') |
| 168 |
let last = array[array.length - 1] |
| 169 |
if (last.length < min.length) { |
| 170 |
min = last |
| 171 |
} |
| 172 |
}) |
| 173 |
|
| 174 |
lines[lines.length - 1] = min |
| 175 |
decl.raws.before = lines.join('\n') |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
set(decl, prefix) { |
| 182 |
decl.prop = this.prefixed(decl.prop, prefix) |
| 183 |
return decl |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
module.exports = Declaration |