flex.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. let list = require('postcss').list
  2. let flexSpec = require('./flex-spec')
  3. let Declaration = require('../declaration')
  4. class Flex extends Declaration {
  5. /**
  6. * Return property name by final spec
  7. */
  8. normalize() {
  9. return 'flex'
  10. }
  11. /**
  12. * Change property name for 2009 spec
  13. */
  14. prefixed(prop, prefix) {
  15. let spec
  16. ;[spec, prefix] = flexSpec(prefix)
  17. if (spec === 2009) {
  18. return prefix + 'box-flex'
  19. }
  20. return super.prefixed(prop, prefix)
  21. }
  22. /**
  23. * Spec 2009 supports only first argument
  24. * Spec 2012 disallows unitless basis
  25. */
  26. set(decl, prefix) {
  27. let spec = flexSpec(prefix)[0]
  28. if (spec === 2009) {
  29. decl.value = list.space(decl.value)[0]
  30. decl.value = Flex.oldValues[decl.value] || decl.value
  31. return super.set(decl, prefix)
  32. }
  33. if (spec === 2012) {
  34. let components = list.space(decl.value)
  35. if (components.length === 3 && components[2] === '0') {
  36. decl.value = components.slice(0, 2).concat('0px').join(' ')
  37. }
  38. }
  39. return super.set(decl, prefix)
  40. }
  41. }
  42. Flex.names = ['flex', 'box-flex']
  43. Flex.oldValues = {
  44. auto: '1',
  45. none: '0'
  46. }
  47. module.exports = Flex