1 | var DOM = require("ess-compiler/dom");
|
2 | var Root = require("ess-compiler/lib/dom/root");
|
3 | var Rule = require("ess-compiler/lib/dom/rule");
|
4 | var Selector = require("ess-compiler/lib/dom/selector");
|
5 |
|
6 | var root = '__ROOT__';
|
7 | var rootRe = new RegExp('^' + root + '-*');
|
8 | var DEFAULT = '$default';
|
9 |
|
10 | module.exports = function(render) {
|
11 | return function(prefix, gensym, props) {
|
12 | var selectors = {};
|
13 |
|
14 | if (prefix) prefix += ' ';
|
15 |
|
16 |
|
17 | var toString = Selector.prototype.toString;
|
18 | Selector.prototype.toString = function() {
|
19 | var sel = toString.call(this).replace(rootRe, '');
|
20 |
|
21 |
|
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 | };
|