grid-template.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. (function() {
  2. var Declaration, GridTemplate, parser,
  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. parser = require('postcss-value-parser');
  6. Declaration = require('../declaration');
  7. GridTemplate = (function(superClass) {
  8. extend(GridTemplate, superClass);
  9. function GridTemplate() {
  10. return GridTemplate.__super__.constructor.apply(this, arguments);
  11. }
  12. GridTemplate.names = ['grid-template-rows', 'grid-template-columns', 'grid-rows', 'grid-columns'];
  13. GridTemplate.prototype.prefixed = function(prop, prefix) {
  14. if (prefix === '-ms-') {
  15. return prefix + prop.replace('template-', '');
  16. } else {
  17. return GridTemplate.__super__.prefixed.call(this, prop, prefix);
  18. }
  19. };
  20. GridTemplate.prototype.normalize = function(prop) {
  21. return prop.replace(/^grid-(rows|columns)/, 'grid-template-$1');
  22. };
  23. GridTemplate.prototype.walkRepeat = function(node) {
  24. var count, first, fixed, i, j, len, ref;
  25. fixed = [];
  26. ref = node.nodes;
  27. for (j = 0, len = ref.length; j < len; j++) {
  28. i = ref[j];
  29. if (i.nodes) {
  30. this.walkRepeat(i);
  31. }
  32. fixed.push(i);
  33. if (i.type === 'function' && i.value === 'repeat') {
  34. first = i.nodes.shift();
  35. if (first) {
  36. count = first.value;
  37. i.nodes.shift();
  38. i.value = '';
  39. fixed.push({
  40. type: 'word',
  41. value: "[" + count + "]"
  42. });
  43. }
  44. }
  45. }
  46. return node.nodes = fixed;
  47. };
  48. GridTemplate.prototype.changeRepeat = function(value) {
  49. var ast;
  50. ast = parser(value);
  51. this.walkRepeat(ast);
  52. return ast.toString();
  53. };
  54. GridTemplate.prototype.set = function(decl, prefix) {
  55. if (prefix === '-ms-' && decl.value.indexOf('repeat(') !== -1) {
  56. decl.value = this.changeRepeat(decl.value);
  57. }
  58. return GridTemplate.__super__.set.call(this, decl, prefix);
  59. };
  60. return GridTemplate;
  61. })(Declaration);
  62. module.exports = GridTemplate;
  63. }).call(this);