index.es.mjs 2.1 KB

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