| 1 | 'use strict';
|
|---|
| 2 | const { list } = require('postcss');
|
|---|
| 3 | const { unit } = require('postcss-value-parser');
|
|---|
| 4 | const stylehacks = require('stylehacks');
|
|---|
| 5 | const canMerge = require('../canMerge.js');
|
|---|
| 6 | const getDecls = require('../getDecls.js');
|
|---|
| 7 | const getValue = require('../getValue.js');
|
|---|
| 8 | const mergeRules = require('../mergeRules.js');
|
|---|
| 9 | const insertCloned = require('../insertCloned.js');
|
|---|
| 10 | const isCustomProp = require('../isCustomProp.js');
|
|---|
| 11 | const canExplode = require('../canExplode.js');
|
|---|
| 12 |
|
|---|
| 13 | const properties = ['column-width', 'column-count'];
|
|---|
| 14 | const auto = 'auto';
|
|---|
| 15 | const inherit = 'inherit';
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Normalize a columns shorthand definition. Both of the longhand
|
|---|
| 19 | * properties' initial values are 'auto', and as per the spec,
|
|---|
| 20 | * omitted values are set to their initial values. Thus, we can
|
|---|
| 21 | * remove any 'auto' definition when there are two values.
|
|---|
| 22 | *
|
|---|
| 23 | * Specification link: https://www.w3.org/TR/css3-multicol/
|
|---|
| 24 | *
|
|---|
| 25 | * @param {[string, string]} values
|
|---|
| 26 | * @return {string}
|
|---|
| 27 | */
|
|---|
| 28 | function normalize(values) {
|
|---|
| 29 | if (values[0].toLowerCase() === auto) {
|
|---|
| 30 | return values[1];
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | if (values[1].toLowerCase() === auto) {
|
|---|
| 34 | return values[0];
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | if (
|
|---|
| 38 | values[0].toLowerCase() === inherit &&
|
|---|
| 39 | values[1].toLowerCase() === inherit
|
|---|
| 40 | ) {
|
|---|
| 41 | return inherit;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | return values.join(' ');
|
|---|
| 45 | }
|
|---|
| 46 | /**
|
|---|
| 47 | * @param {import('postcss').Rule} rule
|
|---|
| 48 | * @return {void}
|
|---|
| 49 | */
|
|---|
| 50 | function explode(rule) {
|
|---|
| 51 | rule.walkDecls(/^columns$/i, (decl) => {
|
|---|
| 52 | if (!canExplode(decl)) {
|
|---|
| 53 | return;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | if (stylehacks.detect(decl)) {
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | let values = list.space(decl.value);
|
|---|
| 61 |
|
|---|
| 62 | if (values.length === 1) {
|
|---|
| 63 | values.push(auto);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | values.forEach((value, i) => {
|
|---|
| 67 | let prop = properties[1];
|
|---|
| 68 | const dimension = unit(value);
|
|---|
| 69 | if (value.toLowerCase() === auto) {
|
|---|
| 70 | prop = properties[i];
|
|---|
| 71 | } else if (dimension && dimension.unit !== '') {
|
|---|
| 72 | prop = properties[0];
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | insertCloned(/** @type {import('postcss').Rule} */ (decl.parent), decl, {
|
|---|
| 76 | prop,
|
|---|
| 77 | value,
|
|---|
| 78 | });
|
|---|
| 79 | });
|
|---|
| 80 |
|
|---|
| 81 | decl.remove();
|
|---|
| 82 | });
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * @param {import('postcss').Rule} rule
|
|---|
| 87 | * @return {void}
|
|---|
| 88 | */
|
|---|
| 89 | function cleanup(rule) {
|
|---|
| 90 | let decls = getDecls(rule, ['columns'].concat(properties));
|
|---|
| 91 |
|
|---|
| 92 | while (decls.length) {
|
|---|
| 93 | const lastNode = decls[decls.length - 1];
|
|---|
| 94 |
|
|---|
| 95 | // remove properties of lower precedence
|
|---|
| 96 | const lesser = decls.filter(
|
|---|
| 97 | (node) =>
|
|---|
| 98 | !stylehacks.detect(lastNode) &&
|
|---|
| 99 | !stylehacks.detect(node) &&
|
|---|
| 100 | node !== lastNode &&
|
|---|
| 101 | node.important === lastNode.important &&
|
|---|
| 102 | lastNode.prop === 'columns' &&
|
|---|
| 103 | node.prop !== lastNode.prop
|
|---|
| 104 | );
|
|---|
| 105 |
|
|---|
| 106 | for (const node of lesser) {
|
|---|
| 107 | node.remove();
|
|---|
| 108 | }
|
|---|
| 109 | decls = decls.filter((node) => !lesser.includes(node));
|
|---|
| 110 |
|
|---|
| 111 | // get duplicate properties
|
|---|
| 112 | let duplicates = decls.filter(
|
|---|
| 113 | (node) =>
|
|---|
| 114 | !stylehacks.detect(lastNode) &&
|
|---|
| 115 | !stylehacks.detect(node) &&
|
|---|
| 116 | node !== lastNode &&
|
|---|
| 117 | node.important === lastNode.important &&
|
|---|
| 118 | node.prop === lastNode.prop &&
|
|---|
| 119 | !(!isCustomProp(node) && isCustomProp(lastNode))
|
|---|
| 120 | );
|
|---|
| 121 |
|
|---|
| 122 | for (const node of duplicates) {
|
|---|
| 123 | node.remove();
|
|---|
| 124 | }
|
|---|
| 125 | decls = decls.filter(
|
|---|
| 126 | (node) => node !== lastNode && !duplicates.includes(node)
|
|---|
| 127 | );
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * @param {import('postcss').Rule} rule
|
|---|
| 133 | * @return {void}
|
|---|
| 134 | */
|
|---|
| 135 | function merge(rule) {
|
|---|
| 136 | mergeRules(rule, properties, (rules, lastNode) => {
|
|---|
| 137 | if (canMerge(rules) && !rules.some(stylehacks.detect)) {
|
|---|
| 138 | insertCloned(
|
|---|
| 139 | /** @type {import('postcss').Rule} */ (lastNode.parent),
|
|---|
| 140 | lastNode,
|
|---|
| 141 | {
|
|---|
| 142 | prop: 'columns',
|
|---|
| 143 | value: normalize(/** @type [string, string] */ (rules.map(getValue))),
|
|---|
| 144 | }
|
|---|
| 145 | );
|
|---|
| 146 |
|
|---|
| 147 | for (const node of rules) {
|
|---|
| 148 | node.remove();
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | return true;
|
|---|
| 152 | }
|
|---|
| 153 | return false;
|
|---|
| 154 | });
|
|---|
| 155 |
|
|---|
| 156 | cleanup(rule);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | module.exports = {
|
|---|
| 160 | explode,
|
|---|
| 161 | merge,
|
|---|
| 162 | };
|
|---|