flex.js 3.2 KB

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