Skip to content

Commit 5c91e86

Browse files
committed
Add pseudo selector variable resolution from base
1 parent f862a18 commit 5c91e86

6 files changed

+38
-7
lines changed

lib/generate-descendant-pieces-from-selector.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ var generateDescendantPiecesFromSelector = function(selector) {
1616
.map(function(piece) {
1717
// Trim whitespace which would be a normal descendant selector
1818
// and trim off the CSS4 descendant `>>` into a normal descendant selector
19-
return piece.trim().replace(/\s*?>>\s*?/, function(match) {
20-
return '';
21-
});
19+
return piece.trim().replace(/\s*?>>\s*?/g, '');
2220
});
2321
};
2422

lib/generate-direct-descendant-pieces-from-selector.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ var generateDirectDescendantPiecesFromSelector = function(selector) {
1414
.map(function(piece) {
1515
// Trim whitespace which would be a normal descendant selector
1616
// and trim off the CSS4 descendant `>>` into a normal descendant selector
17-
return piece.trim().replace(/\s*?>\s*?/, function(match) {
18-
return '';
19-
});
17+
return piece.trim().replace(/\s*?>\s*?/g, '');
2018
});
2119
};
2220

lib/is-under-scope.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ var escapeStringRegexp = require('escape-string-regexp');
33
var isPieceAlwaysAncestorSelector = require('./is-piece-always-ancestor-selector');
44
var generateDirectDescendantPiecesFromSelector = require('./generate-direct-descendant-pieces-from-selector');
55

6+
var RE_AT_RULE_SCOPE_PIECE = (/^@.*/);
7+
// This will match pseudo selectors that have a base part
8+
// ex. .foo:hover
9+
// It will NOT match `:root`
10+
var RE_PSEUDO_SELECTOR = (/([^\s]+)((?::|::).*?)(?:\s+|$)/);
611

712

813
function getScopeMatchResults(nodeScopeList, scopeNodeScopeList) {
@@ -12,7 +17,20 @@ function getScopeMatchResults(nodeScopeList, scopeNodeScopeList) {
1217
// Check each comma separated piece of the complex selector
1318
var doesMatchScope = scopeNodeScopeList.some(function(scopeNodeScopePieces) {
1419
return nodeScopeList.some(function(nodeScopePieces) {
20+
// Because we only care about the scopeNodeScope matching to the nodeScope
21+
// Remove the pseudo selectors from the nodeScope so it can match a broader version
22+
// ex. `.foo:hover` can resolve variables from `.foo`
23+
nodeScopePieces = nodeScopePieces
24+
.map(function(descendantPiece) {
25+
// If not an at-rule piece, remove the pseudo selector part `@media (max-width: 300px)`
26+
if(!RE_AT_RULE_SCOPE_PIECE.test(descendantPiece)) {
27+
return descendantPiece.replace(new RegExp(RE_PSEUDO_SELECTOR.source, 'g'), function(whole, baseSelector, pseudo) {
28+
return baseSelector;
29+
});
30+
}
1531

32+
return descendantPiece;
33+
});
1634

1735
currentPieceOffset = null;
1836
var wasEveryPieceFound = true;

test/fixtures/pseudo-selector.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.foo {
2+
--foo-color: #ff0000;
3+
color: var(--foo-color);
4+
}
5+
6+
.foo:before {
7+
color: var(--foo-color);
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.foo {
2+
color: #ff0000;
3+
}
4+
5+
.foo:before {
6+
color: #ff0000;
7+
}

test/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ describe('postcss-css-variables', function() {
9292
});
9393

9494

95-
95+
it('should work with pseudo selectors', function() {
96+
return testPlugin('./test/fixtures/pseudo-selector.css', './test/fixtures/pseudo-selector.expected.css');
97+
});
9698

9799

98100
it('should work with variables defined in comma separated selector', function() {

0 commit comments

Comments
 (0)