transform-decl.js 3.8 KB

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