| 1 | let OldValue = require('../old-value') |
| 2 | let Value = require('../value') |
| 3 | |
| 4 | function regexp(name) { |
| 5 | return new RegExp(`(^|[\\s,(])(${name}($|[\\s),]))`, 'gi') |
| 6 | } |
| 7 | |
| 8 | class Intrinsic extends Value { |
| 9 | add(decl, prefix) { |
| 10 | if (decl.prop.includes('grid') && prefix !== '-webkit-') { |
| 11 | return undefined |
| 12 | } |
| 13 | return super.add(decl, prefix) |
| 14 | } |
| 15 | |
| 16 | isStretch() { |
| 17 | return ( |
| 18 | this.name === 'stretch' || |
| 19 | this.name === 'fill' || |
| 20 | this.name === 'fill-available' |
| 21 | ) |
| 22 | } |
| 23 | |
| 24 | old(prefix) { |
| 25 | let prefixed = prefix + this.name |
| 26 | if (this.isStretch()) { |
| 27 | if (prefix === '-moz-') { |
| 28 | prefixed = '-moz-available' |
| 29 | } else if (prefix === '-webkit-') { |
| 30 | prefixed = '-webkit-fill-available' |
| 31 | } |
| 32 | } |
| 33 | return new OldValue(this.name, prefixed, prefixed, regexp(prefixed)) |
| 34 | } |
| 35 | |
| 36 | regexp() { |
| 37 | if (!this.regexpCache) this.regexpCache = regexp(this.name) |
| 38 | return this.regexpCache |
| 39 | } |
| 40 | |
| 41 | replace(string, prefix) { |
| 42 | if (prefix === '-moz-' && this.isStretch()) { |
| 43 | return string.replace(this.regexp(), '$1-moz-available$3') |
| 44 | } |
| 45 | if (prefix === '-webkit-' && this.isStretch()) { |
| 46 | return string.replace(this.regexp(), '$1-webkit-fill-available$3') |
| 47 | } |
| 48 | return super.replace(string, prefix) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Intrinsic.names = [ |
| 53 | 'max-content', |
| 54 | 'min-content', |
| 55 | 'fit-content', |
| 56 | 'fill', |
| 57 | 'fill-available', |
| 58 | 'stretch' |
| 59 | ] |
| 60 | |
| 61 | module.exports = Intrinsic |