Skip to content
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

Allow custom @-rules to execute custom functions #2852

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/less/functions/function-caller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ functionCaller.prototype.call = function(args) {
// https://github.com/less/less.js/issues/2477
if (Array.isArray(args)) {
args = args.filter(function (item) {
if (item.type === "Comment") {
if (item && item.type && item.type === "Comment") {
return false;
}
return true;
})
.map(function(item) {
if (item.type === "Expression") {
if (item && item.type && item.type === "Expression") {
var subNodes = item.value.filter(function (item) {
if (item.type === "Comment") {
return false;
Expand Down
24 changes: 22 additions & 2 deletions lib/less/tree/directive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Node = require("./node"),
Selector = require("./selector"),
Ruleset = require("./ruleset");
Ruleset = require("./ruleset"),
FunctionCaller = require("../functions/function-caller");

var Directive = function (name, value, rules, index, currentFileInfo, debugInfo, isRooted, visibilityInfo) {
var i;
Expand Down Expand Up @@ -57,7 +58,8 @@ Directive.prototype.genCSS = function (context, output) {
}
};
Directive.prototype.eval = function (context) {
var mediaPathBackup, mediaBlocksBackup, value = this.value, rules = this.rules;
var mediaPathBackup, mediaBlocksBackup, value = this.value, rules = this.rules,
result, funcCaller = new FunctionCaller(this.name, context, this.index, this.currentFileInfo);

//media stored inside other directive should not bubble over it
//backpup media bubbling information
Expand All @@ -75,6 +77,24 @@ Directive.prototype.eval = function (context) {
rules = [rules[0].eval(context)];
rules[0].root = true;
}

if (funcCaller.isValid()) {
try {
result = funcCaller.call([this.name, value ? value.value : value, rules ? rules[0] : rules]);
} catch (e) {
throw { type: e.type || "Runtime",
message: "error evaluating custom directive `" + this.name + "`" +
(e.message ? ': ' + e.message : ''),
index: this.index, filename: this.currentFileInfo.filename };
}

if (result != null) {
result.index = this.index;
result.currentFileInfo = this.currentFileInfo;
return result;
}
}

//restore media bubbling information
context.mediaPath = mediaPathBackup;
context.mediaBlocks = mediaBlocksBackup;
Expand Down
4 changes: 4 additions & 0 deletions test/css/plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@
prop: value;
}
@arbitrary value after ();
@new-directive success;
test {
foo: bar;
}
8 changes: 8 additions & 0 deletions test/less/plugin.less
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@
test-directive("@charset"; '"utf-8"');
test-directive("@arbitrary"; "value after ()");

@plugin "./plugin/plugin-custom-directives";

@make-directive new-directive;
@eval-rules test {
foo: bar;
}



9 changes: 9 additions & 0 deletions test/less/plugin/plugin-custom-directives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

functions.addMultiple({
"@make-directive" : function(name, value, rules) {
return new tree.Directive('@' + value, new tree.Anonymous('success'));
},
"@eval-rules" : function(name, value, rules) {
return new tree.Ruleset([ new tree.Selector([new tree.Element("", value)]) ], rules.rules);
}
});