UNPKG

1.23 kBJavaScriptView Raw
1var DOM = require("ess-compiler/dom");
2var Root = require("ess-compiler/lib/dom/root");
3var Rule = require("ess-compiler/lib/dom/rule");
4var Selector = require("ess-compiler/lib/dom/selector");
5
6var root = '__ROOT__';
7var rootRe = new RegExp('^' + root + '-*');
8var DEFAULT = '$default';
9
10module.exports = function(render) {
11 return function(prefix, gensym, props) {
12 var selectors = {};
13
14 if (prefix) prefix += ' ';
15
16 // monkeypatch selector prototype
17 var toString = Selector.prototype.toString;
18 Selector.prototype.toString = function() {
19 var sel = toString.call(this).replace(rootRe, '');
20
21 // they're tring to style elements!
22 if (sel.charAt(0) === ' ') return prefix + '.' + gensym();
23
24 var parts = sel.split(/\:/);
25 var postfix = parts[1];
26 sel = parts[0] || DEFAULT;
27
28 return prefix + '.' + (selectors[sel] = selectors[sel] || gensym()) + (postfix ? ':' + postfix : '');
29 };
30
31 try {
32 var children = DOM([root], null, render(DOM, null, props));
33 var css = (new Root(children)).toString();
34 } catch(_) {}
35
36 Selector.prototype.toString = toString;
37
38 selectors.toString = function() {
39 return css || '';
40 };
41 return selectors;
42 };
43};