tizen beta release
[framework/web/web-ui-fw.git] / build-tools / lib / less / tree / import.js
1 (function (tree) {
2 //
3 // CSS @import node
4 //
5 // The general strategy here is that we don't want to wait
6 // for the parsing to be completed, before we start importing
7 // the file. That's because in the context of a browser,
8 // most of the time will be spent waiting for the server to respond.
9 //
10 // On creation, we push the import path to our import queue, though
11 // `import,push`, we also pass it a callback, which it'll call once
12 // the file has been fetched, and parsed.
13 //
14 tree.Import = function (path, imports) {
15     var that = this;
16
17     this._path = path;
18
19     // The '.less' extension is optional
20     if (path instanceof tree.Quoted) {
21         this.path = /\.(le?|c)ss(\?.*)?$/.test(path.value) ? path.value : path.value + '.less';
22     } else {
23         this.path = path.value.value || path.value;
24     }
25
26     this.css = /css(\?.*)?$/.test(this.path);
27
28     // Only pre-compile .less files
29     if (! this.css) {
30         imports.push(this.path, function (root) {
31             if (! root) {
32                 throw new(Error)("Error parsing " + that.path);
33             }
34             that.root = root;
35         });
36     }
37 };
38
39 //
40 // The actual import node doesn't return anything, when converted to CSS.
41 // The reason is that it's used at the evaluation stage, so that the rules
42 // it imports can be treated like any other rules.
43 //
44 // In `eval`, we make sure all Import nodes get evaluated, recursively, so
45 // we end up with a flat structure, which can easily be imported in the parent
46 // ruleset.
47 //
48 tree.Import.prototype = {
49     toCSS: function () {
50         if (this.css) {
51             return "@import " + this._path.toCSS() + ';\n';
52         } else {
53             return "";
54         }
55     },
56     eval: function (env) {
57         var ruleset;
58
59         if (this.css) {
60             return this;
61         } else {
62             ruleset = new(tree.Ruleset)(null, this.root.rules.slice(0));
63
64             for (var i = 0; i < ruleset.rules.length; i++) {
65                 if (ruleset.rules[i] instanceof tree.Import) {
66                     Array.prototype
67                          .splice
68                          .apply(ruleset.rules,
69                                 [i, 1].concat(ruleset.rules[i].eval(env)));
70                 }
71             }
72             return ruleset.rules;
73         }
74     }
75 };
76
77 })(require('less/tree'));