-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3263 from matthew-dean/feature/each-2
Fixes #2270 - Adds each() function to Less functions
- Loading branch information
Showing
7 changed files
with
256 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
var Dimension = require('../tree/dimension'), | ||
Declaration = require('../tree/declaration'), | ||
Ruleset = require('../tree/ruleset'), | ||
Selector = require('../tree/selector'), | ||
Element = require('../tree/element'), | ||
functionRegistry = require('./function-registry'); | ||
|
||
var getItemsFromNode = function(node) { | ||
// handle non-array values as an array of length 1 | ||
// return 'undefined' if index is invalid | ||
var items = Array.isArray(node.value) ? | ||
node.value : Array(node); | ||
|
||
return items; | ||
}; | ||
|
||
functionRegistry.addMultiple({ | ||
_SELF: function(n) { | ||
return n; | ||
}, | ||
extract: function(values, index) { | ||
index = index.value - 1; // (1-based index) | ||
|
||
return getItemsFromNode(values)[index]; | ||
}, | ||
length: function(values) { | ||
return new Dimension(getItemsFromNode(values).length); | ||
}, | ||
each: function(list, rs) { | ||
var i = 0, rules = [], newRules, iterator; | ||
|
||
if (list.value) { | ||
if (Array.isArray(list.value)) { | ||
iterator = list.value; | ||
} else { | ||
iterator = [list.value]; | ||
} | ||
} else if (list.ruleset) { | ||
iterator = list.ruleset.rules; | ||
} else if (Array.isArray(list)) { | ||
iterator = list; | ||
} else { | ||
iterator = [list]; | ||
} | ||
|
||
var valueName = '@value', | ||
keyName = '@key', | ||
indexName = '@index'; | ||
|
||
if (rs.params) { | ||
valueName = rs.params[0] && rs.params[0].name; | ||
keyName = rs.params[1] && rs.params[1].name; | ||
indexName = rs.params[2] && rs.params[2].name; | ||
rs = rs.rules; | ||
} else { | ||
rs = rs.ruleset; | ||
} | ||
|
||
iterator.forEach(function(item) { | ||
i = i + 1; | ||
var key, value; | ||
if (item instanceof Declaration) { | ||
key = typeof item.name === 'string' ? item.name : item.name[0].value; | ||
value = item.value; | ||
} else { | ||
key = new Dimension(i); | ||
value = item; | ||
} | ||
|
||
newRules = rs.rules.slice(0); | ||
if (valueName) { | ||
newRules.push(new Declaration(valueName, | ||
value, | ||
false, false, this.index, this.currentFileInfo)); | ||
} | ||
if (indexName) { | ||
newRules.push(new Declaration(indexName, | ||
new Dimension(i), | ||
false, false, this.index, this.currentFileInfo)); | ||
} | ||
if (keyName) { | ||
newRules.push(new Declaration(keyName, | ||
key, | ||
false, false, this.index, this.currentFileInfo)); | ||
} | ||
|
||
rules.push(new Ruleset([ new(Selector)([ new Element("", '&') ]) ], | ||
newRules, | ||
rs.strictImports, | ||
rs.visibilityInfo() | ||
)); | ||
}); | ||
|
||
return new Ruleset([ new(Selector)([ new Element("", '&') ]) ], | ||
rules, | ||
rs.strictImports, | ||
rs.visibilityInfo() | ||
).eval(this.context); | ||
|
||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.sel-blue { | ||
a: b; | ||
} | ||
.sel-green { | ||
a: b; | ||
} | ||
.sel-red { | ||
a: b; | ||
} | ||
.each { | ||
index: 1, 2, 3, 4; | ||
item1: a; | ||
item2: b; | ||
item3: c; | ||
item4: d; | ||
nest-1-1: 10px 1; | ||
nest-2-1: 15px 2; | ||
nest-1-2: 20px 1; | ||
nest-2-2: 25px 2; | ||
padding: 10px 20px 30px 40px; | ||
} | ||
.each .nest-anon { | ||
nest-1-1: a c; | ||
nest-1-2: a d; | ||
nest-2-1: b c; | ||
nest-2-2: b d; | ||
} | ||
.set { | ||
one: blue; | ||
two: green; | ||
three: red; | ||
} | ||
.set-2 { | ||
one-1: blue; | ||
two-2: green; | ||
three-3: red; | ||
} | ||
.single { | ||
val: true; | ||
val2: 2; | ||
val3: 4; | ||
} | ||
.box { | ||
-less-log: a; | ||
-less-log: b; | ||
-less-log: c; | ||
-less-log: d; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
@selectors: blue, green, red; | ||
@list: a b c d; | ||
|
||
each(@selectors, { | ||
.sel-@{value} { | ||
a: b; | ||
} | ||
}); | ||
|
||
.each { | ||
each(@list, { | ||
index+: @index; | ||
item@{index}: @value; | ||
}); | ||
|
||
// nested each | ||
each(10px 15px, 20px 25px; { | ||
// demonstrates nesting of each() | ||
each(@value; #(@v, @k, @i) { | ||
nest-@{i}-@{index}: @v @k; | ||
}); | ||
}); | ||
|
||
// nested anonymous mixin | ||
.nest-anon { | ||
each(a b, .(@v;@i) { | ||
each(c d, .(@vv;@ii) { | ||
nest-@{i}-@{ii}: @v @vv; | ||
}); | ||
}); | ||
} | ||
|
||
// vector math | ||
each(1 2 3 4, { | ||
padding+_: (@value * 10px); | ||
}); | ||
} | ||
|
||
@set: { | ||
one: blue; | ||
two: green; | ||
three: red; | ||
} | ||
.set { | ||
each(@set, { | ||
@{key}: @value; | ||
}); | ||
} | ||
.set-2() { | ||
one: blue; | ||
two: green; | ||
three: red; | ||
} | ||
.set-2 { | ||
each(.set-2(), .(@v, @k, @i) { | ||
@{k}-@{i}: @v; | ||
}); | ||
} | ||
|
||
.pick(@a) when (@a = 4) { | ||
val3: @a; | ||
} | ||
.single { | ||
each(true, { | ||
val: @value; | ||
}); | ||
@exp: 1 + 1; | ||
each(@exp, { | ||
val2: @value; | ||
}); | ||
each(1 2 3 4, { | ||
.pick(@value); | ||
}); | ||
} | ||
|
||
@list: a b c d; | ||
.box { | ||
each(@list, { | ||
-less-log: extract(@list, @index); | ||
}) | ||
} |