parseWsc.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseWsc;
  6. var _postcss = require('postcss');
  7. var _validateWsc = require('./validateWsc');
  8. const none = /^\s*(none|medium)(\s+none(\s+(none|currentcolor))?)?\s*$/i;
  9. const varRE = /(^.*var)(.*\(.*--.*\))(.*)/i;
  10. const varPreserveCase = p => `${p[1].toLowerCase()}${p[2]}${p[3].toLowerCase()}`;
  11. const toLower = v => {
  12. const match = varRE.exec(v);
  13. return match ? varPreserveCase(match) : v.toLowerCase();
  14. };
  15. function parseWsc(value) {
  16. if (none.test(value)) {
  17. return ['medium', 'none', 'currentcolor'];
  18. }
  19. let width, style, color;
  20. const values = _postcss.list.space(value);
  21. if (values.length > 1 && (0, _validateWsc.isStyle)(values[1]) && values[0].toLowerCase() === 'none') {
  22. values.unshift();
  23. width = '0';
  24. }
  25. const unknown = [];
  26. values.forEach(v => {
  27. if ((0, _validateWsc.isStyle)(v)) {
  28. style = toLower(v);
  29. } else if ((0, _validateWsc.isWidth)(v)) {
  30. width = toLower(v);
  31. } else if ((0, _validateWsc.isColor)(v)) {
  32. color = toLower(v);
  33. } else {
  34. unknown.push(v);
  35. }
  36. });
  37. if (unknown.length) {
  38. if (!width && style && color) {
  39. width = unknown.pop();
  40. }
  41. if (width && !style && color) {
  42. style = unknown.pop();
  43. }
  44. if (width && style && !color) {
  45. color = unknown.pop();
  46. }
  47. }
  48. return [width, style, color];
  49. }
  50. module.exports = exports['default'];