Skip to content

Commit 85ddc9f

Browse files
committed
Add parent selector to user variable
1 parent 0a5245b commit 85ddc9f

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/less/parser.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,10 +1001,29 @@ less.Parser = function Parser(env) {
10011001
selector: function () {
10021002
var sel, e, elements = [], c, match;
10031003

1004-
if ($('(')) {
1004+
if (peek(/^&?\(/)) {
1005+
// variable selectors:
1006+
// allow & before a selector to allow variable selectors
1007+
// to be at the same level, e.g.
1008+
// .a {
1009+
// &(~".b") {
1010+
//
1011+
// Ideally this would be part of the element function.. this would allow
1012+
// (@a).b(@c)
1013+
// however this syntax conflicts with the supported syntax
1014+
// :nth-child(@a)
1015+
// vs
1016+
// .a:hover(@a)
1017+
1018+
e = $('&');
1019+
$('(');
1020+
if (e) {
1021+
elements.push(new(tree.Element)('', e, i));
1022+
}
10051023
sel = $(this.entity);
10061024
expect(')');
1007-
return new(tree.Selector)([new(tree.Element)('', sel, i)]);
1025+
elements.push(new(tree.Element)('', sel, i));
1026+
return new(tree.Selector)(elements);
10081027
}
10091028

10101029
while (e = $(this.element)) {

test/css/selectors.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,12 @@ p a span {
113113
.other::bnord {
114114
color: #ff0000;
115115
}
116+
.a .b {
117+
color: red;
118+
}
119+
.a.c {
120+
color: black;
121+
}
122+
.a :nth-child(3) {
123+
color: purple;
124+
}

test/less/selectors.less

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,16 @@ a {
107107
::bnord {color: red }
108108
&::bnord {color: red }
109109
}
110+
111+
.a {
112+
(~".b") {
113+
color: red;
114+
}
115+
&(~".c") {
116+
color: black;
117+
}
118+
@d: 3;
119+
:nth-child(@d) {
120+
color: purple;
121+
}
122+
}

0 commit comments

Comments
 (0)