flex.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. var flexSpec = require('./flex-spec');
  6. var Declaration = require('../declaration');
  7. var list = require('postcss/lib/list');
  8. var Flex = function (_Declaration) {
  9. _inherits(Flex, _Declaration);
  10. function Flex() {
  11. _classCallCheck(this, Flex);
  12. return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
  13. }
  14. /**
  15. * Change property name for 2009 spec
  16. */
  17. Flex.prototype.prefixed = function prefixed(prop, prefix) {
  18. var spec = void 0;
  19. var _flexSpec = flexSpec(prefix);
  20. spec = _flexSpec[0];
  21. prefix = _flexSpec[1];
  22. if (spec === 2009) {
  23. return prefix + 'box-flex';
  24. } else {
  25. return _Declaration.prototype.prefixed.call(this, prop, prefix);
  26. }
  27. };
  28. /**
  29. * Return property name by final spec
  30. */
  31. Flex.prototype.normalize = function normalize() {
  32. return 'flex';
  33. };
  34. /**
  35. * Spec 2009 supports only first argument
  36. * Spec 2012 disallows unitless basis
  37. */
  38. Flex.prototype.set = function set(decl, prefix) {
  39. var spec = flexSpec(prefix)[0];
  40. if (spec === 2009) {
  41. decl.value = list.space(decl.value)[0];
  42. decl.value = Flex.oldValues[decl.value] || decl.value;
  43. return _Declaration.prototype.set.call(this, decl, prefix);
  44. } else if (spec === 2012) {
  45. var components = list.space(decl.value);
  46. if (components.length === 3 && components[2] === '0') {
  47. decl.value = components.slice(0, 2).concat('0px').join(' ');
  48. }
  49. }
  50. return _Declaration.prototype.set.call(this, decl, prefix);
  51. };
  52. return Flex;
  53. }(Declaration);
  54. Object.defineProperty(Flex, 'names', {
  55. enumerable: true,
  56. writable: true,
  57. value: ['flex', 'box-flex']
  58. });
  59. Object.defineProperty(Flex, 'oldValues', {
  60. enumerable: true,
  61. writable: true,
  62. value: {
  63. auto: '1',
  64. none: '0'
  65. }
  66. });
  67. module.exports = Flex;