From 72924766f8d1ead36294ecfaa2eb627b497fa834 Mon Sep 17 00:00:00 2001 From: David Clark Date: Tue, 13 Jun 2017 08:29:17 -0700 Subject: [PATCH] WIP --- index.js | 6 ++++-- test/postcss-nesting-test.js | 22 ++++++++++++++++++++++ test/util.js | 20 +++++++++++++++++--- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8659841..fd21912 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const postcss = require('postcss'); + module.exports = function resolveNestedSelector(selector, node) { var parent = node.parent; var parentIsNestAtRule = parent.type === 'atrule' && parent.name === 'nest'; @@ -6,11 +8,11 @@ module.exports = function resolveNestedSelector(selector, node) { if (parent.type !== 'rule' && !parentIsNestAtRule) return resolveNestedSelector(selector, parent); var parentSelectors = (parentIsNestAtRule) - ? parent.params.split(',').map(function(s) { return s.trim(); }) + ? postcss.list.comma(parent.params) : parent.selectors; var resolvedSelectors = parentSelectors.reduce(function(result, parentSelector) { - if (selector.indexOf('&') !== -1) { + if (/&/.test(selector)) { var newlyResolvedSelectors = resolveNestedSelector(parentSelector, parent).map(function(resolvedParentSelector) { return selector.replace(/&/g, resolvedParentSelector); }); diff --git a/test/postcss-nesting-test.js b/test/postcss-nesting-test.js index ae93dc8..364d71b 100644 --- a/test/postcss-nesting-test.js +++ b/test/postcss-nesting-test.js @@ -140,3 +140,25 @@ test('postcss-nesting Test Case 6', async t => { ['a', 'a', 'a b', 'a b', 'a c'], ); }); + +test('postcss-nesting TestCase 7', async t => { + const code = `.de { + @nest + .cd:not(:hover, :focus) & { + & .fg { + color: red; + } + } + }`; + const postcssNestingResult = await util.postcssNestingResolve(code); + postcssNestingResult.sort(); + const actual = await util.allExpected(code); + actual.sort(); + + t.deepEqual(postcssNestingResult, actual); + t.deepEqual(actual, [ + '.cd:not(:hover, :focus) .de', + '.cd:not(:hover, :focus) .de .fg', + '.de' + ]); +}); diff --git a/test/util.js b/test/util.js index 1426116..25354cb 100644 --- a/test/util.js +++ b/test/util.js @@ -30,8 +30,12 @@ function allExpected(code) { result.root.walk(function(node) { if (node.type !== 'rule' && node.type !== 'atrule') return; - const nodeContainsDeclChild = node.some(function(descendant) { - return descendant.type === 'decl'; + let nodeContainsDeclChild = false; + node.walk(function(descendant) { + if (descendant.type === 'decl') { + nodeContainsDeclChild = true; + return false; + } }); var nodeContainsBlockDescendant = false; @@ -45,7 +49,17 @@ function allExpected(code) { if (node.type !== 'atrule' && !nodeContainsDeclChild && nodeContainsBlockDescendant) return; if (node.type === 'atrule' && !nodeContainsDeclChild) return; - var selectors = (node.type === 'atrule') ? ['&'] : node.selectors; + var selectors; + if (node.type === 'atrule') { + if (node.name === 'nest') { + selectors = postcss.list.comma(node.params); + } else { + selectors = ['&']; + } + } else { + selectors = node.selectors; + } + selectors.forEach(function(selector) { resolvedSelectors = resolvedSelectors.concat(resolveNestedSelector(selector, node)); });