source: trip-planner-front/node_modules/postcss-normalize-repeat-style/dist/index.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
10var _cssnanoUtils = require("cssnano-utils");
11
12var _map = _interopRequireDefault(require("./lib/map"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function evenValues(list, index) {
17 return index % 2 === 0;
18}
19
20const repeatKeywords = _map.default.map(mapping => mapping[0]);
21
22const getMatch = (0, _cssnanoUtils.getMatch)(_map.default);
23
24function isCommaNode(node) {
25 return node.type === 'div' && node.value === ',';
26}
27
28function isVariableFunctionNode(node) {
29 if (node.type !== 'function') {
30 return false;
31 }
32
33 return ['var', 'env'].includes(node.value.toLowerCase());
34}
35
36function transform(value) {
37 const parsed = (0, _postcssValueParser.default)(value);
38
39 if (parsed.nodes.length === 1) {
40 return value;
41 }
42
43 const ranges = [];
44 let rangeIndex = 0;
45 let shouldContinue = true;
46 parsed.nodes.forEach((node, index) => {
47 // After comma (`,`) follows next background
48 if (isCommaNode(node)) {
49 rangeIndex += 1;
50 shouldContinue = true;
51 return;
52 }
53
54 if (!shouldContinue) {
55 return;
56 } // After separator (`/`) follows `background-size` values
57 // Avoid them
58
59
60 if (node.type === 'div' && node.value === '/') {
61 shouldContinue = false;
62 return;
63 }
64
65 if (!ranges[rangeIndex]) {
66 ranges[rangeIndex] = {
67 start: null,
68 end: null
69 };
70 } // Do not try to be processed `var and `env` function inside background
71
72
73 if (isVariableFunctionNode(node)) {
74 shouldContinue = false;
75 ranges[rangeIndex].start = null;
76 ranges[rangeIndex].end = null;
77 return;
78 }
79
80 const isRepeatKeyword = node.type === 'word' && repeatKeywords.includes(node.value.toLowerCase());
81
82 if (ranges[rangeIndex].start === null && isRepeatKeyword) {
83 ranges[rangeIndex].start = index;
84 ranges[rangeIndex].end = index;
85 return;
86 }
87
88 if (ranges[rangeIndex].start !== null) {
89 if (node.type === 'space') {
90 return;
91 } else if (isRepeatKeyword) {
92 ranges[rangeIndex].end = index;
93 return;
94 }
95
96 return;
97 }
98 });
99 ranges.forEach(range => {
100 if (range.start === null) {
101 return;
102 }
103
104 const nodes = parsed.nodes.slice(range.start, range.end + 1);
105
106 if (nodes.length !== 3) {
107 return;
108 }
109
110 const match = getMatch(nodes.filter(evenValues).map(n => n.value.toLowerCase()));
111
112 if (match) {
113 nodes[0].value = match;
114 nodes[1].value = nodes[2].value = '';
115 }
116 });
117 return parsed.toString();
118}
119
120function pluginCreator() {
121 return {
122 postcssPlugin: 'postcss-normalize-repeat-style',
123
124 prepare() {
125 const cache = {};
126 return {
127 OnceExit(css) {
128 css.walkDecls(/^(background(-repeat)?|(-\w+-)?mask-repeat)$/i, decl => {
129 const value = decl.value;
130
131 if (!value) {
132 return;
133 }
134
135 if (cache[value]) {
136 decl.value = cache[value];
137 return;
138 }
139
140 const result = transform(value);
141 decl.value = result;
142 cache[value] = result;
143 });
144 }
145
146 };
147 }
148
149 };
150}
151
152pluginCreator.postcss = true;
153var _default = pluginCreator;
154exports.default = _default;
155module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.