grid-template-areas.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 DOTS = /^\.+$/;
  8. function track(start, end) {
  9. return { start: start, end: end, span: end - start };
  10. }
  11. function getRows(tpl) {
  12. return tpl.trim().slice(1, -1).split(/['"]\s*['"]?/g);
  13. }
  14. function getColumns(line) {
  15. return line.trim().split(/\s+/g);
  16. }
  17. function parseGridAreas(tpl) {
  18. return getRows(tpl).reduce(function (areas, line, rowIndex) {
  19. if (line.trim() === '') return areas;
  20. getColumns(line).forEach(function (area, columnIndex) {
  21. if (DOTS.test(area)) return;
  22. if (typeof areas[area] === 'undefined') {
  23. areas[area] = {
  24. column: track(columnIndex + 1, columnIndex + 2),
  25. row: track(rowIndex + 1, rowIndex + 2)
  26. };
  27. } else {
  28. var _areas$area = areas[area],
  29. column = _areas$area.column,
  30. row = _areas$area.row;
  31. column.start = Math.min(column.start, columnIndex + 1);
  32. column.end = Math.max(column.end, columnIndex + 2);
  33. column.span = column.end - column.start;
  34. row.start = Math.min(row.start, rowIndex + 1);
  35. row.end = Math.max(row.end, rowIndex + 2);
  36. row.span = row.end - row.start;
  37. }
  38. });
  39. return areas;
  40. }, {});
  41. }
  42. var GridTemplateAreas = function (_Declaration) {
  43. _inherits(GridTemplateAreas, _Declaration);
  44. function GridTemplateAreas() {
  45. _classCallCheck(this, GridTemplateAreas);
  46. return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
  47. }
  48. GridTemplateAreas.prototype.getRoot = function getRoot(parent) {
  49. if (parent.type === 'atrule' || !parent.parent) {
  50. return parent;
  51. }
  52. return this.getRoot(parent.parent);
  53. };
  54. /**
  55. * Translate grid-template-areas to separate -ms- prefixed properties
  56. */
  57. GridTemplateAreas.prototype.insert = function insert(decl, prefix, prefixes, result) {
  58. if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  59. var areas = parseGridAreas(decl.value);
  60. var missed = Object.keys(areas);
  61. this.getRoot(decl.parent).walkDecls('grid-area', function (gridArea) {
  62. var value = gridArea.value;
  63. var area = areas[value];
  64. missed = missed.filter(function (e) {
  65. return e !== value;
  66. });
  67. if (area) {
  68. gridArea.parent.walkDecls(/-ms-grid-(row|column)/, function (d) {
  69. d.remove();
  70. });
  71. gridArea.cloneBefore({
  72. prop: '-ms-grid-row',
  73. value: String(area.row.start)
  74. });
  75. if (area.row.span > 1) {
  76. gridArea.cloneBefore({
  77. prop: '-ms-grid-row-span',
  78. value: String(area.row.span)
  79. });
  80. }
  81. gridArea.cloneBefore({
  82. prop: '-ms-grid-column',
  83. value: String(area.column.start)
  84. });
  85. if (area.column.span > 1) {
  86. gridArea.cloneBefore({
  87. prop: '-ms-grid-column-span',
  88. value: String(area.column.span)
  89. });
  90. }
  91. }
  92. return undefined;
  93. });
  94. if (missed.length > 0) {
  95. decl.warn(result, 'Can not find grid areas: ' + missed.join(', '));
  96. }
  97. return decl;
  98. };
  99. return GridTemplateAreas;
  100. }(Declaration);
  101. Object.defineProperty(GridTemplateAreas, 'names', {
  102. enumerable: true,
  103. writable: true,
  104. value: ['grid-template-areas']
  105. });
  106. module.exports = GridTemplateAreas;