flex-flow.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. let flexSpec = require('./flex-spec')
  2. let Declaration = require('../declaration')
  3. class FlexFlow 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 values = decl.value
  14. .split(/\s+/)
  15. .filter(i => i !== 'wrap' && i !== 'nowrap' && 'wrap-reverse')
  16. if (values.length === 0) {
  17. return undefined
  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 value = values[0]
  27. let orient = value.includes('row') ? 'horizontal' : 'vertical'
  28. let dir = value.includes('reverse') ? 'reverse' : 'normal'
  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. FlexFlow.names = ['flex-flow', 'box-direction', 'box-orient']
  46. module.exports = FlexFlow