postcss.js 1.6 KB

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