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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[9af201e]1'use strict';
2const { list } = require('postcss');
3const { isWidth, isStyle, isColor } = require('./validateWsc.js');
4
5const none = /^\s*(none|medium)(\s+none(\s+(none|currentcolor))?)?\s*$/i;
6
7/* Approximate https://drafts.csswg.org/css-values-4/#typedef-dashed-ident */
8// eslint-disable-next-line no-control-regex
9const varRE = /--(\w|-|[^\x00-\x7F])+/g;
10/** @type {(v: string) => string} */
11const toLower = (v) => {
12 let match;
13 let lastIndex = 0;
14 let result = '';
15 varRE.lastIndex = 0;
16 while ((match = varRE.exec(v)) !== null) {
17 if (match.index > lastIndex) {
18 result += v.substring(lastIndex, match.index).toLowerCase();
19 }
20 result += match[0];
21 lastIndex = match.index + match[0].length;
22 }
23 if (lastIndex < v.length) {
24 result += v.substring(lastIndex).toLowerCase();
25 }
26 if (result === '') {
27 return v;
28 }
29 return result;
30};
31
32/**
33 * @param {string} value
34 * @return {[string, string, string]}
35 */
36module.exports = function parseWsc(value) {
37 if (none.test(value)) {
38 return ['medium', 'none', 'currentcolor'];
39 }
40
41 let width, style, color;
42
43 const values = list.space(value);
44 if (
45 values.length > 1 &&
46 isStyle(values[1]) &&
47 values[0].toLowerCase() === 'none'
48 ) {
49 values.unshift();
50 width = '0';
51 }
52
53 /** @type {string[]} */
54 const unknown = [];
55
56 values.forEach((v) => {
57 if (isStyle(v)) {
58 style = toLower(v);
59 } else if (isWidth(v)) {
60 width = toLower(v);
61 } else if (isColor(v)) {
62 color = toLower(v);
63 } else {
64 unknown.push(v);
65 }
66 });
67
68 if (unknown.length) {
69 if (!width && style && color) {
70 width = unknown.pop();
71 }
72
73 if (width && !style && color) {
74 style = unknown.pop();
75 }
76
77 if (width && style && !color) {
78 color = unknown.pop();
79 }
80 }
81
82 return /** @type {[string, string, string]} */ ([width, style, color]);
83};
Note: See TracBrowser for help on using the repository browser.