|
1 | 1 | import postcss from "postcss" |
2 | | -import list from "postcss/lib/list" |
3 | | - |
4 | | -import balancedMatch from "balanced-match" |
5 | | - |
6 | | -const pseudoClass = ":matches" |
7 | | - |
8 | | -function explodeSelector(selector, options) { |
9 | | - if (selector && selector.indexOf(pseudoClass) > -1) { |
10 | | - let newSelectors = [] |
11 | | - const preWhitespaceMatches = selector.match(/^\s+/) |
12 | | - const preWhitespace = preWhitespaceMatches |
13 | | - ? preWhitespaceMatches[0] |
14 | | - : "" |
15 | | - const selectorPart = list.comma(selector) |
16 | | - selectorPart.forEach(part => { |
17 | | - const position = part.indexOf(pseudoClass) |
18 | | - const pre = part.slice(0, position) |
19 | | - const body = part.slice(position) |
20 | | - const matches = balancedMatch("(", ")", body) |
21 | | - |
22 | | - const bodySelectors = matches && matches.body ? |
23 | | - list |
24 | | - .comma(matches.body) |
25 | | - .reduce((acc, s) => [ |
26 | | - ...acc, |
27 | | - ...explodeSelector(s, options), |
28 | | - ], []) |
29 | | - : [body] |
30 | | - |
31 | | - const postSelectors = matches && matches.post |
32 | | - ? explodeSelector(matches.post, options) |
33 | | - : [] |
34 | | - |
35 | | - let newParts |
36 | | - if (postSelectors.length === 0) { |
37 | | - newParts = bodySelectors.map((s) => preWhitespace + pre + s) |
38 | | - } |
39 | | - else { |
40 | | - newParts = [] |
41 | | - postSelectors.forEach(postS => { |
42 | | - bodySelectors.forEach(s => { |
43 | | - newParts.push(pre + s + postS) |
44 | | - }) |
45 | | - }) |
46 | | - } |
47 | | - newSelectors = [ |
48 | | - ...newSelectors, |
49 | | - ...newParts, |
50 | | - ] |
51 | | - }) |
52 | | - |
53 | | - return newSelectors |
54 | | - } |
55 | | - return [selector] |
56 | | -} |
57 | | - |
58 | | -function replaceRuleSelector(rule, options) { |
59 | | - const indentation = rule.before |
60 | | - ? rule.before.split("\n").pop() |
61 | | - : "" |
62 | | - return ( |
63 | | - explodeSelector(rule.selector, options) |
64 | | - .join("," + (options.lineBreak ? "\n" + indentation : " ")) |
65 | | - ) |
66 | | - |
67 | | -} |
| 2 | +import replaceRuleSelector from "./replaceRuleSelector" |
68 | 3 |
|
69 | 4 | function explodeSelectors(options = {}) { |
70 | 5 | return (css) => { |
71 | 6 | css.eachRule(rule => { |
72 | | - if (rule.selector && rule.selector.indexOf(pseudoClass) > -1) { |
| 7 | + if (rule.selector && rule.selector.indexOf(":matches") > -1) { |
73 | 8 | rule.selector = replaceRuleSelector(rule, options) |
74 | 9 | } |
75 | 10 | }) |
76 | 11 | } |
77 | 12 |
|
78 | 13 | } |
79 | 14 |
|
80 | | -const plugin = postcss.plugin( |
| 15 | +export default postcss.plugin( |
81 | 16 | "postcss-selector-matches", |
82 | 17 | explodeSelectors |
83 | 18 | ) |
84 | | - |
85 | | -// expose for postcss-custom-selectors |
86 | | -export {replaceRuleSelector} |
87 | | -// old school fallback |
88 | | -plugin.replaceRuleSelector = replaceRuleSelector |
89 | | - |
90 | | -export default plugin |
0 commit comments