-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
138 lines (117 loc) · 4.68 KB
/
index.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
'use strict';
var KEBAB_REGEX = /[A-Z]/g;
var hash = function (str) {
var h = 5381, i = str.length;
while (i) h = (h * 33) ^ str.charCodeAt(--i);
return '_' + (h >>> 0).toString(36);
};
exports.create = function (config) {
config = config || {};
var assign = config.assign || Object.assign;
var client = typeof window === 'object';
// Check if we are really in browser environment.
if (process.env.NODE_ENV !== 'production') {
if (client) {
if ((typeof document !== 'object') || !document.getElementsByTagName('HTML')) {
console.error(
'nano-css detected browser environment because of "window" global, but ' +
'"document" global seems to be defective.'
);
}
}
}
var renderer = assign({
raw: '',
pfx: '_',
client: client,
assign: assign,
stringify: JSON.stringify,
kebab: function (prop) {
return prop.replace(KEBAB_REGEX, '-$&').toLowerCase();
},
decl: function (key, value) {
key = renderer.kebab(key);
return key + ':' + value + ';';
},
hash: function (obj) {
return hash(renderer.stringify(obj));
},
selector: function (parent, selector) {
return parent + (selector[0] === ':' ? '' : ' ') + selector;
},
putRaw: function (rawCssRule) {
renderer.raw += rawCssRule;
}
}, config);
if (renderer.client) {
if (!renderer.sh)
document.head.appendChild(renderer.sh = document.createElement('style'));
if (process.env.NODE_ENV !== 'production') {
renderer.sh.setAttribute('data-nano-css-dev', '');
// Test style sheet used in DEV mode to test if .insetRule() would throw.
renderer.shTest = document.createElement('style');
renderer.shTest.setAttribute('data-nano-css-dev-tests', '');
document.head.appendChild(renderer.shTest);
}
renderer.putRaw = function (rawCssRule) {
// .insertRule() is faster than .appendChild(), that's why we use it in PROD.
// But CSS injected using .insertRule() is not displayed in Chrome Devtools,
// that's why we use .appendChild in DEV.
if (process.env.NODE_ENV === 'production') {
var sheet = renderer.sh.sheet;
// Unknown pseudo-selectors will throw, this try/catch swallows all errors.
try {
sheet.insertRule(rawCssRule, sheet.cssRules.length);
// eslint-disable-next-line no-empty
} catch (error) {}
} else {
// Test if .insertRule() works in dev mode. Unknown pseudo-selectors will throw when
// .insertRule() is used, but .appendChild() will not throw.
try {
renderer.shTest.sheet.insertRule(rawCssRule, renderer.shTest.sheet.cssRules.length);
} catch (error) {
if (config.verbose) {
console.error(error);
}
}
// Insert pretty-printed CSS for dev mode.
renderer.sh.appendChild(document.createTextNode(rawCssRule));
}
};
}
renderer.put = function (selector, decls, atrule) {
var str = '';
var prop, value;
var postponed = [];
for (prop in decls) {
value = decls[prop];
if ((value instanceof Object) && !(value instanceof Array)) {
postponed.push(prop);
} else {
if ((process.env.NODE_ENV !== 'production') && !renderer.sourcemaps) {
str += ' ' + renderer.decl(prop, value, selector, atrule) + '\n';
} else {
str += renderer.decl(prop, value, selector, atrule);
}
}
}
if (str) {
if ((process.env.NODE_ENV !== 'production') && !renderer.sourcemaps) {
str = '\n' + selector + ' {\n' + str + '}\n';
} else {
str = selector + '{' + str + '}';
}
renderer.putRaw(atrule ? atrule + '{' + str + '}' : str);
}
for (var i = 0; i < postponed.length; i++) {
prop = postponed[i];
if (prop[0] === '@' && prop !== '@font-face') {
renderer.putAt(selector, decls[prop], prop);
} else {
renderer.put(renderer.selector(selector, prop), decls[prop], atrule);
}
}
};
renderer.putAt = renderer.put;
return renderer;
};