prefixSelector.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, /**
  6. * @template {string | import('postcss-selector-parser').Root} T
  7. *
  8. * Prefix all classes in the selector with the given prefix
  9. *
  10. * It can take either a string or a selector AST and will return the same type
  11. *
  12. * @param {string} prefix
  13. * @param {T} selector
  14. * @param {boolean} prependNegative
  15. * @returns {T}
  16. */ "default", {
  17. enumerable: true,
  18. get: function() {
  19. return _default;
  20. }
  21. });
  22. const _postcssselectorparser = /*#__PURE__*/ _interop_require_default(require("postcss-selector-parser"));
  23. function _interop_require_default(obj) {
  24. return obj && obj.__esModule ? obj : {
  25. default: obj
  26. };
  27. }
  28. function _default(prefix, selector, prependNegative = false) {
  29. if (prefix === "") {
  30. return selector;
  31. }
  32. /** @type {import('postcss-selector-parser').Root} */ let ast = typeof selector === "string" ? (0, _postcssselectorparser.default)().astSync(selector) : selector;
  33. ast.walkClasses((classSelector)=>{
  34. let baseClass = classSelector.value;
  35. let shouldPlaceNegativeBeforePrefix = prependNegative && baseClass.startsWith("-");
  36. classSelector.value = shouldPlaceNegativeBeforePrefix ? `-${prefix}${baseClass.slice(1)}` : `${prefix}${baseClass}`;
  37. });
  38. return typeof selector === "string" ? ast.toString() : ast;
  39. }