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. * Change property name for 2009 spec
  7. */
  8. prefixed(prop, prefix) {
  9. let spec
  10. ;[spec, prefix] = flexSpec(prefix)
  11. if (spec === 2009) {
  12. return prefix + 'box-flex'
  13. }
  14. return super.prefixed(prop, prefix)
  15. }
  16. /**
  17. * Return property name by final spec
  18. */
  19. normalize() {
  20. return 'flex'
  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