1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var node_1 = tslib_1.__importDefault(require("./node"));
|
---|
5 | var anonymous_1 = tslib_1.__importDefault(require("./anonymous"));
|
---|
6 | var function_caller_1 = tslib_1.__importDefault(require("../functions/function-caller"));
|
---|
7 | //
|
---|
8 | // A function call node.
|
---|
9 | //
|
---|
10 | var Call = function (name, args, index, currentFileInfo) {
|
---|
11 | this.name = name;
|
---|
12 | this.args = args;
|
---|
13 | this.calc = name === 'calc';
|
---|
14 | this._index = index;
|
---|
15 | this._fileInfo = currentFileInfo;
|
---|
16 | };
|
---|
17 | Call.prototype = Object.assign(new node_1.default(), {
|
---|
18 | type: 'Call',
|
---|
19 | accept: function (visitor) {
|
---|
20 | if (this.args) {
|
---|
21 | this.args = visitor.visitArray(this.args);
|
---|
22 | }
|
---|
23 | },
|
---|
24 | //
|
---|
25 | // When evaluating a function call,
|
---|
26 | // we either find the function in the functionRegistry,
|
---|
27 | // in which case we call it, passing the evaluated arguments,
|
---|
28 | // if this returns null or we cannot find the function, we
|
---|
29 | // simply print it out as it appeared originally [2].
|
---|
30 | //
|
---|
31 | // The reason why we evaluate the arguments, is in the case where
|
---|
32 | // we try to pass a variable to a function, like: `saturate(@color)`.
|
---|
33 | // The function should receive the value, not the variable.
|
---|
34 | //
|
---|
35 | eval: function (context) {
|
---|
36 | var _this = this;
|
---|
37 | /**
|
---|
38 | * Turn off math for calc(), and switch back on for evaluating nested functions
|
---|
39 | */
|
---|
40 | var currentMathContext = context.mathOn;
|
---|
41 | context.mathOn = !this.calc;
|
---|
42 | if (this.calc || context.inCalc) {
|
---|
43 | context.enterCalc();
|
---|
44 | }
|
---|
45 | var exitCalc = function () {
|
---|
46 | if (_this.calc || context.inCalc) {
|
---|
47 | context.exitCalc();
|
---|
48 | }
|
---|
49 | context.mathOn = currentMathContext;
|
---|
50 | };
|
---|
51 | var result;
|
---|
52 | var funcCaller = new function_caller_1.default(this.name, context, this.getIndex(), this.fileInfo());
|
---|
53 | if (funcCaller.isValid()) {
|
---|
54 | try {
|
---|
55 | result = funcCaller.call(this.args);
|
---|
56 | exitCalc();
|
---|
57 | }
|
---|
58 | catch (e) {
|
---|
59 | if (e.hasOwnProperty('line') && e.hasOwnProperty('column')) {
|
---|
60 | throw e;
|
---|
61 | }
|
---|
62 | throw {
|
---|
63 | type: e.type || 'Runtime',
|
---|
64 | message: "Error evaluating function `" + this.name + "`" + (e.message ? ": " + e.message : ''),
|
---|
65 | index: this.getIndex(),
|
---|
66 | filename: this.fileInfo().filename,
|
---|
67 | line: e.lineNumber,
|
---|
68 | column: e.columnNumber
|
---|
69 | };
|
---|
70 | }
|
---|
71 | }
|
---|
72 | if (result !== null && result !== undefined) {
|
---|
73 | // Results that that are not nodes are cast as Anonymous nodes
|
---|
74 | // Falsy values or booleans are returned as empty nodes
|
---|
75 | if (!(result instanceof node_1.default)) {
|
---|
76 | if (!result || result === true) {
|
---|
77 | result = new anonymous_1.default(null);
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | result = new anonymous_1.default(result.toString());
|
---|
81 | }
|
---|
82 | }
|
---|
83 | result._index = this._index;
|
---|
84 | result._fileInfo = this._fileInfo;
|
---|
85 | return result;
|
---|
86 | }
|
---|
87 | var args = this.args.map(function (a) { return a.eval(context); });
|
---|
88 | exitCalc();
|
---|
89 | return new Call(this.name, args, this.getIndex(), this.fileInfo());
|
---|
90 | },
|
---|
91 | genCSS: function (context, output) {
|
---|
92 | output.add(this.name + "(", this.fileInfo(), this.getIndex());
|
---|
93 | for (var i = 0; i < this.args.length; i++) {
|
---|
94 | this.args[i].genCSS(context, output);
|
---|
95 | if (i + 1 < this.args.length) {
|
---|
96 | output.add(', ');
|
---|
97 | }
|
---|
98 | }
|
---|
99 | output.add(')');
|
---|
100 | }
|
---|
101 | });
|
---|
102 | exports.default = Call;
|
---|
103 | //# sourceMappingURL=call.js.map |
---|