| 1 | 'use strict';
|
|---|
| 2 | const { list } = require('postcss');
|
|---|
| 3 | const { isWidth, isStyle, isColor } = require('./validateWsc.js');
|
|---|
| 4 |
|
|---|
| 5 | const 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
|
|---|
| 9 | const varRE = /--(\w|-|[^\x00-\x7F])+/g;
|
|---|
| 10 | /** @type {(v: string) => string} */
|
|---|
| 11 | const 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 | */
|
|---|
| 36 | module.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 | };
|
|---|