grid-template-areas.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  4. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  5. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  6. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  7. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  8. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  9. 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; }
  10. var Declaration = require('../declaration');
  11. var _require = require('./grid-utils'),
  12. parseGridAreas = _require.parseGridAreas,
  13. warnMissedAreas = _require.warnMissedAreas,
  14. prefixTrackProp = _require.prefixTrackProp,
  15. prefixTrackValue = _require.prefixTrackValue,
  16. getGridGap = _require.getGridGap,
  17. warnGridGap = _require.warnGridGap,
  18. inheritGridGap = _require.inheritGridGap;
  19. function getGridRows(tpl) {
  20. return tpl.trim().slice(1, -1).split(/["']\s*["']?/g);
  21. }
  22. var GridTemplateAreas = /*#__PURE__*/function (_Declaration) {
  23. _inheritsLoose(GridTemplateAreas, _Declaration);
  24. var _super = _createSuper(GridTemplateAreas);
  25. function GridTemplateAreas() {
  26. return _Declaration.apply(this, arguments) || this;
  27. }
  28. var _proto = GridTemplateAreas.prototype;
  29. /**
  30. * Translate grid-template-areas to separate -ms- prefixed properties
  31. */
  32. _proto.insert = function insert(decl, prefix, prefixes, result) {
  33. if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  34. var hasColumns = false;
  35. var hasRows = false;
  36. var parent = decl.parent;
  37. var gap = getGridGap(decl);
  38. gap = inheritGridGap(decl, gap) || gap; // remove already prefixed rows
  39. // to prevent doubling prefixes
  40. parent.walkDecls(/-ms-grid-rows/, function (i) {
  41. return i.remove();
  42. }); // add empty tracks to rows
  43. parent.walkDecls(/grid-template-(rows|columns)/, function (trackDecl) {
  44. if (trackDecl.prop === 'grid-template-rows') {
  45. hasRows = true;
  46. var prop = trackDecl.prop,
  47. value = trackDecl.value;
  48. trackDecl.cloneBefore({
  49. prop: prefixTrackProp({
  50. prop: prop,
  51. prefix: prefix
  52. }),
  53. value: prefixTrackValue({
  54. value: value,
  55. gap: gap.row
  56. })
  57. });
  58. } else {
  59. hasColumns = true;
  60. }
  61. });
  62. var gridRows = getGridRows(decl.value);
  63. if (hasColumns && !hasRows && gap.row && gridRows.length > 1) {
  64. decl.cloneBefore({
  65. prop: '-ms-grid-rows',
  66. value: prefixTrackValue({
  67. value: "repeat(" + gridRows.length + ", auto)",
  68. gap: gap.row
  69. }),
  70. raws: {}
  71. });
  72. } // warnings
  73. warnGridGap({
  74. gap: gap,
  75. hasColumns: hasColumns,
  76. decl: decl,
  77. result: result
  78. });
  79. var areas = parseGridAreas({
  80. rows: gridRows,
  81. gap: gap
  82. });
  83. warnMissedAreas(areas, decl, result);
  84. return decl;
  85. };
  86. return GridTemplateAreas;
  87. }(Declaration);
  88. _defineProperty(GridTemplateAreas, "names", ['grid-template-areas']);
  89. module.exports = GridTemplateAreas;