-
Notifications
You must be signed in to change notification settings - Fork 9
/
tass.js
152 lines (139 loc) · 4.51 KB
/
tass.js
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
/**
* @license MIT copyright (c) 2012 cho45 ( www.lowreal.net )
* https://github.com/cho45/tasscss/blob/master/LICENSE
*/
function TASS (c, callback) {
function processInclude (c) {
return c.replace(/@include\s+([\w\-]+)(\(.*?\))?\s*;/g, function (_, name, args) {
var mixin = TASS.mixins[name];
if (!mixin) return '/* unknown mixin: ' + name + ' */';
var content = mixin.content;
if (args) {
var scan = args.slice(1, -1);
var r = /\s*((?:'(?:[^']|\\')*'|"(?:[^"]|\\")*"|\([^\)]*\)|[^,()])+)|,|./g;
var a = [], m; while ((m = r.exec(scan))) if (m[1]) a.push(m[1]);
for (var i = 0, len = mixin.args.length; i < len; i++) content = mixin.args[i] + " : " + a[i] + ";\n" + content;
}
return content;
});
}
function paralell (obj, cont) {
var results = {}, n = 0;
for (var key in obj) if (obj.hasOwnProperty(key)) (function (k, f, i) {
f(function (r) { results[k] = r; if (--n <= 0) cont(results) });
})(key, obj[key], n++);
if (!n) cont(results);
}
var external = /^@include\s+(.+?\..+?)\s*;\n?/gm;
var resources = {}; c.replace(external, function (_, path) {
resources[path] = function (cont) {
TASS.read(path, function (data) {
if (/\.css$/.test(path)) {
cont(data);
} else {
TASS(data, cont);
}
});
};
return _;
});
paralell(resources, function (resources) {
c = c.replace(/^@mixin\s+([\w\-]+)\s*(\([^\)]+\))?\s*\{([\s\S]*?)^\}/gm, function (_, name, args, content) {
if (args) args = args.slice(1, -1).split(/\s*,\s*/);
TASS.mixins[name] = {
args : args,
content : processInclude(content)
};
return '';
});
var nesting = [ [] ];
var level = [];
var variables = TASS.variables;
c = processInclude(c).replace(/(^[^\{\};]+\{)|(\})|^\s*(\$[\w\-]+\s*:.+);|(\$[\w\-]+)/gm, function (_, open, close, vardef, varget) {
if (open) {
var scope = function (_parent) { this._parent = _parent };
scope.prototype = variables;
variables = new scope(variables);
var selectors = open.replace(/^\s*|\s*\{\s*$/g, '').split(/\s*,\s*/);
var nextnesting = [];
for (var j = 0, lenj = selectors.length; j < lenj; j++)
for (var i = 0, len = nesting.length; i < len; i++)
nextnesting.push( nesting[i].concat(selectors[j]) );
nesting = nextnesting;
level.push(selectors);
var expanded = [];
for (var i = 0, len = nesting.length; i < len; i++) expanded.push(nesting[i].join(" "));
return (nesting[0].length - 1 ? "}\n" : "") + expanded.join(",\n") + " {";
} else
if (close) {
variables = variables._parent;
nesting.length = nesting.length / level.pop().length;
for (var i = 0, len = nesting.length; i < len; i++) nesting[i].pop();
return (nesting[0].length ? "" : "}");
} else
if (vardef) {
vardef = vardef.match(/^(\$[\w\-]+)?\s*:\s*(.*)$/);
variables[vardef[1]] = vardef[2];
return '';
} else
if (varget) {
return typeof variables[varget] === 'undefined' ? '/* unknown variable: ' + verget + ' */': variables[varget];
}
});
c = c.replace(/ &/g, '').replace(/^\s+/gm, '');
c = c.replace(external, function (_, path) {
return (typeof resources[path] !== 'string') ? '/* failed to load: ' + path + '*/' : resources[path];
});
callback(c);
});
}
TASS.version = "0.2.0";
TASS.init = function () {
TASS.mixins = {};
TASS.variables = {};
};
TASS.read = function (path, callback) {
throw "Not implemented";
};
TASS.init();
this.TASS = TASS;
if (typeof document != 'undefined') (function () {
TASS.read = function (path, callback) {
var req;
try {
req = new XMLHttpRequest();
} catch (e) {
try {
req = new ActiveXObject('MSXML2.XMLHTTP.6.0');
} catch (e) {
try {
req = new ActiveXObject('MSXML2.XMLHTTP.3.0');
} catch (e) {
console.log('[TASS] Error: Cannot create XMLHttpRequest object');
}
}
}
req.open('GET', path, true);
req.onreadystatechange = function (e) {
if (req.readyState != 4) return;
if (req.status == 200) {
callback(req.responseText);
} else {
callback('/* [TASS] Error:' + req.status + ' ' + req.responseText + ' */');
}
};
req.send(null);
};
var links = document.getElementsByTagName('link');
for (var i = 0, it; (it = links[i]); i++) (function (link) {
if (link.rel != 'stylesheet/tass' || !link.href) return;
TASS.read(link.href, function (data) {
TASS(data, function (css) {
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(css));
link.parentNode.insertBefore(style, link);
});
});
})(it);
})();