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. regexp() {
  8. if (!this.regexpCache) this.regexpCache = regexp(this.name)
  9. return this.regexpCache
  10. }
  11. isStretch() {
  12. return (
  13. this.name === 'stretch' ||
  14. this.name === 'fill' ||
  15. this.name === 'fill-available'
  16. )
  17. }
  18. replace(string, prefix) {
  19. if (prefix === '-moz-' && this.isStretch()) {
  20. return string.replace(this.regexp(), '$1-moz-available$3')
  21. }
  22. if (prefix === '-webkit-' && this.isStretch()) {
  23. return string.replace(this.regexp(), '$1-webkit-fill-available$3')
  24. }
  25. return super.replace(string, prefix)
  26. }
  27. old(prefix) {
  28. let prefixed = prefix + this.name
  29. if (this.isStretch()) {
  30. if (prefix === '-moz-') {
  31. prefixed = '-moz-available'
  32. } else if (prefix === '-webkit-') {
  33. prefixed = '-webkit-fill-available'
  34. }
  35. }
  36. return new OldValue(this.name, prefixed, prefixed, regexp(prefixed))
  37. }
  38. add(decl, prefix) {
  39. if (decl.prop.includes('grid') && prefix !== '-webkit-') {
  40. return undefined
  41. }
  42. return super.add(decl, 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