Skip to content

Commit

Permalink
Fixes stylus#879 Extends with parent reference
Browse files Browse the repository at this point in the history
  • Loading branch information
tonistiigi committed Nov 20, 2012
1 parent b2944cc commit b0808e8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/visitor/compiler.js
Expand Up @@ -454,6 +454,20 @@ Compiler.prototype.compileSelectors = function(arr){
, selectors = []
, buf = [];

function interpolateParent(selector, buf) {
var str = selector.val.trim();
if (buf.length) {
for (var i = 0, len = buf.length; i < len; ++i) {
if (~buf[i].indexOf('&')) {
str = buf[i].replace(/&/g, str).trim();
} else {
str += ' ' + buf[i].trim();
}
}
}
return str;
}

function compile(arr, i) {
if (i) {
arr[i].forEach(function(selector){
Expand All @@ -462,21 +476,12 @@ Compiler.prototype.compileSelectors = function(arr){
compile(arr, i - 1);
buf.shift();
} else {
selectors.push(selector.val);
selectors.push(interpolateParent(selector, buf));
}
});
} else {
arr[0].forEach(function(selector){
var str = selector.val.trim();
if (buf.length) {
for (var i = 0, len = buf.length; i < len; ++i) {
if (~buf[i].indexOf('&')) {
str = buf[i].replace(/&/g, str).trim();
} else {
str += ' ' + buf[i].trim();
}
}
}
var str = interpolateParent(selector, buf);
selectors.push(self.indent + str.trimRight());
});
}
Expand Down
8 changes: 8 additions & 0 deletions test/cases/extend.with.parent.reference.css
@@ -0,0 +1,8 @@
#button_a:hover,
#button_b:hover {
color: #fff;
}
body #button_c:hover,
body #button_d:hover {
color: #000;
}
12 changes: 12 additions & 0 deletions test/cases/extend.with.parent.reference.styl
@@ -0,0 +1,12 @@
#button_a
&:hover
color #fff
#button_b
@extend #button_a

body
#button_c
&:hover
color #000
#button_d
@extend body #button_c

0 comments on commit b0808e8

Please sign in to comment.