transform-decl.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. (function() {
  2. var Declaration, TransformDecl,
  3. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  4. hasProp = {}.hasOwnProperty;
  5. Declaration = require('../declaration');
  6. TransformDecl = (function(superClass) {
  7. extend(TransformDecl, superClass);
  8. function TransformDecl() {
  9. return TransformDecl.__super__.constructor.apply(this, arguments);
  10. }
  11. TransformDecl.names = ['transform', 'transform-origin'];
  12. TransformDecl.functions3d = ['matrix3d', 'translate3d', 'translateZ', 'scale3d', 'scaleZ', 'rotate3d', 'rotateX', 'rotateY', 'perspective'];
  13. TransformDecl.prototype.keyframeParents = function(decl) {
  14. var parent;
  15. parent = decl.parent;
  16. while (parent) {
  17. if (parent.type === 'atrule' && parent.name === 'keyframes') {
  18. return true;
  19. }
  20. parent = parent.parent;
  21. }
  22. return false;
  23. };
  24. TransformDecl.prototype.contain3d = function(decl) {
  25. var func, i, len, ref;
  26. if (decl.prop === 'transform-origin') {
  27. return false;
  28. }
  29. ref = TransformDecl.functions3d;
  30. for (i = 0, len = ref.length; i < len; i++) {
  31. func = ref[i];
  32. if (decl.value.indexOf(func + "(") !== -1) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. };
  38. TransformDecl.prototype.set = function(decl, prefix) {
  39. decl = TransformDecl.__super__.set.apply(this, arguments);
  40. if (prefix === '-ms-') {
  41. decl.value = decl.value.replace(/rotateZ/gi, 'rotate');
  42. }
  43. return decl;
  44. };
  45. TransformDecl.prototype.insert = function(decl, prefix, prefixes) {
  46. if (prefix === '-ms-') {
  47. if (!this.contain3d(decl) && !this.keyframeParents(decl)) {
  48. return TransformDecl.__super__.insert.apply(this, arguments);
  49. }
  50. } else if (prefix === '-o-') {
  51. if (!this.contain3d(decl)) {
  52. return TransformDecl.__super__.insert.apply(this, arguments);
  53. }
  54. } else {
  55. return TransformDecl.__super__.insert.apply(this, arguments);
  56. }
  57. };
  58. return TransformDecl;
  59. })(Declaration);
  60. module.exports = TransformDecl;
  61. }).call(this);