grid-template-areas.js 4.6 KB

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