replaceRuleSelector.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = replaceRuleSelector;
  6. var _list = require("postcss/lib/list");
  7. var _list2 = _interopRequireDefault(_list);
  8. var _balancedMatch = require("balanced-match");
  9. var _balancedMatch2 = _interopRequireDefault(_balancedMatch);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  12. var pseudoClass = ":matches";
  13. var selectorElementRE = /^[a-zA-Z]/;
  14. function isElementSelector(selector) {
  15. var matches = selectorElementRE.exec(selector);
  16. // console.log({selector, matches})
  17. return matches;
  18. }
  19. function normalizeSelector(selector, preWhitespace, pre) {
  20. if (isElementSelector(selector) && !isElementSelector(pre)) {
  21. return `${preWhitespace}${selector}${pre}`;
  22. }
  23. return `${preWhitespace}${pre}${selector}`;
  24. }
  25. function explodeSelector(selector, options) {
  26. if (selector && selector.indexOf(pseudoClass) > -1) {
  27. var newSelectors = [];
  28. var preWhitespaceMatches = selector.match(/^\s+/);
  29. var preWhitespace = preWhitespaceMatches ? preWhitespaceMatches[0] : "";
  30. var selectorPart = _list2.default.comma(selector);
  31. selectorPart.forEach(function (part) {
  32. var position = part.indexOf(pseudoClass);
  33. var pre = part.slice(0, position);
  34. var body = part.slice(position);
  35. var matches = (0, _balancedMatch2.default)("(", ")", body);
  36. var bodySelectors = matches && matches.body ? _list2.default.comma(matches.body).reduce(function (acc, s) {
  37. return [].concat(_toConsumableArray(acc), _toConsumableArray(explodeSelector(s, options)));
  38. }, []) : [body];
  39. var postSelectors = matches && matches.post ? explodeSelector(matches.post, options) : [];
  40. var newParts = void 0;
  41. if (postSelectors.length === 0) {
  42. // the test below is a poor way to try we are facing a piece of a
  43. // selector...
  44. if (position === -1 || pre.indexOf(" ") > -1) {
  45. newParts = bodySelectors.map(function (s) {
  46. return preWhitespace + pre + s;
  47. });
  48. } else {
  49. newParts = bodySelectors.map(function (s) {
  50. return normalizeSelector(s, preWhitespace, pre);
  51. });
  52. }
  53. } else {
  54. newParts = [];
  55. postSelectors.forEach(function (postS) {
  56. bodySelectors.forEach(function (s) {
  57. newParts.push(preWhitespace + pre + s + postS);
  58. });
  59. });
  60. }
  61. newSelectors = [].concat(_toConsumableArray(newSelectors), _toConsumableArray(newParts));
  62. });
  63. return newSelectors;
  64. }
  65. return [selector];
  66. }
  67. function replaceRuleSelector(rule, options) {
  68. var indentation = rule.raws && rule.raws.before ? rule.raws.before.split("\n").pop() : "";
  69. return explodeSelector(rule.selector, options).join("," + (options.lineBreak ? "\n" + indentation : " "));
  70. }
  71. module.exports = exports.default;