postcss.mjs 1.4 KB

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