[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
|
---|
| 4 |
|
---|
| 5 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
---|
| 6 |
|
---|
| 7 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
---|
| 8 |
|
---|
| 9 | var parser = require('postcss-value-parser');
|
---|
| 10 |
|
---|
| 11 | var list = require('postcss').list;
|
---|
| 12 |
|
---|
| 13 | var uniq = require('../utils').uniq;
|
---|
| 14 |
|
---|
| 15 | var escapeRegexp = require('../utils').escapeRegexp;
|
---|
| 16 |
|
---|
| 17 | var splitSelector = require('../utils').splitSelector;
|
---|
| 18 |
|
---|
| 19 | function convert(value) {
|
---|
| 20 | if (value && value.length === 2 && value[0] === 'span' && parseInt(value[1], 10) > 0) {
|
---|
| 21 | return [false, parseInt(value[1], 10)];
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | if (value && value.length === 1 && parseInt(value[0], 10) > 0) {
|
---|
| 25 | return [parseInt(value[0], 10), false];
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | return [false, false];
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | function translate(values, startIndex, endIndex) {
|
---|
| 32 | var startValue = values[startIndex];
|
---|
| 33 | var endValue = values[endIndex];
|
---|
| 34 |
|
---|
| 35 | if (!startValue) {
|
---|
| 36 | return [false, false];
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | var _convert = convert(startValue),
|
---|
| 40 | start = _convert[0],
|
---|
| 41 | spanStart = _convert[1];
|
---|
| 42 |
|
---|
| 43 | var _convert2 = convert(endValue),
|
---|
| 44 | end = _convert2[0],
|
---|
| 45 | spanEnd = _convert2[1];
|
---|
| 46 |
|
---|
| 47 | if (start && !endValue) {
|
---|
| 48 | return [start, false];
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | if (spanStart && end) {
|
---|
| 52 | return [end - spanStart, spanStart];
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | if (start && spanEnd) {
|
---|
| 56 | return [start, spanEnd];
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if (start && end) {
|
---|
| 60 | return [start, end - start];
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return [false, false];
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | function parse(decl) {
|
---|
| 67 | var node = parser(decl.value);
|
---|
| 68 | var values = [];
|
---|
| 69 | var current = 0;
|
---|
| 70 | values[current] = [];
|
---|
| 71 |
|
---|
| 72 | for (var _iterator = _createForOfIteratorHelperLoose(node.nodes), _step; !(_step = _iterator()).done;) {
|
---|
| 73 | var i = _step.value;
|
---|
| 74 |
|
---|
| 75 | if (i.type === 'div') {
|
---|
| 76 | current += 1;
|
---|
| 77 | values[current] = [];
|
---|
| 78 | } else if (i.type === 'word') {
|
---|
| 79 | values[current].push(i.value);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return values;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | function insertDecl(decl, prop, value) {
|
---|
| 87 | if (value && !decl.parent.some(function (i) {
|
---|
| 88 | return i.prop === "-ms-" + prop;
|
---|
| 89 | })) {
|
---|
| 90 | decl.cloneBefore({
|
---|
| 91 | prop: "-ms-" + prop,
|
---|
| 92 | value: value.toString()
|
---|
| 93 | });
|
---|
| 94 | }
|
---|
| 95 | } // Track transforms
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | function prefixTrackProp(_ref) {
|
---|
| 99 | var prop = _ref.prop,
|
---|
| 100 | prefix = _ref.prefix;
|
---|
| 101 | return prefix + prop.replace('template-', '');
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | function transformRepeat(_ref2, _ref3) {
|
---|
| 105 | var nodes = _ref2.nodes;
|
---|
| 106 | var gap = _ref3.gap;
|
---|
| 107 |
|
---|
| 108 | var _nodes$reduce = nodes.reduce(function (result, node) {
|
---|
| 109 | if (node.type === 'div' && node.value === ',') {
|
---|
| 110 | result.key = 'size';
|
---|
| 111 | } else {
|
---|
| 112 | result[result.key].push(parser.stringify(node));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | return result;
|
---|
| 116 | }, {
|
---|
| 117 | key: 'count',
|
---|
| 118 | size: [],
|
---|
| 119 | count: []
|
---|
| 120 | }),
|
---|
| 121 | count = _nodes$reduce.count,
|
---|
| 122 | size = _nodes$reduce.size; // insert gap values
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | if (gap) {
|
---|
| 126 | var _ret = function () {
|
---|
| 127 | size = size.filter(function (i) {
|
---|
| 128 | return i.trim();
|
---|
| 129 | });
|
---|
| 130 | var val = [];
|
---|
| 131 |
|
---|
| 132 | var _loop = function _loop(i) {
|
---|
| 133 | size.forEach(function (item, index) {
|
---|
| 134 | if (index > 0 || i > 1) {
|
---|
| 135 | val.push(gap);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | val.push(item);
|
---|
| 139 | });
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | for (var i = 1; i <= count; i++) {
|
---|
| 143 | _loop(i);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return {
|
---|
| 147 | v: val.join(' ')
|
---|
| 148 | };
|
---|
| 149 | }();
|
---|
| 150 |
|
---|
| 151 | if (typeof _ret === "object") return _ret.v;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | return "(" + size.join('') + ")[" + count.join('') + "]";
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | function prefixTrackValue(_ref4) {
|
---|
| 158 | var value = _ref4.value,
|
---|
| 159 | gap = _ref4.gap;
|
---|
| 160 | var result = parser(value).nodes.reduce(function (nodes, node) {
|
---|
| 161 | if (node.type === 'function' && node.value === 'repeat') {
|
---|
| 162 | return nodes.concat({
|
---|
| 163 | type: 'word',
|
---|
| 164 | value: transformRepeat(node, {
|
---|
| 165 | gap: gap
|
---|
| 166 | })
|
---|
| 167 | });
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | if (gap && node.type === 'space') {
|
---|
| 171 | return nodes.concat({
|
---|
| 172 | type: 'space',
|
---|
| 173 | value: ' '
|
---|
| 174 | }, {
|
---|
| 175 | type: 'word',
|
---|
| 176 | value: gap
|
---|
| 177 | }, node);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return nodes.concat(node);
|
---|
| 181 | }, []);
|
---|
| 182 | return parser.stringify(result);
|
---|
| 183 | } // Parse grid-template-areas
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | var DOTS = /^\.+$/;
|
---|
| 187 |
|
---|
| 188 | function track(start, end) {
|
---|
| 189 | return {
|
---|
| 190 | start: start,
|
---|
| 191 | end: end,
|
---|
| 192 | span: end - start
|
---|
| 193 | };
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | function getColumns(line) {
|
---|
| 197 | return line.trim().split(/\s+/g);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | function parseGridAreas(_ref5) {
|
---|
| 201 | var rows = _ref5.rows,
|
---|
| 202 | gap = _ref5.gap;
|
---|
| 203 | return rows.reduce(function (areas, line, rowIndex) {
|
---|
| 204 | if (gap.row) rowIndex *= 2;
|
---|
| 205 | if (line.trim() === '') return areas;
|
---|
| 206 | getColumns(line).forEach(function (area, columnIndex) {
|
---|
| 207 | if (DOTS.test(area)) return;
|
---|
| 208 | if (gap.column) columnIndex *= 2;
|
---|
| 209 |
|
---|
| 210 | if (typeof areas[area] === 'undefined') {
|
---|
| 211 | areas[area] = {
|
---|
| 212 | column: track(columnIndex + 1, columnIndex + 2),
|
---|
| 213 | row: track(rowIndex + 1, rowIndex + 2)
|
---|
| 214 | };
|
---|
| 215 | } else {
|
---|
| 216 | var _areas$area = areas[area],
|
---|
| 217 | column = _areas$area.column,
|
---|
| 218 | row = _areas$area.row;
|
---|
| 219 | column.start = Math.min(column.start, columnIndex + 1);
|
---|
| 220 | column.end = Math.max(column.end, columnIndex + 2);
|
---|
| 221 | column.span = column.end - column.start;
|
---|
| 222 | row.start = Math.min(row.start, rowIndex + 1);
|
---|
| 223 | row.end = Math.max(row.end, rowIndex + 2);
|
---|
| 224 | row.span = row.end - row.start;
|
---|
| 225 | }
|
---|
| 226 | });
|
---|
| 227 | return areas;
|
---|
| 228 | }, {});
|
---|
| 229 | } // Parse grid-template
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | function testTrack(node) {
|
---|
| 233 | return node.type === 'word' && /^\[.+]$/.test(node.value);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | function verifyRowSize(result) {
|
---|
| 237 | if (result.areas.length > result.rows.length) {
|
---|
| 238 | result.rows.push('auto');
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | return result;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | function parseTemplate(_ref6) {
|
---|
| 245 | var decl = _ref6.decl,
|
---|
| 246 | gap = _ref6.gap;
|
---|
| 247 | var gridTemplate = parser(decl.value).nodes.reduce(function (result, node) {
|
---|
| 248 | var type = node.type,
|
---|
| 249 | value = node.value;
|
---|
| 250 | if (testTrack(node) || type === 'space') return result; // area
|
---|
| 251 |
|
---|
| 252 | if (type === 'string') {
|
---|
| 253 | result = verifyRowSize(result);
|
---|
| 254 | result.areas.push(value);
|
---|
| 255 | } // values and function
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | if (type === 'word' || type === 'function') {
|
---|
| 259 | result[result.key].push(parser.stringify(node));
|
---|
| 260 | } // divider(/)
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | if (type === 'div' && value === '/') {
|
---|
| 264 | result.key = 'columns';
|
---|
| 265 | result = verifyRowSize(result);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | return result;
|
---|
| 269 | }, {
|
---|
| 270 | key: 'rows',
|
---|
| 271 | columns: [],
|
---|
| 272 | rows: [],
|
---|
| 273 | areas: []
|
---|
| 274 | });
|
---|
| 275 | return {
|
---|
| 276 | areas: parseGridAreas({
|
---|
| 277 | rows: gridTemplate.areas,
|
---|
| 278 | gap: gap
|
---|
| 279 | }),
|
---|
| 280 | columns: prefixTrackValue({
|
---|
| 281 | value: gridTemplate.columns.join(' '),
|
---|
| 282 | gap: gap.column
|
---|
| 283 | }),
|
---|
| 284 | rows: prefixTrackValue({
|
---|
| 285 | value: gridTemplate.rows.join(' '),
|
---|
| 286 | gap: gap.row
|
---|
| 287 | })
|
---|
| 288 | };
|
---|
| 289 | } // Insert parsed grid areas
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
| 292 | * Get an array of -ms- prefixed props and values
|
---|
| 293 | * @param {Object} [area] area object with column and row data
|
---|
| 294 | * @param {Boolean} [addRowSpan] should we add grid-column-row value?
|
---|
| 295 | * @param {Boolean} [addColumnSpan] should we add grid-column-span value?
|
---|
| 296 | * @return {Array<Object>}
|
---|
| 297 | */
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 | function getMSDecls(area, addRowSpan, addColumnSpan) {
|
---|
| 301 | if (addRowSpan === void 0) {
|
---|
| 302 | addRowSpan = false;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | if (addColumnSpan === void 0) {
|
---|
| 306 | addColumnSpan = false;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | return [].concat({
|
---|
| 310 | prop: '-ms-grid-row',
|
---|
| 311 | value: String(area.row.start)
|
---|
| 312 | }, area.row.span > 1 || addRowSpan ? {
|
---|
| 313 | prop: '-ms-grid-row-span',
|
---|
| 314 | value: String(area.row.span)
|
---|
| 315 | } : [], {
|
---|
| 316 | prop: '-ms-grid-column',
|
---|
| 317 | value: String(area.column.start)
|
---|
| 318 | }, area.column.span > 1 || addColumnSpan ? {
|
---|
| 319 | prop: '-ms-grid-column-span',
|
---|
| 320 | value: String(area.column.span)
|
---|
| 321 | } : []);
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | function getParentMedia(parent) {
|
---|
| 325 | if (parent.type === 'atrule' && parent.name === 'media') {
|
---|
| 326 | return parent;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | if (!parent.parent) {
|
---|
| 330 | return false;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | return getParentMedia(parent.parent);
|
---|
| 334 | }
|
---|
| 335 | /**
|
---|
| 336 | * change selectors for rules with duplicate grid-areas.
|
---|
| 337 | * @param {Array<Rule>} rules
|
---|
| 338 | * @param {Array<String>} templateSelectors
|
---|
| 339 | * @return {Array<Rule>} rules with changed selectors
|
---|
| 340 | */
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | function changeDuplicateAreaSelectors(ruleSelectors, templateSelectors) {
|
---|
| 344 | ruleSelectors = ruleSelectors.map(function (selector) {
|
---|
| 345 | var selectorBySpace = list.space(selector);
|
---|
| 346 | var selectorByComma = list.comma(selector);
|
---|
| 347 |
|
---|
| 348 | if (selectorBySpace.length > selectorByComma.length) {
|
---|
| 349 | selector = selectorBySpace.slice(-1).join('');
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | return selector;
|
---|
| 353 | });
|
---|
| 354 | return ruleSelectors.map(function (ruleSelector) {
|
---|
| 355 | var newSelector = templateSelectors.map(function (tplSelector, index) {
|
---|
| 356 | var space = index === 0 ? '' : ' ';
|
---|
| 357 | return "" + space + tplSelector + " > " + ruleSelector;
|
---|
| 358 | });
|
---|
| 359 | return newSelector;
|
---|
| 360 | });
|
---|
| 361 | }
|
---|
| 362 | /**
|
---|
| 363 | * check if selector of rules are equal
|
---|
| 364 | * @param {Rule} ruleA
|
---|
| 365 | * @param {Rule} ruleB
|
---|
| 366 | * @return {Boolean}
|
---|
| 367 | */
|
---|
| 368 |
|
---|
| 369 |
|
---|
| 370 | function selectorsEqual(ruleA, ruleB) {
|
---|
| 371 | return ruleA.selectors.some(function (sel) {
|
---|
| 372 | return ruleB.selectors.some(function (s) {
|
---|
| 373 | return s === sel;
|
---|
| 374 | });
|
---|
| 375 | });
|
---|
| 376 | }
|
---|
| 377 | /**
|
---|
| 378 | * Parse data from all grid-template(-areas) declarations
|
---|
| 379 | * @param {Root} css css root
|
---|
| 380 | * @return {Object} parsed data
|
---|
| 381 | */
|
---|
| 382 |
|
---|
| 383 |
|
---|
| 384 | function parseGridTemplatesData(css) {
|
---|
| 385 | var parsed = []; // we walk through every grid-template(-areas) declaration and store
|
---|
| 386 | // data with the same area names inside the item
|
---|
| 387 |
|
---|
| 388 | css.walkDecls(/grid-template(-areas)?$/, function (d) {
|
---|
| 389 | var rule = d.parent;
|
---|
| 390 | var media = getParentMedia(rule);
|
---|
| 391 | var gap = getGridGap(d);
|
---|
| 392 | var inheritedGap = inheritGridGap(d, gap);
|
---|
| 393 |
|
---|
| 394 | var _parseTemplate = parseTemplate({
|
---|
| 395 | decl: d,
|
---|
| 396 | gap: inheritedGap || gap
|
---|
| 397 | }),
|
---|
| 398 | areas = _parseTemplate.areas;
|
---|
| 399 |
|
---|
| 400 | var areaNames = Object.keys(areas); // skip node if it doesn't have areas
|
---|
| 401 |
|
---|
| 402 | if (areaNames.length === 0) {
|
---|
| 403 | return true;
|
---|
| 404 | } // check parsed array for item that include the same area names
|
---|
| 405 | // return index of that item
|
---|
| 406 |
|
---|
| 407 |
|
---|
| 408 | var index = parsed.reduce(function (acc, _ref7, idx) {
|
---|
| 409 | var allAreas = _ref7.allAreas;
|
---|
| 410 | var hasAreas = allAreas && areaNames.some(function (area) {
|
---|
| 411 | return allAreas.includes(area);
|
---|
| 412 | });
|
---|
| 413 | return hasAreas ? idx : acc;
|
---|
| 414 | }, null);
|
---|
| 415 |
|
---|
| 416 | if (index !== null) {
|
---|
| 417 | // index is found, add the grid-template data to that item
|
---|
| 418 | var _parsed$index = parsed[index],
|
---|
| 419 | allAreas = _parsed$index.allAreas,
|
---|
| 420 | rules = _parsed$index.rules; // check if rule has no duplicate area names
|
---|
| 421 |
|
---|
| 422 | var hasNoDuplicates = rules.some(function (r) {
|
---|
| 423 | return r.hasDuplicates === false && selectorsEqual(r, rule);
|
---|
| 424 | });
|
---|
| 425 | var duplicatesFound = false; // check need to gather all duplicate area names
|
---|
| 426 |
|
---|
| 427 | var duplicateAreaNames = rules.reduce(function (acc, r) {
|
---|
| 428 | if (!r.params && selectorsEqual(r, rule)) {
|
---|
| 429 | duplicatesFound = true;
|
---|
| 430 | return r.duplicateAreaNames;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | if (!duplicatesFound) {
|
---|
| 434 | areaNames.forEach(function (name) {
|
---|
| 435 | if (r.areas[name]) {
|
---|
| 436 | acc.push(name);
|
---|
| 437 | }
|
---|
| 438 | });
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | return uniq(acc);
|
---|
| 442 | }, []); // update grid-row/column-span values for areas with duplicate
|
---|
| 443 | // area names. @see #1084 and #1146
|
---|
| 444 |
|
---|
| 445 | rules.forEach(function (r) {
|
---|
| 446 | areaNames.forEach(function (name) {
|
---|
| 447 | var area = r.areas[name];
|
---|
| 448 |
|
---|
| 449 | if (area && area.row.span !== areas[name].row.span) {
|
---|
| 450 | areas[name].row.updateSpan = true;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | if (area && area.column.span !== areas[name].column.span) {
|
---|
| 454 | areas[name].column.updateSpan = true;
|
---|
| 455 | }
|
---|
| 456 | });
|
---|
| 457 | });
|
---|
| 458 | parsed[index].allAreas = uniq([].concat(allAreas, areaNames));
|
---|
| 459 | parsed[index].rules.push({
|
---|
| 460 | hasDuplicates: !hasNoDuplicates,
|
---|
| 461 | params: media.params,
|
---|
| 462 | selectors: rule.selectors,
|
---|
| 463 | node: rule,
|
---|
| 464 | duplicateAreaNames: duplicateAreaNames,
|
---|
| 465 | areas: areas
|
---|
| 466 | });
|
---|
| 467 | } else {
|
---|
| 468 | // index is NOT found, push the new item to the parsed array
|
---|
| 469 | parsed.push({
|
---|
| 470 | allAreas: areaNames,
|
---|
| 471 | areasCount: 0,
|
---|
| 472 | rules: [{
|
---|
| 473 | hasDuplicates: false,
|
---|
| 474 | duplicateRules: [],
|
---|
| 475 | params: media.params,
|
---|
| 476 | selectors: rule.selectors,
|
---|
| 477 | node: rule,
|
---|
| 478 | duplicateAreaNames: [],
|
---|
| 479 | areas: areas
|
---|
| 480 | }]
|
---|
| 481 | });
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | return undefined;
|
---|
| 485 | });
|
---|
| 486 | return parsed;
|
---|
| 487 | }
|
---|
| 488 | /**
|
---|
| 489 | * insert prefixed grid-area declarations
|
---|
| 490 | * @param {Root} css css root
|
---|
| 491 | * @param {Function} isDisabled check if the rule is disabled
|
---|
| 492 | * @return {void}
|
---|
| 493 | */
|
---|
| 494 |
|
---|
| 495 |
|
---|
| 496 | function insertAreas(css, isDisabled) {
|
---|
| 497 | // parse grid-template declarations
|
---|
| 498 | var gridTemplatesData = parseGridTemplatesData(css); // return undefined if no declarations found
|
---|
| 499 |
|
---|
| 500 | if (gridTemplatesData.length === 0) {
|
---|
| 501 | return undefined;
|
---|
| 502 | } // we need to store the rules that we will insert later
|
---|
| 503 |
|
---|
| 504 |
|
---|
| 505 | var rulesToInsert = {};
|
---|
| 506 | css.walkDecls('grid-area', function (gridArea) {
|
---|
| 507 | var gridAreaRule = gridArea.parent;
|
---|
| 508 | var hasPrefixedRow = gridAreaRule.first.prop === '-ms-grid-row';
|
---|
| 509 | var gridAreaMedia = getParentMedia(gridAreaRule);
|
---|
| 510 |
|
---|
| 511 | if (isDisabled(gridArea)) {
|
---|
| 512 | return undefined;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | var gridAreaRuleIndex = gridAreaMedia ? css.index(gridAreaMedia) : css.index(gridAreaRule);
|
---|
| 516 | var value = gridArea.value; // found the data that matches grid-area identifier
|
---|
| 517 |
|
---|
| 518 | var data = gridTemplatesData.filter(function (d) {
|
---|
| 519 | return d.allAreas.includes(value);
|
---|
| 520 | })[0];
|
---|
| 521 |
|
---|
| 522 | if (!data) {
|
---|
| 523 | return true;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | var lastArea = data.allAreas[data.allAreas.length - 1];
|
---|
| 527 | var selectorBySpace = list.space(gridAreaRule.selector);
|
---|
| 528 | var selectorByComma = list.comma(gridAreaRule.selector);
|
---|
| 529 | var selectorIsComplex = selectorBySpace.length > 1 && selectorBySpace.length > selectorByComma.length; // prevent doubling of prefixes
|
---|
| 530 |
|
---|
| 531 | if (hasPrefixedRow) {
|
---|
| 532 | return false;
|
---|
| 533 | } // create the empty object with the key as the last area name
|
---|
| 534 | // e.g if we have templates with "a b c" values, "c" will be the last area
|
---|
| 535 |
|
---|
| 536 |
|
---|
| 537 | if (!rulesToInsert[lastArea]) {
|
---|
| 538 | rulesToInsert[lastArea] = {};
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | var lastRuleIsSet = false; // walk through every grid-template rule data
|
---|
| 542 |
|
---|
| 543 | for (var _iterator2 = _createForOfIteratorHelperLoose(data.rules), _step2; !(_step2 = _iterator2()).done;) {
|
---|
| 544 | var rule = _step2.value;
|
---|
| 545 | var area = rule.areas[value];
|
---|
| 546 | var hasDuplicateName = rule.duplicateAreaNames.includes(value); // if we can't find the area name, update lastRule and continue
|
---|
| 547 |
|
---|
| 548 | if (!area) {
|
---|
| 549 | var lastRuleIndex = css.index(rulesToInsert[lastArea].lastRule);
|
---|
| 550 |
|
---|
| 551 | if (gridAreaRuleIndex > lastRuleIndex) {
|
---|
| 552 | rulesToInsert[lastArea].lastRule = gridAreaMedia || gridAreaRule;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | continue;
|
---|
| 556 | } // for grid-templates inside media rule we need to create empty
|
---|
| 557 | // array to push prefixed grid-area rules later
|
---|
| 558 |
|
---|
| 559 |
|
---|
| 560 | if (rule.params && !rulesToInsert[lastArea][rule.params]) {
|
---|
| 561 | rulesToInsert[lastArea][rule.params] = [];
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | if ((!rule.hasDuplicates || !hasDuplicateName) && !rule.params) {
|
---|
| 565 | // grid-template has no duplicates and not inside media rule
|
---|
| 566 | getMSDecls(area, false, false).reverse().forEach(function (i) {
|
---|
| 567 | return gridAreaRule.prepend(Object.assign(i, {
|
---|
| 568 | raws: {
|
---|
| 569 | between: gridArea.raws.between
|
---|
| 570 | }
|
---|
| 571 | }));
|
---|
| 572 | });
|
---|
| 573 | rulesToInsert[lastArea].lastRule = gridAreaRule;
|
---|
| 574 | lastRuleIsSet = true;
|
---|
| 575 | } else if (rule.hasDuplicates && !rule.params && !selectorIsComplex) {
|
---|
| 576 | (function () {
|
---|
| 577 | // grid-template has duplicates and not inside media rule
|
---|
| 578 | var cloned = gridAreaRule.clone();
|
---|
| 579 | cloned.removeAll();
|
---|
| 580 | getMSDecls(area, area.row.updateSpan, area.column.updateSpan).reverse().forEach(function (i) {
|
---|
| 581 | return cloned.prepend(Object.assign(i, {
|
---|
| 582 | raws: {
|
---|
| 583 | between: gridArea.raws.between
|
---|
| 584 | }
|
---|
| 585 | }));
|
---|
| 586 | });
|
---|
| 587 | cloned.selectors = changeDuplicateAreaSelectors(cloned.selectors, rule.selectors);
|
---|
| 588 |
|
---|
| 589 | if (rulesToInsert[lastArea].lastRule) {
|
---|
| 590 | rulesToInsert[lastArea].lastRule.after(cloned);
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | rulesToInsert[lastArea].lastRule = cloned;
|
---|
| 594 | lastRuleIsSet = true;
|
---|
| 595 | })();
|
---|
| 596 | } else if (rule.hasDuplicates && !rule.params && selectorIsComplex && gridAreaRule.selector.includes(rule.selectors[0])) {
|
---|
| 597 | // grid-template has duplicates and not inside media rule
|
---|
| 598 | // and the selector is complex
|
---|
| 599 | gridAreaRule.walkDecls(/-ms-grid-(row|column)/, function (d) {
|
---|
| 600 | return d.remove();
|
---|
| 601 | });
|
---|
| 602 | getMSDecls(area, area.row.updateSpan, area.column.updateSpan).reverse().forEach(function (i) {
|
---|
| 603 | return gridAreaRule.prepend(Object.assign(i, {
|
---|
| 604 | raws: {
|
---|
| 605 | between: gridArea.raws.between
|
---|
| 606 | }
|
---|
| 607 | }));
|
---|
| 608 | });
|
---|
| 609 | } else if (rule.params) {
|
---|
| 610 | (function () {
|
---|
| 611 | // grid-template is inside media rule
|
---|
| 612 | // if we're inside media rule, we need to store prefixed rules
|
---|
| 613 | // inside rulesToInsert object to be able to preserve the order of media
|
---|
| 614 | // rules and merge them easily
|
---|
| 615 | var cloned = gridAreaRule.clone();
|
---|
| 616 | cloned.removeAll();
|
---|
| 617 | getMSDecls(area, area.row.updateSpan, area.column.updateSpan).reverse().forEach(function (i) {
|
---|
| 618 | return cloned.prepend(Object.assign(i, {
|
---|
| 619 | raws: {
|
---|
| 620 | between: gridArea.raws.between
|
---|
| 621 | }
|
---|
| 622 | }));
|
---|
| 623 | });
|
---|
| 624 |
|
---|
| 625 | if (rule.hasDuplicates && hasDuplicateName) {
|
---|
| 626 | cloned.selectors = changeDuplicateAreaSelectors(cloned.selectors, rule.selectors);
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | cloned.raws = rule.node.raws;
|
---|
| 630 |
|
---|
| 631 | if (css.index(rule.node.parent) > gridAreaRuleIndex) {
|
---|
| 632 | // append the prefixed rules right inside media rule
|
---|
| 633 | // with grid-template
|
---|
| 634 | rule.node.parent.append(cloned);
|
---|
| 635 | } else {
|
---|
| 636 | // store the rule to insert later
|
---|
| 637 | rulesToInsert[lastArea][rule.params].push(cloned);
|
---|
| 638 | } // set new rule as last rule ONLY if we didn't set lastRule for
|
---|
| 639 | // this grid-area before
|
---|
| 640 |
|
---|
| 641 |
|
---|
| 642 | if (!lastRuleIsSet) {
|
---|
| 643 | rulesToInsert[lastArea].lastRule = gridAreaMedia || gridAreaRule;
|
---|
| 644 | }
|
---|
| 645 | })();
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | return undefined;
|
---|
| 650 | }); // append stored rules inside the media rules
|
---|
| 651 |
|
---|
| 652 | Object.keys(rulesToInsert).forEach(function (area) {
|
---|
| 653 | var data = rulesToInsert[area];
|
---|
| 654 | var lastRule = data.lastRule;
|
---|
| 655 | Object.keys(data).reverse().filter(function (p) {
|
---|
| 656 | return p !== 'lastRule';
|
---|
| 657 | }).forEach(function (params) {
|
---|
| 658 | if (data[params].length > 0 && lastRule) {
|
---|
| 659 | lastRule.after({
|
---|
| 660 | name: 'media',
|
---|
| 661 | params: params
|
---|
| 662 | });
|
---|
| 663 | lastRule.next().append(data[params]);
|
---|
| 664 | }
|
---|
| 665 | });
|
---|
| 666 | });
|
---|
| 667 | return undefined;
|
---|
| 668 | }
|
---|
| 669 | /**
|
---|
| 670 | * Warn user if grid area identifiers are not found
|
---|
| 671 | * @param {Object} areas
|
---|
| 672 | * @param {Declaration} decl
|
---|
| 673 | * @param {Result} result
|
---|
| 674 | * @return {void}
|
---|
| 675 | */
|
---|
| 676 |
|
---|
| 677 |
|
---|
| 678 | function warnMissedAreas(areas, decl, result) {
|
---|
| 679 | var missed = Object.keys(areas);
|
---|
| 680 | decl.root().walkDecls('grid-area', function (gridArea) {
|
---|
| 681 | missed = missed.filter(function (e) {
|
---|
| 682 | return e !== gridArea.value;
|
---|
| 683 | });
|
---|
| 684 | });
|
---|
| 685 |
|
---|
| 686 | if (missed.length > 0) {
|
---|
| 687 | decl.warn(result, 'Can not find grid areas: ' + missed.join(', '));
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | return undefined;
|
---|
| 691 | }
|
---|
| 692 | /**
|
---|
| 693 | * compare selectors with grid-area rule and grid-template rule
|
---|
| 694 | * show warning if grid-template selector is not found
|
---|
| 695 | * (this function used for grid-area rule)
|
---|
| 696 | * @param {Declaration} decl
|
---|
| 697 | * @param {Result} result
|
---|
| 698 | * @return {void}
|
---|
| 699 | */
|
---|
| 700 |
|
---|
| 701 |
|
---|
| 702 | function warnTemplateSelectorNotFound(decl, result) {
|
---|
| 703 | var rule = decl.parent;
|
---|
| 704 | var root = decl.root();
|
---|
| 705 | var duplicatesFound = false; // slice selector array. Remove the last part (for comparison)
|
---|
| 706 |
|
---|
| 707 | var slicedSelectorArr = list.space(rule.selector).filter(function (str) {
|
---|
| 708 | return str !== '>';
|
---|
| 709 | }).slice(0, -1); // we need to compare only if selector is complex.
|
---|
| 710 | // e.g '.grid-cell' is simple, but '.parent > .grid-cell' is complex
|
---|
| 711 |
|
---|
| 712 | if (slicedSelectorArr.length > 0) {
|
---|
| 713 | var gridTemplateFound = false;
|
---|
| 714 | var foundAreaSelector = null;
|
---|
| 715 | root.walkDecls(/grid-template(-areas)?$/, function (d) {
|
---|
| 716 | var parent = d.parent;
|
---|
| 717 | var templateSelectors = parent.selectors;
|
---|
| 718 |
|
---|
| 719 | var _parseTemplate2 = parseTemplate({
|
---|
| 720 | decl: d,
|
---|
| 721 | gap: getGridGap(d)
|
---|
| 722 | }),
|
---|
| 723 | areas = _parseTemplate2.areas;
|
---|
| 724 |
|
---|
| 725 | var hasArea = areas[decl.value]; // find the the matching selectors
|
---|
| 726 |
|
---|
| 727 | for (var _iterator3 = _createForOfIteratorHelperLoose(templateSelectors), _step3; !(_step3 = _iterator3()).done;) {
|
---|
| 728 | var tplSelector = _step3.value;
|
---|
| 729 |
|
---|
| 730 | if (gridTemplateFound) {
|
---|
| 731 | break;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | var tplSelectorArr = list.space(tplSelector).filter(function (str) {
|
---|
| 735 | return str !== '>';
|
---|
| 736 | });
|
---|
| 737 | gridTemplateFound = tplSelectorArr.every(function (item, idx) {
|
---|
| 738 | return item === slicedSelectorArr[idx];
|
---|
| 739 | });
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | if (gridTemplateFound || !hasArea) {
|
---|
| 743 | return true;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | if (!foundAreaSelector) {
|
---|
| 747 | foundAreaSelector = parent.selector;
|
---|
| 748 | } // if we found the duplicate area with different selector
|
---|
| 749 |
|
---|
| 750 |
|
---|
| 751 | if (foundAreaSelector && foundAreaSelector !== parent.selector) {
|
---|
| 752 | duplicatesFound = true;
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | return undefined;
|
---|
| 756 | }); // warn user if we didn't find template
|
---|
| 757 |
|
---|
| 758 | if (!gridTemplateFound && duplicatesFound) {
|
---|
| 759 | decl.warn(result, 'Autoprefixer cannot find a grid-template ' + ("containing the duplicate grid-area \"" + decl.value + "\" ") + ("with full selector matching: " + slicedSelectorArr.join(' ')));
|
---|
| 760 | }
|
---|
| 761 | }
|
---|
| 762 | }
|
---|
| 763 | /**
|
---|
| 764 | * warn user if both grid-area and grid-(row|column)
|
---|
| 765 | * declarations are present in the same rule
|
---|
| 766 | * @param {Declaration} decl
|
---|
| 767 | * @param {Result} result
|
---|
| 768 | * @return {void}
|
---|
| 769 | */
|
---|
| 770 |
|
---|
| 771 |
|
---|
| 772 | function warnIfGridRowColumnExists(decl, result) {
|
---|
| 773 | var rule = decl.parent;
|
---|
| 774 | var decls = [];
|
---|
| 775 | rule.walkDecls(/^grid-(row|column)/, function (d) {
|
---|
| 776 | if (!d.prop.endsWith('-end') && !d.value.startsWith('span')) {
|
---|
| 777 | decls.push(d);
|
---|
| 778 | }
|
---|
| 779 | });
|
---|
| 780 |
|
---|
| 781 | if (decls.length > 0) {
|
---|
| 782 | decls.forEach(function (d) {
|
---|
| 783 | d.warn(result, 'You already have a grid-area declaration present in the rule. ' + ("You should use either grid-area or " + d.prop + ", not both"));
|
---|
| 784 | });
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | return undefined;
|
---|
| 788 | } // Gap utils
|
---|
| 789 |
|
---|
| 790 |
|
---|
| 791 | function getGridGap(decl) {
|
---|
| 792 | var gap = {}; // try to find gap
|
---|
| 793 |
|
---|
| 794 | var testGap = /^(grid-)?((row|column)-)?gap$/;
|
---|
| 795 | decl.parent.walkDecls(testGap, function (_ref8) {
|
---|
| 796 | var prop = _ref8.prop,
|
---|
| 797 | value = _ref8.value;
|
---|
| 798 |
|
---|
| 799 | if (/^(grid-)?gap$/.test(prop)) {
|
---|
| 800 | var _parser$nodes = parser(value).nodes,
|
---|
| 801 | row = _parser$nodes[0],
|
---|
| 802 | column = _parser$nodes[2];
|
---|
| 803 | gap.row = row && parser.stringify(row);
|
---|
| 804 | gap.column = column ? parser.stringify(column) : gap.row;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | if (/^(grid-)?row-gap$/.test(prop)) gap.row = value;
|
---|
| 808 | if (/^(grid-)?column-gap$/.test(prop)) gap.column = value;
|
---|
| 809 | });
|
---|
| 810 | return gap;
|
---|
| 811 | }
|
---|
| 812 | /**
|
---|
| 813 | * parse media parameters (for example 'min-width: 500px')
|
---|
| 814 | * @param {String} params parameter to parse
|
---|
| 815 | * @return {}
|
---|
| 816 | */
|
---|
| 817 |
|
---|
| 818 |
|
---|
| 819 | function parseMediaParams(params) {
|
---|
| 820 | if (!params) {
|
---|
| 821 | return false;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | var parsed = parser(params);
|
---|
| 825 | var prop;
|
---|
| 826 | var value;
|
---|
| 827 | parsed.walk(function (node) {
|
---|
| 828 | if (node.type === 'word' && /min|max/g.test(node.value)) {
|
---|
| 829 | prop = node.value;
|
---|
| 830 | } else if (node.value.includes('px')) {
|
---|
| 831 | value = parseInt(node.value.replace(/\D/g, ''));
|
---|
| 832 | }
|
---|
| 833 | });
|
---|
| 834 | return [prop, value];
|
---|
| 835 | }
|
---|
| 836 | /**
|
---|
| 837 | * Compare the selectors and decide if we
|
---|
| 838 | * need to inherit gap from compared selector or not.
|
---|
| 839 | * @type {String} selA
|
---|
| 840 | * @type {String} selB
|
---|
| 841 | * @return {Boolean}
|
---|
| 842 | */
|
---|
| 843 |
|
---|
| 844 |
|
---|
| 845 | function shouldInheritGap(selA, selB) {
|
---|
| 846 | var result; // get arrays of selector split in 3-deep array
|
---|
| 847 |
|
---|
| 848 | var splitSelectorArrA = splitSelector(selA);
|
---|
| 849 | var splitSelectorArrB = splitSelector(selB);
|
---|
| 850 |
|
---|
| 851 | if (splitSelectorArrA[0].length < splitSelectorArrB[0].length) {
|
---|
| 852 | // abort if selectorA has lower descendant specificity then selectorB
|
---|
| 853 | // (e.g '.grid' and '.hello .world .grid')
|
---|
| 854 | return false;
|
---|
| 855 | } else if (splitSelectorArrA[0].length > splitSelectorArrB[0].length) {
|
---|
| 856 | // if selectorA has higher descendant specificity then selectorB
|
---|
| 857 | // (e.g '.foo .bar .grid' and '.grid')
|
---|
| 858 | var idx = splitSelectorArrA[0].reduce(function (res, _ref9, index) {
|
---|
| 859 | var item = _ref9[0];
|
---|
| 860 | var firstSelectorPart = splitSelectorArrB[0][0][0];
|
---|
| 861 |
|
---|
| 862 | if (item === firstSelectorPart) {
|
---|
| 863 | return index;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | return false;
|
---|
| 867 | }, false);
|
---|
| 868 |
|
---|
| 869 | if (idx) {
|
---|
| 870 | result = splitSelectorArrB[0].every(function (arr, index) {
|
---|
| 871 | return arr.every(function (part, innerIndex) {
|
---|
| 872 | return (// because selectorA has more space elements, we need to slice
|
---|
| 873 | // selectorA array by 'idx' number to compare them
|
---|
| 874 | splitSelectorArrA[0].slice(idx)[index][innerIndex] === part
|
---|
| 875 | );
|
---|
| 876 | });
|
---|
| 877 | });
|
---|
| 878 | }
|
---|
| 879 | } else {
|
---|
| 880 | // if selectorA has the same descendant specificity as selectorB
|
---|
| 881 | // this condition covers cases such as: '.grid.foo.bar' and '.grid'
|
---|
| 882 | result = splitSelectorArrB.some(function (byCommaArr) {
|
---|
| 883 | return byCommaArr.every(function (bySpaceArr, index) {
|
---|
| 884 | return bySpaceArr.every(function (part, innerIndex) {
|
---|
| 885 | return splitSelectorArrA[0][index][innerIndex] === part;
|
---|
| 886 | });
|
---|
| 887 | });
|
---|
| 888 | });
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | return result;
|
---|
| 892 | }
|
---|
| 893 | /**
|
---|
| 894 | * inherit grid gap values from the closest rule above
|
---|
| 895 | * with the same selector
|
---|
| 896 | * @param {Declaration} decl
|
---|
| 897 | * @param {Object} gap gap values
|
---|
| 898 | * @return {Object | Boolean} return gap values or false (if not found)
|
---|
| 899 | */
|
---|
| 900 |
|
---|
| 901 |
|
---|
| 902 | function inheritGridGap(decl, gap) {
|
---|
| 903 | var rule = decl.parent;
|
---|
| 904 | var mediaRule = getParentMedia(rule);
|
---|
| 905 | var root = rule.root(); // get an array of selector split in 3-deep array
|
---|
| 906 |
|
---|
| 907 | var splitSelectorArr = splitSelector(rule.selector); // abort if the rule already has gaps
|
---|
| 908 |
|
---|
| 909 | if (Object.keys(gap).length > 0) {
|
---|
| 910 | return false;
|
---|
| 911 | } // e.g ['min-width']
|
---|
| 912 |
|
---|
| 913 |
|
---|
| 914 | var _parseMediaParams = parseMediaParams(mediaRule.params),
|
---|
| 915 | prop = _parseMediaParams[0];
|
---|
| 916 |
|
---|
| 917 | var lastBySpace = splitSelectorArr[0]; // get escaped value from the selector
|
---|
| 918 | // if we have '.grid-2.foo.bar' selector, will be '\.grid\-2'
|
---|
| 919 |
|
---|
| 920 | var escaped = escapeRegexp(lastBySpace[lastBySpace.length - 1][0]);
|
---|
| 921 | var regexp = new RegExp("(" + escaped + "$)|(" + escaped + "[,.])"); // find the closest rule with the same selector
|
---|
| 922 |
|
---|
| 923 | var closestRuleGap;
|
---|
| 924 | root.walkRules(regexp, function (r) {
|
---|
| 925 | var gridGap; // abort if are checking the same rule
|
---|
| 926 |
|
---|
| 927 | if (rule.toString() === r.toString()) {
|
---|
| 928 | return false;
|
---|
| 929 | } // find grid-gap values
|
---|
| 930 |
|
---|
| 931 |
|
---|
| 932 | r.walkDecls('grid-gap', function (d) {
|
---|
| 933 | return gridGap = getGridGap(d);
|
---|
| 934 | }); // skip rule without gaps
|
---|
| 935 |
|
---|
| 936 | if (!gridGap || Object.keys(gridGap).length === 0) {
|
---|
| 937 | return true;
|
---|
| 938 | } // skip rules that should not be inherited from
|
---|
| 939 |
|
---|
| 940 |
|
---|
| 941 | if (!shouldInheritGap(rule.selector, r.selector)) {
|
---|
| 942 | return true;
|
---|
| 943 | }
|
---|
| 944 |
|
---|
| 945 | var media = getParentMedia(r);
|
---|
| 946 |
|
---|
| 947 | if (media) {
|
---|
| 948 | // if we are inside media, we need to check that media props match
|
---|
| 949 | // e.g ('min-width' === 'min-width')
|
---|
| 950 | var propToCompare = parseMediaParams(media.params)[0];
|
---|
| 951 |
|
---|
| 952 | if (propToCompare === prop) {
|
---|
| 953 | closestRuleGap = gridGap;
|
---|
| 954 | return true;
|
---|
| 955 | }
|
---|
| 956 | } else {
|
---|
| 957 | closestRuleGap = gridGap;
|
---|
| 958 | return true;
|
---|
| 959 | }
|
---|
| 960 |
|
---|
| 961 | return undefined;
|
---|
| 962 | }); // if we find the closest gap object
|
---|
| 963 |
|
---|
| 964 | if (closestRuleGap && Object.keys(closestRuleGap).length > 0) {
|
---|
| 965 | return closestRuleGap;
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | return false;
|
---|
| 969 | }
|
---|
| 970 |
|
---|
| 971 | function warnGridGap(_ref10) {
|
---|
| 972 | var gap = _ref10.gap,
|
---|
| 973 | hasColumns = _ref10.hasColumns,
|
---|
| 974 | decl = _ref10.decl,
|
---|
| 975 | result = _ref10.result;
|
---|
| 976 | var hasBothGaps = gap.row && gap.column;
|
---|
| 977 |
|
---|
| 978 | if (!hasColumns && (hasBothGaps || gap.column && !gap.row)) {
|
---|
| 979 | delete gap.column;
|
---|
| 980 | decl.warn(result, 'Can not implement grid-gap without grid-template-columns');
|
---|
| 981 | }
|
---|
| 982 | }
|
---|
| 983 | /**
|
---|
| 984 | * normalize the grid-template-rows/columns values
|
---|
| 985 | * @param {String} str grid-template-rows/columns value
|
---|
| 986 | * @return {Array} normalized array with values
|
---|
| 987 | * @example
|
---|
| 988 | * let normalized = normalizeRowColumn('1fr repeat(2, 20px 50px) 1fr')
|
---|
| 989 | * normalized // <= ['1fr', '20px', '50px', '20px', '50px', '1fr']
|
---|
| 990 | */
|
---|
| 991 |
|
---|
| 992 |
|
---|
| 993 | function normalizeRowColumn(str) {
|
---|
| 994 | var normalized = parser(str).nodes.reduce(function (result, node) {
|
---|
| 995 | if (node.type === 'function' && node.value === 'repeat') {
|
---|
| 996 | var key = 'count';
|
---|
| 997 |
|
---|
| 998 | var _node$nodes$reduce = node.nodes.reduce(function (acc, n) {
|
---|
| 999 | if (n.type === 'word' && key === 'count') {
|
---|
| 1000 | acc[0] = Math.abs(parseInt(n.value));
|
---|
| 1001 | return acc;
|
---|
| 1002 | }
|
---|
| 1003 |
|
---|
| 1004 | if (n.type === 'div' && n.value === ',') {
|
---|
| 1005 | key = 'value';
|
---|
| 1006 | return acc;
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
| 1009 | if (key === 'value') {
|
---|
| 1010 | acc[1] += parser.stringify(n);
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | return acc;
|
---|
| 1014 | }, [0, '']),
|
---|
| 1015 | count = _node$nodes$reduce[0],
|
---|
| 1016 | value = _node$nodes$reduce[1];
|
---|
| 1017 |
|
---|
| 1018 | if (count) {
|
---|
| 1019 | for (var i = 0; i < count; i++) {
|
---|
| 1020 | result.push(value);
|
---|
| 1021 | }
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 | return result;
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 | if (node.type === 'space') {
|
---|
| 1028 | return result;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 | return result.concat(parser.stringify(node));
|
---|
| 1032 | }, []);
|
---|
| 1033 | return normalized;
|
---|
| 1034 | }
|
---|
| 1035 | /**
|
---|
| 1036 | * Autoplace grid items
|
---|
| 1037 | * @param {Declaration} decl
|
---|
| 1038 | * @param {Result} result
|
---|
| 1039 | * @param {Object} gap gap values
|
---|
| 1040 | * @param {String} autoflowValue grid-auto-flow value
|
---|
| 1041 | * @return {void}
|
---|
| 1042 | * @see https://github.com/postcss/autoprefixer/issues/1148
|
---|
| 1043 | */
|
---|
| 1044 |
|
---|
| 1045 |
|
---|
| 1046 | function autoplaceGridItems(decl, result, gap, autoflowValue) {
|
---|
| 1047 | if (autoflowValue === void 0) {
|
---|
| 1048 | autoflowValue = 'row';
|
---|
| 1049 | }
|
---|
| 1050 |
|
---|
| 1051 | var parent = decl.parent;
|
---|
| 1052 | var rowDecl = parent.nodes.find(function (i) {
|
---|
| 1053 | return i.prop === 'grid-template-rows';
|
---|
| 1054 | });
|
---|
| 1055 | var rows = normalizeRowColumn(rowDecl.value);
|
---|
| 1056 | var columns = normalizeRowColumn(decl.value); // Build array of area names with dummy values. If we have 3 columns and
|
---|
| 1057 | // 2 rows, filledRows will be equal to ['1 2 3', '4 5 6']
|
---|
| 1058 |
|
---|
| 1059 | var filledRows = rows.map(function (_, rowIndex) {
|
---|
| 1060 | return Array.from({
|
---|
| 1061 | length: columns.length
|
---|
| 1062 | }, function (v, k) {
|
---|
| 1063 | return k + rowIndex * columns.length + 1;
|
---|
| 1064 | }).join(' ');
|
---|
| 1065 | });
|
---|
| 1066 | var areas = parseGridAreas({
|
---|
| 1067 | rows: filledRows,
|
---|
| 1068 | gap: gap
|
---|
| 1069 | });
|
---|
| 1070 | var keys = Object.keys(areas);
|
---|
| 1071 | var items = keys.map(function (i) {
|
---|
| 1072 | return areas[i];
|
---|
| 1073 | }); // Change the order of cells if grid-auto-flow value is 'column'
|
---|
| 1074 |
|
---|
| 1075 | if (autoflowValue.includes('column')) {
|
---|
| 1076 | items = items.sort(function (a, b) {
|
---|
| 1077 | return a.column.start - b.column.start;
|
---|
| 1078 | });
|
---|
| 1079 | } // Insert new rules
|
---|
| 1080 |
|
---|
| 1081 |
|
---|
| 1082 | items.reverse().forEach(function (item, index) {
|
---|
| 1083 | var column = item.column,
|
---|
| 1084 | row = item.row;
|
---|
| 1085 | var nodeSelector = parent.selectors.map(function (sel) {
|
---|
| 1086 | return sel + (" > *:nth-child(" + (keys.length - index) + ")");
|
---|
| 1087 | }).join(', '); // create new rule
|
---|
| 1088 |
|
---|
| 1089 | var node = parent.clone().removeAll(); // change rule selector
|
---|
| 1090 |
|
---|
| 1091 | node.selector = nodeSelector; // insert prefixed row/column values
|
---|
| 1092 |
|
---|
| 1093 | node.append({
|
---|
| 1094 | prop: '-ms-grid-row',
|
---|
| 1095 | value: row.start
|
---|
| 1096 | });
|
---|
| 1097 | node.append({
|
---|
| 1098 | prop: '-ms-grid-column',
|
---|
| 1099 | value: column.start
|
---|
| 1100 | }); // insert rule
|
---|
| 1101 |
|
---|
| 1102 | parent.after(node);
|
---|
| 1103 | });
|
---|
| 1104 | return undefined;
|
---|
| 1105 | }
|
---|
| 1106 |
|
---|
| 1107 | module.exports = {
|
---|
| 1108 | parse: parse,
|
---|
| 1109 | translate: translate,
|
---|
| 1110 | parseTemplate: parseTemplate,
|
---|
| 1111 | parseGridAreas: parseGridAreas,
|
---|
| 1112 | warnMissedAreas: warnMissedAreas,
|
---|
| 1113 | insertAreas: insertAreas,
|
---|
| 1114 | insertDecl: insertDecl,
|
---|
| 1115 | prefixTrackProp: prefixTrackProp,
|
---|
| 1116 | prefixTrackValue: prefixTrackValue,
|
---|
| 1117 | getGridGap: getGridGap,
|
---|
| 1118 | warnGridGap: warnGridGap,
|
---|
| 1119 | warnTemplateSelectorNotFound: warnTemplateSelectorNotFound,
|
---|
| 1120 | warnIfGridRowColumnExists: warnIfGridRowColumnExists,
|
---|
| 1121 | inheritGridGap: inheritGridGap,
|
---|
| 1122 | autoplaceGridItems: autoplaceGridItems
|
---|
| 1123 | }; |
---|