index.cjs.js 2.3 KB

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