intrinsic.js 1.4 KB

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