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