flex-direction.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. let flexSpec = require('./flex-spec')
  2. let Declaration = require('../declaration')
  3. class FlexDirection extends Declaration {
  4. /**
  5. * Return property name by final spec
  6. */
  7. normalize() {
  8. return 'flex-direction'
  9. }
  10. /**
  11. * Use two properties for 2009 spec
  12. */
  13. insert(decl, prefix, prefixes) {
  14. let spec
  15. ;[spec, prefix] = flexSpec(prefix)
  16. if (spec !== 2009) {
  17. return super.insert(decl, prefix, prefixes)
  18. }
  19. let already = decl.parent.some(
  20. i =>
  21. i.prop === prefix + 'box-orient' || i.prop === prefix + 'box-direction'
  22. )
  23. if (already) {
  24. return undefined
  25. }
  26. let v = decl.value
  27. let orient, dir
  28. if (v === 'inherit' || v === 'initial' || v === 'unset') {
  29. orient = v
  30. dir = v
  31. } else {
  32. orient = v.includes('row') ? 'horizontal' : 'vertical'
  33. dir = v.includes('reverse') ? 'reverse' : 'normal'
  34. }
  35. let cloned = this.clone(decl)
  36. cloned.prop = prefix + 'box-orient'
  37. cloned.value = orient
  38. if (this.needCascade(decl)) {
  39. cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
  40. }
  41. decl.parent.insertBefore(decl, cloned)
  42. cloned = this.clone(decl)
  43. cloned.prop = prefix + 'box-direction'
  44. cloned.value = dir
  45. if (this.needCascade(decl)) {
  46. cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
  47. }
  48. return decl.parent.insertBefore(decl, cloned)
  49. }
  50. /**
  51. * Clean two properties for 2009 spec
  52. */
  53. old(prop, prefix) {
  54. let spec
  55. ;[spec, prefix] = flexSpec(prefix)
  56. if (spec === 2009) {
  57. return [prefix + 'box-orient', prefix + 'box-direction']
  58. } else {
  59. return super.old(prop, prefix)
  60. }
  61. }
  62. }
  63. FlexDirection.names = ['flex-direction', 'box-direction', 'box-orient']
  64. module.exports = FlexDirection