-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Each() function #2786
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
Closed
Closed
Each() function #2786
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,55 @@ | ||
var Ruleset = require("../tree/ruleset"), | ||
Rule = require("../tree/rule"), | ||
Selector = require("../tree/selector"), | ||
Element = require("../tree/element"), | ||
Dimension = require("../tree/dimension"), | ||
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({ | ||
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, vars, ruleset) { | ||
var i = 0, rules = [], rs, newRules; | ||
|
||
rs = ruleset ? ruleset.ruleset : vars.ruleset; | ||
|
||
list.value.forEach(function(item) { | ||
i = i + 1; | ||
newRules = rs.rules.slice(0); | ||
newRules.push(new Rule(ruleset && vars.value[1] ? '@' + vars.value[1].value : '@item', | ||
item, | ||
false, false, this.index, this.currentFileInfo)); | ||
newRules.push(new Rule(ruleset && vars.value[0] ? '@' + vars.value[0].value : '@index', | ||
new Dimension(i), | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
SyntaxError: properties must be inside selector blocks, they cannot be in the root. in {path}detached-ruleset-3.less on line 2, column 3: | ||
SyntaxError: Properties must be inside selector blocks. They cannot be in the root. in {path}detached-ruleset-3.less on line 2, column 3: | ||
1 @a: { | ||
2 b: 1; | ||
3 }; |
This file contains hidden or 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 @@ | ||
func(); |
This file contains hidden or 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,2 @@ | ||
SyntaxError: Custom function 'func' is not registered. in {path}plugin-undefined-1.less on line 1, column 1: | ||
1 func(); |
This file contains hidden or 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,2 @@ | ||
@plugin "../plugin/plugin-root.js"; | ||
test-undefined(); |
This file contains hidden or 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,3 @@ | ||
SyntaxError: Custom function 'test-undefined' is not registered. in {path}plugin-undefined-2.less on line 2, column 1: | ||
1 @plugin "../plugin/plugin-root.js"; | ||
2 test-undefined(); |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
SyntaxError: properties must be inside selector blocks, they cannot be in the root. in {path}property-in-root.less on line 2, column 3: | ||
SyntaxError: Properties must be inside selector blocks. They cannot be in the root. in {path}property-in-root.less on line 2, column 3: | ||
1 .a() { | ||
2 prop:1; | ||
3 } |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
SyntaxError: properties must be inside selector blocks, they cannot be in the root. in {path}property-in-root.less on line 2, column 3: | ||
SyntaxError: Properties must be inside selector blocks. They cannot be in the root. in {path}property-in-root.less on line 2, column 3: | ||
1 .a() { | ||
2 prop:1; | ||
3 } |
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
SyntaxError: properties must be inside selector blocks, they cannot be in the root. in {path}property-in-root3.less on line 1, column 1: | ||
SyntaxError: Properties must be inside selector blocks. They cannot be in the root. in {path}property-in-root3.less on line 1, column 1: | ||
1 prop:1; | ||
2 .a { |
This file contains hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
functions.addMultiple({ | ||
"test-detached-ruleset" : function() { | ||
var rule = new tree.Rule('prop', new tree.Anonymous('value')); | ||
return new tree.DetachedRuleset(new tree.Ruleset("", [ rule ])); | ||
}, | ||
"test-directive": function(arg1, arg2) { | ||
return new tree.Directive(arg1.value, new tree.Anonymous('"' + arg2.value + '"')); | ||
}, | ||
// Functions must return something. Must 'return true' if they produce no output. | ||
"test-undefined": function() { } | ||
}); |
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.
@matthew-dean By the way. If we allow all these "mixin-like" arguments for functions, will there be any difference between
mixin.arguments()
andfunction.arguments()
beside the "named arguments" thing? Obviously I'm just considering if this code can be reused somehow. I understand that because of the "named args" they need to return different structures, but are there any other differences?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.
There were various bits I modified from mixin args to functions args. One is that there are legacy IE "function-like" calls that need to be parsed but that mixins don't parse. (Which I'm not sure are still needed, but it's possible that legacy CSS code continues to persist in currently-used mixins.) It's possible that may be the only real variation, at least for calls. The code for mixin args is re-used between both mixin calls and mixin definitions, so it's possible we could collapse args for "function calls" and "mixin calls", and break out mixin definitions? Or maybe could just collapse all 3 with appropriate
if
statements.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.
Thanks... Yes, it seems to need more investigations on possible unifications.
I was just about to experiment with some
@var(args...);
stuff, and realized that having three distinct functions to handle pretty much the same syntax would be somewhat an overkill. Additionally I recall that for #1857, I was also thinking of allowing@arg...
stuff for functions too, but quickly bailed out because of all these differences :(