applyImportantSelector.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "applyImportantSelector", {
  6. enumerable: true,
  7. get: function() {
  8. return applyImportantSelector;
  9. }
  10. });
  11. const _postcssselectorparser = /*#__PURE__*/ _interop_require_default(require("postcss-selector-parser"));
  12. const _pseudoElements = require("./pseudoElements");
  13. function _interop_require_default(obj) {
  14. return obj && obj.__esModule ? obj : {
  15. default: obj
  16. };
  17. }
  18. function applyImportantSelector(selector, important) {
  19. let sel = (0, _postcssselectorparser.default)().astSync(selector);
  20. sel.each((sel)=>{
  21. // For nesting, we only need to wrap a selector with :is() if it has a top-level combinator,
  22. // e.g. `.dark .text-white`, to be independent of DOM order. Any other selector, including
  23. // combinators inside of pseudos like `:where()`, are ok to nest.
  24. let shouldWrap = sel.nodes.some((node)=>node.type === "combinator");
  25. if (shouldWrap) {
  26. sel.nodes = [
  27. _postcssselectorparser.default.pseudo({
  28. value: ":is",
  29. nodes: [
  30. sel.clone()
  31. ]
  32. })
  33. ];
  34. }
  35. (0, _pseudoElements.movePseudos)(sel);
  36. });
  37. return `${important} ${sel.toString()}`;
  38. }