Skip to content

Commit c30af53

Browse files
committed
Fixes leafo#149 - parent selector in string
1 parent ae54bd0 commit c30af53

File tree

5 files changed

+59
-27
lines changed

5 files changed

+59
-27
lines changed

src/Compiler.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,21 @@ protected function evalSelectors($selectors)
545545
$newSelectors = array();
546546

547547
foreach ($selectors as $selector) {
548-
if (is_array($selector[0][0]) || strpos($selector[0][0], ',') === false) {
548+
if (is_array($selector[0][0])) {
549+
$newSelectors[] = $selector;
550+
} elseif (strpos($selector[0][0], ',') === false) {
551+
if ($selector[0][0][0] === '&') {
552+
$selector = array(array(array('self'), substr($selector[0][0], 1)));
553+
}
554+
549555
$newSelectors[] = $selector;
550556
} else {
551557
foreach (array_map(function ($s) { return trim($s, " \t\n\r\0\x0b'\""); }, explode(',', $selector[0][0])) as $newSelectorPart) {
552-
$newSelectors[] = array(array($newSelectorPart));
558+
if ($newSelectorPart[0] === '&') {
559+
$newSelectors[] = array(array(array('self'), substr($newSelectorPart, 1)));
560+
} else {
561+
$newSelectors[] = array(array($newSelectorPart));
562+
}
553563
}
554564
}
555565
}

tests/FailingTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,6 @@ public function provideFailing()
181181
, <<<END_OF_EXPECTED
182182
body .to-extend, body .test {
183183
color: red; }
184-
END_OF_EXPECTED
185-
),
186-
array(
187-
'#149 - parent selector (&) inside string does not work', <<<'END_OF_SCSS'
188-
.parent {
189-
$sub: unquote(".child");
190-
$self: unquote("&.self2");
191-
&.self { // works perfectly
192-
content: "should match .parent.self";
193-
}
194-
#{$sub} { // works as it should
195-
content: "should match .parent .child";
196-
}
197-
#{$self} { // does not work (see below)
198-
content: "should match .parent.self2";
199-
}
200-
}
201-
END_OF_SCSS
202-
, <<<END_OF_EXPECTED
203-
.parent.self {
204-
content: "should match .parent.self"; }
205-
.parent .child {
206-
content: "should match .parent .child"; }
207-
.parent.self2 {
208-
content: "should match .parent.self2"; }
209184
END_OF_EXPECTED
210185
),
211186
/*************************************************************

tests/inputs/selectors.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,24 @@ $all: $span, $p, $div;
246246
color: red;
247247
}
248248
}
249+
250+
.parent {
251+
$sub: unquote(".child");
252+
$self: unquote("&.self2");
253+
&.self { // works perfectly
254+
content: "should match .parent.self";
255+
}
256+
#{$sub} { // works as it should
257+
content: "should match .parent .child";
258+
}
259+
#{$self} { // does not work (see below)
260+
content: "should match .parent.self2";
261+
}
262+
}
263+
264+
.parent {
265+
$selfMultiple: unquote("&.self1, &.self2");
266+
#{$selfMultiple} {
267+
content: "should match .parent.self1, .parent.self2";
268+
}
269+
}

tests/outputs/selectors.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,13 @@ div.foo div {
351351

352352
span a, p a, div a {
353353
color: red; }
354+
355+
.parent.self {
356+
content: "should match .parent.self"; }
357+
.parent .child {
358+
content: "should match .parent .child"; }
359+
.parent.self2 {
360+
content: "should match .parent.self2"; }
361+
362+
.parent.self1, .parent.self2 {
363+
content: "should match .parent.self1, .parent.self2"; }

tests/outputs_numbered/selectors.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,19 @@ div.foo div {
377377

378378
span a, p a, div a {
379379
color: red; }
380+
/* line 250, inputs/selectors.scss */
381+
/* line 253, inputs/selectors.scss */
382+
383+
.parent.self {
384+
content: "should match .parent.self"; }
385+
/* line 256, inputs/selectors.scss */
386+
.parent .child {
387+
content: "should match .parent .child"; }
388+
/* line 259, inputs/selectors.scss */
389+
.parent.self2 {
390+
content: "should match .parent.self2"; }
391+
/* line 264, inputs/selectors.scss */
392+
/* line 266, inputs/selectors.scss */
393+
394+
.parent.self1, .parent.self2 {
395+
content: "should match .parent.self1, .parent.self2"; }

0 commit comments

Comments
 (0)