| 1 | 'use strict';
|
|---|
| 2 | const joinGridValue = require('../lib/joinGridValue');
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * @param {import('postcss-value-parser').ParsedValue} gridAutoFlow
|
|---|
| 6 | * @return {import('postcss-value-parser').ParsedValue | string}
|
|---|
| 7 | */
|
|---|
| 8 | const normalizeGridAutoFlow = (gridAutoFlow) => {
|
|---|
| 9 | let newValue = { front: '', back: '' };
|
|---|
| 10 | let shouldNormalize = false;
|
|---|
| 11 | gridAutoFlow.walk((node) => {
|
|---|
| 12 | if (node.value === 'dense') {
|
|---|
| 13 | shouldNormalize = true;
|
|---|
| 14 | newValue.back = node.value;
|
|---|
| 15 | } else if (['row', 'column'].includes(node.value.trim().toLowerCase())) {
|
|---|
| 16 | shouldNormalize = true;
|
|---|
| 17 | newValue.front = node.value;
|
|---|
| 18 | } else {
|
|---|
| 19 | shouldNormalize = false;
|
|---|
| 20 | }
|
|---|
| 21 | });
|
|---|
| 22 | if (shouldNormalize) {
|
|---|
| 23 | return `${newValue.front.trim()} ${newValue.back.trim()}`;
|
|---|
| 24 | }
|
|---|
| 25 | return gridAutoFlow;
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * @param {import('postcss-value-parser').ParsedValue} gridGap
|
|---|
| 30 | * @return {import('postcss-value-parser').ParsedValue | string}
|
|---|
| 31 | */
|
|---|
| 32 | const normalizeGridColumnRowGap = (gridGap) => {
|
|---|
| 33 | let newValue = { front: '', back: '' };
|
|---|
| 34 | let shouldNormalize = false;
|
|---|
| 35 | gridGap.walk((node) => {
|
|---|
| 36 | // console.log(node);
|
|---|
| 37 | if (node.value === 'normal') {
|
|---|
| 38 | shouldNormalize = true;
|
|---|
| 39 | newValue.front = node.value;
|
|---|
| 40 | } else {
|
|---|
| 41 | newValue.back = `${newValue.back} ${node.value}`;
|
|---|
| 42 | }
|
|---|
| 43 | });
|
|---|
| 44 | if (shouldNormalize) {
|
|---|
| 45 | return `${newValue.front.trim()} ${newValue.back.trim()}`;
|
|---|
| 46 | }
|
|---|
| 47 | return gridGap;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * @param {import('postcss-value-parser').ParsedValue} grid
|
|---|
| 52 | * @return {string | string[]}
|
|---|
| 53 | */
|
|---|
| 54 | const normalizeGridColumnRow = (grid) => {
|
|---|
| 55 | // cant do normalization here using node, so copy it as a string
|
|---|
| 56 | let gridValue = grid.toString().split('/'); // node -> string value, split -> " 2 / 3 span " -> [' 2','3 span ']
|
|---|
| 57 | if (gridValue.length > 1) {
|
|---|
| 58 | return joinGridValue(
|
|---|
| 59 | gridValue.map((gridLine) => {
|
|---|
| 60 | let normalizeValue = {
|
|---|
| 61 | front: '',
|
|---|
| 62 | back: '',
|
|---|
| 63 | };
|
|---|
| 64 | gridLine = gridLine.trim(); // '3 span ' -> '3 span'
|
|---|
| 65 | gridLine.split(' ').forEach((node) => {
|
|---|
| 66 | // ['3','span']
|
|---|
| 67 | if (node === 'span') {
|
|---|
| 68 | normalizeValue.front = node; // span _
|
|---|
| 69 | } else {
|
|---|
| 70 | normalizeValue.back = `${normalizeValue.back} ${node}`; // _ 3
|
|---|
| 71 | }
|
|---|
| 72 | });
|
|---|
| 73 | return `${normalizeValue.front.trim()} ${normalizeValue.back.trim()}`; // span 3
|
|---|
| 74 | })
|
|---|
| 75 | // returns "2 / span 3"
|
|---|
| 76 | );
|
|---|
| 77 | }
|
|---|
| 78 | // doing this separating if `/` is not present as while joining('/') , it will add `/` at the end
|
|---|
| 79 | return gridValue.map((gridLine) => {
|
|---|
| 80 | let normalizeValue = {
|
|---|
| 81 | front: '',
|
|---|
| 82 | back: '',
|
|---|
| 83 | };
|
|---|
| 84 | gridLine = gridLine.trim();
|
|---|
| 85 | gridLine.split(' ').forEach((node) => {
|
|---|
| 86 | if (node === 'span') {
|
|---|
| 87 | normalizeValue.front = node;
|
|---|
| 88 | } else {
|
|---|
| 89 | normalizeValue.back = `${normalizeValue.back} ${node}`;
|
|---|
| 90 | }
|
|---|
| 91 | });
|
|---|
| 92 | return `${normalizeValue.front.trim()} ${normalizeValue.back.trim()}`;
|
|---|
| 93 | });
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | module.exports = {
|
|---|
| 97 | normalizeGridAutoFlow,
|
|---|
| 98 | normalizeGridColumnRowGap,
|
|---|
| 99 | normalizeGridColumnRow,
|
|---|
| 100 | };
|
|---|