From 85ddc9f4e72e66a846640722f4e25b33108baeb7 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Sun, 19 Aug 2012 12:02:58 +0100 Subject: [PATCH] Add parent selector to user variable --- lib/less/parser.js | 23 +++++++++++++++++++++-- test/css/selectors.css | 9 +++++++++ test/less/selectors.less | 13 +++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index 92a79bdd5..2bc229c6d 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -1001,10 +1001,29 @@ less.Parser = function Parser(env) { selector: function () { var sel, e, elements = [], c, match; - if ($('(')) { + if (peek(/^&?\(/)) { + // variable selectors: + // allow & before a selector to allow variable selectors + // to be at the same level, e.g. + // .a { + // &(~".b") { + // + // Ideally this would be part of the element function.. this would allow + // (@a).b(@c) + // however this syntax conflicts with the supported syntax + // :nth-child(@a) + // vs + // .a:hover(@a) + + e = $('&'); + $('('); + if (e) { + elements.push(new(tree.Element)('', e, i)); + } sel = $(this.entity); expect(')'); - return new(tree.Selector)([new(tree.Element)('', sel, i)]); + elements.push(new(tree.Element)('', sel, i)); + return new(tree.Selector)(elements); } while (e = $(this.element)) { diff --git a/test/css/selectors.css b/test/css/selectors.css index 720b88c38..a9e165fd9 100644 --- a/test/css/selectors.css +++ b/test/css/selectors.css @@ -113,3 +113,12 @@ p a span { .other::bnord { color: #ff0000; } +.a .b { + color: red; +} +.a.c { + color: black; +} +.a :nth-child(3) { + color: purple; +} diff --git a/test/less/selectors.less b/test/less/selectors.less index 23be9fbd7..7d18b3912 100644 --- a/test/less/selectors.less +++ b/test/less/selectors.less @@ -107,3 +107,16 @@ a { ::bnord {color: red } &::bnord {color: red } } + +.a { + (~".b") { + color: red; + } + &(~".c") { + color: black; + } + @d: 3; + :nth-child(@d) { + color: purple; + } +}