source: frontend/node_modules/postcss-merge-longhand/src/lib/mergeRules.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[9af201e]1'use strict';
2const hasAllProps = require('./hasAllProps.js');
3const getDecls = require('./getDecls.js');
4const getRules = require('./getRules.js');
5
6/**
7 * @param {import('postcss').Declaration} propA
8 * @param {import('postcss').Declaration} propB
9 * @return {boolean}
10 */
11function isConflictingProp(propA, propB) {
12 if (
13 !propB.prop ||
14 propB.important !== propA.important ||
15 propA.prop === propB.prop
16 ) {
17 return false;
18 }
19
20 const partsA = propA.prop.split('-');
21 const partsB = propB.prop.split('-');
22
23 /* Be safe: check that the first part matches. So we don't try to
24 * combine e.g. border-color and color.
25 */
26 if (partsA[0] !== partsB[0]) {
27 return false;
28 }
29
30 const partsASet = new Set(partsA);
31 return partsB.every((partB) => partsASet.has(partB));
32}
33
34/**
35 * @param {import('postcss').Declaration[]} match
36 * @param {import('postcss').Declaration[]} nodes
37 * @return {boolean}
38 */
39function hasConflicts(match, nodes) {
40 const firstNode = Math.min(...match.map((n) => nodes.indexOf(n)));
41 const lastNode = Math.max(...match.map((n) => nodes.indexOf(n)));
42 const between = nodes.slice(firstNode + 1, lastNode);
43
44 return match.some((a) => between.some((b) => isConflictingProp(a, b)));
45}
46
47/**
48 * @param {import('postcss').Rule} rule
49 * @param {string[]} properties
50 * @param {(rules: import('postcss').Declaration[], last: import('postcss').Declaration, props: import('postcss').Declaration[]) => boolean} callback
51 * @return {void}
52 */
53module.exports = function mergeRules(rule, properties, callback) {
54 let decls = getDecls(rule, properties);
55
56 while (decls.length) {
57 const last = decls[decls.length - 1];
58 const props = decls.filter((node) => node.important === last.important);
59 const rules = getRules(props, properties);
60
61 if (
62 hasAllProps(rules, ...properties) &&
63 !hasConflicts(
64 rules,
65 /** @type import('postcss').Declaration[]*/ (rule.nodes)
66 )
67 ) {
68 if (callback(rules, last, props)) {
69 decls = decls.filter((node) => !rules.includes(node));
70 }
71 }
72
73 decls = decls.filter((node) => node !== last);
74 }
75};
Note: See TracBrowser for help on using the repository browser.