source: imaps-frontend/node_modules/nano-css/addon/virtual.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[d565449]1'use strict';
2
3var createMemoizer = function (pfx) {
4 var offset = 10;
5 var msb = 35;
6 var power = 1;
7
8 var self = {
9 cache: {},
10 length: 0,
11
12 next: function () {
13 var vcount = self.length + offset;
14
15 if (vcount === msb) {
16 offset += (msb + 1) * 9;
17 msb = Math.pow(36, ++power) - 1;
18 }
19 self.length++;
20
21 return vcount;
22 },
23
24 get: function () {
25 var curr = self.cache;
26 var lastIndex = arguments.length - 1;
27 var lastStep = arguments[lastIndex];
28
29 for (var i = 0; i < lastIndex; i++) {
30 var step = arguments[i] || '_';
31
32 if (!curr[step]) curr[step] = {};
33 curr = curr[step];
34 }
35
36 if (!curr[lastStep]) curr[lastStep] = pfx + self.next().toString(36);
37
38 return curr[lastStep];
39 }
40 };
41
42 return self;
43};
44
45exports.addon = function (renderer) {
46 if (process.env.NODE_ENV !== 'production') {
47 require('./__dev__/warnOnMissingDependencies')('virtual', renderer, ['rule', 'putRaw']);
48 }
49
50 renderer.memo = createMemoizer(renderer.pfx);
51
52 renderer.atomic = function (selectorTemplate, rawDecl, atrule) {
53 var memo = renderer.memo;
54 var memoLength = memo.length;
55 var className = memo.get(atrule, selectorTemplate, rawDecl);
56
57 if (memoLength < memo.length) {
58 var selector = selectorTemplate.replace(/&/g, '.' + className);
59 var str = selector + '{' + rawDecl + '}';
60
61 if (atrule) {
62 str = atrule + '{' + str + '}';
63 }
64
65 renderer.putRaw(str);
66 }
67
68 return className;
69 };
70
71 renderer.virtual = function (selectorTemplate, decls, atrule) {
72 selectorTemplate = selectorTemplate || '&';
73
74 var classNames = '';
75
76 for (var prop in decls) {
77 var value = decls[prop];
78
79 if (prop.indexOf('keyframes') > -1) {
80 renderer.putAt('', value, prop);
81 continue;
82 }
83
84 if ((value instanceof Object) && !(value instanceof Array)) {
85 if (prop[0] === '@') {
86 classNames += renderer.virtual(selectorTemplate, value, prop);
87 } else {
88 classNames += renderer.virtual(renderer.selector(selectorTemplate, prop), value, atrule);
89 }
90 } else {
91 var rawDecl = renderer.decl(prop, value);
92 var rawDecls = rawDecl.split(';');
93
94 for (var i = 0; i < rawDecls.length; i++) {
95 var d = rawDecls[i];
96 if (d) classNames += ' ' + renderer.atomic(selectorTemplate, d, atrule);
97 }
98 }
99 }
100
101 return classNames;
102 };
103
104 renderer.rule = function (decls) {
105 return renderer.virtual('&', decls);
106 };
107};
Note: See TracBrowser for help on using the repository browser.