Skip to content

Commit a8e987c

Browse files
committed
fixes leafo#281
1 parent 6163f86 commit a8e987c

File tree

5 files changed

+137
-56
lines changed

5 files changed

+137
-56
lines changed

src/Compiler.php

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,34 @@ protected function mediaParent($scope)
530530
return $scope;
531531
}
532532

533+
/**
534+
* Compile keyframe block
535+
*
536+
* @param \stdClass $block
537+
* @param array $selectors
538+
*/
539+
protected function compileKeyframeBlock($block, $selectors)
540+
{
541+
$env = $this->pushEnv($block);
542+
543+
$envs = $this->compactEnv($env);
544+
545+
$this->env = $this->extractEnv(array_filter($envs, function ($e) {
546+
return ! isset($e->block->selectors);
547+
}));
548+
549+
$this->scope = $this->makeOutputBlock($block->type, $selectors);
550+
$this->scope->depth = 1;
551+
$this->scope->parent->children[] = $this->scope;
552+
553+
$this->compileChildren($block->children, $this->scope);
554+
555+
$this->scope = $this->scope->parent;
556+
$this->env = $this->extractEnv($envs);
557+
558+
$this->popEnv();
559+
}
560+
533561
/**
534562
* Compile nested block
535563
*
@@ -1049,7 +1077,11 @@ protected function compileChild($child, $out)
10491077
$s .= ' ' . $this->compileValue($directive->value);
10501078
}
10511079

1052-
$this->compileNestedBlock($directive, array($s));
1080+
if ($directive->name === 'keyframes' || substr($directive->name, -10) === '-keyframes') {
1081+
$this->compileKeyframeBlock($directive, array($s));
1082+
} else {
1083+
$this->compileNestedBlock($directive, array($s));
1084+
}
10531085
break;
10541086

10551087
case 'media':
@@ -2245,7 +2277,7 @@ protected function extractInterpolation($list)
22452277
foreach ($items as $i => $item) {
22462278
if ($item[0] === 'interpolate') {
22472279
$before = array('list', $list[1], array_slice($items, 0, $i));
2248-
$after = array('list', $list[1], array_slice($items, $i + 1));
2280+
$after = array('list', $list[1], array_slice($items, $i + 1));
22492281

22502282
return array('interpolated', $item, $before, $after);
22512283
}
@@ -2263,16 +2295,15 @@ protected function extractInterpolation($list)
22632295
*/
22642296
protected function multiplySelectors($env)
22652297
{
2266-
for ($envs = array(); $env; $env = $env->parent) {
2267-
if (count($env->selectors)) {
2268-
$envs[] = $env;
2269-
}
2270-
}
2271-
2272-
$selectors = array();
2298+
$envs = $this->compactEnv($env);
2299+
$selectors = array();
22732300
$parentSelectors = array(array());
22742301

22752302
while ($env = array_pop($envs)) {
2303+
if (empty($env->selectors)) {
2304+
continue;
2305+
}
2306+
22762307
$selectors = array();
22772308

22782309
foreach ($env->selectors as $selector) {
@@ -2366,6 +2397,39 @@ protected function multiplyMedia($env, $childQueries = null)
23662397
return $this->multiplyMedia($env->parent, $childQueries);
23672398
}
23682399

2400+
/**
2401+
* Convert env linked list to stack
2402+
*
2403+
* @param \stdClass $env
2404+
*
2405+
* @return array
2406+
*/
2407+
private function compactEnv($env)
2408+
{
2409+
for ($envs = array(); $env; $env = $env->parent) {
2410+
$envs[] = $env;
2411+
}
2412+
2413+
return $envs;
2414+
}
2415+
2416+
/**
2417+
* Convert env stack to singly linked list
2418+
*
2419+
* @param array $envs
2420+
*
2421+
* @return \stdClass
2422+
*/
2423+
private function extractEnv($envs)
2424+
{
2425+
for ($env = null; $e = array_pop($envs);) {
2426+
$e->parent = $env;
2427+
$env = $e;
2428+
}
2429+
2430+
return $env;
2431+
}
2432+
23692433
/**
23702434
* Push environment
23712435
*

tests/FailingTest.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -124,53 +124,6 @@ public function provideFailing()
124124
125125
small {
126126
font-weight: italic; }
127-
END_OF_EXPECTED
128-
),
129-
array(
130-
'#281 - nested animation selector', <<<'END_OF_SCSS'
131-
.custom-selector {
132-
133-
& {
134-
color:blue;
135-
}
136-
137-
@-webkit-keyframes zoomer {
138-
from {
139-
transform:scale(0.5);
140-
}
141-
142-
to {
143-
transform:scale(1);
144-
}
145-
}
146-
147-
@keyframes zoomer {
148-
from {
149-
transform:scale(0.5);
150-
}
151-
152-
to {
153-
transform:scale(1);
154-
}
155-
}
156-
157-
}
158-
END_OF_SCSS
159-
, <<<END_OF_EXPECTED
160-
.custom-selector {
161-
color: blue; }
162-
163-
@-webkit-keyframes zoomer {
164-
from {
165-
transform: scale(0.5); }
166-
to {
167-
transform: scale(1); } }
168-
169-
@keyframes zoomer {
170-
from {
171-
transform: scale(0.5); }
172-
to {
173-
transform: scale(1); } }
174127
END_OF_EXPECTED
175128
),
176129
/*************************************************************

tests/inputs/directives.scss

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,31 @@ div {
125125
.icon-ajax {
126126
@extend .bold;
127127
}
128+
129+
.custom-selector {
130+
131+
& {
132+
color:blue;
133+
}
134+
135+
@-webkit-keyframes zoomer {
136+
from {
137+
transform:scale(0.5);
138+
}
139+
140+
to {
141+
transform:scale(1);
142+
}
143+
}
144+
145+
@keyframes zoomer {
146+
from {
147+
transform:scale(0.5);
148+
}
149+
150+
to {
151+
transform:scale(1);
152+
}
153+
}
154+
155+
}

tests/outputs/directives.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,18 @@ div {
8585

8686
.bold, .icon-ajax {
8787
font-weight: 700; }
88+
89+
.custom-selector {
90+
color: blue; }
91+
92+
@-webkit-keyframes zoomer {
93+
from {
94+
transform: scale(0.5); }
95+
to {
96+
transform: scale(1); } }
97+
98+
@keyframes zoomer {
99+
from {
100+
transform: scale(0.5); }
101+
to {
102+
transform: scale(1); } }

tests/outputs_numbered/directives.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,24 @@ body {
9595
.bold, .icon-ajax {
9696
font-weight: 700; }
9797
/* line 125, inputs/directives.scss */
98+
/* line 129, inputs/directives.scss */
99+
100+
/* line 131, inputs/directives.scss */
101+
.custom-selector {
102+
color: blue; }
103+
104+
@-webkit-keyframes zoomer {
105+
/* line 136, inputs/directives.scss */
106+
from {
107+
transform: scale(0.5); }
108+
/* line 140, inputs/directives.scss */
109+
to {
110+
transform: scale(1); } }
111+
112+
@keyframes zoomer {
113+
/* line 146, inputs/directives.scss */
114+
from {
115+
transform: scale(0.5); }
116+
/* line 150, inputs/directives.scss */
117+
to {
118+
transform: scale(1); } }

0 commit comments

Comments
 (0)