transform-decl.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Declaration = require('../declaration');
  7. var TransformDecl = function (_Declaration) {
  8. _inherits(TransformDecl, _Declaration);
  9. function TransformDecl() {
  10. _classCallCheck(this, TransformDecl);
  11. return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
  12. }
  13. /**
  14. * Recursively check all parents for @keyframes
  15. */
  16. TransformDecl.prototype.keyframeParents = function keyframeParents(decl) {
  17. var parent = decl.parent;
  18. while (parent) {
  19. if (parent.type === 'atrule' && parent.name === 'keyframes') {
  20. return true;
  21. }
  22. var _parent = parent;
  23. parent = _parent.parent;
  24. }
  25. return false;
  26. };
  27. /**
  28. * Is transform contain 3D commands
  29. */
  30. TransformDecl.prototype.contain3d = function contain3d(decl) {
  31. if (decl.prop === 'transform-origin') {
  32. return false;
  33. }
  34. for (var _iterator = TransformDecl.functions3d, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  35. var _ref;
  36. if (_isArray) {
  37. if (_i >= _iterator.length) break;
  38. _ref = _iterator[_i++];
  39. } else {
  40. _i = _iterator.next();
  41. if (_i.done) break;
  42. _ref = _i.value;
  43. }
  44. var func = _ref;
  45. if (decl.value.indexOf(func + '(') !== -1) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. };
  51. /**
  52. * Replace rotateZ to rotate for IE 9
  53. */
  54. TransformDecl.prototype.set = function set(decl, prefix) {
  55. decl = _Declaration.prototype.set.call(this, decl, prefix);
  56. if (prefix === '-ms-') {
  57. decl.value = decl.value.replace(/rotateZ/gi, 'rotate');
  58. }
  59. return decl;
  60. };
  61. /**
  62. * Don't add prefix for IE in keyframes
  63. */
  64. TransformDecl.prototype.insert = function insert(decl, prefix, prefixes) {
  65. if (prefix === '-ms-') {
  66. if (!this.contain3d(decl) && !this.keyframeParents(decl)) {
  67. return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  68. }
  69. } else if (prefix === '-o-') {
  70. if (!this.contain3d(decl)) {
  71. return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  72. }
  73. } else {
  74. return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  75. }
  76. return undefined;
  77. };
  78. return TransformDecl;
  79. }(Declaration);
  80. Object.defineProperty(TransformDecl, 'names', {
  81. enumerable: true,
  82. writable: true,
  83. value: ['transform', 'transform-origin']
  84. });
  85. Object.defineProperty(TransformDecl, 'functions3d', {
  86. enumerable: true,
  87. writable: true,
  88. value: ['matrix3d', 'translate3d', 'translateZ', 'scale3d', 'scaleZ', 'rotate3d', 'rotateX', 'rotateY', 'perspective']
  89. });
  90. module.exports = TransformDecl;