grid-template-areas.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. var Declaration = require('../declaration');
  6. var _require = require('./grid-utils'),
  7. parseGridAreas = _require.parseGridAreas,
  8. warnMissedAreas = _require.warnMissedAreas,
  9. prefixTrackProp = _require.prefixTrackProp,
  10. prefixTrackValue = _require.prefixTrackValue,
  11. getGridGap = _require.getGridGap,
  12. warnGridGap = _require.warnGridGap,
  13. inheritGridGap = _require.inheritGridGap;
  14. function getGridRows(tpl) {
  15. return tpl.trim().slice(1, -1).split(/["']\s*["']?/g);
  16. }
  17. var GridTemplateAreas = /*#__PURE__*/function (_Declaration) {
  18. _inheritsLoose(GridTemplateAreas, _Declaration);
  19. function GridTemplateAreas() {
  20. return _Declaration.apply(this, arguments) || this;
  21. }
  22. var _proto = GridTemplateAreas.prototype;
  23. /**
  24. * Translate grid-template-areas to separate -ms- prefixed properties
  25. */
  26. _proto.insert = function insert(decl, prefix, prefixes, result) {
  27. if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  28. var hasColumns = false;
  29. var hasRows = false;
  30. var parent = decl.parent;
  31. var gap = getGridGap(decl);
  32. gap = inheritGridGap(decl, gap) || gap; // remove already prefixed rows
  33. // to prevent doubling prefixes
  34. parent.walkDecls(/-ms-grid-rows/, function (i) {
  35. return i.remove();
  36. }); // add empty tracks to rows
  37. parent.walkDecls(/grid-template-(rows|columns)/, function (trackDecl) {
  38. if (trackDecl.prop === 'grid-template-rows') {
  39. hasRows = true;
  40. var prop = trackDecl.prop,
  41. value = trackDecl.value;
  42. trackDecl.cloneBefore({
  43. prop: prefixTrackProp({
  44. prop: prop,
  45. prefix: prefix
  46. }),
  47. value: prefixTrackValue({
  48. value: value,
  49. gap: gap.row
  50. })
  51. });
  52. } else {
  53. hasColumns = true;
  54. }
  55. });
  56. var gridRows = getGridRows(decl.value);
  57. if (hasColumns && !hasRows && gap.row && gridRows.length > 1) {
  58. decl.cloneBefore({
  59. prop: '-ms-grid-rows',
  60. value: prefixTrackValue({
  61. value: "repeat(" + gridRows.length + ", auto)",
  62. gap: gap.row
  63. }),
  64. raws: {}
  65. });
  66. } // warnings
  67. warnGridGap({
  68. gap: gap,
  69. hasColumns: hasColumns,
  70. decl: decl,
  71. result: result
  72. });
  73. var areas = parseGridAreas({
  74. rows: gridRows,
  75. gap: gap
  76. });
  77. warnMissedAreas(areas, decl, result);
  78. return decl;
  79. };
  80. return GridTemplateAreas;
  81. }(Declaration);
  82. _defineProperty(GridTemplateAreas, "names", ['grid-template-areas']);
  83. module.exports = GridTemplateAreas;