| [2058e5c] | 1 | let OldValue = require('./old-value')
|
|---|
| 2 | let Prefixer = require('./prefixer')
|
|---|
| 3 | let utils = require('./utils')
|
|---|
| 4 | let vendor = require('./vendor')
|
|---|
| 5 |
|
|---|
| 6 | class Value extends Prefixer {
|
|---|
| 7 | /**
|
|---|
| 8 | * Clone decl for each prefixed values
|
|---|
| 9 | */
|
|---|
| 10 | static save(prefixes, decl) {
|
|---|
| 11 | let prop = decl.prop
|
|---|
| 12 | let result = []
|
|---|
| 13 |
|
|---|
| 14 | for (let prefix in decl._autoprefixerValues) {
|
|---|
| 15 | let value = decl._autoprefixerValues[prefix]
|
|---|
| 16 |
|
|---|
| 17 | if (value === decl.value) {
|
|---|
| 18 | continue
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | let item
|
|---|
| 22 | let propPrefix = vendor.prefix(prop)
|
|---|
| 23 |
|
|---|
| 24 | if (propPrefix === '-pie-') {
|
|---|
| 25 | continue
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | if (propPrefix === prefix) {
|
|---|
| 29 | item = decl.value = value
|
|---|
| 30 | result.push(item)
|
|---|
| 31 | continue
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | let prefixed = prefixes.prefixed(prop, prefix)
|
|---|
| 35 | let rule = decl.parent
|
|---|
| 36 |
|
|---|
| 37 | if (!rule.every(i => i.prop !== prefixed)) {
|
|---|
| 38 | result.push(item)
|
|---|
| 39 | continue
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | let trimmed = value.replace(/\s+/, ' ')
|
|---|
| 43 | let already = rule.some(
|
|---|
| 44 | i => i.prop === decl.prop && i.value.replace(/\s+/, ' ') === trimmed
|
|---|
| 45 | )
|
|---|
| 46 |
|
|---|
| 47 | if (already) {
|
|---|
| 48 | result.push(item)
|
|---|
| 49 | continue
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | let cloned = this.clone(decl, { value })
|
|---|
| 53 | item = decl.parent.insertBefore(decl, cloned)
|
|---|
| 54 |
|
|---|
| 55 | result.push(item)
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | return result
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Save values with next prefixed token
|
|---|
| 63 | */
|
|---|
| 64 | add(decl, prefix) {
|
|---|
| 65 | if (!decl._autoprefixerValues) {
|
|---|
| 66 | decl._autoprefixerValues = {}
|
|---|
| 67 | }
|
|---|
| 68 | let value = decl._autoprefixerValues[prefix] || this.value(decl)
|
|---|
| 69 |
|
|---|
| 70 | let before
|
|---|
| 71 | do {
|
|---|
| 72 | before = value
|
|---|
| 73 | value = this.replace(value, prefix)
|
|---|
| 74 | if (value === false) return
|
|---|
| 75 | } while (value !== before)
|
|---|
| 76 |
|
|---|
| 77 | decl._autoprefixerValues[prefix] = value
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Is declaration need to be prefixed
|
|---|
| 82 | */
|
|---|
| 83 | check(decl) {
|
|---|
| 84 | let value = decl.value
|
|---|
| 85 | if (!value.includes(this.name)) {
|
|---|
| 86 | return false
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | return !!value.match(this.regexp())
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Return function to fast find prefixed value
|
|---|
| 94 | */
|
|---|
| 95 | old(prefix) {
|
|---|
| 96 | return new OldValue(this.name, prefix + this.name)
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Lazy regexp loading
|
|---|
| 101 | */
|
|---|
| 102 | regexp() {
|
|---|
| 103 | return this.regexpCache || (this.regexpCache = utils.regexp(this.name))
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Add prefix to values in string
|
|---|
| 108 | */
|
|---|
| 109 | replace(string, prefix) {
|
|---|
| 110 | return string.replace(this.regexp(), `$1${prefix}$2`)
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Get value with comments if it was not changed
|
|---|
| 115 | */
|
|---|
| 116 | value(decl) {
|
|---|
| 117 | if (decl.raws.value && decl.raws.value.value === decl.value) {
|
|---|
| 118 | return decl.raws.value.raw
|
|---|
| 119 | } else {
|
|---|
| 120 | return decl.value
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | module.exports = Value
|
|---|