1 | import parser from 'postcss-selector-parser';
|
---|
2 | import postcss from 'postcss';
|
---|
3 |
|
---|
4 | const selectorRegExp = /:has/;
|
---|
5 | var postcss$1 = postcss.plugin('css-has-pseudo', opts => {
|
---|
6 | const preserve = Boolean('preserve' in Object(opts) ? opts.preserve : true);
|
---|
7 | return root => {
|
---|
8 | root.walkRules(selectorRegExp, rule => {
|
---|
9 | const modifiedSelector = parser(selectors => {
|
---|
10 | selectors.walkPseudos(selector => {
|
---|
11 | if (selector.value === ':has' && selector.nodes) {
|
---|
12 | const isNotHas = checkIfParentIsNot(selector);
|
---|
13 | selector.value = isNotHas ? ':not-has' : ':has';
|
---|
14 | const attribute = parser.attribute({
|
---|
15 | attribute: encodeURIComponent(String(selector)).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',').replace(/[():%\[\],]/g, '\\$&')
|
---|
16 | });
|
---|
17 |
|
---|
18 | if (isNotHas) {
|
---|
19 | selector.parent.parent.replaceWith(attribute);
|
---|
20 | } else {
|
---|
21 | selector.replaceWith(attribute);
|
---|
22 | }
|
---|
23 | }
|
---|
24 | });
|
---|
25 | }).processSync(rule.selector);
|
---|
26 | const clone = rule.clone({
|
---|
27 | selector: modifiedSelector
|
---|
28 | });
|
---|
29 |
|
---|
30 | if (preserve) {
|
---|
31 | rule.before(clone);
|
---|
32 | } else {
|
---|
33 | rule.replaceWith(clone);
|
---|
34 | }
|
---|
35 | });
|
---|
36 | };
|
---|
37 | });
|
---|
38 |
|
---|
39 | function checkIfParentIsNot(selector) {
|
---|
40 | return Object(Object(selector.parent).parent).type === 'pseudo' && selector.parent.parent.value === ':not';
|
---|
41 | }
|
---|
42 |
|
---|
43 | export default postcss$1;
|
---|
44 | //# sourceMappingURL=postcss.mjs.map
|
---|