-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #2270 - Adds each() function to Less functions #3263
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7a548a6
WIP each() re-implementation
matthew-dean 15f7b1a
Added each() and tests
matthew-dean 31b73b3
Merge branch 'master' into feature/each-2
matthew-dean a95823d
Added tests calling mixins from each()
matthew-dean defc03f
Merge branch 'master' into feature/each-2
matthew-dean 3c7ccdb
Allows named args for detached ruleset (anonymous mixin)
matthew-dean e1e1c39
Added .() example to each
matthew-dean 7104c3c
add explicit nested anonymous mixin test
calvinjuarez c49b11e
add explicit nested anonymous mixin test result
calvinjuarez c974d4e
derp: align test with expected output
calvinjuarez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
.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; | ||
}); | ||
}); | ||
|
||
This comment was marked as outdated.
Sorry, something went wrong. |
||
// 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); | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the movement of the functions into the file where they're most relevant!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍