index.js 1023 B

12345678910111213141516171819202122232425
  1. module.exports = function resolveNestedSelector(selector, node) {
  2. var parent = node.parent;
  3. var parentIsNestAtRule = parent.type === 'atrule' && parent.name === 'nest';
  4. if (parent.type === 'root') return [selector];
  5. if (parent.type !== 'rule' && !parentIsNestAtRule) return resolveNestedSelector(selector, parent);
  6. var parentSelectors = (parentIsNestAtRule)
  7. ? parent.params.split(',').map(function(s) { return s.trim(); })
  8. : parent.selectors;
  9. var resolvedSelectors = parentSelectors.reduce(function(result, parentSelector) {
  10. if (selector.indexOf('&') !== -1) {
  11. var newlyResolvedSelectors = resolveNestedSelector(parentSelector, parent).map(function(resolvedParentSelector) {
  12. return selector.replace(/&/g, resolvedParentSelector);
  13. });
  14. return result.concat(newlyResolvedSelectors);
  15. }
  16. var combinedSelector = [ parentSelector, selector ].join(' ');
  17. return result.concat(resolveNestedSelector(combinedSelector, parent));
  18. }, []);
  19. return resolvedSelectors;
  20. }