getStrippedSelectorSource.cjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const selectorParser = require('postcss-selector-parser');
  5. const { isComment } = selectorParser;
  6. /**
  7. * Get the source of a selector, excluding surrounding whitespace and comments.
  8. *
  9. * @param {import('postcss-selector-parser').Selector} node
  10. *
  11. * @returns {{selector: string, index: number, endIndex: number}}
  12. */
  13. function getStrippedSelectorSource(node) {
  14. const firstIndex = node.nodes.findIndex((x) => !isComment(x));
  15. const lastIndex = node.nodes.findLastIndex((x) => !isComment(x));
  16. const first = node.nodes[firstIndex];
  17. let last = node.nodes[lastIndex];
  18. if (!first || !last) {
  19. const selector = node.toString().trim();
  20. const index = node.sourceIndex ?? 0;
  21. const endIndex = index + selector.length;
  22. return {
  23. selector,
  24. index,
  25. endIndex,
  26. };
  27. }
  28. last = last.rawSpaceAfter.length ? last.clone() : last;
  29. last.rawSpaceAfter = '';
  30. let selector = node.nodes.slice(firstIndex, lastIndex).map(String).join('');
  31. selector += last.toString();
  32. selector = selector.trim();
  33. const index = first.sourceIndex ?? 0;
  34. const endIndex = index + selector.length;
  35. return {
  36. selector,
  37. index,
  38. endIndex,
  39. };
  40. }
  41. module.exports = getStrippedSelectorSource;