Skip to content

Commit

Permalink
Merge pull request #1624 from seven-phases-max/mixins-with-interpolat…
Browse files Browse the repository at this point in the history
…ed-selectors

Experimental support for mixins with interpolated selectors
  • Loading branch information
lukeapage committed Dec 20, 2013
2 parents c064091 + 2ad97c9 commit 5196277
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/less/tree/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,32 @@ tree.Selector.prototype = {
var elements = this.elements,
len = elements.length,
oelements, olen, max, i;

oelements = other.elements.map( function(v) {
return v.combinator.value + (v.value.value || v.value);
}).join("").match(/[,&#\.\w-]([\w-]|(\\.))*/g);
// ^ regexp could be more simple but see test/less/css-escapes.less:17, doh!

if (!oelements) {
return 0;
}

oelements = other.elements.slice(
(other.elements.length && other.elements[0].value === "&") ? 1 : 0);
olen = oelements.length;
max = Math.min(len, olen);
if (oelements[0] === "&") {
oelements.shift();
}

olen = oelements.length;
if (olen === 0 || len < olen) {
return 0;
} else {
max = Math.min(len, olen);
for (i = 0; i < max; i++) {
if (elements[i].value !== oelements[i].value) {
if (elements[i].value !== oelements[i]) {
return 0;
}
}
}
return max; // return number of matched selectors
return max; // return number of matched elements
},
eval: function (env) {
var evaldCondition = this.condition && this.condition.eval(env),
Expand Down
39 changes: 39 additions & 0 deletions test/css/mixins-interpolated.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.foo {
a: 1;
}
.foo {
a: 2;
}
#foo {
a: 3;
}
#foo {
a: 4;
}
mi-test-a {
a: 1;
a: 2;
a: 3;
a: 4;
}
.b .bb.foo-xxx .yyy-foo#foo .foo.bbb {
b: 1;
}
mi-test-b {
b: 1;
}
#foo-foo > .bar .baz {
c: c;
}
mi-test-c-1 > .bar .baz {
c: c;
}
mi-test-c-2 .baz {
c: c;
}
mi-test-c-3 {
c: c;
}
mi-test-d {
gender: "Male";
}
69 changes: 69 additions & 0 deletions test/less/mixins-interpolated.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

@a1: foo;
@a2: ~".foo";
@a4: ~"#foo";

.@{a1} {
a: 1;
}

@{a2} {
a: 2;
}

#@{a1} {
a: 3;
}

@{a4} {
a: 4;
}

mi-test-a {
.foo;
#foo;
}

.b .bb {
&.@{a1}-xxx .yyy-@{a1}@{a4} {
& @{a2}.bbb {
b: 1;
}
}
}

mi-test-b {
.b.bb.foo-xxx.yyy-foo#foo.foo.bbb;
}

@c1: @a1;
@c2: bar;
@c3: baz;

#@{c1}-foo {
> .@{c2} {
.@{c3} {
c: c;
}
}
}

mi-test-c {
&-1 {#foo-foo;}
&-2 {#foo-foo > .bar;}
&-3 {#foo-foo > .bar.baz;}
}

.Person(@name, @gender_) {
.@{name} {
@gender: @gender_;
.sayGender() {
gender: @gender;
}
}
}

mi-test-d {
.Person(person, "Male");
.person.sayGender();
}

0 comments on commit 5196277

Please sign in to comment.