Skip to content

Commit 3463d7d

Browse files
committed
Merge pull request leafo#47 from robocoder/multi-extend
@extend fixes
2 parents 9951ee5 + 45efd5b commit 3463d7d

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

scss.inc.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ protected function makeOutputBlock($type, $selectors = null) {
132132
return $out;
133133
}
134134

135-
protected function matchExtendsSingle($single, &$out_origin, &$out_rem) {
135+
protected function matchExtendsSingle($single, &$out_origin) {
136136
$counts = array();
137137
foreach ($single as $part) {
138138
if (!is_string($part)) return false; // hmm
@@ -145,20 +145,30 @@ protected function matchExtendsSingle($single, &$out_origin, &$out_rem) {
145145
}
146146
}
147147

148+
$out_origin = array();
149+
$found = false;
150+
148151
foreach ($counts as $idx => $count) {
149152
list($target, $origin) = $this->extends[$idx];
153+
150154
// check count
151155
if ($count != count($target)) continue;
156+
152157
// check if target is subset of single
153158
if (array_diff(array_intersect($single, $target), $target)) continue;
154159

155-
$out_origin = $origin;
156-
$out_rem = array_diff($single, $target);
160+
$rem = array_diff($single, $target);
157161

158-
return true;
162+
foreach ($origin as $j => $new) {
163+
$origin[$j][count($origin[$j]) - 1] = $this->combineSelectorSingle(end($new), $rem);
164+
}
165+
166+
$out_origin = array_merge($out_origin, $origin);
167+
168+
$found = true;
159169
}
160170

161-
return false;
171+
return $found;
162172
}
163173

164174
protected function combineSelectorSingle($base, $other) {
@@ -167,7 +177,7 @@ protected function combineSelectorSingle($base, $other) {
167177

168178
foreach (array($base, $other) as $single) {
169179
foreach ($single as $part) {
170-
if (preg_match('/^[^.#:]/', $part)) {
180+
if (preg_match('/^[^\[.#:]/', $part)) {
171181
$tag = $part;
172182
} else {
173183
$out[] = $part;
@@ -186,15 +196,13 @@ protected function matchExtends($selector, &$out, $from = 0, $initial=true) {
186196
foreach ($selector as $i => $part) {
187197
if ($i < $from) continue;
188198

189-
if ($this->matchExtendsSingle($part, $origin, $rem)) {
199+
if ($this->matchExtendsSingle($part, $origin)) {
190200
$before = array_slice($selector, 0, $i);
191201
$after = array_slice($selector, $i + 1);
192202

193203
foreach ($origin as $new) {
194-
$new[count($new) - 1] =
195-
$this->combineSelectorSingle(end($new), $rem);
196-
197204
$k = 0;
205+
198206
// remove shared parts
199207
if ($initial) {
200208
foreach ($before as $k => $val) {
@@ -332,7 +340,7 @@ protected function flattenSelectorSingle($single) {
332340
foreach ($single as $part) {
333341
if (empty($joined) ||
334342
!is_string($part) ||
335-
preg_match('/[.:#%]/', $part))
343+
preg_match('/[\[.:#%]/', $part))
336344
{
337345
$joined[] = $part;
338346
continue;
@@ -1572,7 +1580,7 @@ protected function coerceString($value) {
15721580

15731581
protected function assertList($value) {
15741582
if ($value[0] != "list")
1575-
throw new exception("expecting list");
1583+
throw new Exception("expecting list");
15761584
return $value;
15771585
}
15781586

@@ -3988,7 +3996,7 @@ public function serve() {
39883996
if ($this->needsCompile($input, $output)) {
39893997
try {
39903998
echo $this->compile($input, $output);
3991-
} catch (exception $e) {
3999+
} catch (Exception $e) {
39924000
header('HTTP/1.1 500 Internal Server Error');
39934001
echo "Parse error: " . $e->getMessage() . "\n";
39944002
}

tests/inputs/extends.scss

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,43 @@ wassup {
131131
color: blue;
132132
}
133133
}
134+
135+
// multi-extend
136+
137+
#something {
138+
color: red;
139+
}
140+
141+
.x {
142+
@extend #something;
143+
}
144+
145+
.y {
146+
@extend #something;
147+
}
148+
149+
// multi-extend with nesting
150+
151+
.btn:hover,
152+
.btn:active,
153+
.btn.active,
154+
.btn.disabled,
155+
.btn[disabled] {
156+
color: red;
157+
}
158+
.edit .actions {
159+
button {
160+
float: right;
161+
@extend .btn;
162+
}
163+
}
164+
.edit {
165+
.new {
166+
.actions {
167+
padding: 0;
168+
}
169+
.actions button {
170+
@extend .btn;
171+
}
172+
}
173+
}

tests/outputs/extends.css

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ hello {
1616
.blue, .me {
1717
color: purple; }
1818

19-
a:hover, .hoverlink {
19+
a:hover, .hoverlink, #demo .overview .fakelink:hover {
2020
text-decoration: underline; }
2121

2222
div.hello.world.hmm, pre div.okay.span.world.hmm, pre #butt .umm div.sure.span.world.hmm, #butt .umm pre div.sure.span.world.hmm, code div.okay.span.world.hmm, code #butt .umm div.sure.span.world.hmm, #butt .umm code div.sure.span.world.hmm {
@@ -70,3 +70,15 @@ wassup {
7070

7171
.foo .wassup {
7272
color: blue; }
73+
74+
#something, .x, .y {
75+
color: red; }
76+
77+
.btn:hover, .edit .actions button:hover, .edit .new .actions button:hover, .btn:active, .edit .actions button:active, .edit .new .actions button:active, .btn.active, .edit .actions button.active, .edit .new .actions button.active, .btn.disabled, .edit .actions button.disabled, .edit .new .actions button.disabled, .btn[disabled], .edit .actions button[disabled], .edit .new .actions button[disabled] {
78+
color: red; }
79+
80+
.edit .actions button {
81+
float: right; }
82+
83+
.edit .new .actions {
84+
padding: 0; }

0 commit comments

Comments
 (0)