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. * Use two properties for 2009 spec
  6. */
  7. insert(decl, prefix, prefixes) {
  8. let spec
  9. ;[spec, prefix] = flexSpec(prefix)
  10. if (spec !== 2009) {
  11. return super.insert(decl, prefix, prefixes)
  12. }
  13. let already = decl.parent.some(
  14. i =>
  15. i.prop === prefix + 'box-orient' || i.prop === prefix + 'box-direction'
  16. )
  17. if (already) {
  18. return undefined
  19. }
  20. let v = decl.value
  21. let orient, dir
  22. if (v === 'inherit' || v === 'initial' || v === 'unset') {
  23. orient = v
  24. dir = v
  25. } else {
  26. orient = v.includes('row') ? 'horizontal' : 'vertical'
  27. dir = v.includes('reverse') ? 'reverse' : 'normal'
  28. }
  29. let cloned = this.clone(decl)
  30. cloned.prop = prefix + 'box-orient'
  31. cloned.value = orient
  32. if (this.needCascade(decl)) {
  33. cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
  34. }
  35. decl.parent.insertBefore(decl, cloned)
  36. cloned = this.clone(decl)
  37. cloned.prop = prefix + 'box-direction'
  38. cloned.value = dir
  39. if (this.needCascade(decl)) {
  40. cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
  41. }
  42. return decl.parent.insertBefore(decl, cloned)
  43. }
  44. /**
  45. * Return property name by final spec
  46. */
  47. normalize() {
  48. return 'flex-direction'
  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