1 | import postcss from 'postcss';
|
---|
2 | import parser from 'postcss-selector-parser';
|
---|
3 |
|
---|
4 | const anyAnyLinkMatch = /:any-link/;
|
---|
5 | var index = postcss.plugin('postcss-pseudo-class-any-link', opts => {
|
---|
6 | const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : true;
|
---|
7 | return root => {
|
---|
8 | // walk each matching rule
|
---|
9 | root.walkRules(anyAnyLinkMatch, rule => {
|
---|
10 | const rawSelector = rule.raws.selector && rule.raws.selector.raw || rule.selector; // workaround for https://github.com/postcss/postcss-selector-parser/issues/28#issuecomment-171910556
|
---|
11 |
|
---|
12 | if (rawSelector[rawSelector.length - 1] !== ':') {
|
---|
13 | // update the selector
|
---|
14 | const updatedSelector = parser(selectors => {
|
---|
15 | // cache variables
|
---|
16 | let node;
|
---|
17 | let nodeIndex;
|
---|
18 | let selector;
|
---|
19 | let selectorLink;
|
---|
20 | let selectorVisited; // cache the selector index
|
---|
21 |
|
---|
22 | let selectorIndex = -1; // for each selector
|
---|
23 |
|
---|
24 | while (selector = selectors.nodes[++selectorIndex]) {
|
---|
25 | // reset the node index
|
---|
26 | nodeIndex = -1; // for each node
|
---|
27 |
|
---|
28 | while (node = selector.nodes[++nodeIndex]) {
|
---|
29 | // if the node value matches the any-link value
|
---|
30 | if (node.value === ':any-link') {
|
---|
31 | // clone the selector
|
---|
32 | selectorLink = selector.clone();
|
---|
33 | selectorVisited = selector.clone(); // update the matching clone values
|
---|
34 |
|
---|
35 | selectorLink.nodes[nodeIndex].value = ':link';
|
---|
36 | selectorVisited.nodes[nodeIndex].value = ':visited'; // replace the selector with the clones and roll back the selector index
|
---|
37 |
|
---|
38 | selectors.nodes.splice(selectorIndex--, 1, selectorLink, selectorVisited); // stop updating the selector
|
---|
39 |
|
---|
40 | break;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }).processSync(rawSelector);
|
---|
45 |
|
---|
46 | if (updatedSelector !== rawSelector) {
|
---|
47 | if (preserve) {
|
---|
48 | rule.cloneBefore({
|
---|
49 | selector: updatedSelector
|
---|
50 | });
|
---|
51 | } else {
|
---|
52 | rule.selector = updatedSelector;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | });
|
---|
57 | };
|
---|
58 | });
|
---|
59 |
|
---|
60 | export default index;
|
---|
61 | //# sourceMappingURL=index.es.mjs.map
|
---|