๐Ÿ“ฆ EdwonLim / node-less

๐Ÿ“„ mixin.js ยท 226 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226(function (module) {

    var Selector =require('./selector.js'),
        Element = require('./element.js'),
        Expression = require('./expression.js'),
        Rule = require('./rule.js'),
        Ruleset = require('./ruleset.js'),
        evalEnv = require('./evalEnv.js');

    var mixin = {};

    mixin.Call = function (elements, args, index, currentFileInfo, important) {
        this.selector = new Selector(elements);
        this.arguments = args;
        this.index = index;
        this.currentFileInfo = currentFileInfo;
        this.important = important;
    };

    mixin.Call.prototype = {
        type: "MixinCall",
        accept: function (visitor) {
            this.selector = visitor.visit(this.selector);
            this.arguments = visitor.visit(this.arguments);
        },
        eval: function (env) {
            var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound;
            args = this.arguments && this.arguments.map(function (a) {
                return { name: a.name, value: a.value.eval(env) };
            });
            for (i = 0; i < env.frames.length; i++) {
                if ((mixins = env.frames[i].find(this.selector)).length > 0) {
                    isOneFound = true;
                    for (m = 0; m < mixins.length; m++) {
                        mixin = mixins[m];
                        isRecursive = false;
                        for(f = 0; f < env.frames.length; f++) {
                            if ((!(mixin instanceof mixin.Definition)) && mixin === (env.frames[f].originalRuleset || env.frames[f])) {
                                isRecursive = true;
                                break;
                            }
                        }
                        if (isRecursive) {
                            continue;
                        }
                        if (mixin.matchArgs(args, env)) {
                            if (!mixin.matchCondition || mixin.matchCondition(args, env)) {
                                try {
                                    Array.prototype.push.apply(
                                          rules, mixin.eval(env, args, this.important).rules);
                                } catch (e) {
                                    throw { message: e.message, index: this.index, filename: this.currentFileInfo.filename, stack: e.stack };
                                }
                            }
                            match = true;
                        }
                    }
                    if (match) {
                        return rules;
                    }
                }
            }
            if (isOneFound) {
                throw { type:    'Runtime',
                        message: 'No matching definition was found for `' +
                                  this.selector.toCSS().trim() + '('      +
                                  (args ? args.map(function (a) {
                                      var argValue = "";
                                      if (a.name) {
                                          argValue += a.name + ":";
                                      }
                                      if (a.value.toCSS) {
                                          argValue += a.value.toCSS();
                                      } else {
                                          argValue += "???";
                                      }
                                      return argValue;
                                  }).join(', ') : "") + ")`",
                        index:   this.index, filename: this.currentFileInfo.filename };
            } else {
                throw { type: 'Name',
                    message: this.selector.toCSS().trim() + " is undefined",
                    index: this.index, filename: this.currentFileInfo.filename };
            }
        }
    };

    mixin.Definition = function (name, params, rules, condition, variadic) {
        this.name = name;
        this.selectors = [new Selector([new Element(null, name)])];
        this.params = params;
        this.condition = condition;
        this.variadic = variadic;
        this.arity = params.length;
        this.rules = rules;
        this._lookups = {};
        this.required = params.reduce(function (count, p) {
            if (!p.name || (p.name && !p.value)) { return count + 1 }
            else                                 { return count }
        }, 0);
        this.parent = Ruleset.prototype;
        this.frames = [];
    };

    mixin.Definition.prototype = {
        type: "MixinDefinition",
        accept: function (visitor) {
            this.params = visitor.visit(this.params);
            this.rules = visitor.visit(this.rules);
            this.condition = visitor.visit(this.condition);
        },
        toCSS:     function ()     { return ""; },
        variable:  function (name) { return this.parent.variable.call(this, name); },
        variables: function ()     { return this.parent.variables.call(this); },
        find:      function ()     { return this.parent.find.apply(this, arguments); },
        rulesets:  function ()     { return this.parent.rulesets.apply(this); },
        evalParams: function (env, mixinEnv, args, evaldArguments) {
            var frame = new Ruleset(null, []),
                varargs, arg,
                params = this.params.slice(0),
                i, j, val, name, isNamedFound, argIndex;
            mixinEnv = new evalEnv(mixinEnv, [frame].concat(mixinEnv.frames));
            if (args) {
                args = args.slice(0);
                for(i = 0; i < args.length; i++) {
                    arg = args[i];
                    if (name = (arg && arg.name)) {
                        isNamedFound = false;
                        for(j = 0; j < params.length; j++) {
                            if (!evaldArguments[j] && name === params[j].name) {
                                evaldArguments[j] = arg.value.eval(env);
                                frame.rules.unshift(new Rule(name, arg.value.eval(env)));
                                isNamedFound = true;
                                break;
                            }
                        }
                        if (isNamedFound) {
                            args.splice(i, 1);
                            i--;
                            continue;
                        } else {
                            throw { type: 'Runtime', message: "Named argument for " + this.name +
                                ' ' + args[i].name + ' not found' };
                        }
                    }
                }
            }
            argIndex = 0;
            for (i = 0; i < params.length; i++) {
                if (evaldArguments[i]) continue;
                arg = args && args[argIndex];
                if (name = params[i].name) {
                    if (params[i].variadic && args) {
                        varargs = [];
                        for (j = argIndex; j < args.length; j++) {
                            varargs.push(args[j].value.eval(env));
                        }
                        frame.rules.unshift(new Rule(name, new Expression(varargs).eval(env)));
                    } else {
                        val = arg && arg.value;
                        if (val) {
                            val = val.eval(env);
                        } else if (params[i].value) {
                            val = params[i].value.eval(mixinEnv);
                            frame.resetCache();
                        } else {
                            throw { type: 'Runtime', message: "wrong number of arguments for " + this.name +
                                ' (' + args.length + ' for ' + this.arity + ')' };
                        }
                        frame.rules.unshift(new Rule(name, val));
                        evaldArguments[i] = val;
                    }
                }
                if (params[i].variadic && args) {
                    for (j = argIndex; j < args.length; j++) {
                        evaldArguments[j] = args[j].value.eval(env);
                    }
                }
                argIndex++;
            }
            return frame;
        },
        eval: function (env, args, important) {
            var _arguments = [],
                mixinFrames = this.frames.concat(env.frames),
                frame = this.evalParams(env, new evalEnv(env, mixinFrames), args, _arguments),
                context, rules, start, ruleset;
            frame.rules.unshift(new Rule('@arguments', new Expression(_arguments).eval(env)));
            rules = important ?
                this.parent.makeImportant.apply(this).rules : this.rules.slice(0);
            ruleset = new Ruleset(null, rules).eval(new evalEnv(env, [this, frame].concat(mixinFrames)));
            ruleset.originalRuleset = this;
            return ruleset;
        },
        matchCondition: function (args, env) {
            if (this.condition && !this.condition.eval(
                new evalEnv(env,
                    [this.evalParams(env, new evalEnv(env, this.frames.concat(env.frames)), args, [])]
                        .concat(env.frames)))) {
                return false;
            }
            return true;
        },
        matchArgs: function (args, env) {
            var argsLength = (args && args.length) || 0, len, frame;
            if (! this.variadic) {
                if (argsLength < this.required)                               { return false }
                if (argsLength > this.params.length)                          { return false }
                if ((this.required > 0) && (argsLength > this.params.length)) { return false }
            }
            len = Math.min(argsLength, this.arity);
            for (var i = 0; i < len; i++) {
                if (!this.params[i].name && !this.params[i].variadic) {
                    if (args[i].value.eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) {
                        return false;
                    }
                }
            }
            return true;
        }
    };

    module.exports = mixin;

})(module);